前言

docker-compose 搭建prometheus+granafa+alertmanager+dingtalk;node-expoter使用service形式启动。


一、docker-compose安装

参考:
https://blog.csdn.net/qq_38986854/article/details/122426393

1.1 安装docker

#获取yum源
sudo yum install -y yum-utils;
sudo yum-config-manager --add-repo  http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo;
#安装服务
yum install docker-ce docker-ce-cli -y;
systemctl restart docker ;
systemctl enable  docker ;

1.2 安装docker-compose

#获取pip,python版本是2.7.5
wget https://bootstrap.pypa.io/pip/2.7/get-pip.py;
#python get-pip.py --default-timeout=100 -i https://pypi.douban.com/simple;
#当存在No matching distribution found for pip<21.0提示时使用以下阿里云的源
python get-pip.py -i  http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
#安装docker-compose
#pip install docker-compose -i https://pypi.douban.com/simple;
pip install docker-compose -i  http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

二、路径准备

我这里使用的路径是/data/prom/,部署的时候按照各自机器的分布规划来安排即可。

#设定路径信息
dir=data
#新建路径
mkdir -p /$dir/prom/{prometheus,prometheus/data,alertmanager,grafana}
#这一步务必要做
chmod 777 /$dir/prom/{prometheus/data,grafana}
 
cd /$dir/prom/

三、配置文件准备

3.1 prometheus.yml

/$dir/prom/prometheus/prometheus.yml
node-expoter由于使用的是service形式,所以没有使用域名加端口形式

global:
  scrape_interval:     15s
  evaluation_interval: 15s
 
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      - alertmanager:9093

rule_files:
  - "*rules.yml"

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
    - targets: ['prometheus:9090']

  - job_name: 'node'
    static_configs:
    - targets: ['xx.xx.xx.xx:9100']

  - job_name: 'alertmanager'
    static_configs:
    - targets: ['alertmanager:9093']

3.2 alert-rules.yml

/$dir/prom/prometheus/alert-rules.yml
具体的硬件信息根据自己机器信息修改

groups:
  - name: node-alert
    rules:
    - alert: NodeDown
      expr: up == 0
      for: 5m
      labels:
        severity: critical
        instance: "{{ $labels.instance }}"
      annotations:
        summary: "instance: {{ $labels.instance }} down"
        description: "Instance: {{ $labels.instance }} 已经宕机 5分钟"
        value: "{{ $value }}"

    - alert: NodeCpuHigh
      expr: (1 - avg by (instance) (irate(node_cpu_seconds_total{mode="idle"}[5m]))) * 100 > 85
      for: 5m
      labels:
        severity: warning
        instance: "{{ $labels.instance }}"
      annotations:
        summary: "instance: {{ $labels.instance }} cpu使用率过高"
        description: "CPU 使用率超过 80%"
        value: "{{ $value }}"

    - alert: NodeCpuIowaitHigh
      expr: avg by (instance) (irate(node_cpu_seconds_total{mode="iowait"}[5m])) * 100 > 80
      for: 5m
      labels:
        severity: warning
        instance: "{{ $labels.instance }}"
      annotations:
        summary: "instance: {{ $labels.instance }} cpu iowait 使用率过高"
        description: "CPU iowait 使用率超过 50%"
        value: "{{ $value }}"

    - alert: NodeLoad5High
      expr: node_load5 > (count by (instance) (node_cpu_seconds_total{mode='system'})) * 1.2
      for: 5m
      labels:
        severity: warning
        instance: "{{ $labels.instance }}"
      annotations:
        summary: "instance: {{ $labels.instance }} load(5m) 过高"
        description: "Load(5m) 过高,超出cpu核数 1.2倍"
        value: "{{ $value }}"

    - alert: NodeMemoryHigh
      expr: (1 - node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes) * 100 > 90
      for: 5m
      labels:
        severity: warning
        instance: "{{ $labels.instance }}"
      annotations:
        summary: "instance: {{ $labels.instance }} memory 使用率过高"
        description: "Memory 使用率超过 90%"
        value: "{{ $value }}"
 
    - alert: NodeDiskRootHigh
      expr: (1 - node_filesystem_avail_bytes{fstype=~"ext.*|xfs",mountpoint ="/"} / node_filesystem_size_bytes{fstype=~"ext.*|xfs",mountpoint ="/"}) * 100 > 90
      for: 10m
      labels:
        severity: warning
        instance: "{{ $labels.instance }}"
      annotations:
        summary: "instance: {{ $labels.instance }} disk(/ 分区) 使用率过高"
        description: "Disk(/ 分区) 使用率超过 90%"
        value: "{{ $value }}"

    - alert: NodeDiskBootHigh
      expr: (1 - node_filesystem_avail_bytes{fstype=~"ext.*|xfs",mountpoint ="/boot"} / node_filesystem_size_bytes{fstype=~"ext.*|xfs",mountpoint ="/boot"}) * 100 > 80
      for: 10m
      labels:
        severity: warning
        instance: "{{ $labels.instance }}"
      annotations:
        summary: "instance: {{ $labels.instance }} disk(/boot 分区) 使用率过高"
        description: "Disk(/boot 分区) 使用率超过 80%"
        value: "{{ $value }}"

    - alert: NodeDiskReadHigh
      expr: irate(node_disk_read_bytes_total[5m]) > 20 * (1024 ^ 2)
      for: 5m
      labels:
        severity: warning
        instance: "{{ $labels.instance }}"
      annotations:
        summary: "instance: {{ $labels.instance }} disk 读取字节数 速率过高"
        description: "Disk 读取字节数 速率超过 20 MB/s"
        value: "{{ $value }}"

    - alert: NodeDiskWriteHigh
      expr: irate(node_disk_written_bytes_total[5m]) > 20 * (1024 ^ 2)
      for: 5m
      labels:
        severity: warning
        instance: "{{ $labels.instance }}"
      annotations:
        summary: "instance: {{ $labels.instance }} disk 写入字节数 速率过高"
        description: "Disk 写入字节数 速率超过 20 MB/s"
        value: "{{ $value }}"

    - alert: NodeDiskReadRateCountHigh
      expr: irate(node_disk_reads_completed_total[5m]) > 3000
      for: 5m
      labels:
        severity: warning
        instance: "{{ $labels.instance }}"
      annotations:
        summary: "instance: {{ $labels.instance }} disk iops 每秒读取速率过高"
        description: "Disk iops 每秒读取速率超过 3000 iops"
        value: "{{ $value }}"

    - alert: NodeDiskWriteRateCountHigh
      expr: irate(node_disk_writes_completed_total[5m]) > 3000
      for: 5m
      labels:
        severity: warning
        instance: "{{ $labels.instance }}"
      annotations:
        summary: "instance: {{ $labels.instance }} disk iops 每秒写入速率过高"
        description: "Disk iops 每秒写入速率超过 3000 iops"
        value: "{{ $value }}"

    - alert: NodeInodeRootUsedPercentHigh
      expr: (1 - node_filesystem_files_free{fstype=~"ext4|xfs",mountpoint="/"} / node_filesystem_files{fstype=~"ext4|xfs",mountpoint="/"}) * 100 > 80
      for: 10m
      labels:
        severity: warning
        instance: "{{ $labels.instance }}"
      annotations:
        summary: "instance: {{ $labels.instance }} disk(/ 分区) inode 使用率过高"
        description: "Disk (/ 分区) inode 使用率超过 80%"
        value: "{{ $value }}"

    - alert: NodeInodeBootUsedPercentHigh
      expr: (1 - node_filesystem_files_free{fstype=~"ext4|xfs",mountpoint="/boot"} / node_filesystem_files{fstype=~"ext4|xfs",mountpoint="/boot"}) * 100 > 80
      for: 10m
      labels:
        severity: warning
        instance: "{{ $labels.instance }}"
      annotations:
        summary: "instance: {{ $labels.instance }} disk(/boot 分区) inode 使用率过高"
        description: "Disk (/boot 分区) inode 使用率超过 80%"
        value: "{{ $value }}"

    - alert: NodeFilefdAllocatedPercentHigh
      expr: node_filefd_allocated / node_filefd_maximum * 100 > 80
      for: 10m
      labels:
        severity: warning
        instance: "{{ $labels.instance }}"
      annotations:
        summary: "instance: {{ $labels.instance }} filefd 打开百分比过高"
        description: "Filefd 打开百分比 超过 80%"
        value: "{{ $value }}"

    - alert: NodeNetworkNetinBitRateHigh
      expr: avg by (instance) (irate(node_network_receive_bytes_total{device=~"eth0|eth1|ens33|ens37"}[1m]) * 8) > 20 * (1024 ^ 2) * 8
      for: 3m
      labels:
        severity: warning
        instance: "{{ $labels.instance }}"
      annotations:
        summary: "instance: {{ $labels.instance }} network 接收比特数 速率过高"
        description: "Network 接收比特数 速率超过 20MB/s"
        value: "{{ $value }}"

    - alert: NodeNetworkNetoutBitRateHigh
      expr: avg by (instance) (irate(node_network_transmit_bytes_total{device=~"eth0|eth1|ens33|ens37"}[1m]) * 8) > 20 * (1024 ^ 2) * 8
      for: 3m
      labels:
        severity: warning
        instance: "{{ $labels.instance }}"
      annotations:
        summary: "instance: {{ $labels.instance }} network 发送比特数 速率过高"
        description: "Network 发送比特数 速率超过 20MB/s"
        value: "{{ $value }}"

    - alert: NodeNetworkNetinPacketErrorRateHigh
      expr: avg by (instance) (irate(node_network_receive_errs_total{device=~"eth0|eth1|ens33|ens37"}[1m])) > 15
      for: 3m
      labels:
        severity: warning
        instance: "{{ $labels.instance }}"
      annotations:
        summary: "instance: {{ $labels.instance }} 接收错误包 速率过高"
        description: "Network 接收错误包 速率超过 15个/秒"
        value: "{{ $value }}"

    - alert: NodeNetworkNetoutPacketErrorRateHigh
      expr: avg by (instance) (irate(node_network_transmit_packets_total{device=~"eth0|eth1|ens33|ens37"}[1m])) > 15
      for: 3m
      labels:
        severity: warning
        instance: "{{ $labels.instance }}"
      annotations:
        summary: "instance: {{ $labels.instance }} 发送错误包 速率过高"
        description: "Network 发送错误包 速率超过 15个/秒"
        value: "{{ $value }}"

    - alert: NodeProcessBlockedHigh
      expr: node_procs_blocked{job="node"} > 10
      for: 10m
      labels:
        severity: warning
        instance: "{{ $labels.instance }}"
      annotations:
        summary: "instance: {{ $labels.instance }} 当前被阻塞的任务的数量过多"
        description: "Process 当前被阻塞的任务的数量超过 10个"
        value: "{{ $value }}"

    - alert: NodeTimeOffsetHigh
      expr: abs(node_timex_offset_seconds{job="node"}) > 3 * 60
      for: 2m
      labels:
        severity: info
        instance: "{{ $labels.instance }}"
      annotations:
        summary: "instance: {{ $labels.instance }} 时间偏差过大"
        description: "Time 节点的时间偏差超过 3m"
        value: "{{ $value }}"

3.3 钉钉机器人告警配置config.yml

/$dir/prom/alertmanager/config.yml

targets:
  webhook:
    url: https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxx             #修改为钉钉机器人的webhook
    mention:
      all: true

3.4 alertmanager.yml

/$dir/prom/alertmanager/alertmanager.yml

可以配置多个接收告警邮箱的邮箱

global:
  resolve_timeout: 5m
  smtp_smarthost: 'smtp.163.com:465'             #邮箱smtp服务器代理,启用SSL发信, 端口一般是465
  smtp_from: 'sender@163.com'              #发送邮箱名称
  smtp_auth_username: 'sender@163.com'              #邮箱名称
  smtp_auth_password: 'Authorization Code'                #授权码
  smtp_require_tls: false
 
route:
  receiver: 'default'
  group_wait: 10s
  group_interval: 1m
  repeat_interval: 1h
  group_by: ['alertname']
 
inhibit_rules:
- source_match:
    severity: 'critical'
  target_match:
    severity: 'warning'
  equal: ['alertname', 'instance']
  
receivers:
- name: 'default'
  email_configs:
  - to: 'receiver1@163.com,receiver2@163.com'
    send_resolved: true
  webhook_configs:
  - url: 'http://dingtalk:8060/dingtalk/webhook/send'
    send_resolved: true

备注:
以163邮箱为例:授权码需要是打开IMAP/SMTP服务、POP3/SMTP服务两个服务后新申请的授权码,第一次开启时自动给的授权码不行
在这里插入图片描述

四、docker-compose配置文件准备

/$dir/prom/prom.yml

version: '3.7'

services:

  dingtalk:
    image: timonwong/prometheus-webhook-dingtalk:latest
    container_name: prom_dingtalk
    environment:
      - TZ=Asia/Shanghai
    volumes:
      - type: bind
        source: ./alertmanager/config.yml
        target: /etc/prometheus-webhook-dingtalk/config.yml
        read_only: true
    ports:
      - "8060:8060"
    networks:
      - prom

  alertmanager:
    depends_on:
      - dingtalk
    image: prom/alertmanager:latest
    environment:
      - TZ=Asia/Shanghai
    container_name: alertmanager
    volumes:
      - type: bind
        source: ./alertmanager/alertmanager.yml
        target: /etc/alertmanager/alertmanager.yml
        read_only: true
    ports:
      - "9093:9093"
      - "9094:9094"
    networks:
      - prom
 
  prometheus:
    depends_on:
      - alertmanager
    image: prom/prometheus:latest
    container_name: prometheus
    # add configurations "user:root" to solve permission issues
    user: root
    environment:
      - TZ=Asia/Shanghai
    volumes:
      - type: bind
        source: ./prometheus/prometheus.yml
        target: /etc/prometheus/prometheus.yml
        read_only: true
      - type: bind
        source: ./prometheus/alert-rules.yml
        target: /etc/prometheus/alert-rules.yml
        read_only: true
      - type: volume
        source: prometheus
        target: /prometheus
    ports:
      - "9090:9090"
    networks:
      - prom

  grafana:
    depends_on:
      - prometheus
    image: grafana/grafana:latest
    environment:
      - TZ=Asia/Shanghai
    container_name: grafana
    volumes:
      - type: volume
        source: grafana
        target: /var/lib/grafana
    ports:
      - "3000:3000"
    networks:
      - prom

volumes:
  prometheus:
    driver: local
    driver_opts:
      type: none
      o: bind
      device: /data/prom/prometheus/data  
  grafana:
    driver: local
    driver_opts:
      type: none
      o: bind
      device: /data/prom/grafana  

networks:
  prom:
    driver: bridge

五、启动容器

docker-compose -f /$dir/prom/prom.yml up -d

启动完成后检查启动结果

docker-compose -f /$dir/prom/prom.yml ps
#输出
    Name                   Command               State                                         Ports
-------------------------------------------------------------------------------------------------------------------------------------------
alertmanager    /bin/alertmanager --config ...   Up      0.0.0.0:9093->9093/tcp,:::9093->9093/tcp, 0.0.0.0:9094->9094/tcp,:::9094->9094/tcp
granafa         /run.sh                          Up      0.0.0.0:3000->3000/tcp,:::3000->3000/tcp
prom_dingtalk   /bin/prometheus-webhook-di ...   Up      0.0.0.0:8060->8060/tcp,:::8060->8060/tcp
prometheus      /bin/prometheus --config.f ...   Up      0.0.0.0:9090->9090/tcp,:::9090->9090/tcp

备注:增加开启自启动

chmod a+x /etc/rc.local
# 追加启动命令
cat >> /etc/rc.local <<EOF
docker-compose -f /data/prom/prom.yml up -d
EOF

六、granafa配置数据源

数据源处url可以配置为http://ip:9090
也可以配置为http://prometheus:9090

七、service形式启动node-expoter

直接使用service形式启动感觉收集数据更准确,也不用特殊配置映射,简单一点。
如果使用docker容器形式,docker服务死了就没数据了,和物理机监控的目的背离了。

7.1 下载软件包

https://github.com/prometheus/node_exporter/releases
node_exporter的软件包下载地址,按照需求选择自己的版本即可。
我这里的例子中使用的是1.3.1版本

wget https://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_exporter-1.3.1.linux-amd64.tar.gz

7.2 执行部署脚本

脚本中node-expoter的端口设置的9100,可以根据自己环境的要求替换成不冲突的端口

#!/bin/bash

set -e
tar -xvf ./node_exporter-1.3.1.linux-amd64.tar.gz
if [ -d ./node_exporter-1.3.1.linux-amd64/ ];then
  sudo cp ./node_exporter-1.3.1.linux-amd64/node_exporter /usr/local/bin/
else
  echo "请检查 node_exporter 所在文件目录"
  exit 1
fi

sudo touch /usr/lib/systemd/system/node_exporter.service
sudo chmod o+w /usr/lib/systemd/system/node_exporter.service
sudo chmod o+x /usr/local/bin/node_exporter

cat << EOF > /usr/lib/systemd/system/node_exporter.service 
[Service]
User=$USER
Group=$USER
ExecStart=/usr/local/bin/node_exporter --web.listen-address=":9100"

[Install]
WantedBy=multi-user.target

[Unit]
Description=node_exporter
After=network.target 
EOF

sudo systemctl daemon-reload
sudo systemctl start node_exporter
sudo systemctl enable node_exporter
sudo systemctl status node_exporter
echo 'end success'

总结

好使,prometheus的轻度使用可以直接满足,重度使用的话还是要多增加一些配置,或是使用自己构建的镜像。

参考:https://blog.csdn.net/weixin_42900170/article/details/122886745
参考:https://blog.csdn.net/qq_33326449/article/details/120203438

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐