ELK-7.1.1 安装部署数据收集展示

本贴最后更新于 1696 天前,其中的信息可能已经东海扬尘

前言

什么是 ELK

通俗来讲,ELK 是由 Elasticsearch、Logstash、Kibana 三个开源软件的组成的一个组合体,这三个软件当中,每个软件用于完成不同的功能,ELK 又称为 ELK stack,官方域名为 stactic.co

ELK stack 的主要优点有如下几个:

(1)处理方式灵活: elasticsearch 是实时全文索引,具有强大的搜索功能
(2)配置相对简单:elasticsearch 全部使用 JSON 接口,logstash 使用模块配置,kibana 的配置文件部分更简单。
(3)检索性能高效:基于优秀的设计,虽然每次查询都是实时,但是也可以达到百亿(4)级数据的查询秒级响应。
(5)集群线性扩展:elasticsearch 和 logstash 都可以灵活线性扩展
(6)前端操作绚丽:kibana 的前端设计比较绚丽,而且操作简单

什么是 Elasticsearch

Elastic Search 是一个基于 Lucene 的开源分布式搜索服务器。它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,RESTful 风格接口,多数据源,自动搜索负载等。它提供了一个分布式多用户能力的全文搜索引擎,基于 RESTful web 接口。Elasticsearch 是用 Java 开发的,并作为 Apache 许可条款下的开放源码发布,是非常流行的企业搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。
在 elasticsearch 中,所有节点的数据是均等的。

什么是 Logstash

Logstash 是一个完全开源的工具,它可以对你的日志进行收集、过滤、分析,支持大量的数据获取方法,并将其存储供以后使用(如搜索)。说到搜索,logstash 带有一个 web 界面,搜索和展示所有日志。一般工作方式为 c/s 架构,client 端安装在需要收集日志的主机上,server 端负责将收到的各节点日志进行过滤、修改等操作在一并发往 elasticsearch 上去。

什么是 Kibana

Kibana 是一个基于浏览器页面的 Elasticsearch 前端展示工具,也是一个开源和免费的工具,Kibana 可以为 Logstash 和 ElasticSearch 提供的日志分析友好的 Web 界面,可以帮助您汇总、分析和搜索重要数据日志。

一、elasticsearch 部署:

1、环境准备

服务版本 IP 地址 主机名
elasticsearch-7.1.1 192.168.66.15 linux-host1.exmaple.com
elasticsearch-7.1.1 192.168.66.16 linux-host2.exmaple.com

关闭防所有服务器的火墙和 selinux


[root@localhost ~ ]# systemctl  disable  NetworkManager

[root@localhost ~ ]# sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config

[root@localhost ~ ]# echo "* soft nofile 65536" >> /etc/security/limits.conf

[root@localhost ~ ]# echo "* hard nofile 65536" >> /etc/security/limits.conf

设置 epel 源、安装基本操作命令并同步时间:


[root@linux-host1 ~]# yum install -y net-tools vim lrzsz tree screen lsof tcpdump wget ntpdate

[root@linux-host1 ~]# cp /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime 

[root@linux-host1 ~]# echo "*/5 * * * *  ntpdate time1.aliyun.com &> /dev/null && hwclock -w" >> /var/spool/cron/root

[root@linux-host1 ~]# systemctl  restart crond

[root@linux-host1 ~]# reboot 

在两台 ES 服务器准备 java 环境:

下载地址:https://www.oracle.com/technetwork/java/javase/downloads/jdk12-downloads-5295953.html
[root@linux-host1 ~]# tar -xf jdk-11.0.2_linux-x64_bin.tar.gz -C /usr/local/
[root@linux-host1 ~]# cd /usr/local/
[root@linux-host1 local]# mv jdk-11.0.2  jdk

[root@linux-host1 ~]# vim /etc/profile
export HISTTIMEFORMAT="%F %T `whoami` "
export JAVA_HOME=/usr/local/jdk
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin
[root@linux-host1 ~]# source  /etc/profile
[root@linux-host1 ~]# java -version
java version "11.0.2" 2019-01-15 LTS  #确认可以出现当前的java版本号
Java(TM) SE Runtime Environment 18.9 (build 11.0.2+9-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.2+9-LTS, mixed mode)

2、官网下载 elasticsearch 并安装:

[root@linux-host1 ~]# rpm -ivh elk/elasticsearch-7.1.1-x86_64.rpm

## 编辑各elasticsearch服务器的服务配置文件
[root@linux-host1 ~]#grep '^[a-Z]' /etc/elasticsearch/elasticsearch.yml 
cluster.name: es.cluster
node.name: node-1
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
network.host: 0.0.0.0
http.port: 9200
discovery.seed_hosts: ["192.168.66.15", "192.168.66.16"]
cluster.initial_master_nodes: ["192.168.66.15", "192.168.66.16"] 

[root@linux-host2 ~]# grep '^[a-Z]' /etc/elasticsearch/elasticsearch.yml 
cluster.name: es.cluster
node.name: node-2
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
network.host: 0.0.0.0
http.port: 9200
discovery.seed_hosts: ["192.168.66.15", "192.168.66.16"]
cluster.initial_master_nodes: ["192.168.66.15", "192.168.66.16"]
  • 启动 elasticsearch 服务并验证:
[root@linux-host1 ~]# systemctl  restart elasticsearch
[root@linux-host2 ~]# systemctl  restart elasticsearch

image.png

  • 通过浏览器访问 elasticsearch 服务端口

image.png

3、安装 elasticsearch 插件之 head:(两台其中一台安装即可)

插件是为了完成不同的功能,官方提供了一些插件但大部分是收费的,另外也有一些开发爱好者提供的插件,可以实现对elasticsearch集群的状态监控与管理配置等功能。
Elasticsearch7.x版本不能使用命令直接安装head插件
# 修改配置文件/etc/elasticsearch/elasticsearch.yml增加参数
# 增加参数,使head插件可以访问es 
[root@linux-host1 ~]# vim /etc/elasticsearch/elasticsearch.yml
http.cors.enabled: true 
http.cors.allow-origin: "*"

下载 head 插件 解压至/usr/local 目录下

[root@linux-host1 ~]# cd /usr/local

[root@linux-host1 local ]# wget  https://github.com/mobz/elasticsearch-head/archive/master.zip
[root@linux-host1 local ]# unzip master.zip

安装 node

[root@linux-host1 local ]# wget https://npm.taobao.org/mirrors/node/latest-v12.x/node-v12.0.0-linux-x64.tar.gz

[root@linux-host1 local ]# tar -zxvf node-v12.0.0-linux-x64.tar.gz

修改环境变量/etc/profile 添加

[root@linux-host1 local ]# vim /etc/profile
export NODE_HOME=/usr/local/node-v12.0.0-linux-x64
export PATH=$PATH:$NODE_HOME/bin
export NODE_PATH=$NODE_HOME/lib/node_modules

#设置生效
[root@linux-host1 local ]# source /etc/profile

安装 grunt

[root@linux-host1 local ]# cd /usr/local/elasticsearch-head-master

[root@linux-host1 local ]# npm install -g grunt-cli 

修改 head 插件源码/usr/local/elasticsearch-head-master/Gruntfile.js

[root@linux-host1 local ]# vim /usr/local/elasticsearch-head-master/Gruntfile.js

PS:hostname是新增的,不要忘记原有的true后面加,符号

image.png

修改连接地址/usr/local/elasticsearch-head-master/_site/app.js

[root@linux-host1 local ]# vim /usr/local/elasticsearch-head-master/_site/app.js

image.png

下载运行 head 必要的文件(放置在文件夹/tmp 下)

[root@linux-host1 ~]# cd /tmp

[root@linux-host1 ~]# wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2

[root@linux-host1 ~]# yum -y install bzip2

#运行head
[root@linux-host1 ~]# cd /usr/local/elasticsearch-head-master

[root@linux-host1 ~]# npm install 

#后台启动
 [root@linux-host1 ~]# grunt server &

#设置开机自启
编辑启动脚本
[root@linux-host1 ~]# cd /usr/local/elasticsearch-head/

[root@linux-host1 elasticsearch-head]# vim elasticsearch-head
#!/bin/sh
# elasticsearch-head 的路径
cd /usr/local/elasticsearch-head
nohup npm run start >/usr/local/elasticsearch-head/nohup.out 2>&1 &

由systemd进行管理
[root@linux-host1 elasticsearch-head]# cd /etc/systemd/system
[root@linux-host1 system]# vim elasticsearch-head.service
[Unit]
Description=elasticsearch-head
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/elasticsearch-head/elasticsearch-head

[Install]
WantedBy=multi-user.target

设置开机启动
[root@linux-host1 system]# systemctl enable elasticsearch-head.service

[root@linux-host1 system]# systemctl list-unit-files | grep elasticsearch-head.service 

web 页面验证,http://192.168.66.15:9100

  • 测试提交数据:

image.png

  • 测试提交数据:

image.png

  • 验证索引是否存在:

image.png

  • 查看数据:

image.png

使用 Google 商店的 head 插件进行监控

前提是可以访问到谷歌商店
image.png
image.png

4、监控 elasticsearch 集群状态:

  • 通过 shell 命令获取集群状态:
[root@linux-host1 elk]# curl -sXGET  http://192.168.66.15:9200/_cluster/health?pretty=true
 
#获取到的是一个json格式的返回值,那就可以通过python对其中的信息进行分析,例如对status进行分析,如果等于green(绿色)就是运行在正常,等于yellow(×××)表示副本分片丢失,red(红色)表示主分片丢失

image.png

  • python 脚本
[root@linux-host1 ~]# cat els-cluster-monitor.py 
#!/usr/bin/env python
#coding:utf-8

import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
import subprocess
body = ""
false="false"
obj = subprocess.Popen(("curl -sXGET http://192.168.66.15:9200/_cluster/health?pretty=true"),shell=True, stdout=subprocess.PIPE)
data =  obj.stdout.read()
data1 = eval(data)
status = data1.get("status")
if status == "green":
    print "50"
else:
    print "100"
  • 脚本执行结果:
[root@linux-host1 ~]# python els-cluster-monitor.py 
50

二、部署 logstash:

1、环境准备

服务版本 IP 地址 主机名
logstash-7.1.1 192.168.66.17 linux-host3.exmaple.com

关闭防火墙和 selinux,并且安装 java 环境

[root@linux-host3 ~]# systemctl  disable  firewalld
[root@linux-host3 ~]# sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config

2、安装 java 环境

[root@linux-host3 ~]# tar -xf jdk-11.0.2_linux-x64_bin.tar.gz -C /usr/local/
[root@linux-host3 ~]# cd /usr/local/
[root@linux-host3 local]# mv jdk-11.0.2  jdk

[root@linux-host3 ~]# vim /etc/profile
export HISTTIMEFORMAT="%F %T `whoami` "
export JAVA_HOME=/usr/local/jdk
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin

[root@linux-host1 ~]# source  /etc/profile

[root@linux-host1 ~]# java -version
java version "11.0.2" 2019-01-15 LTS  #确认可以出现当前的java版本号
Java(TM) SE Runtime Environment 18.9 (build 11.0.2+9-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.2+9-LTS, mixed mode)

[root@linux-host3 ~]# reboot

3、安装 logstash 并配置

[root@linux-host3 ~]# rpm -ivh logstash-7.1.1.rpm
[root@linux-host3 ~]# vim /etc/logstash/logstash.yml
[root@linux-host3 ~]# grep -n ^[a-Z] /etc/logstash/logstash.yml 
19:node.name: linux-host3		#节点名称,一般为主机域名
28:path.data: /var/lib/logstash	#logstash和插件使用的持久化目录
77:config.reload.automatic: true	#开启配置文件自动加载
81:config.reload.interval: 10s	#配置文件自动加载时间间隔
190:http.host: "192.168.66.17"	#定义访问主机名,一般为本机IP或者主机域名
208:path.logs: /var/log/logstash	#日志目录
 

image.png

[root@linux-host3 ~]# chown  –R logstash.logstash /usr/share/logstash/data/queue 
#权限更改为logstash用户和组,否则启动的时候日志报错

4、启动 Logstash

[root@linux-host3 ~]# systemctl start logstash

出现问题:服务启动不了;查看系统日志发现Logstash无法找到JAVA;检测JAVA环境都没有问题

原因分析:Logstash启动时默认使用的/usr/bin下面的JAVA进行启动;而源码安装的JDK路径为/usr/local下面,所有会启动失败

解决方案:将JDK源码安装路径软链到/usr/bin下后问题解决
[root@linux-host3 ~]# ln -sv /usr/local/jdk/bin/java /usr/bin/
"/usr/bin/java" -> "/usr/local/jdk/bin/java"

[root@linux-host3 ~]# ln -sv /usr/local/jdk/bin/javac /usr/bin/
"/usr/bin/javac" -> "/usr/local/jdk/bin/javac"

[root@linux-host3 ~]# systemctl enable logstas

[root@linux-host3 ~]# systemctl status logstash

image.png

[root@linux-host3 ~]# ss -tnlp

image.png

5、测试标准输入输出

[root@linux-host3 ~]# /usr/share/logstash/bin/logstash   -e 'input {  stdin{} } output { stdout{  codec => rubydebug }}'  #标准输入和输出
hello
{
    "@timestamp" => 2019-05-31T16:37:48.276Z, #当前事件的发生时间,
      "@version" => "1", #事件版本号,一个事件就是一个ruby对象
          "host" => "linux-host3.exmaple.com", #标记事件发生在哪里
       "message" => "hello"  #消息的具体内容
}
 

image.png

6、测试输出到文件

[root@linux-host3 ~]# /usr/share/logstash/bin/logstash   -e 'input {  stdin{} } output { file { path => "/tmp/log-%{+YYYY.MM.dd}messages.gz"}}'

  • 检查生成的文件
    image.png
    image.png

  • 查看文件内容

[root@linux-host3 tmp]# tail log-2019.05.31messages.gz 
{"@version":"1","message":"hello","host":"linux-host3.exmaple.com","@timestamp":"2019-05-31T16:41:56.639Z"}

7、测试输出到 elasticsearch

[root@linux-host3 ~]# /usr/share/logstash/bin/logstash   -e 'input {  stdin{} } output { elasticsearch {hosts => ["192.168.66.15:9200"] index => "mytest-%{+YYYY.MM.dd}" }}'

image.png

8、elasticsearch 服务器验证收到数据

[root@linux-host1 ~]# cd /var/lib/elasticsearch/nodes/0/indices/

[root@linux-host1 indices]# ls
AYJBXOZjQGWQVnmXvRe0Nw  wby7uIAtQAyuvk0fQvIdQw
 

image.png

三、部署 kibana:

Kibana 是一个通过调用 elasticsearch 服务器进行图形化展示搜索结果的开源项目。

服务版本 IP 地址 主机名
kibana-7.1.1 192.168.66.18 linux-host2.exmaple.com

1、安装 kibana

[root@linux-host4 ~]# rpm -ivh kibana-7.1.1-x86_64.rpm

2、配置 kibana

[root@linux-host4 ~]#  grep  "^[a-Z]" /etc/kibana/kibana.yml
server.port: 5601
server.host: "0.0.0.0"
elasticsearch.hosts: ["http://192.168.66.15:9200"]
i18n.locale: "zh-CN"		#kibana7官方支持中文

3、启动 kibana 服务并验证

[root@linux-host4 ~]# systemctl  start kibana
[root@linux-host4 ~]# systemctl  enable  kibana
[root@linux-host4 ~]# ss -tnl | grep 5601

4、查看状态

http://192.168.66.18:5601/status

image.png
image.png
image.png
image.png

5、配置 logstash 收集系统日志

在 Logstash 服务器配置片段文件,将系统日志发送给 ES 服务器

[root@linux-host3 ~]# cd /etc/logstash/conf.d/

[root@linux-host3 conf.d]# vim system-log.conf
input {
  file {
    path => "/var/log/message"	#日志路径,默认权限为600,需要进行授权
    start_position => "beginning" 	#第一次从头收集,之后从新添加的日志收集
    type => "system-log"	#定义事件唯一类型
    stat_interval => "3" 		#日志收集的间隔时间
  }
}

output {
    elasticsearch {
       hosts =>["192.168.66.15:9200"]	#输出到ES服务器
       index =>"system-log-%{+YYYY.MM.dd}"
    }
}

  • 注意,需要给/var/log/messages 文件其他人添加读的权限
[root@linux-host3 conf.d]# chmod 644 /var/log/messages

5.1:检测语法

[root@linux-host3 conf.d]# /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/system-log.conf -t

5.2:重启 Logstash

[root@linux-host3 ~]# systemctl restart logstash
[root@linux-host3 ~]# ss -tnlp | grep 9600

6、在 ES 插件 head 页面查看

head 插件部署在 ES-node1 服务器上,通过 es-node1 服务器 IP+9100 端口进行访问查看

http://192.168.66.15:9100/

image.png

7、在 kibana 界面添加 system-log 索引

7.1:进入 Kibana 界面点击管理

image.png

7.2:然后点击索引管理

image.png

7.3:点击创建索引模式 Create index pattern

image.png

7.4:添加定义的 system-log 索引

image.png
image.png

7.5:索引管理

image.png
image.png

8、展示日志收集数据

image.png

  • 默认显示最近 15 分钟的日志
    image.png

9、Kibana 操作

  • 显示指定时间范围的日志信息

image.png

  • 查看日志详细信息

image.png

  • 添加选项

image.png

四、收集 nginx 的访问日志

服务版本 IP 地址 主机名
nginx-1.16.0 192.168.66.19 linux-host5.exmaple.com

收集流程
在 192.168.56.19 服务器上安装 filebeat,通过配置 filebeat 来收集 access.log,同时将收集到的日志输出到 redis 上,redis 服务器地址为 192.168.56.20
然后 192.168.56.17 上的 logstash 收集 redis 中存放的日志,将日志存放到 els 中,最终通过配置 kibana 实现业务展示

1、部署 Nginx

1.1:部署 nginx 服务:

[root@linux-host5 ~]# yum install gcc gcc-c++ automake pcre pcre-devel zlip zlib-devel openssl openssl-devel


[root@linux-host5 ~]# useradd nginx -r


[root@linux-host5 ~]# tar -xf nginx-1.16.0.tar.gz 


[root@linux-host5 ~]# cd nginx

[root@linux-host5 nginx]# ./configure --prefix=/usr/local/nginx --conf-path=/usr/local/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_dav_module --with-http_stub_status_module --with-threads --with-file-aio --with-http_sub_module

[root@linux-host5 nginx]# make && make install
[root@linux-host5 nginx]# chown -R nginx. /usr/local/nginx/

1.2:编辑配置文件并准备 web 页面:

[root@linux-host5 ~]# cd /usr/local/nginx
[root@linux-host6 nginx]# vim conf/nginx.conf
        location /web {
             root   html;
             index  index.html index.htm;
         }

image.png

[root@linux-host5 nginx]# mkdir  /usr/local/nginx/html/web

[root@linux-host5 nginx]# echo " Nginx WebPage! " > /usr/local/nginx/html/web/index.html

1.3:将 nginx 日志转换为 json 格式

[root@linux-host5 ~]# vim /usr/local/nginx/conf/nginx.conf

    log_format access_log_json '{"user_ip":"$http_x_forwarded_for","lan_ip":"$remote_addr","log_time":"$time_iso8601","user_rqp":"$request","http_code":"$status","body_bytes_sent":"$body_bytes_sent","req_time":"$request_time","user_ua":"$http_user_agent"}';
    access_log /var/log/nginx/access.log  access_log_json;

image.png

[root@linux-host5 ~]# nginx -t

nginx: the configuration file /usr/local/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/nginx.conf test is successful
[root@linux-host5 ~]# nginx -s reload

1.4:配置 Nginx 启动脚本:

[root@linux-host5 ~]#vim /usr/lib/systemd/system/nginx.service

[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
#EnvironmentFile=/etc/sysconfig/rdisc
#ExecStart=/sbin/rdisc $RDISCOPTS

[Install]
WantedBy=multi-user.target

[root@linux-host5 ~]# vim /etc/profile
 

1.5:启动 nginx 并验证:

[root@linux-host5 ~]# nginx -t   #测试配置文件语法

[root@linux-host5 ~]# systemctl start nginx  #启动服务
[root@linux-host5 ~]# systemctl enable nginx #开机自启动

[root@linux-host5 ~]# nginx -s reload #重读配置文件

[root@linux-host6 nginx]# lsof  -i:80
 

1.6:访问 nginx 页面:

http://192.168.66.19/web/

image.png

1.7:确认日志格式为 json 格式

[root@linux-host5 ~]# tail -f /var/log/nginx/access.log

  • 刷新页面 web 页面

image.png

2、部署 Redis

服务版本 IP 地址 主机名
redis-5.0.3 192.168.66.20 linux-host6.exmaple.com

2.1:下载源码包并安装

[root@linux-host6 ~]# tar -xf redis-5.0.3.tar.gz -C /usr/local/

[root@linux-host6 ~]# cd /usr/local/
[root@linux-host6 local]# mv redis-5.0.3/ redis
[root@linux-host6 local]# cd redis/
[root@linux-host6 redis]# make

2.2:编辑配置文件

[root@linux-host6 redis]# vim redis.conf  

image.png

[root@linux-host6 redis]# ln -sv /usr/local/redis/src/redis-server /usr/bin/

"/usr/bin/redis-server" -> "/usr/local/redis/src/redis-server"

[root@linux-host6 redis]# ln -sv /usr/local/redis/src/redis-cli /usr/bin/
"/usr/bin/redis-cli" -> "/usr/local/redis/src/redis-cli"

2.3:设置 Redis 访问密码

为安全考虑,生产环境必须设置 reids 连接密码:

  • 方式 1:动态设置,重启后失效
[root@linux-host6 redis]# redis-cli 

127.0.0.1:6379>
  • 方式 2:永久生效,修改配置文件
[root@linux-host6 redis]# vim /usr/local/redis/redis.conf

507 requirepass 123456

image.png


注意:如果 Redis 已经启动,那么更改配置文件后需要重启 Redis 并加载配置文件

[root@linux-host6 redis]# redis-cli -a 123456   #输入连接密码

127.0.0.1:6379> shutdown	#关闭Redis

[root@linux-host6 redis]# redis-server /usr/local/redis/redis.conf &	#放在后台运行
  • 如果 Redis 没有配置为 service 服务,可以通过以下方式重启
[root@linux-host6 redis]# /usr/local/bin/redis-cli shutdown

[root@linux-host6 redis]# /usr/local/bin/redis-server /etc/redis.conf

2.4:测试 Redis

如没有启动需要进行启动:

[root@linux-host6 redis]# redis-server /usr/local/redis/redis.conf &

设置 Redis 认证密码后,客户端登录时需要使用-a 参数输入认证密码,不添加该参数虽然也可以登录成功,但是没有任何操作权限

[root@linux-host6 redis]# redis-cli 

127.0.0.1:6379> KEYS *
(error) NOAUTH Authentication required.	#需要进行密码验证
127.0.0.1:6379> AUTH 123456		#密码认证
OK
127.0.0.1:6379> ping
PONG

3、Nginx 服务器上安装 filebeat

3.1:下载并安装 filebeat

下载安装 rpm 包并进行安装

[root@linux-host5 ~]# rpm -ivh filebeat-7.1.1-x86_64.rpm

3.2:filebeat 收集 Nginx 日志并写入 redis:

Filebeat 支持将数据直接写入到 redis 服务器,本步骤为写入到 redis 当中的一个可以,另外 filebeat 还支持写入到 elasticsearch、logstash 等服务器。

[root@linux-host5 ~]# vim /etc/filebeat/filebeat.yml
[root@linux-host5 ~]# grep -v "#" /etc/filebeat/filebeat.yml 

15:filebeat.inputs:
21:- type: log
24:  enabled: true
27:  paths:
28:    - /var/log/nginx/access.log	 #日志路径
33:  exclude_lines: ['^DBG',"^$"]	#不收取的
41:  exclude_files: ['.gz$']
42:  document_type: "nginx-log"	#类型,会在每条日志中插入标记;默认配置文件中不存在
68:filebeat.config.modules:
70:  path: ${path.config}/modules.d/*.yml
73:  reload.enabled: false
80:setup.template.settings:
81:  index.number_of_shards: 1
117:setup.kibana:
170:output.redis:	#在logstash output段中定义
171:  hosts: ["192.168.66.20:6379"]  #redis服务器地址
172:  key: "nginx-log"	#为了后期日志处理,建议自定义key名称
173:  db: 0		#使用第几个库
174:  timeout: 5	#超时时间
175: password:123456	#redis访问密码
179:processors:
180:  - add_host_metadata: ~
181:  - add_cloud_metadata: ~ 

image.png

注意:将配置文件中 Elasticsearch output 段中的内容注释掉

image.png

3.3:启动 filebeat

[root@linux-host5 ~]# systemctl  start filebeat

[root@linux-host5 ~]# systemctl enable filebeat
[root@linux-host5 ~]# systemctl status filebeat

image.png

3.4:验证 redis 服务器是否有数据

[root@linux-host6 ~]# redis-cli -a 123456

127.0.0.1:6379> KEYS *
1) "nginx-log"
127.0.0.1:6379>  

image.png

查看日志内容;注意选择的 db 是否和 filebeat 写入一致

127.0.0.1:6379> SELECT 0

OK
127.0.0.1:6379> RPOP nginx-log

4、logstash 服务器读取 Redis 上存储的日志

4.1:配置收集规则

[root@linux-host3 ~]# vim /etc/logstash/conf.d/nginx-log.conf

input {

	redis {
           host => "192.168.66.20"
           port => "6379"
           db => "1"
           key => "nginx-log"
           data_type => "list"
           password => "123456"
    	}
}

filter {
        json {
           source => "message"
        }
        useragent {
           source =>"user_ua"	#这个表示对message里面的哪个字段进行分析
           target =>"userAgent"	#agent将收集出的user agent的信息配置到了单独的字段中
        }
}

output {
    elasticsearch {
       hosts => ["192.168.66.15:9200"]
       index => "nginx-log"
   }
}

4.2:重启 logstash

[root@linux-host3 ~]# systemctl  restart logstash
[root@linux-host3 ~]# ss -tnlp | grep 9600

5、kibana 展示

在 ES 服务中的 head 插件上查看是否有定义的索引

http://192.168.66.15:9100/

image.png

5.1:创建索引

image.png

image.png

image.png

image.png

5.2:展示收集的日志

image.png

确保日志信息为 json 格式显示,且没有关于索引无法输出到 ES 的报错信息

image.png

5.3:定义图像,展示

image.png

image.png

image.png

6、ELK 定时删除 30 天前日志

  • 在 192.168.66.15 上写脚本
[root@linux-host1 opt]# cat /home/elk_del.sh 

#!/bin/bash
DATE=`date -d "30 days ago" +%Y.%m.%d`
curl -s  -XGET http://127.0.0.1:9200/_cat/indices?v| grep $DATE | awk -F '[ ]+' '{print $3}' >/tmp/elk.log
for elk in `cat /tmp/elk.log`
do
        curl  -XDELETE  "http://127.0.0.1:9200/$elk"
done
  • 添加定时任务
00 01 * * * bash /home/elk_del.sh &>/dev/null

filebeat 收集日志传给 ES 并自定义索引的相关配置

[root@linux-host5 ~]# vim /etc/filebeat/filebeat.yml
155 #-------------------------- Elasticsearch output ------------------------------

156 setup.ilm.enabled: false
157 setup.template.name: "access-web"
158 setup.template.pattern: "access-web-*"
159 output.elasticsearch:
160   # Array of hosts to connect to.
161   hosts: ["192.168.66.15:9200"]
162   index: "access-web-log"

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...
GeekBoyDqz
从不拘泥任何世俗凡人的目光,我要奔向前方那光芒