1.环境准备

  • 节点

Centos7

IP

prometheus & grafana&prometheus node

192.168.0.xxx

  • 软件包

Prometheus-2.45.1: https://github.com/prometheus/prometheus/releases/download/v2.45.1/prometheus-2.45.1.linux-amd64.tar.gz

node_exporter-1.6.1: https://github.com/prometheus/node_exporter/releases/download/v1.6.1/node_exporter-1.6.1.linux-amd64.tar.gz

Grafana-10.2.0: https://dl.grafana.com/enterprise/release/grafana-enterprise-10.2.0-1.x86_64.rpm

alertmanager-0.27.0: https://github.com/prometheus/alertmanager/releases/download/v0.27.0/alertmanager-0.27.0.linux-amd64.tar.gz

2.部署prometheus

下载prometheus部署包

wget https://github.com/prometheus/prometheus/releases/download/v2.45.1/prometheus-2.45.1.linux-amd64.tar.gz
  • 1.

解压二进制文件

tar  -zxvf  prometheus-2.45.1.linux-amd64.tar.gz  -C   /data/
cd   /data/
mv  ./prometheus-2.45.1.linux-amd64   prometheus
  • 1.
  • 2.
  • 3.

严重

[root@dev-vm-2 /]# cd /data/prometheus/
[root@dev-vm-2 prometheus]# ./prometheus  --version
prometheus, version 2.45.1 (branch: HEAD, revision: c62de5d1075d9c24a281b4b719f124f14e730fa5)
  build user:       root@827ead67e852
  build date:       20230929-21:10:51
  go version:       go1.20.8
  platform:         linux/amd64
  tags:             netgo,builtinassets,stringlabels
[root@dev-vm-2 prometheus]#
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

新建用户

groupadd  prometheus
useradd  -g  prometheus -s /sbin/nologin prometheus
  • 1.
  • 2.

修改安装目录权限

chown -R  prometheus:prometheus /data/prometheus/
  • 1.

创建prometheus运行数据目录

mkdir  -p  /data/prometheus/data
chown -R prometheus:prometheus /data/prometheus/data
  • 1.
  • 2.

配置文件

 prometheus监控系统,prometheus的职责是采集,查询和存储和推送报警到alertmanager。本文主要介绍下prometheus的配置文件。

1.配置文件格式

配置文件格式是yaml格式

.yml或者.yaml 都是 yaml格式的文件。
yaml格式json交互比较容易,python/go/java/php 有yaml格式库,方便语言之间解析,并且这种格式存储的信息量很大。
  • 1.
  • 2.

2.配置文件指标说明

cat prometheus.yml 
# my global config
global:
  scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
    - static_configs:
        - targets:
          # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: "prometheus"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["localhost:9090"]
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
global指标
# my global config
global:
   scrape_interval: 15s        # 默认15s 全局每次数据收集的间隔
   evaluation_interval: 15s    # 规则扫描时间间隔是15秒,默认不填写是 1分钟
   scrape_timeout: 5s          # 超时时间
   external_labels:            # 用于外部系统标签的,不是用于metrics(度量)数据
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 常用的命令行参数
./prometheus -h
  --config.file="/opt/prometheus/prometheus.yml"  # 读取指定配置文件
  --web.listen-address="0.0.0.0:9090"  # 指定prometheus运行端口 
  --log.level=info # 日志级别
  --alertmanager.timeout=10s # 与报警组件的超时时间
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
alerting
# Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

这里定义和prometheus集成的alertmanager插件,用于监控报警。后续会单独进行alertmanger插件的配置、配置说明、报警媒介以及route路由规则记录。

rule_files

这个主要是用来设置告警规则,基于设定什么指标进行报警(类似触发器trigger)。这里设定好规则以后,prometheus会根据全局global设定的evaluation_interval参数进行扫描加载,规则改动后会自动加载。其报警媒介和route路由由alertmanager插件实现。

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"
  • 1.
  • 2.
  • 3.
  • 4.
scrape_configs 配置采集目标 endpoints
scrape_configs 默认规则:
scrape_configs:
  # The job name is added as a label `job=` to any timeseries scraped from this config.
 - job_name: 'prometheus'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
    - targets: ['localhost:9090']
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

支持的配置:

job_name: 任务目标名,可以理解成分组,每个分组包含具体的target组员。

scrape_interval: 5s 单独设定的话,会覆盖global设定的参数,拉取时间间隔为5s

metrics_path 监控项访问的url路径

targets: Endpoint 监控目标访问地址

上述为静态规则,没有设置自动发现。这种情况下增加主机需要自行修改规则,通过supervisor reload 对应任务,也是缺点:每次静态规则添加都要重启prometheus服务,不利于运维自动化。 也可以通过热加载的方式进行。

prometheus支持服务发现:

基于文件的服务发现方式不需要依赖其他平台与第三方服务,用户只需将要新的target信息以yaml或json文件格式添加到target文件中 ,prometheus会定期从指定文件中读取target信息并更新 好处: (1)不需要一个一个的手工去添加到主配置文件,只需要提交到要加载目录里边的json或yaml文件就可以了; (2)方便维护,且不需要每次都重启prometheus服务端。

Prometheus提供两种重新加载配置文件prometheus.yml的方法: 1.向prometheus进行发信号

kill -HUP  pid
  • 1.

2.向prometheus发送HTTP请求

curl -XPOST http://127.0.0.1:9090/-/reload

“/-/reload” 只接收POST请求,并且需要在启动prometheus进程时,指定 “–web.enable-lifecycle”
  • 1.
  • 2.
  • 3.

创建服务脚本systemd管理

vim  /usr/lib/systemd/system/prometheus.service

[Unit]
Description=Prometheus
After=network.target

[Service]
Type=simple
User=prometheus
Group=prometheus
ExecStart=/data/prometheus/prometheus \
--config.file=/data/prometheus/prometheus.yml \
--storage.tsdb.path=/data/prometheus/data \
--storage.tsdb.retention.time=15d \
--web.console.templates=/data/prometheus/consoles \
--web.console.libraries=/data/prometheus/console_libraries \
--web.max-connections=512 \
--web.external-url "http://192.168.0.234:9090" \
--web.listen-address "0.0.0.0:9090" \
--web.enable-admin-api \
--web.enable-lifecycle
Restart=on-failure

[Install]
WantedBy=multi-user.target
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
启动并验证
  • 查看服务状态
systemctl daemon-reload
systemctl enable prometheus
systemctl start  prometheus
systemctl status prometheus
  • 1.
  • 2.
  • 3.
  • 4.
  • 查看服务端口
[root@dev-vm-2 prometheus]# ss -tunlp | grep 9090
tcp    LISTEN     0      128    [::]:9090               [::]:*                   users:(("prometheus",pid=24334,fd=7))
[root@dev-vm-2 prometheus]#
  • 1.
  • 2.
  • 3.
  • 配置文件语法检测
[root@dev-vm-2 prometheus]# ./promtool check config prometheus.yml
Checking prometheus.yml
 SUCCESS: prometheus.yml is valid prometheus config file syntax

[root@dev-vm-2 prometheus]#
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

访问web页面  http://192.168.0.234:9090

在Status菜单下,Configuration,Rule,Targets等:

Status–>Configuration展示prometheus.yml的配置,如下:

Status–>Targets展示监控具体的监控目标:

访问: http://192.168.0.234:9090/metrics,查看从exporter具体能抓到的数据:

在输入框中任意输入1个exporter能抓取得值,点击"Execute"按钮,即可见相应抓取数据的图形,数据以table和Graph:

3.部署node_exporter

Node_exporter收集机器的系统数据,这里采用prometheus官方提供的exporter,除node_exporter外,官方还提供consul,memcached,haproxy,mysqld等exporter,具体可查看官网。

安装 node_exporter

下载软件包

wget https://github.com/prometheus/node_exporter/releases/download/v1.6.1/node_exporter-1.6.1.linux-amd64.tar.gz
  • 1.

解压缩文件

tar -zxvf node_exporter-1.6.1.linux-amd64.tar.gz -C /data/
cd /data/
mv node_exporter-1.6.1.linux-amd64/  node_exporter
  • 1.
  • 2.
  • 3.

修改安装目录权限

chown -R prometheus:prometheus /data/node_exporter
  • 1.

注册服务

vim  /lib/systemd/system/node_exporter.service
[Unit]
Descriptinotallow=Prometheus Node_exporter
After=network.target prometheus.service

[Service]
Type=simple
User=prometheus
Group=prometheus
ExecStart=/data/node_exporter/node_exporter --web.listen-address=0.0.0.0:9101
Restart=on-failure

[Install]
WantedBy=multi-user.target
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

启动服务

systemctl daemon-reload
systemctl enable node_exporter
systemctl start node_exporter
  • 1.
  • 2.
  • 3.

验证

查看 node_exporter服务 http://192.168.0.234:9101/metrics (默认端口为: 9101,默认9100)

添加 node_exporter到配置文件

安装好 node_exporter 后,我们需要在 Prometheus 中对其进行集成 1.修改 Prometheus 的配置文件 (prometheus.yml),添加 node 服务 (在 scrape_configs 属性下添加)

cat >> prometheus.yml <<EOF
  - job_name: 'test'
    static_configs:
    - targets: ['NodeIP:9101']
EOF
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

重启prometheus 服务

systemctl restart prometheus
  • 1.

4.部署grafana

下载

wget https://dl.grafana.com/enterprise/release/grafana-enterprise-10.2.0-1.x86_64.rpm
  • 1.

安装

yum -y install grafana-enterprise-10.2.0-1.x86_64.rpm
  • 1.

配置文件

配置文件位于/etc/grafana/grafana.ini,保持默认配置即可。
  • 1.

设置开机启动

systemctl enable grafana-server
systemctl start grafana-server
  • 1.
  • 2.

添加数据源

1.登录

访问: http://192.168.0.xxx:3000,默认账号/密码:admin/admin,首次登陆需要修改默认的管理员密码

2.添加数据源

在登陆首页,点击”Add data source”按钮,跳转到添加数据源页面,配置如下:

填写 URL: http://localhost:9090/:

设置页面为中文

vim /usr/share/grafana/conf/defaults.ini

#default_language = en-US
default_language = zh-Hans

重启grafana服务
systemctl restart grafana-server
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

导入主机详情模板:英文版-11074,中文-16098

5.部署alertmanager

下载

wget https://github.com/prometheus/alertmanager/releases/download/v0.27.0/alertmanager-0.27.0.linux-amd64.tar.gz
  • 1.

解压

tar -zxvf alertmanager-0.27.0.linux-amd64.tar.gz -C /data/
cd /data
mv alertmanager-0.27.0.linux-amd64 alertmanager
  • 1.
  • 2.
  • 3.

修改安装目录权限

mkdir -p /data/alertmanager/data
chown -R prometheus:prometheus /data/alertmanager
  • 1.
  • 2.

查看版本信息

[root@dev-vm-2 alertmanager]# ./alertmanager --version
alertmanager, version 0.27.0 (branch: HEAD, revision: 0aa3c2aad14cff039931923ab16b26b7481783b5)
  build user:       root@22cd11f671e9
  build date:       20240228-11:51:20
  go version:       go1.21.7
  platform:         linux/amd64
  tags:             netgo
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

设置开机启动

vim /usr/lib/systemd/system/alertmanager.service

[Unit]
Descriptinotallow=alertmanager server daemon
Documentatinotallow=https://prometheus.io/docs/introduction/overview/
After=network.target

[Service]
ExecStart=/data/alertmanager/alertmanager --config.file=/data/alertmanager/alertmanager.yml --storage.path=/data/alertmanager/data
ExecReload=/bin/kill -HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.

启动服务

systemctl daemon-reload
systemctl start alertmanager
systemctl status alertmanager
systemctl enable alertmanager
systemctl stop alertmaneger
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

访问: http://192.168.0.xxx:9093

访问: http://192.168.0.xxx:9093/metrics

6.配置企业微信报警

修改prometheus.yml配置文件

# Alertmanager configuration
# 改为alertmanager的地址
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      - 192.168.0.234:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
# 指定规则文件
rule_files:
  - "/data/prometheus/rules/*.yml"
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

添加告警规则模板

#在prometheus目录新建 rules 目录
mkdir rules

***************** vi node_alived.yml **************8
groups:
- name: 主机状态-监控告警
  rules:
  - alert: 主机状态
    expr: up *on(instance)group_left(nodename)(node_uname_info) == 0
    for: 5m
    labels:
      level: waring
    annotations:
      summary: "{{$labels.instance}}:服务器宕机"
      description: "{{$labels.instance}}({{$labels.nodename}}):服务器延时超过3分钟"

********************* vi memory_over.yml*************************
groups:
- name: 主机内存使用情况-监控告警
  rules:
  - alert: 主机内存使用情况
    expr: (node_memory_MemTotal_bytes - (node_memory_MemFree_bytes + node_memory_Buffers_bytes + node_memory_Cached_bytes)) / node_memory_MemTotal_bytes* 100 *on(instance)group_left(nodename)(node_uname_info) > 80
    for: 3m
    labels:
      level: waring
    annotations:
      summary: "{{$labels.instance}}: High Memory usage detected"
      description: "{{$labels.instance}}({{$labels.nodename}}): 内存使用率超过 80% (当前使用率: {{ $value }}%)"

*************** vi cpu_over.yml *******************************
groups:
- name: 主机cpu使用情况-监控告警
  rules:
  - alert: 主机cpu使用情况
    expr: 100-avg(irate(node_cpu_seconds_total{mode="idle"}[5m])) by(instance) *100  *on(instance)group_left(nodename)(node_uname_info) > 80
    for: 3m
    labels:
      level: waring
    annotations:
      summary: "{{ $labels.instance }}cpu使用率过高"
      description: "{{ $labels.instance }}({{$labels.nodename}}):cpu使用率超过80%(当前使用率: {{ $value }}%)"

**************** vi disk_over.yml******************************
groups:
- name: 主机磁盘使用情况-监控告警
  rules:
  - alert: 主机磁盘使用情况
    expr: 100-(node_filesystem_free_bytes{fstype=~"ext4|xfs"}/node_filesystem_size_bytes {fstype=~"ext4|xfs"})*100 *on(instance)group_left(nodename)(node_uname_info)  > 80
    for: 3m
    labels:
      level: waring
    annotations:
      summary: "{{ $labels.instance }} 磁盘空间使用率过高!"
      description: "{{ $labels.instance }}({{$labels.nodename}}): 磁盘空间使用大于80%(当前使用率: {{$value}}%)"
      
**************** vi disk_io.yml******************************
groups:
- name: 磁盘IO性能-监控告警
  rules:
  - alert: 磁盘IO性能
    expr: 100-(avg(irate(node_disk_io_time_seconds_total[1m])) by(instance) *100) *on(instance)group_left(nodename)(node_uname_info)   < 60
    for: 3m
    labels:
      level: waring
    annotations:
      summary: "{{ $labels.instance }} 流入磁盘IO使用率过高!"
      description: "{{ $labels.instance }}({{$labels.nodename}}): 流入磁盘IO大于60%(当前使用率: {{$value}}%)"
      
********************* vi outside_network.yml*************************
groups:
- name: outside网络-监控告警
  rules:
  - alert: outside网络
    expr: ((sum(rate (node_network_transmit_bytes_total{device!~'tap.*|veth.*|br.*|docker.*|virbr*|lo*'}[5m])) by (instance)) / 100 ) *on(instance)group_left(nodename)(node_uname_info) > 204800
    for: 3m
    labels:
      level: waring
    annotations:
      summary: "{{ $labels.instance }} 流出网络带宽过高!"
      description: "{{ $labels.instance }}({{$labels.nodename}}): 流出网络带宽持续2分钟高于200M(当前使用: {{$value}})"

*************** vi inside_network.yml *******************************
groups:
- name: inside网络-监控告警
  rules:
  - alert: inside网络
    expr: ((sum(rate (node_network_receive_bytes_total{device!~'tap.*|veth.*|br.*|docker.*|virbr*|lo*'}[5m])) by (instance)) / 100) *on(instance)group_left(nodename)(node_uname_info)   > 204800
    for: 3m
    labels:
      level: waring
    annotations:
      summary: "{{ $labels.instance }} 流入网络带宽过高!"
      description: "{{ $labels.instance }}({{$labels.nodename}}): 流入网络带宽持续2分钟高于200M(当前使用: {{$value}})"

**************** vi tcp_connections.yml******************************
groups:
- name: TCP会话-监控告警
  rules:
  - alert: TCP会话
    expr: node_netstat_Tcp_CurrEstab *on(instance)group_left(nodename)(node_uname_info)  > 1000
    for: 3m
    labels:
      level: waring
    annotations:
      summary: "{{ $labels.instance }} TCP_ESTABLISHED过高!"
      description: "{{ $labels.instance }}({{$labels.nodename}}): TCP_ESTABLISHED大于1000%(当前使用率: {{$value}}%)"
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.

重启prometheus,访问页面查看

安装docker

yum install -y yum-utils
#设置镜像仓库地址
 yum-config-manager \
  --add-repo \
   http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
   
yum makecache fase 

yum install docker-ce docker-ce-cli containerd.io
#启动docker
systemctl  start  docker
#查看docker版本
docker version
#测试
docker run hello-world
docker  images  /docker ps 
systemctl enable docker

添加json配置
vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://78q96cy9.mirror.aliyuncs.com"]
} 

systemctl daemon-reload #刷新配置
systemctl start docker  # 启动docker服务
systemctl stop docker  # 停止docker服务
systemctl restart docker  # 重启docker服务
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.

企业微信机器人配置及启动

群聊新建机器人,获取hook地址

启动企业微信机器人

docker run -d --name wechat \
--restart always -p 9081:80 \
guyongquan/webhook-adapter \
--adapter=/app/prometheusalert/wx.js=/wx=https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxx(自己的微信机器人key)
  • 1.
  • 2.
  • 3.
  • 4.

修改alertmanager.yml配置文件

receivers:
- name: 'web.hook'
  webhook_configs:
  - url: 'http://127.0.0.1:9081/dapter/wx'  #容器映射端口
    send_resolved: true
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

检查配置文件语法

[root@dev-vm-2 alertmanager]# ./amtool check-config alertmanager.yml 
Checking 'alertmanager.yml'  SUCCESS
Found:
 - global config
 - route
 - 1 inhibit rules
 - 1 receivers
 - 1 templates
  SUCCESS
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

重启alertmanager服务

systemctl restart alertmanager
  • 1.