Skip to content

Linux搭建 Granfa+Prometheus 主机监控服务

提示

本文环境如下:

  1. 操作系统: Ubuntu Server 22.04
  2. docker version: 27.5.1

依次下载以下工具包

Prometheus Prometheus安装包 node_exporter 主机端点监控服务

安装 Prometheus

  1. 解压
shell
tar zxvf prometheus-3.2.0.linux-amd64.tar.gz
cd prometheus-3.2.0.linux-amd64
  1. 修改配置
yaml
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"]
  - job_name: "Linux"
    file_sd_configs:
      - files:
          - '/opt/prometheus-3.2.0.linux-amd64/config/*.yaml' # /opt/prometheus-3.2.0.linux-amd64/config 此处是我的动态配置发现文件的目录,可根据自身需求变更
  1. 创建 Prometheus 系统服务
shell
cat <<EOF > /lib/systemd/system/prometheus.service
[Unit]
Description=Prometheus Monitoring System
Documentation=https://prometheus.io/docs/introduction/overview/
After=network.target

[Service]
ExecStart=/opt/prometheus-3.2.0.linux-amd64/prometheus \
  --config.file=/opt/prometheus-3.2.0.linux-amd64/prometheus.yml \
  --web.enable-lifecycle
ExecReload=/bin/kill -HUP \$MAINPID
Restart=always
RestartSec=3
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF

# 重新加载服务
sudo systemctl daemon-reload
# 启动 Prometheus
sudo systemctl enable --now prometheus

安装监控工具 node_exporter (此步骤在需要监控的主机上操作)

  1. 解压
shell
tar zxvf node_exporter-1.9.0.linux-amd64.tar.gz
cd node_exporter-1.9.0.linux-amd64
  1. 创建 node_exporter 系统服务
shell
cat <<EOF > /lib/systemd/system/node-exporter.service
[Unit]
Description=Node Exporter
Documentation=https://prometheus.io/docs/introduction/overview/
After=network.target

[Service]
ExecStart=/opt/node_exporter-1.9.0.linux-amd64/node_exporter \
ExecReload=/bin/kill -HUP \$MAINPID
Restart=always
RestartSec=3
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF

# 重新加载服务
sudo systemctl daemon-reload
# 启动 node-exporter
sudo systemctl enable --now node-exporter

修改 Prometheus 配置

  1. 找到一开始 prometheus.yaml 中写入的 文件服务发现 配置目录
text
/opt/prometheus-3.2.0.linux-amd64/config/
  1. 写入需要监控的主机
yaml
- targets: ['192.168.1.71:9100']
  labels: # 可自定义需要的标签,方便后续检索使用
    server-name: qgzz71

基于 MIT 许可发布