一、环境准备
本次我们采用 yum 官方仓库进行安装,之前采用 rpm 包安装的文档大家请看 https://blog.51cto.com/wzlinux/2175836 。
1、软件版本
Filebeat 6.5.1
Logstash 6.5.1
Elasticsearch 6.5.1
Kibana 6.5.1
JDK 1.8.0_181
2、服务器准备
IP | 系统 | 角色 |
---|
172.18.8.200 | CentOS 7.5 Minimal | Elasticsearch + Kibana |
172.18.8.201 | CentOS 7.5 Minimal | Logstash |
172.18.8.202 | CentOS 7.5 Minimal | Filebeat + Nginx |
角色规划架构图如下所示:
3、服务器环境准备
关闭防火墙。
systemctl stop firewalld.service
systemctl disable firewalld.service
添加时间同步。
echo "$((RANDOM%60)) $((RANDOM%24)) * * * /usr/sbin/ntpdate time1.aliyun.com" >> /var/spool/cron/root
关闭SELinux。
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
setenforce 0
添加ELK官方仓库。
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
cat >> /etc/yum.repos.d/elasticsearch.repo <[elasticsearch-6.x]
name&#61;Elasticsearch repository for 6.x packages
baseurl&#61;https://artifacts.elastic.co/packages/6.x/yum
gpgcheck&#61;1
gpgkey&#61;https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled&#61;1
autorefresh&#61;1
type&#61;rpm-md
EOF
二、ES &#43; Kibana 安装(172.18.8.200)
1、Elasticsearch 安装配置
安装JDK。
rpm -ivh jdk-8u181-linux-x64.rpm
软件Elasticsearch安装。
yum install elasticsearch -y
修改配置文件/etc/elasticsearch/elasticsearch.yml
&#xff0c;修改为如下内容&#xff1a;
cluster.name: my-application
node.name: node-1
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
network.host: 172.18.8.200
http.port: 9200
启动服务。
systemctl daemon-reload
systemctl enable elasticsearch.service
systemctl start elasticsearch.service
检查启动状态。
[root&#64;es ~]# curl http://172.18.8.200:9200
{"name" : "node-1","cluster_name" : "my-application","cluster_uuid" : "SK5_gP5eSQaRyWRaJMPG7Q","version" : {"number" : "6.5.2","build_flavor" : "default","build_type" : "rpm","build_hash" : "9434bed","build_date" : "2018-11-29T23:58:20.891072Z","build_snapshot" : false,"lucene_version" : "7.5.0","minimum_wire_compatibility_version" : "5.6.0","minimum_index_compatibility_version" : "5.0.0"},"tagline" : "You Know, for Search"
}
看到这个结果&#xff0c;证明我们安装已经完成。
详细安装步骤&#xff0c;请参见官方文档。
2、Kibana 安装配置
为了节省机器资源&#xff0c;我们这里使用安装在一台机器上面&#xff0c;因为环境上面已经配置好&#xff0c;我们这里直接进行安装。
yum install kibana -y
编辑配置文件/etc/kibana/kibana.yml
&#xff0c;修改如下内容&#xff1a;
server.port: 5601
server.host: "172.18.8.200"
elasticsearch.url: "http://172.18.8.200:9200"
启动服务。
systemctl daemon-reload
systemctl enable kibana.service
systemctl start kibana.service
访问http://172.18.8.200:5601
&#xff0c;画面正常显示即我们安装完成。
具体配置&#xff0c;请参见官方文档。
三、Logstash 安装配置(172.18.8.201)
1、Logstash 安装配置
安装JDK。
rpm -ivh jdk-8u181-linux-x64.rpm
软件Logstash安装。
yum install logstash -y
2、配置nginx日志过滤
一个Logstash的pipeline由3部分组成&#xff1a;input, filter, output&#xff0c;在配置文件中我们也分为三部分。
创建配置文件/etc/logstash/conf.d/logstash.conf
&#xff0c;增加如下内容&#xff1a;
input {beats {port &#61;> 5044}
}filter {if [fields][type] &#61;&#61; "nginx_access" {grok {match &#61;> { "message" &#61;> "%{NGINXACCESS}" }}}if [fields][type] &#61;&#61; "apache_access" {grok {match &#61;> { "message" &#61;> "%{COMBINEDAPACHELOG}" }}} mutate { remove_field &#61;>["message"]remove_field &#61;>["host"]remove_field &#61;>["input"]remove_field &#61;>["prospector"]remove_field &#61;>["beat"]}geoip {source &#61;> "clientip"}date {match &#61;> [ "timestamp" , "dd/MMM/yyyy:HH:mm:ss Z" ]}
}output {if [fields][type] &#61;&#61; "nginx_access" {elasticsearch {hosts &#61;> "172.18.8.200:9200"index &#61;> "%{[fields][type]}-%{&#43;YYYY.MM.dd}"}}if [fields][type] &#61;&#61; "apache_access" {elasticsearch {hosts &#61;> "172.18.8.200:9200"index &#61;> "%{[fields][type]}-%{&#43;YYYY.MM.dd}"}}
}
grok
是我们对输出的数据进行切分格式化。mutate
对我们格式化输出的数据&#xff0c;删除掉一些没用的信息。geoip
是获取IP的地理信息&#xff0c;以便于后面作地图展示。date
是对时间的一种格式化输出。
关于过滤插件还有很多&#xff0c;大家可以参照官方文档或者查看我的博文介绍。
注意&#xff1a;
%{NGINXACCESS}
是一个grok patterns
&#xff0c;可以对我们的日志进行标准化输出&#xff0c;系统安装好之后&#xff0c;会支持很多patterns&#xff0c;可以使用命令rpm -ql logstash|grep patterns
看到很多的patterns
。
然而我们刚刚使用的%{NGINXACCESS}
&#xff0c;默认不存在&#xff0c;需要我们自定义&#xff0c;从上面命令中的patterns
中找到grok-patterns
文件&#xff0c;在最后面加入下面内容&#xff1a;
# Nginx logs
NGUSERNAME [a-zA-Z\.\&#64;\-\&#43;_%]&#43;
NGUSER %{NGUSERNAME}
NGINXACCESS %{IPORHOST:clientip} %{NGUSER:ident} %{NGUSER:auth} \[%{HTTPDATE:timestamp}\] "%{WORD:verb} %{URIPATHPARAM:request} HTTP/%{NUMBER:httpversion}" %{NUMBER:response} (?:%{NUMBER:bytes}|-) (?:"(?:%{URI:referrer}|-)"|%{QS:referrer}) %{QS:agent}
我安装的软件来看&#xff0c;路径是这样的&#xff1a;/usr/share/logstash/vendor/bundle/jruby/2.3.0/gems/logstash-patterns-core-4.1.2/patterns/grok-patterns。
3、启动检测
启动程序。
systemctl daemon-reload
systemctl enable logstash.service
systemctl start logstash.service
检测端口5044是否已经启动。
[root&#64;logstash ~]# netstat -tlnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1098/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1234/master
tcp6 0 0 127.0.0.1:9600 :::* LISTEN 24546/java
tcp6 0 0 :::5044 :::* LISTEN 24546/java
tcp6 0 0 :::22 :::* LISTEN 1098/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1234/master
四、Filebeat 安装配置(172.18.8.202)
因为我们默认的仓库里面没有nginx
软件包&#xff0c;所以我们首先添加epel源。
wget -P /etc/yum.repos.d/ https://mirrors.aliyun.com/repo/epel-7.repo
软件Filebeat Nginx安装。
yum install filebeat nginx -y
编辑文件/etc/filebeat/filebeat.yml
&#xff0c;修改为如下内容&#xff1a;
filebeat.inputs:
- type: logenabled: truepaths:- /var/log/nginx/access.logfields:type: nginx_access
filebeat.config.modules:path: ${path.config}/modules.d/*.ymlreload.enabled: false
setup.template.settings:index.number_of_shards: 3
output.logstash:hosts: ["172.18.8.201:5044"]
processors:- add_host_metadata: ~- add_cloud_metadata: ~
请注意我们上面添加的type: nginx_access
&#xff0c;是我们根据不同日志进行过滤的筛选条件。
启动程序。
systemctl enable filebeat.service
systemctl start filebeat.service
systemctl enable nginx.service
systemctl start nginx.service
五、验证
一切配置好之后&#xff0c;我们访问 nginx 服务&#xff0c;查看日志的收集情况&#xff0c;因为我这边开了调试模式stdout { codec &#61;> rubydebug }
&#xff0c;可以在 logstash 的服务器终端查看到格式化之后的日志格式如下&#xff1a;
{"&#64;version" &#61;> "1","source" &#61;> "/var/log/nginx/access.log","response" &#61;> "404","geoip" &#61;> {},"timestamp" &#61;> "13/Dec/2018:10:16:21 &#43;0800","ident" &#61;> "-","auth" &#61;> "-","verb" &#61;> "GET","fields" &#61;> {"type" &#61;> "nginx_access"},"request" &#61;> "/favicon.ico","tags" &#61;> [[0] "beats_input_codec_plain_applied",[1] "_geoip_lookup_failure"],"httpversion" &#61;> "1.1","bytes" &#61;> "3650","agent" &#61;> "\"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36\"","offset" &#61;> 10265,"clientip" &#61;> "172.18.8.4","&#64;timestamp" &#61;> 2018-12-13T02:16:21.000Z
}
然后我们登录 Kibana&#xff0c;添加索引&#xff0c;插件数据。