Prometheus运维九 监控nginx服务

如鲸向海,似鸟投林

1.安装部署

1.1 部署Prometheus服务

部署Prometheus请点击此链接:https://blog.youkuaiyun.com/ZhanBiaoChina/article/details/107024115

1.2 部署nginx服务

1 首先监控nginx需要用到三个模块:

nginx-vts-exporter:nginx监控模块,能够提供JSON格式的数据产出。
nginx-module-vts:主要收集nginx的监控数据,并给Prometheus提供监控接口,默认端口9913.
Prometheus:监控nginx-module-vts提供的nginx数据,并存储在时间数据库中,可以通过PromQL对时间数据库进行查询和聚合。

nginx-module-vts模块编译:
注意:nginx-vts-exporter依赖nginx-module-vts模块,安装此模块不需要其他依赖
模块与ningx的版本兼容性如下:

1.11.x(last tested:1.11.10)
1.10.x(last tested:1.10.3)
1.8.x(last tested:1.8.0)
1.6.x(last tested:1.6.3)
1.4.x(last tested:1.4.7)

2 编译nginx
安装nginx-module-vts的方法有两种:
1.在安装新的nginx上编译nginx-module-vts
2.在现有的nginx编译nginx-module-vts

查看当前nginx安装哪些模块:

root@iZj6c7q43lhfoywr0015vwZ:/usr/local/src/nginx-1.14.0# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.14.0
built by gcc 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04) 
built with OpenSSL 1.1.1  11 Sep 2018
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --user=root --group=root

下载 ngin-module-vts 模块,并添加模块到 nginx

root@iZj6c7q43lhfoywr0015vwZ:~# cd /usr/local/src/
root@iZj6c7q43lhfoywr0015vwZ:/usr/local/src# git clone git://github.com/vozlt/nginx-module-vts.git
Cloning into 'nginx-module-vts'...
remote: Enumerating objects: 711, done.
remote: Total 711 (delta 0), reused 0 (delta 0), pack-reused 711
Receiving objects: 100% (711/711), 1012.35 KiB | 17.00 KiB/s, done.
Resolving deltas: 100% (479/479), done.
root@iZj6c7q43lhfoywr0015vwZ:/usr/local/src# mv nginx-module-vts/ /usr/local/
# 进入到 nginx 安装包目录
root@iZj6c7q43lhfoywr0015vwZ:/usr/local/src# cd /usr/local/src/nginx-1.14.0/
#不要 make install,不然会覆盖
root@iZj6c7q43lhfoywr0015vwZ:/usr/local/src/nginx-1.14.0# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --user=root --group=root --add-module=/usr/local/nginx-module-vts && make 

3.替换nginx启动文件

root@iZj6c7q43lhfoywr0015vwZ:/usr/local/src/nginx-1.14.0# cp /usr/local/nginx/sbin/nginx  /usr/local/nginx/sbin/nginx.old
root@iZj6c7q43lhfoywr0015vwZ:/usr/local/src/nginx-1.14.0# cp objs/nginx /usr/local/nginx/sbin/
root@iZj6c7q43lhfoywr0015vwZ:/usr/local/src/nginx-1.14.0# nginx -V
nginx version: nginx/1.14.0
built by gcc 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04) 
built with OpenSSL 1.1.1  11 Sep 2018
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --user=root --group=root --add-module=/usr/local/nginx-module-vts

4.修改nginx配置文件

http {
		vhost_traffic_status on;
    	vhost_traffic_status_zone;
    	vhost_traffic_status_filter_by_host on;
 
server {
        location /status {
            stub_status on;
            access_log  off;
            vhost_traffic_status_display;
            vhost_traffic_status_display_format html;
        }
   }
}

建议打开vhost_traffic_status_filter_by_host on; 功能。开启此功能,在nginx配置有多个server_name的情况下,会根据不同的server_name进行流量的统计,否则默认会把流量全部计算到第一个server_name上.

5.查看监控数据
访问 http://localhost/status
在这里插入图片描述

2.在 Prometheus 上安装 nginx-vts-exporter

1.下载nginx-vts-exporter软件包

$ wget -c https://github.com/hnlq715/nginx-vts-exporter/releases/download/v0.9.1/nginx-vts-exporter-0.9.1.linux-amd64.tar.gz
$ tar zxf nginx-vts-exporter-0.9.1.linux-amd64.tar.gz -C /usr/local/
$ chmod +x /usr/local/nginx-vts-exporter-0.9.1.linux-amd64/

2.配置nginx-vts-exoirter systemd服务

$  vim /etc/systemd/system/nginx-vts-exporter.service
[Unit]
Description=nginx-vts-exporter
After=network.target

[Service]
Type=simple
User=root
# 配置 nginx-vts-exporter 的文件所在位置
# 配置 nginx 的机器 ip 地址
ExecStart=/usr/local/nginx-vts-exporter-0.9.1.linux-amd64/nginx-vts-exporter -nginx.scrape_timeout 10 -nginx.scrape_uri=http://xxx.xxx.xxx.xxx/status/format/json
Restart=on-failure

[Install]
WantedBy=multi-user.target

nginx-vts-exoirter配置解释:
#建议exporter和nginx安装在同一台机器,如果不在同一台主机,把scrape_uri改为nginx主机的地址。
#nginx_vts_exporter的默认端口号:9913,对外暴露监控接口http://xxx:9913/metrics.

3.启动nginx-vts-exoirter服务

$ systemctl daemon-reload
$ systemctl start nginx-vts-exporter.service
$ systemctl status nginx-vts-exporter.service  
● nginx-vts-exporter.service - nginx-vts-exporter
   Loaded: loaded (/etc/systemd/system/nginx-vts-exporter.service; disabled; vendor preset: disabled)
   Active: active (running) since 六 2021-01-02 21:07:18 CST; 4s ago
 Main PID: 80220 (nginx-vts-expor)
   CGroup: /system.slice/nginx-vts-exporter.service
           └─80220 /usr/local/nginx-vts-exporter-0.9.1.linux-amd64/nginx-vts-exporter -nginx.scrape_uri=http://8.210.184.164/status/format/json

1月 02 21:07:18 gaohuiwu systemd[1]: Started nginx-vts-exporter.
1月 02 21:07:18 gaohuiwu nginx-vts-exporter[80220]: 2021/01/02 21:07:18 Starting nginx_vts_exporter (version=0.9.1, branch=HEAD, revision=06ade92fb9e98d...489b180c)
1月 02 21:07:18 gaohuiwu nginx-vts-exporter[80220]: 2021/01/02 21:07:18 Build context (go=go1.10, user=root@66925540c2cc, date=20180227-13:06:29)
1月 02 21:07:18 gaohuiwu nginx-vts-exporter[80220]: 2021/01/02 21:07:18 Starting Server at : :9913
1月 02 21:07:18 gaohuiwu nginx-vts-exporter[80220]: 2021/01/02 21:07:18 Metrics endpoint: /metrics
1月 02 21:07:18 gaohuiwu nginx-vts-exporter[80220]: 2021/01/02 21:07:18 Metrics namespace: nginx
1月 02 21:07:18 gaohuiwu nginx-vts-exporter[80220]: 2021/01/02 21:07:18 Scraping information from : http://8.210.184.164/status/format/json
Hint: Some lines were ellipsized, use -l to show in full.

在这里插入图片描述
访问浏览器 http://localhost:9913/metrics

3.Prometheus集成node exporter

1.将node exporter配置到Prometheus配置文件中

# 配置Prometheus,收集node exporter的数据
# 我们还需要在prometheus中配置,让prometheus去pull这个接口的数据。
# 我们去监控主机编辑prometheus.yml文件,修改最后几行,然后重启服务
$ vim /usr/local/prometheus/prometheus.yml
scrape_configs:
  - job_name: nginx
    static_configs:
    - targets: ['localhost:9913']
# node节点的targets处的IP填写你要监控的node的IP.        
$ systemctl restart prometheus

2.登陆Prometheus UI查看节点状态

在这里插入图片描述
在这里插入图片描述

4.配置Grafana

1.安装Grafana

安装Grafana查看此链接:https://blog.youkuaiyun.com/ZhanBiaoChina/article/details/107048324

2.添加nginx模板

官方地址:https://grafana.com/grafana/dashboards/9512

添加模板的方式有两种:
1.下载json文件导入进去
2.使用ID号来添加模板

1.JSON文件导入
下载好json文件导入进去
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
2.使用ID号来添加模板
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

5.配置GeoIP模块

5.1 安装配置nginx

yum -y install epel-release geoip-devel

/usr/local/src/nginx-1.14.0
./configure --add-module=/root/nginx-1.14.0/nginx-module-vts/ --prefix=/usr/local/nginx --user=nginx --group=nginx --with-stream --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_geoip_module
make 

ldd /usr/local/nginx/sbin/nginx |grep libGeoIP
	libGeoIP.so.1 => /lib64/libGeoIP.so.1 (0x00007f69b1645000)

# 引用地址库
vim /usr/local/nginx/conf/nginx.conf
http {

    ...

    vhost_traffic_status_zone;
    geoip_country /usr/share/GeoIP/GeoIP.dat;

    ...
}    

# 重启nginx
/usr/local/nginx/sbin/nginx -s stop
/usr/local/nginx/sbin/nginx

5.2 通过vts自定义key

通过上面部署只能拿到默认的指标,生产环境可能需要监控url的请求量,监控IP访问情况 获取不同agent请求量用于分析等,通过vts模块的vhost_traffic_status_filter_bu_set_key功能可以自定义需要获取的指标。此处指定对于server配置中:

# 添加自定义配置
$ vim /usr/local/nginx/conf/vhost/test.conf
server {
    listen      80;
    server_name localhost;

    vhost_traffic_status_filter_by_set_key $uri uri::$server_name;     #每个uri访问量
    vhost_traffic_status_filter_by_set_key $geoip_country_code country::$server_name;     #不同国家/区域请求量
    vhost_traffic_status_filter_by_set_key $status $server_name;     #http code统计
    vhost_traffic_status_filter_by_set_key $upstream_addr upstream::backend;     #后端转发统计
    vhost_traffic_status_filter_by_set_key $remote_port client::ports::$server_name;     #请求端口统计
    vhost_traffic_status_filter_by_set_key $remote_addr client::addr::$server_name;     #请求IP统计

    location ~ ^/storage/(.+)/.*$ {
        set $volume $1;
        vhost_traffic_status_filter_by_set_key $volume storage::$server_name;     #请求路径统计
    }

}  

参考文献:
https://www.cnblogs.com/you-men/p/13173245.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值