ELK平台搭建+filebeat收集nginx日志

本文实验搭建部分参考https://blog.csdn.net/boling_cavalry/article/details/79836171

elk搭建版本号为5.4.1(大家也可以去官网下载最新版本的,安装步骤都差不多),以下提供下载地址:

elasticsearch:https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.4.1.tar.gz

logstash:https://artifacts.elastic.co/downloads/logstash/logstash-5.4.1.tar.gz

kibana:https://artifacts.elastic.co/downloads/kibana/kibana-5.4.1-linux-x86_64.tar.gz

filebeat:https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-5.4.1-linux-x86_64.tar.gz

0.环境规划

两台实验虚拟机搭载centos6.8,ELK服务器是4G内存,Web服务器是1G内存。

  1. 业务请求到达nginx-server机器上的Nginx;
  2. Nginx响应请求,并在access.log文件中增加访问记录;
  3. FileBeat搜集新增的日志,通过LogStash的5044端口上传日志;
  4. LogStash将日志信息通过本机的9200端口传入到ElasticSerach;
  5. 搜索日志的用户通过浏览器访问Kibana,服务器端口是5601;
  6. Kibana通过9200端口访问ElasticSerach;

一、安装elasticsearch

可以点击跳转参考本人的

《elk日志平台搭建——elasticsearch6.3.2安装配置》

这里就不再赘述了。

elasticsearch.yml配置如下:

elasticsearch成功之后不妨再安装elasticsearch-head(因为不搞集群可忽略,推荐参考)

二、安装logstash

解压安装包,并将目录移至安装目录

[root@localhost opt]# tar zxf logstash-5.4.1.tar.gz 
[root@localhost opt]# ls
elasticsearch-5.4.1.tar.gz  kibana-5.4.1-linux-x86_64.tar.gz  logstash-5.4.1  logstash-5.4.1.tar.gz

[root@localhost opt]# cp -r logstash-5.4.1 /usr/local/logstash 

在/usr/local/logstash 目录下创建一个nginx_logs.conf文件(内容如下):

# 监听5044端口作为输入
input {
    beats {
        port => "5044"
    }
}
# 数据过滤
filter {
    grok {
        match => { "message" => "%{COMBINEDAPACHELOG}" }
    }
    geoip {
        source => "clientip"
    }
}
# 输出配置为本机的9200端口,这是ElasticSerach服务的监听端口
output {
    elasticsearch {
        hosts => ["192.168.10.129:9200"]
    }
}

启动logstash

[root@localhost logstash]# nohup bin/logstash -f nginx_logs.conf &

三、安装kibana

同样解压安装包,并将目录移至安装目录

进入安装目录/usr/local/kibana/修改config下的kibana.yml

修改内容为

 执行启动命令:nohup bin/kibana & 

查看启动日志:tail -f nohup.out 

启动成功浏览器访问http://192.168.10.129:5601/

四、安装nginx服务

在web服务器上安装nginx

我这里直接用下载的rpm包安装

[root@centos6 opt]# rpm -ivh nginx-1.12.2-1.el6.ngx.x86_64.rpm 

启动nginx服务

[root@centos6 nginx]# service nginx start
正在启动 nginx:                                           [确定]

浏览器访问http://192.168.10.130/

查看nginx日志输出

[root@centos6 nginx]# tail -f /var/log/nginx/access.log

五、安装filebeat

在web服务器上安装filebeat用于收集日志,和传输日志数据

[root@centos6 opt]# tar zxf filebeat-5.4.1-linux-x86_64.tar.gz 
[root@centos6 opt]# ls
filebeat-5.4.1-linux-x86_64  filebeat-5.4.1-linux-x86_64.tar.gz  nginx-1.12.2-1.el6.ngx.x86_64.rpm
[root@centos6 opt]# mv filebeat-5.4.1-linux-x86_64 /usr/local/filebeat

修改配置文件内容/usr/local/filebeat/filebeat.yml

修改字段

改为(路径即为nginx日志所在的文件路径):

修改字段

改为(注释掉)

修改字段

改为(elk服务器logstash地址):

启动FileBeat:nohup ./filebeat -e -c filebeat.yml -d “publish”  &

记得查看输出内容tail -f nohup.out有错误及时排错

六、登录kibana

浏览器访问http://192.168.10.129:5601/

多访问几次web服务器就会一堆的日志输出

如果日志没有输出可能的原因是一个web服务器的当前时间和elk的当前时间不一致导致的,解决办法是两台服务器都调节到正确的时区与时间,或者扩大时间展示访问也能看到日志输出。

还有可能的原因是访问地址或者访问权限问题,这个可以检查防火墙端口和各组件的配置修改解决。

如有疏漏请各位指正,谢谢!

猜你喜欢

转载自blog.csdn.net/qq_39626154/article/details/82109107