前言
- 通俗来讲,ELK是由Elasticsearch、Logstash、Kibana 三个开源软件的组成的一个组合体,这三个软件当中,每个软件用于完成不同的功能,ELK 又称为ELK stack,官方域名为stactic.co,ELK stack的主要优点有如下几个:
- 处理方式灵活: elasticsearch是实时全文索引,具有强大的搜索功能
- 配置相对简单:elasticsearch全部使用JSON 接口,logstash使用模块配置,kibana的配置文件部分更简单。
- 检索性能高效:基于优秀的设计,虽然每次查询都是实时,但是也可以达到百亿级数据的查询秒级响应
- 集群线性扩展:elasticsearch和logstash都可以灵活线性扩展
- 前端操作绚丽:kibana的前端设计比较绚丽,而且操作简单
- 是一个高度可扩展的开源全文搜索和分析引擎,它可实现数据的实时全文搜索搜索、支持分布式可实现高可用、提供API接口,可以处理大规模日志数据,比如Nginx、Tomcat、系统日志等功能
- 可以通过插件实现日志收集和转发,支持日志过滤,支持普通log、自定义json格式的日志解析。
- 主要是通过接口调用elasticsearch的数据,并进行前端数据可视化的展现。
一、elasticsearch部署
服务版本 | IP地址 | 主机名 |
elasticsearch-7.5.1-x86_64 | 192.168.66.15 | es-node1 |
elasticsearch-7.5.1-x86_64 | 192.168.66.16 | es-node2 |
1.1:防火墙放通端口
[root@es-node1 ~]# firewall-cmd --permanent --zone=public --add-port=9200/tcp
[root@es-node1 ~]# firewall-cmd --permanent --zone=public --add-port=9300/tcp
[root@es-node1 ~]# firewall-cmd --reload
[root@es-node1 ~]# firewall-cmd --zone=public --list-ports
9200/tcp 9300/tcp
1.2:关闭SELINUX
[root@es-node1 ~]# sed -ri '/SELINUX/s/enforcing/disabled/' /etc/selinux/config
[root@es-node1 ~]# setenforce 0
[root@es-node1 ~]# getenforce
Permissive
1.3:调整可打开的文件数限制
[root@es-node1 ~]# echo "* soft nofile 65535" >> /etc/security/limits.conf
[root@es-node1 ~]# echo "* hard nofile 65535" >> /etc/security/limits.conf
1.4:设置epel源、安装基本操作命令
[root@es-node1 ~]# yum -y install wget
[root@es-node1 ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@es-node1 ~]# yum install -y net-tools vim lrzsz tree screen lsof tcpdump ntpdate
1.5:时间同步
[root@es-node1 ~]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
[root@es-node1 ~]# echo "*/5 * * * * ntpdate time1.aliyun.com &> /dev/null && hwclock -w" >> /var/spool/cron/root
[root@es-node1 ~]# systemctl restart crond
1.6:重启服务器使配置生效
[root@es-node1 ~]# reboot
1.7:JAVA环境部署
[root@es-node1 ~]# mkdir /opt/soft
[root@es-node1 ~]# cd /opt/soft/
[root@logstash soft]# rpm -ivh jdk-8u231-linux-x64.rpm
2.1:下载安装
[root@es-node1 ~]# cd /opt/soft/
[root@es-node1 soft]# rpm -ivh elasticsearch-7.5.1-x86_64.rpm
2.2:编辑各ES服务器的配置文件
2.2.1:es-node1服务器
[root@es-node1 ~]# grep '^[a-Z]' /etc/elasticsearch/elasticsearch.yml
cluster.name: es.cluster #es集群名称
node.name: es-node1 #es节点名称。集群内节点名称不可重复
path.data: /data/elk/data #数据存储路径
path.logs: /data/elk/logs #日志存储路径
network.host: 192.168.66.15 #监听地址,可设置为本机IP或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"]
2.2.2:es-node2服务器
[root@es-node2 ~]# grep '^[a-Z]' /etc/elasticsearch/elasticsearch.yml
cluster.name: es.cluster #es集群名称
node.name: es-node2 #es节点名称
path.data: /data/elk/data
path.logs: /data/elk/data
network.host: 192.168.66.16
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"]
# 注意:配置集群的主机地址,配置之后集群的主机之间可以自动发现,必须至少配置 [discovery.seed_hosts,discovery.seed_providers,cluster.initial_master_nodes] 中的一个
2.3:创建数据目录并赋权
[root@linux-host1 ~]# mkdir -p /data/elk/{data,logs}
[root@linux-host1 ~]# ls /data/elk/
data logs
[root@linux-host1 ~]# chown -R elasticsearch. /data/elk/
- 注意:安装完成elasticsearch服务后,系统会自动创建该用户无法文章来源(Source):https://dqzboy.com再次创建;在进行给数据和日志目录授权时,用户必须为elasticsearch全称,不然会启动失败
2.4:启动elasticsearch服务并验证
[root@es-node1 ~]# systemctl start elasticsearch
[root@es-node1 ~]# systemctl enable elasticsearch
[root@es-node2 ~]# systemctl start elasticsearch
[root@es-node2 ~]# systemctl enable elasticsearch
- 注意:elasticsearch启动比较慢,需要等待一会,然后检查端口
- 如果有问题,查看/data/logs/es.cluster.log日志
2.5:通过浏览器访问elasticsearch服务端口
3、安装elasticsearch插件之head(两台其中一台安装即可)
- 插件是为了完成不同的功能,官方提供文章来源(Source):浅时光博客了一些插件但大部分是收费的,另外也有一些开发爱好者提供的插件,可以实现对elasticsearch集群的状态监控与管理配置等功能。
- Elasticsearch7.x版本不能使用命令直接安装head插件
3.1:修改es服务配置文件增加参数
- 2台ES服务器增加参数,使head插件可以访问es
[root@es-node1 ~]# vim /etc/elasticsearch/elasticsearch.yml
http.cors.enabled: true
http.cors.allow-origin: "*"
[root@es-node1 ~]# systemctl restart elasticsearch
3.2:下载head插件
[root@es-node1 ~]# cd /usr/local
[root@es-node1 local]# git clone https://github.com/mobz/elasticsearch-head.git
[root@es-node1 local]# yum -y install npm
3.3:安装grunt插件
[root@es-node1 local]# cd /usr/local/elasticsearch-head
#修改为淘宝源,不然下载很慢
[root@es-node1 ~]# npm config set registry https://registry.npm.taobao.org
[root@es-node1 elasticsearch-head]# npm install -g grunt-cli
3.4:修改head插件源Gruntfile.js
[root@es-node1 local]# vim /usr/local/elasticsearch-head/Gruntfile.js
- 注意:hostname是新增的,不要忘记原有的true后面加,符号
3.5:修改连接地app.js
[root@es-node1 ~]# vim /usr/local/elasticsearch-head/_site/app.js
3.6:执行安装
[root@es-node1 ~]# cd /usr/local/elasticsearch-head
[root@es-node1 elasticsearch-head]# npm install
[root@es-node1 elasticsearch-head]# npm install phantomjs-prebuilt@2.1.16 --ignore-scripts
[root@es-node1 elasticsearch-head]# npm install
3.7:后台启动
[root@es-node1 elasticsearch-head]# grunt server &
3.8:设置开机自启
3.8.1:编辑启动脚本
[root@es-node1 ~]# cd /usr/local/elasticsearch-head/
[root@es-node1 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 &
[root@es-node1 elasticsearch-head]# chmod +x elasticsearch-head
3.8.2:由systemd进行管理
[root@es-node1 elasticsearch-head]# cd /etc/systemd/system
[root@es-node1 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
3.8.3:设置开机启动
[root@es-node1 system]# systemctl daemon-reload
[root@es-node1 system]# systemctl enable elasticsearch-head.service
[root@es-node1 system]# systemctl list-unit-files | grep elasticsearch-head.service
elasticsearch-head.service enabled
[root@es-node1 system]# systemctl start elasticsearch-head.service
[root@es-node1 system]# systemctl status elasticsearch-head.service
3.9:防火墙放通9100
[root@es-node1 ~]# firewall-cmd --permanent --zone=public --add-port=9100/tcp
[root@es-node1 ~]# firewall-cmd --reload
3.10:页面验证
- 注意:保证集群内所有的es节点服务器都运行正常后,elasticsearch-head插件页面显示状态为正常
3.10.1:测试提交数据
3.10.2:验证索引是否存在
3.10.3:查看数据
3.11:使用谷歌商店的head插件
- 以上方式省去了很多步骤,其实如果一步一步安装head很慢(主要是网被隔离了)
3.11.1:Master与Slave的区别
- Master的职责
- 统计各node节点状态信息、集群状态信息统计、索引的创建和删除、索引分配的管理、关闭node节点等
4.1:通过shell命令获取集群状态
[root@es-node1 ~]# curl -sXGET http://192.168.66.15:9200/_cluster/health?pretty=true
- 获取到的是一个json格式的返回值,那就可以通过python对其中的信息进行分析,例如对status进行分析,如果等于green(绿色)就是运行在正常,等于yellow()表示副本分片丢失,red(红色)表示主分片丢失
4.2:python脚本获取集群状态
[root@es-node1 ~]# vim es-cluster.py
#!/usr/bin/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"
4.2.1:脚本执行结果
[root@es-node1 ~]# python es-cluster.py
50
5、进行版本降级后无法启动
- 将高版本降级至低版本时无法启动,查看日志发现报错,信息如下:
[ERROR][o.e.b.Bootstrap ] [es-node1] Exception
org.elasticsearch.ElasticsearchException: java.io.IOException: failed to read /data/elk/data/nodes/0/_state/node-1.st
- 原因:无法读取到/data/elk/data/nodes/0/_state/node-1.st文件
- 解决方案:删除nodes目录,重启es服务重新生成文件
6、索引展示说明
- 当通过logstash收集数据(或者beats插件收集数据)传给elasticsearch服务时,elasticsearch服务不会即时显示出索引,只有当日志数据路径下有写入新数据时才会在elasticsearch服务中显示出索引
7、插件lmenezes/cerebro安装
7.1:安装部署
7.1.1:下载软件包
7.1.2:上传解压安装
[root@es-node1 soft]# tar -xf cerebro-0.8.3.tgz -C /usr/local/
7.2:修改配置
[root@es-node1 ~]# cd /usr/local/
[root@es-node1 local]# mv cerebro-0.8.3/ cerebro
[root@es-node1 local]# cd cerebro/conf/
[root@es-node1 conf]# vim application.conf
7.3:创建启动脚本
[root@es-node1 ~]# cd /usr/local/cerebro/
[root@es-node1 cerebro]# vim cerebro-up
#!/bin/bash
#端口为自定义,访问页面时需要使用该端口
nohup /usr/local/cerebro/bin/cerebro -Dhttp.port=3399 >/usr/local/cerebro/nohup.out 2>&1 &
[root@linux-host1 cerebro]# chmod +x cerebro-up
[root@linux-host1 cerebro]# vim /etc/systemd/system/cerebro.service
[Unit]
Description=cerebro
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/cerebro/cerebro-up
[Install]
WantedBy=multi-user.target
7.4:启动服务
[root@es-node1 ~]# systemctl daemon-reload
[root@es-node1 ~]# systemctl enable cerebro
[root@es-node1 ~]# systemctl start cerebro
7.5:访问页面
二、部署logstash
服务版本 | IP地址 | 主机名 |
logstash-7.5.1 | 192.168.66.17 | logstash |
1.1:防火墙放通端口
[root@logstash ~]# firewall-cmd --permanent --zone=public --add-port=5044/tcp
[root@logstash ~]# firewall-cmd --permanent --zone=public --add-port=9600/tcp
[root@logstash ~]# firewall-cmd --reload
[root@logstash ~]# firewall-cmd --zone=public --list-ports
5044/tcp 9066/tcp
1.2:关闭SELINUX
[root@logstash ~]# sed -ri '/SELINUX/s/enforcing/disabled/' /etc/selinux/config
[root@logstash ~]# setenforce 0
[root@logstash ~]# getenforce
Permissive
1.3:设置epel源、安装基本操作命令
[root@logstash ~]# yum -y install wget
[root@logstash ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@logstash ~]# yum install -y net-tools vim lrzsz tree screen lsof tcpdump ntpdate
1.4:时间同步
[root@logstash ~]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
[root@logstash ~]# echo "*/5 * * * * ntpdate time1.aliyun.com &> /dev/null && hwclock -w" >> /var/spool/cron/root
[root@logstash ~]# systemctl restart crond
1.5:重启服务器使配置生效
[root@logstash ~]# reboot
1.6:JAVA环境部署
[root@logstash ~]# mkdir /opt/soft
[root@logstash ~]# cd /opt/soft/
[root@logstash soft]# rpm -ivh jdk-8u231-linux-x64.rpm
[root@logstash soft]# rpm -ivh logstash-7.5.1.rpm
[root@logstash soft]# vim /etc/logstash/logstash.yml
[root@logstash ~]# grep -n ^[a-Z] /etc/logstash/logstash.yml
19:node.name: logstash #节点名称,一般为主机域名
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 #日志目录
[root@logstash ~]# chown -R logstash.logstash /usr/share
#权限更改为logstash用户和组,否则启动的时候日志报错
[root@logstash ~]# systemctl enable logstash
[root@logstash ~]# systemctl start logstash
[root@logstash ~]# ss -tnlp
[root@logstash ~]# /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" #消息的具体内容
}
[root@logstash ~]# /usr/share/logstash/bin/logstash -e 'input { stdin{} } output { file { path => "/tmp/log-%{+YYYY.MM.dd}messages.gz"}}'
5.1:检查生成的文件
5.2:查看文件内容
[root@logstash ~]# tail log-2019.05.31messages.gz
{"@version":"1","message":"hello","host":"linux-host3.exmaple.com","@timestamp":"2019-05-31T16:41:56.639Z"}
[root@logstash ~]# /usr/share/logstash/bin/logstash -e 'input { stdin{} } output { elasticsearch {hosts => ["192.168.66.15:9200"] index => "mytest-%{+YYYY.MM.dd}" }}'
[root@logstash ~]# cd /data/elk/data/nodes/0/indices/
//查看索引
[root@logstash ~]# curl 127.0.0.1:9200/_cat/indices
output配置中elasticsearch配置
action
index #给一个文档建立索引
delete #通过id值删除一个文档(这个action需要指定一个id值)
create #插入一条文档信息,如果这条文档信息在索引中已经存在,那么本次插入工作失败
update #通过id值更新一个文档。更新有个特殊的案例upsert,如果被更新的文档还不存在,那么就会用到upsert
写入事件所用的索引。可以动态的使用%{foo}语法,它的默认值是:
“logstash-%{+YYYY.MM.dd}”,以天为单位分割的索引,使你可以很容易的删除老的数据或者搜索指定时间范围内的数据。
索引不能包含大写字母。推荐使用以周为索引的ISO 8601格式,例如logstash-%{+xxxx.ww}
示例:
index => "tomcat_logs_index_%{+YYYY.MM.dd}"
是一个数组类型的值
http协议使用的是http地址,端口是9200,示例:
hosts => ["192.168.66.15:9200", "192.168.66.16:9200"]
定义es索引的type,一般你应该让同一种类型的日志存到同一种type中,比如debug日志和error日志存到不同的type中
你可以设置指向你自己模板的路径。如果没有设置,那么默认的模板会被使用
这个配置项用来定义在Elasticsearch中模板的命名
注意删除旧的模板
curl -XDELETE <http://localhost:9200/_template/OldTemplateName?pretty>
布尔类型 默认为false
设置为true表示如果你有一个自定义的模板叫logstash,那么将会用你自定义模板覆盖默认模板logstash
布尔类型 默认为true
设置为false将关闭logstash自动管理模板功能
比如你定义了一个自定义模板,更加字段名动态生成字段,那么应该设置为false
ELK Stack 在入门学习过程中,必然会碰到自己修改定制索引映射(mapping)乃至模板(template)的问题。
这时候,不少比较认真看 Logstash 文档的新用户会通过下面这段配置来制定自己的模板策略:
output {
elasticsearch {
host => "127.0.0.1"
manage_template => true
template => "/path/to/mytemplate"
template_name => "myname"
}
}
三、部署kibana
- Kibana是一个通过调用elasticsearch服务器进行图形化展示搜索结果的开源项目。
服务版本 | IP地址 | 主机名 |
kibana-7.5.1 | 192.168.66.18 | Kibana |
1.1:防火墙放通端口
[root@kibana ~]# firewall-cmd --permanent --zone=public --add-port=5601/tcp
[root@kibana ~]# firewall-cmd --reload
[root@kibana ~]# firewall-cmd --zone=public --list-ports
5601/tcp
1.2:关闭SELINUX
[root@kibana ~]# sed -ri '/SELINUX/s/enforcing/disabled/' /etc/selinux/config
[root@kibana ~]# setenforce 0
[root@kibana ~]# getenforce
Permissive
1.3:设置epel源、安装基本操作命令
[root@kibana ~]# yum -y install wget
[root@kibana ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@kibana ~]# yum install -y net-tools vim lrzsz tree screen lsof tcpdump wget ntpdate
1.4:时间同步
[root@kibana ~]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
[root@kibana ~]# echo "*/5 * * * * ntpdate time1.aliyun.com &> /dev/null && hwclock -w" >> /var/spool/cron/root
[root@kibana ~]# systemctl restart crond
1.5:重启服务器使配置生效
[root@kibana ~]# reboot
[root@kibana ~]# cd /opt/soft/
[root@kibana soft]# rpm -ivh kibana-7.5.1-x86_64.rpm
[root@kibana ~]# grep "^[a-Z]" /etc/kibana/kibana.yml
2 server.port: 5601
7 server.host: "0.0.0.0"
28 elasticsearch.hosts: ["http://192.168.66.15:9200"]
37 kibana.index: ".kibana" #可开启,此项不开启也可以
114 i18n.local: "zh-CN" #此项去除注释并改为zh-CN
kibana汉化
- 中文包在 /usr/share/kibana/node_modules/x-pack/plugins/translations/translations/zh-CN.jso
[root@kibana ~]# systemctl start kibana
[root@kibana ~]# systemctl enable kibana
[root@kibana ~]# ss -tnlp | grep 5601
- 在Logstash服务器配置片段文件,将系统日志发送给ES服务器
[root@kibana ~]# cd /etc/logstash/conf.d/
[root@kibana conf.d]# vim system-log.conf
input {
file {
path => "/var/log/message" #日志路径
start_position => "beginning" #第一次从头收集,之后从新添加的日志收集
type => "nginx-access.log" #定义事件唯一类型
stat_interval => "3" #日志收集的间隔时间
}
}
output {
elasticsearch {
hosts =>["192.168.66.15:9200"] #输出到ES服务器
index =>"system-log-%{+YYYY.MM.dd}"
}
}
6.1:检测语法
[root@kibana conf.d]# /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/nginx-log.conf -t
6.2:重启Logstash
[root@kibana ~]# systemctl restart logstash
[root@kibana ~]# ss -tnlp | grep 9600
- head插件部署在ES-node1服务器上,通过es-node1服务器IP+9100端口进行访问查看
8、在kibana界面添加system-log索引
8.1:进入Kibana界面点击管理
8.2:然后点击索引管理
8.3:点击创建索引模式Create index pattern
8.4:添加定义的system-log索引
8.5:索引管理
四、收集nginx的访问日志
版本 | IP地址 | 主机名 |
Nginx 1.16.0 | 192.168.66.19 | nginx-web |
- 收集流程
- 在192.168.66.19服务器上安装filebeat,通过配置filebeat来收集access.log,同时将收集到的日志输出到redis上,redis服务器地址为192.168.66.20
- 然后192.168.66.17上的logstash收集redis中存放的日志,将日志存放到es中,最终通过配置kibana实现业务展示
1.1:部署nginx服务
[root@nginx-web ~]# yum install gcc gcc-c++ automake pcre pcre-devel zlip zlib-devel openssl openssl-devel
[root@nginx-web ~]# useradd nginx -r
[root@nginx-web ~]# tar -xf nginx-1.16.0.tar.gz
[root@nginx-web ~]# cd nginx
[root@nginx-web 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@nginx-web nginx]# make && make install
[root@nginx-web nginx]# chown -R nginx. /usr/local/nginx/
1.2:编辑配置文件并准备web页面
[root@nginx-web ~]# cd /usr/local/nginx
[root@nginx-web nginx]# vim conf/nginx.conf
location /web {
root html;
index index.html index.htm;
}
[root@nginx-web nginx]# mkdir /usr/local/nginx/html/web
[root@nginx-web nginx]# echo " Nginx WebPage! " > /usr/local/nginx/html/web/index.html
1.3:将nginx日志转换为json格式
[root@nginx-web ~]# 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;
[root@nginx-web ~]# 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@nginx-web ~]# nginx -s reload
1.4:配置Nginx启动脚本
[root@nginx-web ~]# 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@nginx-web ~]# vim /etc/profile
...
nginx=/usr/local/nginx
export PATH=$nginx/sbin:$PATH
[root@nginx-web ~]# source /etc/profile
1.5:启动nginx并验证
[root@nginx-web ~]# nginx -t #测试配置文件语法
[root@nginx-web ~]# systemctl start nginx #启动服务
[root@nginx-web ~]# systemctl enable nginx #开机自启动
[root@nginx-web ~]# nginx -s reload #重读配置文件
[root@nginx-web nginx]# lsof -i:80
1.6:访问nginx页面
- http://192.168.66.19/web/
1.7:确认日志格式为json格式
[root@nginx-web ~]# tail -f /var/log/nginx/access.log
版本 | IP地址 | 主机名 |
Redis 5.0.3 | 192.168.66.20 | Redis-Server |
2.1:下载源码包并安装
[root@Redis-Server ~]# tar -xf redis-5.0.3.tar.gz -C /usr/local/
[root@Redis-Server ~]# cd /usr/local/
[root@Redis-Server local]# mv redis-5.0.3/ redis
[root@Redis-Server local]# cd redis/
[root@Redis-Server redis]# make
2.2:编辑配置文件
[root@Redis-Server redis]# vim redis.conf
[root@Redis-Server redis]# ln -sv /usr/local/redis/src/redis-server /usr/bin/
"/usr/bin/redis-server" -> "/usr/local/redis/src/redis-server"
[root@Redis-Server redis]# ln -sv /usr/local/redis/src/redis-cli /usr/bin/
"/usr/bin/redis-cli" -> "/usr/local/redis/src/redis-cli"
2.3:设置Redis访问密码
[root@Redis-Server redis]# redis-cli
127.0.0.1:6379>
[root@Redis-Server redis]# vim /usr/local/redis/redis.conf
507 requirepass 123456
- 注意:如果Redis已经启动,那么更改配置文件后需要重启Redis并加载配置文件
[root@Redis-Server redis]# redis-cli -a 123456 #输入连接密码
127.0.0.1:6379> shutdown #关闭Redis
[root@Redis-Server redis]# redis-server /usr/local/redis/redis.conf & #放在后台运行
# 如果Redis没有配置为service服务,可以通过以下方式重启
[root@Redis-Server redis]# /usr/local/bin/redis-cli shutdown
[root@Redis-Server redis]# /usr/local/bin/redis-server /etc/redis.conf
2.4:测试Redis
[root@Redis-Server redis]# redis-server /usr/local/redis/redis.conf &
- 设置Redis认证密码后,客户端登录时需要使用-a参数输入认证密码,不添加该参数虽然也可以登录成功,但是没有任何操作权限
[root@Redis-Server 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.1:下载并安装filebeat
[root@nginx-web ~]# cd /opt/soft
[root@nginx-web soft]# rpm -ivh filebeat-7.5.1-x86_64.rpm
3.2:filebeat收集Nginx日志并写入redis
- Filebeat支持将数据直接写入到redis服务器,本步骤为写入到redis当中的一个可以,另外filebeat还支持写入到elasticsearch、logstash等服务器。
#备份原先的filebeat配置文件,然后新建配置文件
[root@nginx-web soft]# cd /etc/filebeat/
[root@nginx-web filebeat]# mv filebeat.yml{,_bak}
[root@nginx-web filebeat]# vim filebeat.yml
filebeat.inputs:
- type: log
paths:
- /usr/local/nginx/logs/access.log #日志路径
fields:
log_file: access.log
log_type: nginx.log #logstah配置时取该值进行判断输出
fields_under_root: true
encoding: utf-8
output.redis:
hosts: ["192.168.66.20:6379"] #redis服务器地址和端口
key: "nginx-log" #自定义redis存储数据的key名称
db: 0
timeout: 5 #超时时间
password: 123456 #redis密码
3.3:启动filebeat
[root@nginx-web ~ ]# systemctl start filebeat
[root@nginx-web ~ ]# systemctl enable filebeat
[root@nginx-web ~ ]# systemctl status filebeat
3.4:验证redis服务器是否有数据
[root@nginx-web ~ ]# redis-cli -a 123456
127.0.0.1:6379> KEYS *
1) "nginx-log"
127.0.0.1:6379>
- 查看日志内容;注意选择的db是否和filebeat写入一致
127.0.0.1:6379> SELECT 0
OK
127.0.0.1:6379> RPOP nginx-log
4、logstash服务器读取Redis上存储的日志
4.1:配置收集规则
[root@logstash ~]# 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 {
stdout {} #通过控制台实时展示收集的数据信息
if [log_type] == "nginx.log" { #这里判断取值filebeat配置文件中的log_type参数
elasticsearch {
hosts => ["192.168.66.17:9200"]
index => "nginx-log"
}
}
}
4.2:重启logstash
[root@logstash ~]# systemctl restart logstash
[root@logstash ~]# ss -tnlp | grep 9600
- 在ES服务中的head插件上查看是否有定义的索引
- http://192.168.66.15:9100/
5.1:创建索引
5.2:展示收集的日志
- 确保日志信息为json格式显示,且没有关于索引无法输出到ES的报错信息
5.3:定义图像,展示
[root@es-node1 ~]# cat /opt/elk_del.sh
#!/bin/bash
###################################
#删除早于十天的ES集群的索引
###################################
function delete_indices() {
comp_date=`date -d "10 day ago" +"%Y-%m-%d"`
date1="$1 00:00:00"
date2="$comp_date 00:00:00"
t1=`date -d "$date1" +%s`
t2=`date -d "$date2" +%s`
if [ $t1 -le $t2 ]; then
echo "$1时间早于$comp_date,进行索引删除"
format_date=`echo $1| sed 's/-/\./g'`
curl -XDELETE http://127.0.0.1:9200/*$format_date
fi
}
curl -XGET http://127.0.0.1:9200/_cat/indices | awk -F" " '{print $3}' | awk -F"-" '{print $NF}' | egrep "[0-9]*\.[0-9]*\.[0-9]*" | sort | uniq | sed 's/\./-/g' | while read LINE
do
#调用索引删除函数
delete_indices $LINE
done
[root@es-node1 ~]# crontab -e
00 01 * * * bash /opt/elk_del.sh &>/dev/null