prometheus+grafana 監控nginx

麦兜的大表哥發表於2024-10-12

一:安裝外掛

1.1 檢視nginx已啟用tub_status模組

nginx -V 2>&1 | grep -o with-http_stub_status_module 如果在終端輸出with-http_stub_status_module,說明nginx已啟用tub_status模組

在nginx 的配置檔案新增如下配置

    location /nginx_status {
        stub_status on;
        access_log off;
        allow 127.0.0.1;
        deny all;
       }

1.2 下載外掛nginx-prometheus-exporte 外掛

nginx-prometheus-exporter用於把nginx的效能指標(stub_status)轉化為prometheus可用的資料格式

在https://github.com/nginxinc/nginx-prometheus-exporter/releases下載nginx-prometheus-exporter_1.1.0_linux_amd64.tar.gz 安裝包,並上傳到nginx伺服器/usr/local 目錄下

1.3 安裝nginx-prometheus-exporte

[root@rancher local]# mkdir nginx_exporter
[root@rancher local]# mv nginx-prometheus-exporter_1.1.0_linux_amd64.tar.gz ./nginx_exporter/
[root@rancher local]# cd nginx_exporter/
[root@rancher nginx_exporter]# tar -xvf nginx-prometheus-exporter_1.1.0_linux_amd64.tar.gz
[root@rancher local]# vim /usr/lib/systemd/system/nginx_exporter.service

[Unit]
Description=nginx_exporter
After=network.target

[Service]
Restart=on-failure
ExecStart=/usr/local/nginx_exporter/nginx-prometheus-exporter -nginx.scrape-uri http://10.12.121.198:7006/nginx_status

[Install]
WantedBy=multi-user.target

  

[root@rancher local]# systemctl restart nginx_exporter.service
[root@rancher local]# systemctl enable nginx_exporter.service

輸入流量訪問http://10.12.121.198:9113/metrics

1.4 prometheus配置

[root@rancher prometheus]# vim /data/prometheus/prometheus.yml
###nginx
  - job_name: 'nginx_status'
    metrics_path: '/metrics'
    scrape_interval: 5s
    static_configs:
      - targets: ["10.12.121.198:9113"]
    relabel_configs:
    - source_labels: [__address__]
      regex: "([^:]+):\\d+"
      target_label: instance

[root@rancher prometheus]# systemctl reload prometheus.service

登陸prometheus 後臺檢視nginx

Targets

相關文章