Ansible部署Node_exporter

本文分享自天翼云开发者社区《Ansible部署Node_exporter》,作者:SummerSnow

一、简介

Ansible是基于Python开发的自动化运维工具,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。

Exporter是Prometheus的指标数据收集组件,而node_exporter就是我们常用的其中之一,它主要用于采集类UNIX内核的硬件以及系统指标,如磁盘、cpu、内存等信息。

二、环境说明

复制代码
#操作系统版本
[root@XXXXX][~]
$cat /etc/redhat-release 
CentOS Linux release 7.7.1908 (Core)

#Ansible版本
ansible 2.9.25
#node-exporter版本
node_exporter-1.2.2
#环境说明:本操作未涉及容器化部署,同时在centos 7环境进行部署

三、安装Ansible

复制代码
#上传已经准备好的的安装包(内网环境)
[root@XXXXX ~] tar -zxvf ansible.tar.gz

#使用下面的命令进行安装(yum本地安装)
[root@XXXXX ~]# yum localinstall *.rpm -y

#查看ansible版本
[root@XXX][~]
$ansible --version
ansible 2.9.25
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']

四、使用Ansible部署node_exporter

1)填写需要部署的主机清单host

复制代码
#如果机器之间已经做了免密,那就去掉ansible_ssh_pass改配置,多台机器直接追加就行
[root@XXXXXX][~]
$vim host
[node]
XX.XX ansible_ssh_user=XXX ansible_ssh_pass="XXXXXX"
XX.XX ansible_ssh_user=XXX ansible_ssh_pass="XXXXXX"

2)编写Ansible的剧本文件node_exporter.yml

复制代码
---
- hosts: node
  gather_facts: yes
  become: yes
  become_method: sudo
  become_user: root
  tasks: 
    - name: 添加prometheus用户
      user:
        name: prometheus
        password: "{{ 'XXXXX' | password_hash('sha512') }}"
        home: /home/prometheus

    - name: 创建node_exporter_script目录
      file:
        path: /home/prometheus/node_exporter_script
        state: directory
        mode: '0755'
        owner: prometheus
        group: prometheus

    - name: 创建node_exporter_textfile目录
      file:
        path: /home/prometheus/node_exporter_textfile
        state: directory
        mode: '0755'
        owner: prometheus
        group: prometheus

    - name: 安装CentOS7的node_exporter
      unarchive: src=node_exporter-1.2.2.linux-amd64.tar.gz dest=/home/prometheus mode='0755' owner=prometheus group=prometheus
      when:
        - ansible_distribution == "CentOS"
        - ansible_distribution_major_version == "7"

    - name: 添加CentOS7的node_exporter服务
      copy: src=prometheus_node_exporter.service dest=/usr/lib/systemd/system/prometheus_node_exporter.service
      when:
        - ansible_distribution == "CentOS"
        - ansible_distribution_major_version == "7"
    
    - name: 开启centos7的prometheus_node_exporter服务并设置开机自启动
      systemd:
        name: prometheus_node_exporter
        daemon_reload: yes
        state: restarted
        enabled: yes
      when:
        - ansible_distribution == "CentOS"
        - ansible_distribution_major_version == "7"

3)编写node-exporter的注册服务文件

复制代码
[root@XXX][~]
$vim prometheus_node_exporter.service
[Unit]
Description=Prometheus node_exporter
Requires=network.target remote-fs.target
After=network.target remote-fs.target

[Service]
Type=simple
User=prometheus
Group=prometheus
ExecStart=/home/prometheus/node_exporter-1.2.2.linux-amd64/node_exporter --collector.textfile.directory=/home/prometheus/node_exporter_textfile
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=5s

[Install]
WantedBy=multi-user.target

4)命令执行

复制代码
[root@XXX ~]
$ansible-playbook node_exporter.yml -i host

5)服务验证

复制代码
#验证目标端口是否开启
[root@XXXXX ~]
$telnet 目标主机 9100

至此,使用Ansible部署node-exporter完成。

相关推荐
芷栀夏6 小时前
飞牛NAS怎么部署Immich?家庭照片自动备份与远程访问教程
服务器·nginx·ansible
悠然南风11 天前
Ansible常见模块总结及LDAP Role 编写与调试
ansible
祺风挽楠20 天前
ansible编辑
网络·ansible
芳心粽伙饭20 天前
Ansible课后作业
ansible
烁34721 天前
Ansible初识
ansible
烁34721 天前
Ansible安装部署调试
ansible
烁34721 天前
Ansible命令
ansible
小义_22 天前
【Ansible】(三)基础配置与连接设置
云原生·ansible
炸炸鱼.1 个月前
Ansible 企业级实战:Playbook 与 Roles 完全指南
网络·ansible
风曦Kisaki1 个月前
# 自动化运维Day03:Ansible模块进阶(setup,debug),四种常用变量,进阶语法;Ansible Roles(角色)
运维·自动化·ansible