四、监控搭建-Prometheus-采集端批量部署

四、监控搭建-Prometheus-采集端批量部署

1、背景

在前三篇中我们搭建了Prometheus平台,采集端部署和配合图形化grafana部署,将Linux主机进行监控。基本完成了一个最小化的监控模型,本篇是在前三篇的基础上继续操作。在日常使用中很少只有单台设备而是多态设备和服务进行统一化,集中监控。利用ansible自动化工具批量部署node_exporter客户端。

2、目标

使用prometheus平台批量部署,监控Linux主机资源。

3、传承

本篇操作依赖《一、监控搭建-Prometheus》、《二、监控搭建-Prometheus-采集端部署》和《三、监控搭建-Prometheus-grafana部署》的基础上的操作。操作端安装ansible工具,不在本篇做介绍,安装参考ansible安装中文文档

以上安装步骤很全面,也可以参考环境搭建-局域网ansible离线安装

4、操作

4.1、准备部署工具

将上篇中的软件包下载至本地

Linux系统采集模块: node_exporter 下载

上传至指定目录,本篇以/home/backup/prometheus目录为例。

4.2、编制部署脚本

切换到部署文件的目录下

bash 复制代码
cd /home/backup/prometheus

创建deploy_node_exporter.yml文件并输入如下内容

脚本内容分为七步操作:

  • 将客户端工具解压至客户端服务器的指定目录
  • 重命名解压缩后的文件夹
  • 创建文件夹的软链接
  • 上传客户端服务器文件
  • 启动监控客户端
  • 启动监控客户端
  • 启动服务器防火墙
  • 添加客户端监控端口
yaml 复制代码
---
- name: deploy the node_exporter
  hosts: proc
  gather_facts: no
  become: yes
  remote_user: root

  tasks:
  - name: 01-Extract node_exporter
    unarchive:
      src: /home/backup/prometheus/node_exporter-1.6.1.linux-amd64.tar.gz
      dest: /usr/local
  - name: 02-Rename floder name
    shell:
     cmd: mv /usr/local/node_exporter-1.6.1.linux-amd64 /usr/local/node_exporter-1.6.1
  - name: 03-Creating soft connections
    file:
      src: /usr/local/node_exporter-1.6.1
      dest: /usr/local/node_exporter
      state: link
  - name: 04-Copy the node_exporter.service to the client 
    copy:
      src: /usr/lib/systemd/system/node_exporter.service
      dest: /usr/lib/systemd/system
  - name: 05-Start the service of node_exporter
    systemd:
      name: node_exporter
      state: started
      enabled: yes
  - name: 06-Start the service of firewalld
    systemd:
      name: firewalld
      state: started
      enabled: yes
  - name: 07-Set Firewall Polices
    firewalld:
      port: 9100/tcp
      state: enabled
      permanent: yes
      immediate: yes

查看脚本预防错误

bash 复制代码
ansible-playbook --syntax-check deploy_node_exporter.yml 

模拟运行脚本

bash 复制代码
ansible-playbook -C deploy_node_exporter.yml 

以上两步运行无报错提示,则运行以上脚本进行操作部署

bash 复制代码
ansible-playbook -C deploy_node_exporter.yml 

PLAY [deploy the node_exporter] ********************************************************************************************

TASK [01-Extract node_exporter] ********************************************************************************************
changed: [proc-dese2]
changed: [proc-divu1]
changed: [proc-dese1]
changed: [proc-divu2]

TASK [02-Rename floder name] ***********************************************************************************************
changed: [proc-dese2]
changed: [proc-dese1]
changed: [proc-divu1]
changed: [proc-divu2]

TASK [03-Creating soft connections] ****************************************************************************************
changed: [proc-dese1]
changed: [proc-dese2]
changed: [proc-divu1]
changed: [proc-divu2]

TASK [04-Copy the node_exporter.service to the client] *********************************************************************
changed: [proc-dese1]
changed: [proc-dese2]
changed: [proc-divu1]
changed: [proc-divu2]

TASK [05-Start the service of node_exporter] *******************************************************************************
changed: [proc-dese2]
changed: [proc-dese1]
changed: [proc-divu1]
changed: [proc-divu2]

TASK [06-Start the service of firewalld] ***********************************************************************************
changed: [proc-dese1]
changed: [proc-dese2]
changed: [proc-divu1]
changed: [proc-divu2]

TASK [07-Set Firewall Polices] *********************************************************************************************
changed: [proc-dese2]
changed: [proc-divu1]
changed: [proc-dese1]
changed: [proc-divu2]

PLAY RECAP *****************************************************************************************************************
proc-dese1                 : ok=7    changed=7    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
proc-dese2                 : ok=7    changed=7    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
proc-divu1                 : ok=7    changed=7    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
proc-divu2                 : ok=7    changed=7    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

部署完毕后,在云平台的安全组中新增9100端口

4.3、服务端添加客户端

以上部署为在客户端的服务器上部署了采集器,接下来需要在服务端添加客户端的IP和端口

vi /usr/local/prometheus/prometheus.yml

bash 复制代码
- job_name: "host_monitor"
    static_configs:
      - targets: ["localhost:9100","1xx.xx.x.xx:9100","1xx.xx.x.xx:9100","1xx.xx.x.xx:9100","1xx.xx.x.xx:9100"]

编辑完毕后检测语法

bash 复制代码
./promtool check config prometheus.yml

提示SUCCESS字样后,确认文件格式无误,

重启prometheus

bash 复制代码
systemctl restart prometheus

在浏览器中输入部署promethues服务端的IP和端口查看

在浏览器中输入部署的grafana服务端的IP和端口查看

输入用户名和密码,在主机名中选中对应的主机名,查看主机的信息,

注:需要一段采集周期后有指标数据

以上为批量化部署prometheus客户端工具。

相关推荐
heimeiyingwang2 天前
【Prometheus·入门篇】Prometheus 是什么:从 Nagios 到云原生监控的演进
prometheus
秋风点枝3 天前
第一篇:认识 Prometheus —— 云原生监控的基础
云原生·prometheus
名字还没想好☜3 天前
Prometheus 告警实战:写 alerting rules、用 Alertmanager 做路由分组与抑制
运维·前端·javascript·docker·kubernetes·prometheus
SamChan904 天前
用Prometheus+Grafana搭建PDF翻译服务监控看板:指标采集与告警实战
python·ai·pdf·grafana·prometheus·机器翻译
^酸酸4 天前
Prometheus 监控架构部署实战:二进制与 Docker 双方案
docker·架构·prometheus
ylj_dev4 天前
从 0 构建 AI Workload Platform(七):可观测性、故障注入与性能验证
go·prometheus·可观测性·opentelemetry·故障注入
蒲锘4 天前
Devops项目阶段 10:Prometheus 监控与 Alertmanager 邮件告警
prometheus
.柒宇.7 天前
运维常见面试题_07_配置管理与监控(Ansible · Prometheus · Alertmanager)· 容器编排(Kubernetes)
运维·k8s·ansible·prometheus
玉&心8 天前
在grafana中引入prometheus的数据监控
grafana·prometheus
Dovis(誓平步青云)9 天前
从Redis指标采集到异常告警:redis_exporter + Prometheus 完整实战
服务器·数据库·人工智能·redis·架构·prometheus·vibe coding