Prometheus监控nginx

创建nginx-docker配置文件

mkdir /data/nginx/conf.d/ -p
cd /data/nginx/conf.d/
data/nginx/conf.d# ls



nginx/conf.d配置文件

cat >>server.conf<< 'EOF'
server {
    listen 80;
    server_name localhost;
    location / {
        root /usr/share/nginx/html;
        index index.html index.htm;
    }
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }
}
EOF

nginx-docker

mkdir /data/docker-compose -p
cd /data/docker-compose/


cat >docker-compose.yaml<<'EOF'
version: '3'
services:
  nginx:
    image: nginx:1.21.6
    container_name: nginx
    restart: always
    volumes:
      - /data/nginx/conf.d:/etc/nginx/conf.d
      - /data/nginx/html:/usr/share/nginx/html
      - /data/nginx/log:/var/log/nginx
    ports:
      - 80:80
EOF

启动nginx_docker

docker-compose up -d

nginx 开启stub_status


确认是否有该模块 with-http_stub_status_module 模块

docker exec -it nginx nginx -V 2>&1 | grep -o with-http_stub_status_module
with-http_stub_status_module

nginx 开启stub_status配置
```
cat >>server.conf<< 'EOF'
server {
    listen 80;
    server_name localhost;
......
    
    location /stub_status {
        stub_status on;
        access_log off;
        allow 0.0.0.0/0;
        deny all;
    }
......
}
EOF


具体配置
```
server {
    listen 80;
    server_name localhost;

    location /stub_status {
        stub_status on;
        access_log off;
        allow 0.0.0.0/0;
        deny all;
    }
    location / {
        root /usr/share/nginx/html;
        index index.html index.htm;
    }
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }
}

重新加载nginx 配置测试

root@prometheus220:/data/nginx/conf.d# docker exec -it nginx nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful



root@prometheus220:/data/nginx/conf.d# docker exec -it nginx nginx -s reload

root@prometheus220:/data/nginx/conf.d# curl http://localhost/stub_status
Active connections: 1 
server accepts handled requests
 1 1 1 
Reading: 0 Writing: 1 Waiting: 0 
```

配置nginx-exporter

cd /data/nginx/

cat >docker-compose.yaml <<'EOF'
version: '3.3'
services:
  nginx_exporter:
    image: nginx/nginx-prometheus-exporter:0.8.0
    container_name: nginx_exporter
    hostname: nginx_exporter
    command:
      - '-nginx.scrape-uri=http://10.19.1.220/stub_status'
    restart: always
    ports:
      - '9113:9113'
EOF

启动


docker-compose up -d

确认docker ps -a 

http://10.19.1.220:9113/metrics

prometheus.yml 增加配置

nginx_exporter监控样本数据

cat >> prometheus/prometheus.yml << 'EOF'
  - job_name: 'nginx_exporter'
    scrape_interval: 30s
    static_configs:
    - targets: ['10.19.1.220:9113']
      labels:
        instance: nginx服务器 
EOF

curl -X POST http://localhost:9090/-/reload

prometheus 界面显示nginx_exporter

增加告警阀值

cd /data/docker-prometheus

cat >>prometheus/alert.yml << 'EOF'
- name: nginx
  rules:
  #对任何实例操过30s无法联系的情况发出告警
  - alert: NginxDown
    expr: nginx_up == 0
    for: 30s
    labels: 
      serverity: critical
    annotations:
      summary: "nginx异常,实例:{{ $labels.instance }}"
      description: "{{ $labels.job }} nginx 已关闭"
EOF
```
docker exec -it prometheus promtool check config /etc/prometheus/prometheus.yml
curl -X POST http://localhost:9090/-/reload

添加nginx_dashboard

https://grafana.com/grafana/dashboards/12708


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

石兴稳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值