-- 日期: 2020 年 7 月 21 日
-- 作者: 飞翔的小胖猪
文档功能说明:
文档通过 ansible+shell+consul 的方式实现批量下发安装 Linux 操作系统监控的 node_exporter 软件, 自动加载 node_exporter 软件到系统开机启动中并通过 consul 注册的功能. 为部署 prometheus 系统做好前期准备.
适用范围:
文档试用于 CentOS,RedHat 系列操作系统. 由于文档使用了 ansible 对主机操作, 被管理端 Linux 需要有 python2.6 以上的环境才能使用, 对于 CentOS 和 RedHat 系统默认只能控制 6 及以上的系统, 6 以下的操作系统需要单独的升级 python 版本. 所有被管主机上请提前安装好 wget 工具.
注:
对于已有业务承载的 6 以下操作系统不建议升级 python 包来实现, 该部分机器请手动下发软件及脚本.
环境准备:
ansible 服务器一台 批量操作控制主机节点
httpd 服务器一台 存放 soft 文件和脚本文件
consul 服务器一台 实现自动注册 node_exporter 到资源中
ansible 和 consul 可以部署到一台设备上, 也可以分开部署. 文档中不涉及到 ansible 及 consul 服务的安装部署操作, 如有需求请查看作者其他部署笔记或自己百度.
步骤:
1. 在 ansible 中添加被管控节点列表
2. 编写脚本下发软件及脚本至服务器
3. 实现 node_exporter 注册到 consul 中
4. 检查查看 consul 注册情况
step1: 配置 ansible 服务及 ansible 控制清单
文章不对 ansible 安装进行介绍, 在 ansible 安装完成的前提下进行配置. 进行配置前需要保证环境中以完成 ansible 软件安装.
配置配置首次通过 SSH 协议登录被控主机时不用敲 yes.
修改 / etc/ansible/ansible.cfg 文件中的 host_key_checking = False
- # VIM /etc/ansible/ansible.cfg
- # additional paths to search for roles in, colon separated
- #roles_path = /etc/ansible/roles
- # uncomment this to disable SSH key host checking
- host_key_checking = False
- # change the default callback, you can only have one 'stdout' type enabled at a time.
修改 / etc/SSH/ssh_config 中 StrictHostKeyChecking ask 选项为 StrictHostKeyChecking no
- # VIM /etc/SSH/ssh_config
- # CheckHostIP yes
- # AddressFamily any
- # ConnectTimeout 0
- StrictHostKeyChecking no
- # IdentityFile ~/.SSH/id_rsa
- # IdentityFile ~/.SSH/id_dsa
编辑 / etc/ansible/hosts 文件添加需要控制的主机清单, 文档使用明文密码添加内容.
- # VIM /etc/ansible/hosts
- [node_exporter_group]
- node_1 ansible_ssh_host=192.168.111.12 ansible_ssh_user=root ansible_ssh_pass=yinwan
- node_2 ansible_ssh_host=192.168.111.124 ansible_ssh_user=root ansible_ssh_pass=yinwan
配置说明:
ansible_ssh_host: 被控主机 ip 地址或域名
ansible_ssh_user: 被控主机可访问用户
ansible_ssh_pass: 对应 ansible_ssh_user 用户的密码
验证 ansible 服务配置成功, 使用 ansible 调用命令查看被控主机主机名, 如果正常能够显示出主机名.
查看 ansible 中一共有多少个被控主机
# ansible all --list
查看被控主机的主机名, 能够正常查看主机名表示主机能够被 ansible 服务控制, 配置添加成功.
# ansible all -m shell -a 'hostname'
step2: 编写脚本下发软件及脚本至服务器
实验使用 http 放置 node_exporter 软件和各种脚本, 客户主机通过 wget 可从 http 服务器获取到资源. 在进行软件及脚本下发前需要读者自定搭建一个 http 服务, 作者直接使用 yum 命令安装了一个 httpd 服务, 把软件及脚本放置 / var/www/html/soft / 文件夹中.
脚本说明:
存放地址:/var/www/HTML/soft/
# ls -l /var/www/HTML/soft/
get_soft.sh: 客户端主机下载 node_exporter 软件, node_exporter 启动脚本. 并且完成 node_exporter 安装.
auto_start_node_exporter.sh: 客户端启动 node_exporter 脚本
auto_consul_zc.sh: 使用 / etc/ansible/hosts 列表生成注册 consul 命令
auto_prometh_server.sh: 启动 prometheus+consul+alertmanager 脚本
# VIM get_soft.sh
- #!/bin/bash
- if uname -a | grep -i _64 &> /dev/null ;then
- echo "64 位处理方式"
- if [ -f /root/node_exporter-1.0.0.Linux-amd64.tar.gz ];then
- echo 'node_exporter-1.0.0.linux-amd64.tar.gz file exist'
- else
- if timeout 20 wget -P /root http://192.168.111.83/soft/node_exporter-1.0.0.linux-amd64.tar.gz;then
- if [ -f /usr/local/node_exporter/node_exporter ];then
- echo "node_exporter file exist , not exec command."
- else
- if [ -f /root/auto_start_node_exporter.sh ];then
- echo 'auto_start_node_exporter.sh exist'
- else
- if timeout 20 wget -P /root http://192.168.111.83/soft/auto_start_node_exporter.sh;then
- if cat /etc/rc.d/rc.local | grep -i auto_start_node_exporter &> /dev/null ;then
- echo "file in auto startup low"
- else
- echo '/root/auto_start_node_exporter.sh'>> /etc/rc.d/rc.local
- chmod a+x /etc/rc.d/rc.local
- fi
- else
- echo 'auto_start_node_exporter.sh copy faile !!'
- fi
- fi
- mkdir /scripts/soft/ -p
- tar -zxvf /root/node_exporter-1.0.0.Linux-amd64.tar.gz -C /scripts/soft/ && mv /scripts/soft/node_exporter-1.0.0.Linux-amd64/ /usr/local/node_exporter
- chmod a+x /root/auto_start_node_exporter.sh
- fi
- else
- echo "node_exporter soft_file copy faile !!"
- fi
- fi
- else
- echo "32 位处理方式"
- if [ -f /root/node_exporter-1.0.0.Linux-amd64.tar.gz ];then
- echo 'node_exporter-1.0.0.linux-amd64.tar.gz file exist'
- else
- if timeout 20 wget -P /root http://192.168.111.83/soft/node_exporter-1.0.0.linux-386.tar.gz;then
- if [ -f /usr/local/node_exporter/node_exporter ];then
- echo "node_exporter file exist , not exec command."
- else
- if [ -f /root/auto_start_node_exporter.sh ];then
- echo 'auto_start_node_exporter.sh exist'
- else
- if timeout 20 wget -P /root http://192.168.111.83/soft/auto_start_node_exporter.sh;then
- if cat /etc/rc.d/rc.local | grep -i auto_start_node_exporter &> /dev/null ;then
- echo "file in auto startup low"
- else
- echo '/root/auto_start_node_exporter.sh'>> /etc/rc.d/rc.local
- chmod a+x /etc/rc.d/rc.local
- fi
- else
- echo 'auto_start_node_exporter.sh copy faile !!'
- fi
- fi
- mkdir /scripts/soft/ -p
- tar -zxvf /root/node_exporter-1.0.0.Linux-386.tar.gz -C /scripts/soft/ && mv /scripts/soft/node_exporter-1.0.0.Linux-386/ /usr/local/node_exporter
- chmod a+x /root/auto_start_node_exporter.sh
- fi
- else
- echo "node_exporter soft_file copy faile !!"
- fi
- fi
- fi
- View Code
- # VIM auto_start_node_exporter.sh
- #!/bin/bash
- if which netstat &> /dev/null;then
- if netstat -alntup | grep -i 9100 &> /dev/null;then
- echo "9100 port exist netstat !"
- else
- /usr/local/node_exporter/node_exporter &> /dev/null &
- fi
- elif which ss &> /dev/null ;then
- if ss -alntup | grep -i 9100 &> /dev/null;then
- echo "9100 port exist ss !"
- else
- /usr/local/node_exporter/node_exporter &> /dev/null &
- fi
- else
- echo "未知错误, 不启动程序"
- fi
- View Code
- # VIM auto_consul_zc.sh
- #!/bin/bash
- cat /etc/ansible/hosts | grep -v '\[' | grep -v ^$ | sed 's/=/ /' | awk '{print $1,$3}'> /prometheus/tmp_consul_list.txt
- sleep 1
- cat /prometheus/tmp_consul_list.txt | while read host_name host_addr
- do
- echo "curl -X PUT -d' {\"id\": \"${host_name}\",\"name\": \"${host_name}\",\"address\": \"${host_addr}\",\"port\": 9100,\"tags\": [\"test\",\"node\",\"linux\"],\"checks\": [{\"http\": \"http://${host_addr}:9100/metrics\", \"interval\": \"5s\"}]}'http://192.168.111.83:8500/v1/agent/service/register"
- done
- View Code
- # VIM auto_prometh_server.sh
- #!/bin/bash
- sleep 1
- prometheus --config.file="/usr/local/prometheus/prometheus.yml" --storage.tsdb.retention.time=90d &> /dev/null &
- consul agent -server -Bootstrap-expect 1 -data-dir=/usr/local/consul_data/data -ui -bind 192.168.111.83 -client 0.0.0.0 &> /dev/null &
- alertmanager --config.file=/usr/local/alertmanager/alertmanager.YAML --cluster.advertise-address=0.0.0.0:9093 &> /dev/null &
- View Code
执行 ansible 批量下发 node_exporter 和启动脚本并在主机端自动安装 node_exporter 软件加入启动脚本到开机启动.
在下发之前需要确认被控主机节点有 wget 命令, 如果没有 wget 命令则需要先完成 wget 安装.
# ansible all -m shell -a 'which wget'
使用 ansible 的 script 模块执行脚本 / var/www/HTML/soft/get_soft.sh
# ansible all -m script -a '/var/www/html/soft/get_soft.sh'
确认客户端上是否存在 node_exporter 软件, 如果存在 / usr/local/node_exporter/node_exporter 文件表示软件安装成功.
# ansible all -m shell -a 'ls -l /usr/local/node_exporter/node_exporter'
rc=0 表示命令有返回值, 则表示文件存在, 软件安装成功.
确认客户端上是否存在 auto_start_node_exporter.sh 启动脚本, 如果存在 / root/auto_start_node_exporter.sh 则启动脚本获取成功.
# ansible all -m shell -a 'ls -l /root/auto_start_node_exporter.sh'
rc=0 表示命令有返回值, 则表示启动脚本存在.
确认客户端上是否成功添加启动脚本到开机启动, 如果 / etc/rc.d/rc.local 中有 / root/auto_start_node_exporter.sh 行则开机启动添加成功.
# ansible all -m shell -a 'cat /etc/rc.d/rc.local | grep -i auto_start_node_exporter'
rc=0 表示命令有返回值, 则表示 / etc/rc.d/rc.local 文件中有 / root/auto_start_node_exporter.sh 行, 表示脚本开机启动配置完成.
通过命令启动客户端的 node_exporter 软件, 启动后查看客户主机上 9100 端口是否处于监听状态.
# ansible all -m shell -a 'nohup /root/auto_start_node_exporter.sh'
调用客户端脚本启动 node_exporter 软件.
确认客户端 9100 端口有 node_exporter 软件监听.
# ansible all -m shell -a 'ss -alntup | grep -i 9100'
9100 端口启动表示 node_exporter 软件安装完成. 能够正常为 prometheus 提供数据.
step3: 注册客户端 node_exporter 到 consul
使用 shell 脚本从 / etc/ansible/hosts 中提取被控主机信息, 自动生成注册 consul 命令.
在进行该步骤前需要提前完成 consul 软件的安装与配置.
# source /var/www/HTML/soft/auto_consul_zc.sh
注:
auto_consul_zc.sh 脚本中 http://192.168.111.83:8500/v1/agent/service/register 中的 ip 地址是写死的, 读者请根据自己的 consul 服务器地址修改.
复制脚本生成的命令并执行.
- curl -X PUT -d '{"id":"node_1","name":"node_1","address":"192.168.111.12","port": 9100,"tags": ["test","node","linux"],"checks": [{"http":"http://192.168.111.12:9100/metrics","interval":"5s"}]}' http://192.168.111.83:8500/v1/agent/service/register
- curl -X PUT -d '{"id":"node_2","name":"node_2","address":"192.168.111.124","port": 9100,"tags": ["test","node","linux"],"checks": [{"http":"http://192.168.111.124:9100/metrics","interval":"5s"}]}' http://192.168.111.83:8500/v1/agent/service/register
- View Code
step4: 确认 consul 注册信息
使用网页浏览器打开输入 consul 服务器的地址端口查看界面中是否有新注册服务器, 如果有则表示注册成功.
http://192.168.111.83:8500/
能够在 web 界面查看到节点表示节点注册成功, 此时可以在 prometheus 中添加 consul 资源实现对数据的收集工作.
来源: https://www.cnblogs.com/Pigs-Will-Fly/p/13358197.html