ansible基于consul監(jiān)控多臺(tái)主機(jī)

規(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)。

思路:

  1. 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)


image.png

注冊(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,里面保存的用戶(hù)名和密碼。需要把這條配在啟動(dòng)execstart行的node_proter后。

3.編寫(xiě)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í)行:


image.png

看一下consul中,服務(wù)已經(jīng)注冊(cè)進(jìn)來(lái)了


image.png

4.配置prometheus,驗(yàn)證

image.png

image.png

image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容