通过nginx-prometheus-exporter监控nginx指标
https://hub.docker.com/r/nginx/nginx-prometheus-exporter
NGINX 通过 stub_status page 页面公开了一些有用的指标,NGINX Prometheus exporter 则通过获取这些指标,转换成适合Prometheus处理的指标格式,并通过HTTP服务输出,方便我们使用 Prometheus 进行收集和处理。
对于nginx-prometheus-exporter说明,请见这里:https://github.com/nginxinc/nginx-prometheus-exporter
目标:
利用多阶段部署方式,把nginx:alpine镜像合入nginx/nginx-prometheus-exporter中的程序,并使用脚本同时启动二者,注意让nginx运行于前台,且nginx-prometheus-exporter程序能正常输出nginx的指标。

步骤1:设置nginx的stub_status

下载测试到用到镜像:
docker pull nginx:alpine
docker pull nginx/nginx-prometheus-exporter:latest
然后运行一下nginx:alpine容器,看看里面是否包含 stub_status 模块
docker run --name nginx1 -d nginx:alpine
docker exec -it nginx1 /bin/sh
1
/ # /usr/sbin/nginx -V
2
nginx version: nginx/1.17.10 built by gcc 9.2.0 (Alpine 9.2.0) built with OpenSSL 1.1.1d 10 Sep 2019 (running with OpenSSL 1.1.1g 21 Apr 2020) TLS SNI support enabled configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --with-perl_modules_path=/usr/lib/perl5/vendor_perl --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-Os -fomit-frame-pointer' --with-ld-opt=-Wl,--as-needed
Copied!
看到--with-http_stub_status_module,证明这个镜像已经编译好了stub_status 模块
创建/etc/nginx/conf.d/status.conf,加入:
1
server {
2
listen 8080;
3
server_name localhost;
4
location /stub_status {
5
stub_status on;
6
access_log off;
7
}
8
}
Copied!
/usr/sbin/nginx -s reload重启,然后在新terminal访问容器ip:8080/stub_status页面:
1
[email protected]:/home/ubuntu# curl 172.17.0.5:8080/stub_status
2
Active connections: 1
3
server accepts handled requests
4
3 3 3
5
Reading: 0 Writing: 1 Waiting: 0
Copied!
利用while语句做一个循环输出就可以实现简单的访问量监控:
1
while true ; do curl 172.17.0.5:8080/stub_status; sleep 2; done
Copied!
然后把/etc/nginx/conf.d/status.conf复制到宿主机备用:
1
docker cp nginx1:/etc/nginx/conf.d/status.conf ./
Copied!

步骤2:部署nginx-prometheus-exporter容器

通过nginx-prometheus-exporter容器来监控nginx1的指标,命令如下:
1
[email protected]:/home/ubuntu# docker run -p 9113:9113 nginx/nginx-prometheus-exporter:latest -nginx.scrape-uri http://172.17.0.5:8080/stub_status
2
2020/05/01 11:31:38 Starting NGINX Prometheus Exporter Version=0.7.0 GitCommit=a2910f1
3
2020/05/01 11:31:38 Listening on :9113
4
2020/05/01 11:31:38 NGINX Prometheus Exporter has successfully started
Copied!
然后在另一个terminal下每秒访问一次nginx1容器的80服务:
1
while true ; do curl -s -o /dev/null http://172.17.0.2/; sleep 1; done
Copied!
最后访问宿主机的IP:9113/metrics页面,就能看到读取出来的nginx指标数据了。

步骤3:通过多阶段部署把两个步骤整个到一个镜像里

创建一个Dockerfile
1
# 编译阶段 命名为 exporter
2
FROM nginx/nginx-prometheus-exporter:latest as exporter
3
# 运行阶段
4
FROM nginx:alpine
5
COPY ./nginx/status.conf /etc/nginx/conf.d/status.conf
6
COPY --from=exporter /usr/bin/exporter /usr/bin/exporter
7
ADD run.sh /run.sh
8
RUN chmod 777 /run.sh
9
EXPOSE 80 9113
10
CMD ["/bin/sh", "/run.sh"]
Copied!
编写run.sh
1
#!/bin/bash
2
nginx -c /etc/nginx/nginx.conf
3
nginx -s reload
4
/usr/bin/exporter -nginx.scrape-uri http://127.0.0.1/stub_status
5
tail -f /dev/null #实现本shell永不运行完成,容器不退出。
Copied!
把dockerfile生成镜像文件:
1
docker build . -t nginx-exporter:v0.2
Copied!
运行容器:
1
docker run --name exporter2 -d -p 9113:9113 nginx-exporter:v0.2
Copied!
访问宿主机:9113/metrics检查exporter运作情况:
Last modified 1yr ago