本文描述了通过Metricbeat和Filebeat来分别监控Nginx的性能指标和日志,并将数据存储到Elasticsearch中,再通过Kibana进行可视化数据分析。
前置条件:
CentOS7 x86_64
Elastic Stack:
已以rpm方式安装Elastichsearch 7.4.0已以rpm方式安装Kibana 7.4.0如果还没有安装Elasticsearch和Kibana,可以参考:
在CentOS 7上以rpm方式安装Elasticsearch 7.4.0在CentOS7上以rpm方式安装Kibana 7.4.0目标机器:
已以rpm方式安装Nginx参见:
https://www.elastic.co/guide/en/beats/metricbeat/current/metricbeat-installation-configuration.htmlMetribeat 7.4.0:
https://www.elastic.co/cn/downloads/past-releases/metricbeat-7-4-0Metricbeat 最新版:
https://www.elastic.co/cn/downloads/beats/metricbeat下载Metricbeat 7.4.0 rpm和sha512:
# download rpm package wget https://artifacts.elastic.co/downloads/beats/metricbeat/metricbeat-7.4.0-x86_64.rpm # download sha512 wget https://artifacts.elastic.co/downloads/beats/metricbeat/metricbeat-7.4.0-x86_64.rpm.sha512校验sha512:
sha512sum -c metricbeat-7.4.0-x86_64.rpm.sha512安装Metricbeat:
rpm -ivh metricbeat-7.4.0-x86_64.rpmMetricbeat目录:
参见:
https://www.elastic.co/guide/en/beats/metricbeat/current/directory-layout.html配置目录为/etc/metricbeat:
metricbeat.yml 为Metricbeat主配置文件。modules.d/*.yml 为Meticbeat module配置文件程序目录为/usr/share/metricbeat:
bin 目录为可执行文件目录。数据目录为/var/lib/metricbeat,日志目录为/var/log/metricbeat。
如果Metricbeat和Elasticsearch和Kibana安装在同一台机器上,则不需要修改配置。
编辑·/etc/metricbeat/metricbeat.yml,设置Elasticsearch和Kibana的连接。
修改前:
output.elasticsearch: # Array of hosts to connect to. hosts: ["localhost:9200"] # Optional protocol and basic auth credentials. #protocol: "https" #username: "elastic" #password: "changeme" setup.kibana: # Kibana Host # Scheme and port can be left out and will be set to the default (http and 5601) # In case you specify and additional path, the scheme is required: http://localhost:5601/path # IPv6 addresses should always be defined as: https://[2001:db8::1]:5601 #host: "localhost:5601" # Kibana Space ID # ID of the Kibana Space into which the dashboards should be loaded. By default, # the Default Space will be used. #space.id:Metricbeat nginx模块监控Nginx时,需要启用Nginx的stub_status模块(默认不开启)。
检查Nginx是否包含了stub_status模块:
nginx -V 2>&1 | grep --color -- --with-http_stub_status_module在已有的Nginx配置文件(比如/etc/nginx/nginx.conf)的server中添加:
location /nginx_status { stub_status; allow 127.0.0.1; #only allow requests from localhost deny all; #deny all other hosts }测试Nginx配置:
nginx -t重新加载Nginx配置:
nginx -s reload测试stub_status:
curl http://127.0.0.1/nginx_status参见:
https://www.nginx.com/blog/monitoring-nginx/https://www.tecmint.com/enable-nginx-status-page/参见:
https://www.elastic.co/guide/en/beats/metricbeat/current/metricbeat-module-nginx.html查看Metricbeat module:
metricbeat modules list可以看到Metricbeat默认只启用了系统级别的指标数据采集(system)。
启用Metricbeat nginx module:
metricbeat modules enable nginx再次运行metricbeat modules list,可以看到Metricbeat nginx module已经被启用。
Metricbeat nginx module的配置文件为/etc/metricbeat/modules.d/nginx.yml,修改该配置文件。
修改前:
- module: nginx #metricsets: # - stubstatus period: 10s # Nginx hosts hosts: ["http://127.0.0.1"] # Path to server status. Default server-status #server_status_path: "server-status" #username: "user" #password: "secret"修改后:
- module: nginx metricsets: ["stubstatus"] enabled: true period: 10s # Nginx hosts hosts: ["http://127.0.0.1"] # Path to server status. Default server-status server_status_path: "nginx_status"注意:server_status_path需要和启用Nginx stub_status模块时设置的location保持一致。
测试Metricbeat配置:
metricbeat test config -e设置Metricbeat的Elasticsearch Index template和加载Kibana Dashboard:
# -e 表示把日志输出到控制台 metricbeat setup -e以服务方式运行Metricbeat:
systemctl enable metricbeat systemctl start metricbeat查看Metricbeat服务日志:
systemctl status metricbeat -l可以看到,Metricbeat的启动命令如下:
/usr/share/metricbeat/bin/metricbeat -e -c /etc/metricbeat/metricbeat.yml -path.home /usr/share/metricbeat -path.config /etc/metricbeat -path.data /var/lib/metricbeat -path.logs /var/log/metricbeat查看Metricbeat服务详细日志:
journalctl -xefu metricbeat参见:
https://www.elastic.co/guide/en/beats/filebeat/7.9/filebeat-installation-configuration.htmlFilebeat 7.4.0:
https://www.elastic.co/cn/downloads/past-releases/filebeat-7-4-0Filebeat 最新版:
https://www.elastic.co/cn/downloads/beats/filebeat下载Filebeat 7.4.0 rpm和sha512:
# download rpm package wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.4.0-x86_64.rpm # download sha512 wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.4.0-x86_64.rpm.sha512校验sha512:
sha512sum -c filebeat-7.4.0-x86_64.rpm.sha512安装Filebeat:
rpm -ivh filebeat-7.4.0-x86_64.rpmFilebeat目录:
参见:
https://www.elastic.co/guide/en/beats/filebeat/7.9/directory-layout.html配置目录为/etc/filebeat:
filebeat.yml 为Filebeat主配置文件。modules.d/*.yml 为Filebeat module配置文件程序目录为/usr/share/filebeat:
bin 目录为可执行文件目录。数据目录为/var/lib/filebeat,日志目录为/var/log/filebeat。
如果Filebeat和Elasticsearch和Kibana安装在同一台机器上,则不需要修改配置。
编辑·/etc/filebeat/filebeat.yml,设置Elasticsearch和Kibana的连接。
修改前:
output.elasticsearch: # Array of hosts to connect to. hosts: ["localhost:9200"] # Optional protocol and basic auth credentials. #protocol: "https" #username: "elastic" #password: "changeme" setup.kibana: # Kibana Host # Scheme and port can be left out and will be set to the default (http and 5601) # In case you specify and additional path, the scheme is required: http://localhost:5601/path # IPv6 addresses should always be defined as: https://[2001:db8::1]:5601 #host: "localhost:5601" # Kibana Space ID # ID of the Kibana Space into which the dashboards should be loaded. By default, # the Default Space will be used. #space.id:参见:
https://www.elastic.co/guide/en/beats/filebeat/current/filebeat-module-nginx.html查看Filebeat module:
filebeat modules list启用Filebeat nginx module:
filebeat modules enable nginx再次运行filebeat modules list,可以看到Filebeat nginx module已经被启用。
Filebeat nginx module的配置文件为/etc/filebeat/modules.d/nginx.yml,可以修改该配置文件的access.var.paths和error.var.paths来指定要收集的Nginx access logs和error logs。
在CentOS7上,Nginx日志的默认路径为:
access log: /var/log/nginx/access.logerror log: /var/log/nginx/error.log编辑/etc/filebeat/modules.d/nginx.yml,指定要采集的Nginx的日志文件路径,支持glob模糊匹配。
示例:
修改前:
- module: nginx # Access logs access: enabled: true # Set custom paths for the log files. If left empty, # Filebeat will choose the paths depending on your OS. #var.paths: # Error logs error: enabled: true # Set custom paths for the log files. If left empty, # Filebeat will choose the paths depending on your OS. #var.paths:修改后:
- module: nginx # Access logs access: enabled: true # Set custom paths for the log files. If left empty, # Filebeat will choose the paths depending on your OS. var.paths: ["/var/log/nginx/*access.log"] # Error logs error: enabled: true # Set custom paths for the log files. If left empty, # Filebeat will choose the paths depending on your OS. var.paths: ["/var/log/nginx/*error.log"]测试修改后的模块配置文件是否正确:
filebeat test config -e设置Filebeat的Elasticsearch Index template和加载Kibana Dashboard:
# -e 表示把日志输出到控制台 filebeat setup -e以服务方式运行Filebeat:
systemctl enable filebeat systemctl start filebeat查看Filebeat服务日志:
systemctl status filebeat -l可以看到,Filebeat的启动命令如下:
/usr/share/filebeat -path.config /etc/filebeat -path.data /var/lib/filebeat -path.logs /var/log/filebeat查看Filebeat服务详细日志:
journalctl -xefu filebeat在Kibana中
打开Discover菜单,选中filebeat-*,可以看到Filebeat采集并经过提取字段的Nginx日志数据。
打开Logs菜单,就可以看到Filebeat采集的详细的Nginx日志数据了。
打开Dashboard菜单,输入nginx搜索,打开"[Filebeat Nginx] Access and error logs ECS",就可以看到Nginx日志数据的Dashboard了。
在Kibana中
打开Discover菜单,选中metricbeat-*,可以看到Metricbeat采集并经过提取字段的指标数据。
打开Dashboard菜单
输入metricbet nginx搜索,打开"[Metricbeat Nginx] Overview ECS",就可以看到Nginx指标数据的Dashboard了。输入metricbeat system搜索,打开"[Metricbeat System] Overview ECS",就可以看到系统指标数据的Dashboard了,并可切换到Host Overview和Containers overview查看指定的主机和进程的指标数据。下一步还需要定制Kibana Dashboard来根据需要展示数据,并进行深入地可视化分析。
参见:
https://www.elastic.co/guide/en/kibana/7.9/dashboard.htmlhttps://www.elastic.co/cn/blog/how-to-monitor-nginx-web-servers-with-the-elastic-stack
https://logz.io/blog/nginx-web-server-monitoring/
nklinsirui 认证博客专家 DevOps DevOps的实践者和布道者。现在某国际开源软件公司担任高级专家顾问。曾在某电子政务公司担任高级系统架构师。曾在某国际咨询公司担任技术架构交付经理。曾在某跨国银行中国软件开发中心担任高级专家顾问。