本文分享自天翼云开发者社区《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 --version ansible 2.9.25config file = /etc/ansible/ansible.cfgconfigured 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: nodegather_facts: yesbecome: yesbecome_method: sudobecome_user: roottasks: - name: 添加prometheus用户user:name: prometheuspassword: "{{ 'XXXXX' | password_hash('sha512') }}"home: /home/prometheus- name: 创建node_exporter_script目录file:path: /home/prometheus/node_exporter_scriptstate: directorymode: '0755'owner: prometheusgroup: prometheus- name: 创建node_exporter_textfile目录file:path: /home/prometheus/node_exporter_textfilestate: directorymode: '0755'owner: prometheusgroup: prometheus- name: 安装CentOS7的node_exporterunarchive: src=node_exporter-1.2.2.linux-amd64.tar.gz dest=/home/prometheus mode='0755' owner=prometheus group=prometheuswhen:- 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.servicewhen:- ansible_distribution == "CentOS"- ansible_distribution_major_version == "7"- name: 开启centos7的prometheus_node_exporter服务并设置开机自启动systemd:name: prometheus_node_exporterdaemon_reload: yesstate: restartedenabled: yeswhen:- ansible_distribution == "CentOS"- ansible_distribution_major_version == "7"
3)编写node-exporter的注册服务文件
[root@XXX][~] $vim prometheus_node_exporter.service [Unit] Description=Prometheus node_exporter Requires=network.target remote-fs.target After=network.target remote-fs.target[Service] Type=simple User=prometheus Group=prometheus ExecStart=/home/prometheus/node_exporter-1.2.2.linux-amd64/node_exporter --collector.textfile.directory=/home/prometheus/node_exporter_textfile ExecReload=/bin/kill -HUP $MAINPID KillMode=process Restart=on-failure RestartSec=5s[Install] WantedBy=multi-user.target
4)命令执行
[root@XXX ~]
$ansible-playbook node_exporter.yml -i host
5)服务验证
#验证目标端口是否开启 [root@XXXXX ~] $telnet 目标主机 9100
至此,使用Ansible部署node-exporter完成。
