学习参考文章:
https://blog.csdn.net/qq_31725371/article/details/114697770
https://www.cnblogs.com/poloyy/p/12375039.html
文章目录
1、Prometheus普罗米修斯
1.1 Prometheus工具介绍
Prometheus 是一款基于时序数据库的开源监控告警系统,非常适合Kubernetes集群的监控。Prometheus的基本原理是通过HTTP协议周期性抓取被监控组件的状态,任意组件只要提供对应的HTTP接口就可以接入监控。不需要任何SDK或者其他的集成过程。这样做非常适合做虚拟化环境监控系统,比如VM、Docker、Kubernetes等。输出被监控组件信息的HTTP接口被叫做exporter 。目前互联网公司常用的组件大部分都有exporter可以直接使用,比如Varnish、Haproxy、Nginx、MySQL、Linux系统信息(包括磁盘、内存、CPU、网络等等)。
1.2 Centos7.x下安装与使用Prometheus
普罗米修斯下载网址:https://prometheus.io/download/
下载安装Prometheus
PROM_PATH='/data/prometheus'
mkdir -p ${PROM_PATH}
mkdir -p ${PROM_PATH}/{data,conf,logs,bin}
useradd prometheus
cd /usr/local/src
wget https://github.com/prometheus/prometheus/releases/download/v2.13.0/prometheus-2.13.0.linux-amd64.tar.gz
tar -xvf prometheus-2.13.0.linux-amd64.tar.gz
cd prometheus-2.13.0.linux-amd64/
cp prometheus promtool ${PROM_PATH}/bin/
cp prometheus.yml ${PROM_PATH}/conf/
chown -R prometheus.prometheus /data/prometheus
# Setting Variables
cat >> /etc/profile <<EOF
PATH=/data/prometheus/bin:$PATH:$HOME/bin
EOF
为了方便使用,将Prometheus配置为系统服务
cat >>/etc/systemd/system/prometheus.service <<EOF
[Unit]
Description=Prometheus
Documentation=https://prometheus.io/
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/data/prometheus/bin/prometheus --config.file=/data/prometheus/conf/prometheus.yml --storage.tsdb.path=/data/prometheus/data --storage.tsdb.retention=90d
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
生效系统system文件和启动服务
systemctl daemon-reload
systemctl enable prometheus.service
systemctl start prometheus.service
服务访问地址:http://IP:9090
出现以上界面说明服务搭建成功。
1.3 客户端安装node_exporter
在待监控的服务器上安装node_exporter
NODE_PATH='/data/prometheus/node_exporter/'
cd /usr/local/src/
mkdir -p ${NODE_PATH}
wget https://github.com/prometheus/node_exporter/releases/download/v0.18.0/node_exporter-0.18.0.linux-amd64.tar.gz
tar -xvf node_exporter-0.18.0.linux-amd64.tar.gz
cp node_exporter-0.18.0.linux-amd64/node_exporter ${NODE_PATH}
chown -R prometheus.prometheus ${NODE_PATH}
为了方便使用,将node_exporter配置为系统服务
cat > /lib/systemd/system/node_exporter.service <<EOF
[Unit]
Description=node_exporter
Documentation=https://prometheus.io/
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/data/prometheus/node_exporter/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
生效系统system文件和启动服务
systemctl daemon-reload
systemctl enable node_exporter.service
systemctl start node_exporter.service
服务访问地址:http://IP:9100
出现上图说明node_exporter环境搭建成功
1.4 Prometheus服务端配置文件添加监控项
prometheus.yml 配置解释
# my global config
global:
# 默认情况下,每15s拉取一次目标采样点数据。
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
# 每15秒评估一次规则。默认值为每1分钟。
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:
# job名称会增加到拉取到的所有采样点上,同时还有一个instance目标服务的host:port标签也会增加到采样点上
- job_name: 'prometheus'
# 覆盖global的采样点,拉取时间间隔5s
scrape_interval: 5s
static_configs:
- targets: ['localhost:9090']
prometheus.yml配置文件添加监控项,路径是/data/prometheus/conf
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['192.168.2.45:9090']
- job_name: 'linux'
static_configs:
- targets: ['192.168.2.44:9100','192.168.2.43:9100'] # 多个用,分开
# 添加上面三行,可添加多个node_exporter地址
重启prometheus服务
systemctl restart prometheus.service
打开prometheus监控页面,Status -> Targets 查看监控的node_exporter
2、Grafana 展示 Prometheus 数据
2.1.安装Grafana
Grafana官网下载路径:https://grafana.com/grafana/download
国内镜像下载地址:https://mirrors.huaweicloud.com/grafana/
下载安装命令
yum install -y https://mirrors.huaweicloud.com/grafana/9.3.2/grafana-enterprise-9.3.2-1.x86_64.rpm
启动Grafana
systemctl start grafana-server.service
服务访问地址:http://IP:3000
出现以上界面说明Grafana搭建成功
2.1.Grafana 展示 Prometheus 数据
添加数据源
Configuration -> Data Sources ->add data source -> Prometheus
新增Dashboard数据展示
出现以上界面说明已经完成服务器监控。