Prometheus的推模式监控配置

1,363 阅读3分钟

PushGateway采集数据配置

在上一节中我们介绍了Prometheus 采用定时 Pull 模式,但是Pull模式,可能由于子网络或者防火墙的原因,不能直接拉取各个 Target 的指标数据,此时可以采用各个 Target 往 PushGateway 上 Push 数据,然后 Prometheus 去 PushGateway 上定时 pull。 其次在监控各个业务数据时,需要将各个不同的业务数据进行统一汇总,此时也可以采用 PushGateway 来统一收集,然后 Prometheus 来统一拉取。

image-20230331101834387

下载

docker pull prom/pushgateway:v1.5.1

启动

docker run --name pushgateway -d -p 9091:9091 prom/pushgateway:v1.5.1

image-20230331102922150

访问http://localhost:9091/metrics查看是否启动成功,可看到pushgateway自身也带了一些指标,启动成功

image-20230331103026132

边缘服务器配置

下载node-exporter

wget https://github.com/prometheus/node_exporter/releases/download/v1.5.0/node_exporter-1.5.0.linux-arm64.tar.gz

image-20230331104159193

下载完成以后执行tar -xvf node_exporter-1.5.0.linux-arm64.tar.gz && cd node_exporter-1.5.0.linux-arm64进行解压

image-20230331104558522

解压完成以后,执行./node_exporter进行简单验证,

image-20230331104641480

看到以上信息则启动成功,通过浏览器进行访问http://ip:9100/metrics,可以看到以下指标

image-20230331104746188

安装node-exporter

由于直接启动node-exporter关闭窗口此进程就会挂掉,所以不是我们想要的,因此可以采用systemctl方式进行配置

  1. 执行mv node_exporter //usr/local/移动node_exporter文件

  2. /usr/lib/systemd/system/目录,创建node_exporter.service文件,内容如下,ExecStart指向的就是node_exporter执行文件

    cat <<EOF > /usr/lib/systemd/system/node_exporter.service
    [Unit]
    Description=Node Exporter
    
    [Service]
    ExecStart=/usr/local/node_exporter
    Restart=on-failure
    [Install]
    WantedBy=multi-user.target
    EOF
    

    image-20230331105150444

  3. 执行systemctl daemon-reload

  4. 执行 systemctl start node_exporter启动node_exporter

  5. 执行netstat -aon|grep 9100查看9100是否启动成功

    image-20230331105253985

边缘端指标上报

由于node-exporter只提供的数据,默认是由prometheus进行pull的方式来获取指标数据,而我们需要主动push数据到Pushgateway,所以这里需要增加shell脚本,先获取node-exporter数据,然后在调用Pushgateway接口进行push,以下脚本就是就是进行推送的语句

  • PushgatewayIP: 10.211.55.2
  • 边缘服务器IP:10.211.55.6
curl 10.211.55.6:9100/metrics|curl --data-binary @- http://10.211.55.2:9091/metrics/job/agent-server/instance/10.211.55.6/hostname/边缘服务器

手动执行以下脚本,并访问Pushgateway查看是否有对应数据

image-20230331110257299

可以看到数据已上传

image-20230331151959242

虽然以上脚本没问题,但是还需要定时执行才行,所以需要编写shell脚本,并通过crontab进行调用

  • 创建shell脚本

    cat <<EOF > /etc/cron.d/propushgateway.sh
    #!/bin/bash
    curl 10.211.55.6:9100/metrics|curl --data-binary @- http://10.211.55.2:9091/metrics/job/agent-server/instance/hostname/10.211.55.6
    date>> /tmp/date.txt
    EOF
    
  • 分配文件执行权限

      chmod 777 /etc/cron.d/propushgateway.sh
    
  • 配置crontab任务,10s执行一次,由于crontab只支持到分,所以采用采用以下方式配置

    crontab -e
    
    * * * * * /etc/cron.d/propushgateway.sh
    * * * * * sleep 10; /etc/cron.d/propushgateway.sh
    * * * * * sleep 20; /etc/cron.d/propushgateway.sh
    * * * * * sleep 30; /etc/cron.d/propushgateway.sh
    * * * * * sleep 40; /etc/cron.d/propushgateway.sh
    * * * * * sleep 50; /etc/cron.d/propushgateway.sh
    
  • 查看执行日志tail -f /var/log/cron

    可以看到10s执行一次

    image-20230331113545820

修改prometheus配置文件

在prometheus.yml文件中增加如下配置

  - job_name: 'AgentServer'
    # Override the global default and scrape targets from this job every 5 seconds.
    honor_labels: false
    static_configs:
      - targets: ['10.211.55.2:9091'] 
        labels:
          pushgateway_instance: agent-server  ##这里必须加这边标签过滤,不然采集的是pushGateway数据

增加完成以后重启prometheus

docker restart prometheus

启动完成以后访问prometheus地址,查看Pushgateway的target已经生效

image-20230331151237880

通过Grafana查看数据

image-20230331152919905