規(guī)劃:
192.168.1.252 --ansible
192.168.1.222 --prometheus
192.168.1.111-web1
192.168.1.112-web2 (consul)
需求:prometheus監(jiān)控多臺(tái)主機(jī)時(shí)忱叭,基于自動(dòng)發(fā)現(xiàn)consul模塊盾舌,主機(jī)安裝采集器注冊(cè)到那臺(tái)consul,consul識(shí)別到。promethues獲取consul地址上的監(jiān)控主機(jī)列表坪蚁,實(shí)現(xiàn)多臺(tái)主機(jī)自動(dòng)發(fā)現(xiàn)。
思路:
- web機(jī)器安裝node_exporter采集器
2.注冊(cè)node_exporter為系統(tǒng)服務(wù)
3.使用curl -x PUT ... 注冊(cè)到consul機(jī)器中
4.consul監(jiān)控到后,prometheus配置consul地址。
1-3步都可用ansible批量完成受葛。
1.找一臺(tái)主機(jī)(112)安裝啟動(dòng)consul服務(wù)
docker run --name consul -d -p 8500:8500 consul
2.ansible-server(252)上傳ansible需要的軟件
(node_exoporter軟件包 ,system服務(wù)配置文件偎谁,注冊(cè)腳本总滩,注冊(cè)接口如果有密碼驗(yàn)證還需要一個(gè)config.yml)
注冊(cè)腳本(傳參方式注冊(cè)):
#!/bin/bash
service_name=$1
instance_id=$2
ip=$3
port=$4
curl -X PUT -d '{"id": "'"$instance_id"'","name": "'"$service_name"'","address": "'"$ip"'","port": '"$port"',"tags": ["'"$service_name"'"],"checks": [{"http": "http://'"$ip"':'"$port"'","interval": "5s"}]}' http://192.168.1.112:8500/v1/agent/service/register
系統(tǒng)服務(wù)配置文件node_exporter.service
[Unit]
Description=node_exporter
[Service]
ExecStart=/usr/local/jiankong/node_exporter/node_exporter --collector.systemd --collector.systemd.unit-include=(docker|portal|sshd).service
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
[Install]
WantedBy=multi-user.target
--collector.systemd --collector.systemd.unit-include=(docker|portal|sshd).service 配置的意思是只收集docker,portal,sshd服務(wù)的數(shù)據(jù)
--web.config=/usr/local/jiankong/node_exporter/config.yml 如果有密碼驗(yàn)證接口需要指定這個(gè)config.yml,里面保存的用戶名和密碼巡雨。需要把這條配在啟動(dòng)execstart行的node_proter后闰渔。
3.編寫ansible-playbook
cat hosts
[webgg]
192.168.1.111 name=web1
[dbgg]
192.168.1.112 name=db1
cat playbook.yml
---
- hosts: webgg
gather_facts: no
tasks:
- name: 推送采集器安裝包
unarchive: src=node_exporter-1.1.2.linux-amd64.tar.gz dest=/usr/local/jiankong
- name: 重命名
shell: |
cd /usr/local/jiankong
if [ ! -d node_exporter ];then
mv node_exporter-1.1.2.linux-amd64 node_exporter
fi
- name: 推送system文件
copy: src=node_exporter.service dest=/usr/lib/systemd/system
- name: 啟動(dòng)服務(wù)
systemd: name=node_exporter state=started enabled=yes
- name: 推送注冊(cè)腳本
copy: src=consul-register.sh dest=/usr/local/jiankong/node_exporter
- name: 注冊(cè)當(dāng)前節(jié)點(diǎn)
shell: /bin/sh /usr/local/jiankong/node_exporter/consul-register.sh {{ group_names[0] }} {{ name }} {{ inventory_hostname }} 9100
{{ group_names[0] }} --ansible內(nèi)置變量 代表hosts中自定義組名,數(shù)組形式铐望,[0]取第一個(gè)
{{ name }} -- hosts文件中定義主機(jī)的名字冈涧,如name=web1
{{ inventory_hostname }} 當(dāng)前執(zhí)行主機(jī)的ip
執(zhí)行:
看一下consul中,服務(wù)已經(jīng)注冊(cè)進(jìn)來了