利用ansible批量部署node客户端,并注册consul,实现主机自动发现

发布时间 2023-07-05 15:32:36作者: 会bk的鱼

 

 1.在管理机器上搭建consul 并上传 node_exoporter软件包 ,system服务配置文件,注册脚本

 2.利用ansible对指定机器去分发软件包并启动服务,并curl 注册到consul   编写为node-exporter.yml

 3.prometheus配置consul地址,获取主机信息,自动发现并配合grafana 展示

 

 system服务配置文件

[Unit]
Description=node_exporter
[Service]
ExecStart=/usr/local/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

注册脚本

#!/bin/bash
instance_id=$1
service_name=$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.xx.xx:8500/v1/agent/service/register

node-exporter.yml

---
 - hosts: '{{ env_host }}'    #通过设置变量来指定分组机器

   tasks:
     - name: 推送采集器安装包
       unarchive:
         src: /opt/node_exporter-1.5.0.linux-amd64.tar.gz  # 本地机器上的tar文件路径
         dest: /usr/local      # 解压到的远程目录路径
     - name: 重命名
       shell: |
         cd /usr/local/
         if [ ! -d node_exporter ];then
            mv node_exporter-1.5.0.linux-amd64 node_exporter
         fi
     - name: 推送system文件
       copy: src=node_exporter.service dest=/usr/lib/systemd/system
     - name: 启动服务
       systemd: name=node_exporter state=started enabled=yes
     - name: 推送注册脚本
       copy: src=consul-register.sh dest=/usr/local/node_exporter
     - name: 注册当前节点
       shell: /bin/sh /usr/local/node_exporter/consul-register.sh  {{ name }}  {{ group_names[0] }}  {{ inventory_hostname }} 9100

/etc/ansible/hosts  加标签

 

 prometheus.yml 配置consul

 

 

 

consul 注册与删除
注册

注册的信息,主机名test1 ip:10.23.215.217 consul 地址:10.23.215.236:8500

curl -X PUT -d '{"id": "test1","name": "old_env","address": "10.23.215.217","port": 9100,"tags": ["dev"],"checks": [{"http": "http://10.23.215.217:9100/","interval": "5s"}]}' http://10.23.215.236:8500/v1/agent/service/register
删除 test1指的是ID

curl -X PUT http://10.23.215.236:8500/v1/agent/service/deregister/test1

 

参考链接:

https://www.jianshu.com/p/f243a3aec18e    ansible基于consul监控多台主机

https://blog.csdn.net/heian_99/article/details/119874180   promutheus+consul

 

https://zhuanlan.zhihu.com/p/210352317    Prometheus监控

https://www.jianshu.com/p/d4b85d404f6d         consul 日常接口使用