监控系统主要针对硬件和软件进行监控和告警。企业的 IT 架构逐步从传统的物理服务器,迁移到以虚拟机为主导的 IaaS 云。无论基础架构如何调整,都离不开监控系统的支持。

关于 Prometheus

  目主流的开源监控系统有 Zabbix、Prometheus,夜莺等监控系统。而 Prometheus、Zabbix 占据了大量的份额。Zabbix 在监控界占有不可撼动的地位,功能强大。但容器监控能力较弱。为解决监控容器的问题,谷歌开发了 Prometheus。Prometheus 存储的是时序数列。

Prometheus 安装

下载

  点击 官方网站 进入 Prometheus 官网。这里选择版本为 2.30.3,Linux 操作系统。点击 下载 进入下载。

1
[root@prometheus ~]# wget https://github.com/prometheus/prometheus/releases/download/v2.30.3/prometheus-2.30.3.linux-amd64.tar.gz

解压安装包

  个人喜欢把安装包放于/opt 目录下,然后软连接到/usr/local/下。

1
2
3
[root@prometheus ~]# tar -zxvf prometheus-2.30.3.linux-amd64.tar.gz -C /opt
[root@prometheus ~]# mv /opt/prometheus-2.30.3.linux-amd64 /opt/prometheus
[root@prometheus ~]# ln -s /opt/prometheus /usr/local/prometheus

添加 prometheus.service 并启动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@prometheus ~]# cat > /usr/lib/systemd/system/prometheus.service << EOF
[Unit]
Description=prometheus service

[Service]
User=root
ExecStart=/opt/prometheus/prometheus \\ # 双斜线保证不会被转义掉,建议使用是去掉 service 中的注释
--web.enable-lifecycle \\ # 开启热加载
--config.file=/opt/prometheus/prometheus.yml \\ # 配置文件
--storage.tsdb.path=/data/prometheus/data \\ # 存储位置
--storage.tsdb.retention.time=180d \\

TimeoutStopSec=10
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target
EOF
[root@prometheus ~]# systemctl daemon-reload
[root@prometheus ~]# systemctl enable --now prometheus.service

配置信息

  启动之后我们可以在/usr/local/prometheus/prometheus.yml 增加监控项。这里贴出个人的一个简单配置。

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
# 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:
- 192.168.0.252:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
- "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"
static_configs:
- targets: ["localhost:9090"]
# 这里添加了keepalived_exporter的配置。
- job_name: "node_exporter-Template"
consul_sd_configs:
- server: '192.168.0.250:8500'
services: []
relabel_configs:
- source_labels: [__meta_consul_tags] # 这里通过tag来进行分组,按照不同组展示不同的任务
regex: .*node.*
action: keep
- source_lables: [__meta_consul_service] # 这里将consul中的meta相关的key-value自定义标签信息保存为lable供告警等做选择项
regex: __meta_consul_service_metadata_(.*)
replacement: consul_$1
action: labelmap

  修改了配置文件,使用下面命令热加载配置文件。

1
[root@prometheus ~]# curl -X POST http://192.168.0.254:9090/-/reload