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完成。

相关推荐
tyatyatya2 小时前
Ansible自动化配置,从入门到实战
运维·自动化·ansible
lbb 小魔仙1 天前
【Linux】Ansible 自动化运维实战:2000+ 节点配置标准化教程
linux·运维·ansible
扑火的小飞蛾4 天前
【Ansible学习笔记01】 批量执行 shell 命令
笔记·学习·ansible
oMcLin4 天前
如何在 Red Hat Linux 服务器上使用 Ansible 自动化部署并管理多节点 Hadoop 集群?
linux·服务器·ansible
linux修理工7 天前
vagrant ubuntu 22.04 ansible 配置
ubuntu·ansible·vagrant
biubiubiu07068 天前
Ansible自动化
运维·自动化·ansible
秋4279 天前
ansible配置与模块介绍
ansible
秋4279 天前
ansible剧本
linux·服务器·ansible
码农101号10 天前
Ansible - Role介绍 和 使用playbook部署wordPress
android·ansible
2301_8000509912 天前
Ansible
运维·ansible