跳至主要內容

Prometheus+Grafana可视化监控

知识库运维技巧服务管理服务管理大约 2 分钟

主机准备

## 时间同步
imedatectl set-timezone Asia/Shanghai
yum -y install ntpdate
/usr/sbin/ntpdate -u ntp1.aliyun.com

echo "0 5 * * * /usr/sbin/ntpdate -u ntp1.aliyun.com >/dev/null &" >> /var/spool/cron/root
crontab -l

Prometheus

wget https://github.com/prometheus/prometheus/releases/download/v2.45.3/prometheus-2.45.3.linux-amd64.tar.gz

tar xzf prometheus-2.45.3.linux-amd64.tar.gz -C /usr/local/
ln -s /usr/local/prometheus-2.45.3.linux-amd64 /usr/local/prometheus

## 命令行启动
./prometheus --config.file=prometheus.yml &
## 热启动 
./prometheus --web.enable-lifecycle --config.file=prometheus.yml &
curl -XPOST http://127.0.0.1:9090/-/reload

## 注册服务
cat > /usr/lib/systemd/system/prometheus.service << EOF
[Unit]

[Service]
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml
ExecReload=/bin/kill -HUP \$MAINPID

[Install]
WantedBy=multi-user.target
Alias=dbus-org.fedoraproject.FirewallD1.service
EOF

## 服务启动
systemctl daemon-reload
systemctl status prometheus.service
systemctl start prometheus.service

## 端口访问
curl http://192.168.60.128:9090

Grafana

Grafana 是一个开源的度量分析及可视化套件。通过访问数据库(如InfluxDB、Prometheus),展示自定义图表。

## yum install -y https://dl.grafana.com/enterprise/release/grafana-enterprise-10.3.1-1.x86_64.rpm
yum install -y https://mirrors.huaweicloud.com/grafana/10.3.1/grafana-enterprise-10.3.1-1.x86_64.rpm

ls -l /usr/share/grafana/bin/
systemctl start grafana-server.service
systemctl status grafana-server.service

curl http://192.168.60.128:3000  ## 登录 admin/admin

Exporter

Exporter 是 Prometheus 推出的针对服务器状态监控的 Metrics 工具。目前开发中常见的组件都有对应的 exporter 可以直接使用。常见的有两大类,一种是社区提供的,包含数据库,消息队列,存储,HTTP服务,日志等,比如 node_exporter,mysqld_exporter等;还有一种是用户自定义的 exporter,可以基于官方提供的 Client Library 创建自己的 exporter 程序。
每个 exporter 的一个实例被称为 target,Prometheus 通过轮询的方式定期从这些 target 中获取样本数据。

监控MySQL

wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.15.0/mysqld_exporter-0.15.0.linux-amd64.tar.gz
tar xzf mysqld_exporter-0.15.0.linux-amd64.tar.gz
mkdir -p /usr/local/prometheus-exporter/mysql && mv mysqld_exporter-0.15.0.linux-amd64/mysqld_exporter /usr/local/prometheus-exporter/mysql/
cd /usr/local/prometheus-exporter/mysql

## mysql授权
CREATE USER 'exporter'@'127.0.0.1' IDENTIFIED BY 'exporter' WITH MAX_USER_CONNECTIONS 3;
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'127.0.0.1';
flush privileges;
## 或者
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost' IDENTIFIED BY 'expoter12Ssdc3' WITH MAX_USER_CONNECTIONS 3;
flush privileges;

## 配置Prometheus监控数据库用户信息
cat > .my.cnf <<EOF
[client]
host=127.0.0.1
user=exporter
password=exporter
EOF

## 配置systemd管理
cat > /usr/lib/systemd/system/mysqld_exporter.service << EOF
[Unit]
Description=mysqld_exporter Service
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/prometheus-exporter/mysql/mysqld_exporter --config.my-cnf=/usr/local/prometheus-exporter/mysql/.my.cnf
ExecReload=/bin/kill -HUP \$MAINPID
Restart=on-failure
RestartSec=30s

[Install]
WantedBy=multi-user.target
EOF

systemctl status  mysqld_exporter

##  端口查看
ss -anput |grep 9104

配置Prometheus监控

vi /usr/local/prometheus/prometheus.yml

  - job_name: "MySQL_115"
    static_configs:
      - targets: ["127.0.0.1:9104"]

systemctl restart prometheus.service