ansible批量安装node-exporter

胸怀万里世界, 放眼无限未来。这篇文章主要讲述ansible批量安装node-exporter相关的知识,希望能为你提供帮助。
安装ansible



nginx
  yum install ansible -y


方法一、使用命令批量发送




SQL
  1、创建相应的目录
  mkdir -p /opt/node-export
cd /opt/node-export/
2、下载node-exporter和创建对应的启动文件
  wgethttps://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_exporter-1.3.1.linux-amd64.tar.gz
tar xf node_exporter-1.3.1.linux-amd64.tar.gz
node_exporter-1.3.1.linux-amd64/
3、启动文件
cat node-exporter.service
[Unit]
Description=Prometheus node_exporter
Requires=network.target remote-fs.target
After=network.target remote-fs.target


[Service]
Type=simple
User=root
Group=root
ExecStart=/opt/node_exporter/node_exporter--web.listen-address=0.0.0.0:9100
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=5s


[Install]
WantedBy=multi-user.target
【ansible批量安装node-exporter】

4、创建对应的ip地址
[root@k8s-master node-export]# cat iplist.txt
[20220504]
172.16.5.122
172.16.5.146


5、远程拷贝文件
[root@k8s-master node-export]# ansible 20220504 -m copy -a"src=https://www.songbingjia.com/android/node_exporter dest=/opt/node-exporter/ backup=yes" -i iplist.txt
[WARNING]: Invalid characters were found in group names but not replaced,use -vvvv to see details
172.16.5.122 | CHANGED =>
      "ansible_facts":
              "discovered_interpreter_python": "/usr/bin/python"
      ,
      "changed": true,
      "checksum":"6b88b4e8e0b109cb70d0410fea7df35692fe1216",
      "dest":"/opt/node-exporter/node_exporter",
      "gid": 0,
      "group":"root",
      "md5sum":"e105cbe3e43cc6e133a83358db009d36",
      "mode":"0644",
      "owner":"root",
      "size": 18228926,
      "src":"/root/.ansible/tmp/ansible-tmp-1651660508.81-32118-215511342287544/source",
      "state":"file",
      "uid": 0

172.16.5.146 | CHANGED =>
      "ansible_facts":
              "discovered_interpreter_python": "/usr/bin/python"
      ,
      "changed": true,
      "checksum":"6b88b4e8e0b109cb70d0410fea7df35692fe1216",
      "dest":"/opt/node-exporter/node_exporter",
      "gid": 0,
      "group":"root",
      "md5sum":"e105cbe3e43cc6e133a83358db009d36",
      "mode":"0644",
      "owner":"root",
      "size": 18228926,
      "src":"/root/.ansible/tmp/ansible-tmp-1651660508.8-32120-197167126853989/source",
      "state":"file",
      "uid": 0



6、发送服务自启动配置文件
[root@k8s-master node-export]# ansible 20220504 -m copy -a"src=https://www.songbingjia.com/android/files/node-exporter.service dest=/usr/lib/systemd/system/ backup=yes" -i iplist.txt
[WARNING]: Invalid characters were found in group names but not replaced,use -vvvv to see details
172.16.5.122 | CHANGED =>
      "ansible_facts":
              "discovered_interpreter_python": "/usr/bin/python"
      ,
      "changed": true,
      "checksum":"8523709e50be5c4e1218feafa8b22baf30e5e682",
      "dest":"/usr/lib/systemd/system/node-exporter.service",
      "gid": 0,
      "group":"root",
      "md5sum":"2168230bbf44d0bf8bff4539bce3a357",
      "mode":"0644",
      "owner":"root",
      "size": 367,
      "src":"/root/.ansible/tmp/ansible-tmp-1651660799.58-4285-252476952854520/source",
      "state":"file",
      "uid": 0



7、创建自启动文件
[root@k8s-master node-export]# ansible 20220504 -m shell -a"systemctl enable node-exporter.service" -i iplist.txt
[WARNING]: Invalid characters were found in group names but not replaced,use -vvvv to see details
172.16.5.146 | CHANGED | rc=0 > >
Created symlink from/etc/systemd/system/multi-user.target.wants/node-exporter.service to/usr/lib/systemd/system/node-exporter.service.
172.16.5.122 | CHANGED | rc=0 > >
Created symlink from/etc/systemd/system/multi-user.target.wants/node-exporter.service to/usr/lib/systemd/system/node-exporter.service.


8、赋权限
[root@k8s-master node-export]# ansible 20220504 -m shell -a "chmod755 /opt/node-exporter/node_exporter" -i iplist.txt


9、自启动文件
[root@k8s-master node-export]# ansible 20220504 -m shell -a"systemctl start node-exporter.service" -i iplist.txt 
[WARNING]: Invalid characters were found in group names but not replaced,use -vvvv to see details
172.16.5.146 | CHANGED | rc=0 > >


172.16.5.122 | CHANGED | rc=0 > >




方法二、ansible 部署Prometheus node_exporter通过playbook
1、编写site.yaml文件




YAML
1、创建工作目录
[root@k8s-master ~]# mkdir -p /opt/node-export
[root@k8s-master ~]# cd /opt/node-export/


2、配置site.yaml
[root@k8s-master node-export]# cat  site.yaml
- hosts: node-exporter
  tasks:
      - name: copy node_exporter
          copy:
              src:/opt/node-export/node_exporter
              dest: /opt/node-exporter/
              owner: root
              group: root
              mode: 755
      - name: copynode-exporter.service
          copy:
              src:files/node-exporter.service
              dest:/usr/lib/systemd/system/
          notify:
              - call restart service
      - name: 开机自启动
          shell: systemctl enablenode-exporter.service
      - name: 启动服务
          shell: systemctl startnode-exporter.service


2、创建node_exporter二进制文件和相关的配置
2.1、创建目录files




CoffeeScript
[root@k8s-master node-export]# mkdir files
[root@k8s-master node-export]# cd files/
[root@k8s-master files]#wget  https://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_exporter-1.3.1.linux-amd64.tar.gz










备注:files文件里面包含node_exporter二进制文件;开机自启文件node-exporter.service;node_exporter开机自启服务自定义端口配置node-exporter


2.2、编辑node_exporter开机自启动服务等配置文件




Makefile
[root@k8s-master node-export]# cat  files/node-exporter.service
[Unit]
Description=Prometheus node_exporter
Requires=network.target remote-fs.target
After=network.target remote-fs.target


[Service]
Type=simple
User=root
Group=root
ExecStart=/opt/node_exporter/node_exporter--web.listen-address=0.0.0.0:9100
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=5s


[Install]
WantedBy=multi-user.target


2.3、给/etc/ansible/hosts添加所有的主机




CoffeeScript
[root@k8s-master node-export]# vim /etc/ansible/hosts
[node-exporter]
172.16.5.122
172.16.5.146


2.4、执行ansible-playbook




Go
[root@k8s-master node-export]# ansible-playbook site.yaml







    推荐阅读