多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
&nbsp;&nbsp;&nbsp; 监控nginx ## 一、prometheus监控nginx &nbsp;&nbsp;&nbsp;docker安装(略) &nbsp;&nbsp;&nbsp;docker-compose安装(略) ## 1.1待监控Linux安装nginx &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;创建nginx目录 mkdir /data/nginx -p cd /data/nginx &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;创建docker-compose.yaml vim docker-compose.yaml &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;配置如下: services: nginx: image: registry.cn-hangzhou.aliyuncs.com/ldw520/nginx:latest 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 &nbsp;&nbsp;&nbsp;启动 docker-compose.yaml docker-compose up -d &nbsp;&nbsp;&nbsp;查看docker的信息 docker images docker ps -a &nbsp;&nbsp;&nbsp;创建nginx配置文件 cd /data/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 &nbsp;&nbsp;&nbsp;重启nginx cd /data/nginx docker-compose down docker-compose up -d &nbsp;&nbsp;&nbsp;创建默认index.html vi /data/nginx/html/index.html ![](https://img.kancloud.cn/89/d1/89d120e9a100d1544a355154c1c30bbd_1283x265.png) &nbsp;&nbsp;&nbsp;访问地址 http://192.168.100.141/index ![](https://img.kancloud.cn/48/65/48651f2180e7d4be1fcc24f455391681_1089x252.png) <hr> ## 1.2 nginx开启监控 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;centos2机器上,编辑docker-compose.yaml文件 docker exec -it nginx nginx -V 2>&1 | grep --color -o with-http_stub_status_module * **如果显示了`with-http_stub_status_module`**:这意味着`http_stub_status_module`已经启用,你可以继续配置 Nginx 以使用此模块。 * **如果没有显示**:这意味着在编译 Nginx 时没有包含`http_stub_status_module`。在这种情况下,你通常需要重新编译 Nginx,或者使用一个已经包含该模块的 Nginx 版本。对于 Docker 容器,你可能需要: * 查找一个已经包含此模块的官方或第三方 Docker 镜像。 * 从源代码自行编译 Nginx 并创建一个新的 Docker 镜像。 ##### &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;配置 Nginx 使用`stub_status` 一旦确认`http_stub_status_module`已启用,你可以在 Nginx 配置文件中添加`stub_status`配置。通常,你会在`http`块中添加一个`location`块,如下所示: vi /data/nginx/conf.d/server.conf server { listen 80; server_name localhost; location /nginx_status { stub_status on; access_log off; allow 0.0.0.0/0; # 允许访问的IP地址,可以根据需要修改 deny all; } } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;完成配置后,不要忘记重新加载或重启 Nginx 以应用更改。 docker exec -it nginx nginx -s reload 访问 http://192.168.100.141/stub_status ![](https://img.kancloud.cn/19/31/193146875af75e316ee5b092f7609edb_653x391.png) <hr> ## 二、prometheus监控mysql ## &nbsp;&nbsp;&nbsp;2.1docker安装(略) ## &nbsp;&nbsp;&nbsp;2.2docker-compose安装(略) ## &nbsp;&nbsp;&nbsp;2.3 安装nginx_exporter &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;centos2机器上,编辑docker-compose.yaml文件 vi docker-compose.yaml &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;如图所示:加入一个nginx_exporter配置 ![](https://img.kancloud.cn/31/b1/31b1d5fad753944602f6f1909790c037_826x378.png) nginx-exporter: image: nginx/nginx-prometheus-exporter:0.11 container_name: nginx_exporter hostname: nginx_exporter command: - '-nginx.scrape-uri=http://192.168.100.141/stub_status' restart: always ports: - "9113:9113" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;运行docker-compose up-d的命令,观察mysql的程序是否成功运行 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;宿主机上访问mysqld_exporter的metrics http://192.168.100.141:9113/metrics <hr> ## &nbsp;&nbsp;&nbsp;2.4 prometheus服务器添加nginx_exporter的地址 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;192.168.100.85的centos上,修改prometheus的配置文件 #进入docker-prometheus目录 cd /data/docker-prometheus #修改prometheus.yml vi prometheus/prometheus.yml &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;添加nginx_exporter配置如下图: ![](https://img.kancloud.cn/63/08/63085d958e1c11a69553764062cb3cbf_723x582.png) - job_name: "nginx-exporter" static_configs: - targets: ["192.168.100.141:9113"] labels: istance: "centos2服务器nginx监听" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;保存后输入命令更新: curl -XPOST http://localhost:9090/-/reload &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;成功监听 ![](https://img.kancloud.cn/98/cf/98cfc5df77968b155e52539830a6d698_1814x215.png) <hr/> ## &nbsp;&nbsp;&nbsp;1.4 mysql服务器指标查询 nginx_connections_accepted #接收请求数 nginx_connections_active #活动连接数 nginx_connections_handled #成功处理请求数 nginx _connections_reding #正在进行读操作的请求数 nginx connections_waiting #正在等待的请求数 nginx_connections_writing #正在进行写操作的请求数 nginx connections_requests #总请求数 <hr> &nbsp;&nbsp;&nbsp;2.6 grafana中对mysql进行监控 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;copy id to clipboard->grafana的dashboards中lmport dashboard https://grafana.com/grafana/dashboards/12708-nginx/ ![](https://img.kancloud.cn/f8/cb/f8cb90753e3bf9f3188e62682ecabfb3_1465x947.png) ![](https://img.kancloud.cn/72/65/72651bb3732f62f7a76eb97364a5791d_1621x817.png)