使用elasticsearch kibana和fluentd来可视化nginx的响应时间
简而言之
使用elasticsearch + kibana + fluentd对日志进行可视化。这次我们将对nginx的响应进行可视化,以实现性能可见化。了解当前性能对于性能调优非常重要。
Nginx(被监视日志对象)
本次安装用于测试的nginx。
安装nginx。
apt update
apt install nginx
启动
systemctl status nginx
systemctl start nginx
systemctl status nginx
访问并记录日志
http://你的地址
默认的访问日志格式如下。
192.168.11.10 - - [09/Dec/2023:01:48:13 +0000] "GET /favicon.ico HTTP/1.1" 404 197 "http://192.168.11.16/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36"
因为当前情况下未显示出响应时间,所以需要修改配置文件,使得能够显示出响应时间。关于nginx日志格式的详情,请参考以下文档。
vi /etc/nginx/nginx.conf
### httpのセクションにlog_formatを追加する ###
http {
# ~~~
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" $gzip_ratio $request_length $request_time';
access_log /var/log/nginx/access.log main;
# ~~~
}
重新启动nginx
systemctl restart nginx
让我们重新检查访问日志吧
192.168.11.10 - - [09/Dec/2023:07:43:05 +0000] "GET /favicon.ico HTTP/1.1" 404 392 "http://192.168.11.16/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36" 3.03 904 0.000
我认为在最后一部分中会提到响应时间。
Elasticsearch: 弹性搜索
然后,我们将安装全文搜索引擎elasticsearch。
公式文件
导入 Elasticsearch PGP 密钥
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elasticsearch-keyring.gpg
安装apt-transport-https
apt install apt-transport-https
保存仓库定义。
echo "deb [signed-by=/usr/share/keyrings/elasticsearch-keyring.gpg] https://artifacts.elastic.co/packages/8.x/apt stable main" | sudo tee /etc/apt/sources.list.d/elastic-8.x.list
安装Elasticsearch
apt install elasticsearch
我暂时将内存限制在1G。因为这并不是一个特别大的环境…
vi /etc/elasticsearch/jvm.options
- -Xms2g
- -Xmx2g
+ -Xms1g
+ -Xmx1g
因为我想要许可使用http,请在启动之前修改以下设置文件。
vi /etc/elasticsearch/elasticsearch.yml
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
discovery.type: single-node
xpack.security.enabled: false
xpack.security.transport.ssl.enabled: false
xpack.security.http.ssl.enabled: false
由于此次仅在LAN内部使用,请确保使用http进行连接。如果需要与外部通信,请使用https。
Elasticsearch的启动
systemctl status elasticsearch
systemctl enable elasticsearch
systemctl restart elasticsearch
systemctl status elasticsearch
弹性用户密码自动生成(这可能不需要)
/usr/share/elasticsearch/bin/elasticsearch-reset-password -a -u elastic
如果你想自己创建,只需指定“-i”。
基于一种选项来本地化解释以下内容:Kibana.
下一步是安装Kibana。
Kibana的安装
apt install kibana
开始运行
systemctl status kibana
systemctl enable kibana
systemctl restart kibana
systemctl status kibana
允许从外部连接到Kibana。在这种情况下,我认为可以进行设置,只允许来自局域网的连接。(这次我们将允许任何地方的连接。)
vi /etc/kibana/kibana.yml
+ server.host: "0.0.0.0"
+ i18n.locale: "ja-JP"
试着访问一下
http://youradress:5601
※可以使用kibana安装的服务器IP作为youradress。
获取用于注册的令牌
/usr/share/elasticsearch/bin/elasticsearch-create-enrollment-token -s kibana --url https://youradress:9200
获取验证代码
/usr/share/kibana/bin/kibana-verification-code
流利的流
最后,作为日志收集代理,我们将使用 Fluentd。
安装Fluentd
curl -fsSL https://toolbelt.treasuredata.com/sh/install-ubuntu-jammy-fluent-package5-lts.sh | sh
将Fluentd的执行用户更改为root。(虽然应该正确地管理权限,但是因为麻烦所以我们将其设置为root用户执行。)
vi /lib/systemd/system/fluentd.service
[Service]
User=root
Group=root
因为修改了 `unit` 文件,所以需要进行 `daemon-reload`。
systemctl daemon-reload
我将更改Fluentd的设置。在源文件中写入日志信息。这次是nginx的访问日志。匹配部分包含了用于将日志发送到Elasticsearch的设置。
# nginxログをinputする設定
<source>
@type tail
path /var/log/nginx/access.log
format /^(?<remote>[^ ]*) (?<host>[^ ]*) (?<user>[^ ]*) \[(?<time>[^\]]*)\] "(?<method>\S+)(?: +(?<path>[^\"]*?)(?: +\S*)?)?" (?<code>[^ ]*) (?<size>[^ ]*)(?: "(?<referer>[^\"]*)" "(?<agent>[^\"]*)"(?:\s+(?<http_x_forwarded_for>[^ ]+))?) (?<request_length>[^ ]*) (?<request_time>[^ ]*)"?/
time_format %d/%b/%Y:%H:%M:%S %z
tag nginx.access
pos_file /var/log/nginx/access.log.pos
</source>
# inputしたnginxログをelasticsearchに流し込む
<match nginx.access>
@type elasticsearch
logstash_format true
logstash_prefix nginx-access
logstash_dateformat %Y-%m-%d
host localhost
port 9200
type_name accesslog
</match>
由于fluentd的正则表达式有一些特殊之处,请参考以下网站或者其他资源进行创建。
https://fluentular.herokuapp.com/
https://docs.fluentd.org/parser/regexp
从Kibana界面进行操作
在Kibana的界面上,设置一个index template。该模板应该与`nginx-access-yyyy-mm-dd`匹配。如果不设置index template,那么Elasticsearch将会给该索引分配一个默认的类型。由于在可视化时类型为text可能会产生问题,因此请务必设置好index template。在此网址`http://youradress:5601/app/management/data/index_management/create_template`上创建模板。
# Load JSON
{
"properties": {
"remote": {
"type": "text"
},
"host": {
"type": "text"
},
"user": {
"type": "text"
},
"method": {
"type": "text"
},
"path": {
"type": "text"
},
"code": {
"type": "integer"
},
"size": {
"type": "integer"
},
"referer": {
"type": "text"
},
"agent": {
"type": "text"
},
"http_x_forwarded_for": {
"type": "float"
},
"request_length": {
"type": "integer"
},
"request_time": {
"type": "float"
}
}
}
当设置完成后,nginx的访问日志将流入elasticsearch,并可以通过kibana的可视化功能将日志的响应时间可视化。
