Prometheus + Grafana 展示服务器运行状态

✨ Prometheus 和 Grafana 是一对开源工具,用于监控和可视化服务器运行状态。我们可以使用 Docker 来快速部署它们。首先,在 docker-compose.yml 文件中配置 Prometheus 和 Grafana 的容器。然后,我们需要安装 Node-exporter 来采集服务器的运行指标。最后,我们通过 Grafana 的 Web 界面配置数据源和仪表盘,实现服务器状态的可视化。

Prometheus 是一个开源的基于时间序列数据库的监控和警报工具,它可以帮助您收集、存储和查询服务器性能数据。
Grafana 则是一个强大的数据可视化工具,可以将 Prometheus 收集的数据呈现为漂亮的仪表盘和图表。

之前在 Kubernetes 集群监控中搭建使用过 Prometheus 和 Grafana ,因为新购买了服务器,没有和之前一样部署 Server Status / 哪吒面板 之类的探针,就想着直接用 Prometheus 和 Grafana 来监控服务器状态。

Docker compose 启动

为了方便,直接用 Docker 来安装,Docker 安装步骤省略。

docker-compose.yml

version: '3.9'
services:
  prometheus:
    image: prom/prometheus
    container_name: prometheus
    restart: always
    ports:
      - 9090:9090
    environment:
      TZ: Asia/Shanghai
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
      - prometheus-data:/prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'

  grafana:
    image: grafana/grafana
    container_name: grafana
    restart: always
    ports:
      - 3000:3000
    environment:
      TZ: Asia/Shanghai
    volumes:
      - grafana-data:/var/lib/grafana


volumes:
  prometheus-data:
  grafana-data:

其中 prometheus.yml

global:
    scrape_interval: 15s

scrape_configs:
    - job_name: 'prometheus'
      static_configs:
          - targets:
                - 'localhost:9090'
            labels:
                instance: prometheus 

    - job_name: 'node-exporter'
      static_configs:
          - targets:
                - '<主机IP>:9100' # 此处根据实际情况替换
            labels:
                instance: vps

通过 docker-compose 创建启动

Node-exporter

https://github.com/prometheus/node_exporter

既然要监控服务器运行状态,那么我们就需要使用 Node-exporter 来采集主机的运行指标如CPU, 内存,磁盘等信息。因为是直接读取访问主机系统,就不再使用容器形式了,直接使用上面仓库中的二进制文件进行安装。

curl -OL https://github.com/prometheus/node_exporter/releases/download/v1.6.1/node_exporter-1.6.1.linux-amd64.tar.gz
tar -zxf node_exporter-1.6.1.linux-amd64.tar.gz
cd node_exporter-1.6.1.linux-amd64/
cp node_exporter /usr/loccal/bin

我们可以手动执行 node_exporter来测试一下,启动成功会出现如下日志:

caller=tls_config.go:277 level=info msg="TLS is disabled." http2=false address=[::]:9100

为了使 node_exporter 稳定运行,我们为其创建系统服务

cat > /etc/systemd/system/node_exporter.service <<EOF
[Unit]
Description=Node Exporter

[Service]
User=root
ExecStart=/usr/local/bin/node_exporter

[Install]
WantedBy=multi-user.target
EOF

sudo systemctl daemon-reload
# 启动并设置开机自启
sudo systemctl enable --now node_exporter.service

以上安装node_exporter的操作整理为 Shell 脚本如下:

#!/bin/bash

[ -d /app ] || mkdir -p /app
cd /app
[ -f /app/node_exporter ] || wget https://github.com/prometheus/node_exporter/releases/download/v1.6.1/node_exporter-1.6.1.linux-amd64.tar.gz
tar xf node_exporter-1.6.1.linux-amd64.tar.gz
cp node_exporter-1.6.1.linux-amd64/node_exporter /usr/local/bin/ && rm -rf /app/node_exporter-1.6.1.linux-amd64

cat >/etc/systemd/system/node_exporter.service <<EOF
[Unit]
Description=node_exporter
Documentation=https://prometheus.io/
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/bin/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable --now node_exporter

Grafana 配置

通过上述操作我们已经完成了基本配置,下面可以通过 http://<主机IP>:3000 进入 Grafana 的 Web 界面,默认用户名和密码均为 admin 。

可根据界面中的引导先添加好数据源,再配置 dashboard 。

此处URL直接使用http://prometheus:9090(容器名),当然也可以直接用默认的 http://localhost:9090

可视化面板我们导入官网上的案例,省去自己逐个编辑调试。

https://grafana.com/grafana/dashboards/1860-node-exporter-full/

最终得到如下界面

更多参考