当前位置:   article > 正文

小白实操ELK环境部署+监控nginx日志_两台机器搭建elk+nginx

两台机器搭建elk+nginx

简介:

本次部署应用的是filebeats(客户端),logstash+elasticsearch+kibana(服务端)架构

原理及组件介绍请看我其他文章:https://blog.csdn.net/weixin_39845407/article/details/81479729

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

实验环境:

本次部署的是单点ELK这边只用了两台机器(centos-7.1)

ELK服务端:192.168.137.128

nginx客户端:192.168.137.131

准备:

关闭防火墙

systemctl stop firewalld

或者开放对应端口:https://blog.csdn.net/weixin_39845407/article/details/81510368

下载并解压到/usr/local/

  1. mkdir /elk;cd /elk
  2. wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.3.tar.gz
  3. wget https://artifacts.elastic.co/downloads/logstash/logstash-6.2.3.tar.gz
  4. wget https://artifacts.elastic.co/downloads/kibana/kibana-6.2.3-linux-x86_64.tar.gz
  5. tar xf kibana-6.2.3-linux-x86_64.tar.gz -C /usr/local/
  6. tar xf elasticsearch-6.2.3.tar.gz -C /usr/local
  7. tar xf logstash-6.2.3.tar.gz -C /usr/local

安装java1.8

yum -y install java-1.8*

# 也可用源码安装看心情即可

配置elasticsearch

新建elasticsearch用户(elasticsearch用普通用户启动)并启动

  1. useradd elasticsearch
  2. chown -R elasticsearch.elasticsearch /usr/local/elasticsearch-6.2.3/
  3. su - elasticsearch
  4. cd /usr/local/elasticsearch-6.2.3/
  5. ./bin/elasticsearch -d

# 查看进程如果没有启动成功的话,可以查看日志/usr/local/elasticsearch-6.2.3/logs/elasticsearch.log

测试是否可以正常访问

# 也可以网页访问

配置logstash

收集nginx日志之使用grok过滤插件解析日志

grok作为一个logstash的过滤插件,支持根据模式解析文本日志行,拆成字段。

  • nginx日志的配置:
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"';
  • logstash中grok的正则(添加在vendor/bundle/jruby/2.3.0/gems/logstash-patterns-core-4.1.2/patterns/grok-patterns文件中)为:
  1. WZ ([^ ]*)
  2. NGINXACCESS %{IP:remote_ip} \- \- \[%{HTTPDATE:timestamp}\] "%{WORD:method} %{WZ:request} HTTP/%{NUMBER:httpversion}" %{NUMBER:status} %{NUMBER:bytes} %{QS:referer} %{QS:agent} %{QS:xforward}

创建logstash配置文件

  1. cat /usr/local/logstash-6.2.3/default.conf
  2. input {
  3. beats {
  4. port => "5044"
  5. }
  6. }
  7. # 数据过滤
  8. filter {
  9. grok {
  10. match => { "message" => "%{NGINXACCESS}" }
  11. }
  12. geoip {
  13. # nginx客户端ip
  14. source => "192.168.137.131"
  15. }
  16. }
  17. # 输出配置为本机的9200端口,这是ElasticSerach服务的监听端口
  18. output {
  19. elasticsearch {
  20. hosts => ["127.0.0.1:9200"]
  21. }
  22. }

1.后台启动logstash:nohup bin/logstash -f default.conf &

2.查看启动日志:tailf nohup.out

3.查看端口是否启动:netstat -napt|grep 5044

配置kibana

打开Kibana的配置文件/usr/local/kibana-6.2.3-linux-x86_64/config/kibana.yml,找到下面这行并修改:+

  1. #server.host: "localhost"
  2. 修改为
  3. server.host: "192.168.137.128"

这样其他电脑就能用浏览器访问Kibana的服务了;

2. 进入Kibana的目录:cd /usr/local/kibana-6.2.3-linux-x86_64

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

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

5.查看端口是否启动:netstat -napt|grep 5601

测试:

在浏览器访问192.168.137.128:5601

# 到此。ELK部署完成

nginx客户端配置

下载nginx并解压到/usr/local/

  1. wget http://nginx.org/en/download/nginx-1.12.2.tar.gz
  2. tar xf ./nginx-1.12.2.tar.gz -C /usr/local/

安装

  1. # 安装依赖包
  2. yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
  3. # 编译安装
  4. ./configure --prefix=/usr/local/nginx --sbin-path=/usr/bin/nginx
  5. make && make install

修改日志格式

  1. vim /usr/local/nginx/conf/nginx.conf
  2. # 写入以下内容
  3. log_format main '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"';

下载filebeat并解压到/usr/local/

  1. wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.2.3-linux-x86_64.tar.gz
  2. tar xf ./filebeat-6.2.3-linux-x86_64.tar.gz -C /usr/local/

打开文件/usr/local/filebeat-6.2.3-linux-x86_64/filebeat.yml,找到如下图的位置:

改成如下:

找到如下图位置:

改为如下:

找到如下图位置:

改为如下:

后台启动filebeat:nohup ./filebeat -e -c filebeat.yml &

查看日志:tailf nohup.out

创建Index Patterns

通过浏览器多访问几次nginx服务,这样能多制造一些访问日志,访问地址:https://192.168.137.131 

访问Kibana:https://192.168.137.128:5601,点击左上角的Discover,如下图红框,可以看到访问日志已经被ELK搜集了:

è¿éåå¾çæè¿°

如下图,输入logstash-*,点击”Next step”:

这里写图片描述如下图,选择Time Filter,再点击“Create index pattern”:

这里写图片描述

页面提示创建Index Patterns成功:

这里写图片描述

点击左上角的”Discover”按钮,即可看到最新的日志信息,如下图:

这里写图片描述

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/321172
推荐阅读
相关标签
  

闽ICP备14008679号