Prometheus实战篇:Prometheus监控nginx

准备环境

在此专栏的前几篇文章中已经准备了一台服务器作为我们进行环境的准备.大家也可以通过虚拟机创建俩台服务器,一台作为Prometheus的安装另外一台进行其他软件安装并且进行监控的服务器.
这里我就不赘述nginx的安装教程,相信大家都可以搜到,使用docker或者直接通过安装包解压的方式都可以,我这里是通过docker的方式进行安装的,后面的操作其实都是大差不差的.

nginx开启stub_status

  • 监控nginx需要with-http_stub_status_module这个模块

首先检查是否有安装with-http_stub_status_module模块

docker方式安装

shell 复制代码
docker exec -it nginx nginx -v 2>&1  |grep -o with-http_stub_status_module

nginx安装包方式安装

shell 复制代码
nginx nginx -v 2>&1  |grep -o with-http_stub_status_module

nginx开启stub_status配置

将下面配置文件写到nginx.conf配置文件中:

nginx 复制代码
server {
	.....
	location /stub_status {
		stub_status on;
		access_log off;
		allow 0.0.0.0/0;
		deny all;
	}
	....
}

重新加载配置文件

bash 复制代码
docker exec -it nginx nginx -s reload

检查是否开启成功

arduino 复制代码
curl http://localhost/syub_status

成功如下图:

安装Exporter

在上篇文章中说了Prometheus需要监控什么软件需要对应安装的Exporter,当然这里可以使用二进制安装也可以使用docker安装.这里为了方便,还是选择docker-compose的方式直接安装

docker-compose方式进行安装

这里直接通过创建docker-compose.yaml然后进行追加配置

shell 复制代码
cat >docker-compose.yaml <<FOF
version: '3.3'
services:
 nginx_exproter:
 	image:nginx/nginx-prometheus-exporter:0.11
 	container_name: nginx_exporter
 	hostname: nginx_exporter
 	command:
 	 - '-nginx.scrape-uri=http://localhost/stub_status'
 	 restart: always
 	 port:
 	 - "9113:9113"
EOF

启动

shell 复制代码
docker-compose up -d

检查

makefile 复制代码
查看正在运行的容器
docker ps

或者:

查看nginx_exporter容器的运行日志
docker logs -f nginx_exporter

metrics地址

安装好Exporter后会暴露一个/metrics结尾的服务

名称 地址
nginx_exporter http://localhost:9113/metrics

Prometheus配置

配置Prometheus去采集(拉取)nginx_exporter的监控样本数据

shell 复制代码
cd /data/docker-prometheus

# 在scrapc_configs(搜刮配置):下面增加如下配置:
cat >prometheus/prometheus.yml <<FOF
 - job_name: 'nginx_exporter'
   static_configs:
   - targets: ['localhost:9113']
   	 labels:
   	 	instance: test服务器 
EOF

重新加载配置

shell 复制代码
curl -x POST http://localhost:9090/-/reload

检查

常用的nginx监控指标

复制代码
nginx_connections_accepted	接受请求数
nginx_connections_active	活动连接数
nginx_connections_handled	成功处理请求数
nginx_connections_reding	正在进行读操作的请求数
nginx_connections_waiting	正在等待的请求数
nginx_connections_writing	正在进行写操作的请求数
nginx_connections_requests	总请求数

添加触发器

shell 复制代码
cd /data/docker-prometheus
shell 复制代码
cat >prometheus/alert.yml <<FOF
 -name: nginx
  rules:
  # 任何势力超过30秒无法联系的情况发出警报
  - alert: NginxDown
  	expr: nginx_up ==0
  	for: 30s
  	labels:
  	  severity: critical
  	annotations:
  		summary:"nginx异常,实例:{{$labels.instance }}"
  		description: "{{$lables.job}} nginx已关闭"
EOF

检查:

shell 复制代码
vim prometheus/alert.yml

检查配置

shell 复制代码
docker exec -it prometheus promtool check config /etc/prometheus/prometheus.yml

重新加载配置

shell 复制代码
curl -x POST http://localhost:9090/-/reload

检查

http://localhost:9090/alerts?search=

或:

http://localhost:9090/rules

dashboard

grafana展示Prometheus从nginx_exporter收集到的数据

相关推荐
johnny2338 小时前
运维管理面板:AcePanel、OpenOcta、DeepSentry
运维
青梅橘子皮8 小时前
Linux---基本指令
linux·运维·服务器
REDcker9 小时前
Linux信号机制详解 POSIX语义与内核要点 sigaction与备用栈实践
linux·运维·php
cui_ruicheng10 小时前
Linux进程间通信(三):System V IPC与共享内存
linux·运维·服务器
ACP广源盛1392462567311 小时前
IX8024与科学大模型的碰撞@ACP#筑牢科研 AI 算力高速枢纽分享
运维·服务器·网络·数据库·人工智能·嵌入式硬件·电脑
峥无12 小时前
Linux系统编程基石:静态库·动态库·ELF文件·进程地址空间全景图
linux·运维·服务器
码云数智-大飞13 小时前
本地部署大模型:隐私安全与多元优势一站式解读
运维·网络·人工智能
Harvy_没救了14 小时前
【网络部署】 Win11 + VMware CentOS8 + Nginx 文件共享服务 Wiki
运维·网络·nginx
春风有信14 小时前
【2026.05.01】Windows10安装Docker Desktop 4.71.0.0步骤及问题解决
运维·docker·容器
2401_8734794015 小时前
断网时如何实时判断IP归属?嵌入本地离线库,保障风控不中断
运维·服务器·网络