Ansible部署Node_exporter
本文分享自天翼云開發者社區《Ansible部署Node_exporter》,作者:SummerSnow
一、簡介
Ansible是基于Python開發的自動化運維工具,集合了眾多運維工具(puppet、cfengine、chef、func、fabric)的優點,實現了批量系統配置、批量程序部署、批量運行命令等功能。
Exporter是Prometheus的指標數據收集組件,而node_exporter就是我們常用的其中之一,它主要用于采集類UNIX內核的硬件以及系統指標,如磁盤、cpu、內存等信息。
二、環境說明
#操作系統版本[root@XXXXX][~]$cat /etc/redhat-release CentOS Linux release 7.7.1908 (Core)#Ansible版本ansible 2.9.25#node-exporter版本node_exporter-1.2.2#環境說明:本操作未涉及容器化部署,同時在centos 7環境進行部署
三、安裝Ansible
#上傳已經準備好的的安裝包(內網環境)[root@XXXXX ~] tar -zxvf ansible.tar.gz#使用下面的命令進行安裝(yum本地安裝)[root@XXXXX ~]# yum localinstall *.rpm -y#查看ansible版本[root@XXX][~]$ansible --versionansible 2.9.25 config file = /etc/ansible/ansible.cfg configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
四、使用Ansible部署node_exporter
1)填寫需要部署的主機清單host
#如果機器之間已經做了免密,那就去掉ansible_ssh_pass改配置,多臺機器直接追加就行[root@XXXXXX][~]$vim host[node]XX.XX ansible_ssh_user=XXX ansible_ssh_pass="XXXXXX"XX.XX ansible_ssh_user=XXX ansible_ssh_pass="XXXXXX"
2)編寫Ansible的劇本文件node_exporter.yml
---
- hosts: node
gather_facts: yes
become: yes
become_method: sudo
become_user: root
tasks:
- name: 添加prometheus用戶
user:
name: prometheus
password: "{{ 'XXXXX' | password_hash('sha512') }}"
home: /home/prometheus
- name: 創建node_exporter_script目錄
file:
path: /home/prometheus/node_exporter_script
state: directory
mode: '0755'
owner: prometheus
group: prometheus
- name: 創建node_exporter_textfile目錄
file:
path: /home/prometheus/node_exporter_textfile
state: directory
mode: '0755'
owner: prometheus
group: prometheus
- name: 安裝CentOS7的node_exporter
unarchive: src=node_exporter-1.2.2.linux-amd64.tar.gz dest=/home/prometheus mode='0755' owner=prometheus group=prometheus
when:
- ansible_distribution == "CentOS"
- ansible_distribution_major_version == "7"
- name: 添加CentOS7的node_exporter服務
copy: src=prometheus_node_exporter.service dest=/usr/lib/systemd/system/prometheus_node_exporter.service
when:
- ansible_distribution == "CentOS"
- ansible_distribution_major_version == "7"
- name: 開啟centos7的prometheus_node_exporter服務并設置開機自啟動
systemd:
name: prometheus_node_exporter
daemon_reload: yes
state: restarted
enabled: yes
when:
- ansible_distribution == "CentOS"
- ansible_distribution_major_version == "7"3)編寫node-exporter的注冊服務文件
[root@XXX][~]$vim prometheus_node_exporter.service[Unit]Description=Prometheus node_exporterRequires=network.target remote-fs.targetAfter=network.target remote-fs.target[Service]Type=simpleUser=prometheusGroup=prometheusExecStart=/home/prometheus/node_exporter-1.2.2.linux-amd64/node_exporter --collector.textfile.directory=/home/prometheus/node_exporter_textfileExecReload=/bin/kill -HUP $MAINPIDKillMode=processRestart=on-failureRestartSec=5s[Install]WantedBy=multi-user.target
4)命令執行
[root@XXX ~]$ansible-playbook node_exporter.yml -i host
5)服務驗證
#驗證目標端口是否開啟[root@XXXXX ~]$telnet 目標主機 9100
至此,使用Ansible部署node-exporter完成。
*博客內容為網友個人發布,僅代表博主個人觀點,如有侵權請聯系工作人員刪除。

