ELK Stack是软件集合Elasticsearch、Logstash、Kibana的简称,由这三个软件及其相关的组件能够打造大规模日志实时处理系统。java
其中,Elasticsearch 是一个基于 Lucene 的、支持全文索引的分布式存储和索引引擎,主要负责将日志索引并存储起来,方便业务方检索查询。node
Logstash是一个日志收集、过滤、转发的中间件,主要负责将各条业务线的各种日志统一收集、过滤后,转发给 Elasticsearch 进行下一步处理。linux
Kibana是一个可视化工具,主要负责查询 Elasticsearch 的数据并以可视化的方式展示给业务方,好比各种饼图、直方图、区域图等。nginx
所谓“大规模”,指的是 ELK Stack 组成的系统以一种水平扩展的方式支持天天收集、过滤、索引和存储 TB 规模以上的各种日志。c++
一般,各种文本形式的日志都在处理范围,包括但不限于 Web 访问日志,如 Nginx/Apache Access Log 。web
基于对日志的实时分析,能够随时掌握服务的运行情况、统计 PV/UV、发现异常流量、分析用户行为、查看热门站内搜索关键词等。redis
上图是ELK Stack实际应用中典型的一种架构,其中:json
1)filebeat:部署在具体的业务机器上,经过定时监控的方式获取增量的日志,并转发到Kafka消息系统暂存。bootstrap
2)Kafka:以高吞吐量的特征,做为一个消息系统的角色,接收从filebeat收集转发过来的日志,一般以集群的形式提供服务。vim
3)logstash:而后,Logstash从Kafka中获取日志,并经过Input-Filter-Output三个阶段的处理,更改或过滤日志,最终输出咱们感兴趣的数据。一般,根据Kafka集群上分区(Partition)的数量,1:1肯定Logstash实例的数量,组成Consumer Group进行日志消费。
4)elasticsearch:最后,Elasticsearch存储并索引Logstash转发过来的数据,并经过Kibana查询和可视化展现,达到实时分析日志的目的。
Elasticsearch/Kibana还能够经过安装x-pack插件实现扩展功能,好比监控Elasticsearch集群状态、数据访问受权等。
咱们一步步安装部署Elastic Stack系统的各个组件,而后以网站访问日志为例进行数据实时分析。
首先,到ELK 官网下载须要用到的Filebeat/Logstash/Elasticsearch/Kibana软件安装包。(推荐下载编译好的二进制可执行文件,直接解压执行就能够部署)
System: Centos release 6.8
ElasticSearch: 5.4.1
Logstash: 5.4.1
Kibana: 5.4.1
Java: openjdk version "1.8.0_161"
redis:3.05
Nginx: 1.10.1
1..下载软件包:
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.4.1.tar.gz
wget https://artifacts.elastic.co/downloads/kibana/kibana-5.4.1-linux-x86_64.tar.gz
wget https://artifacts.elastic.co/downloads/logstash/logstash-5.4.1.tar.gz
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-5.4.1-linux-x86_64.tar.gz
wget http://download.redis.io/releases/redis-3.0.5.tar.gz
yum install java-1.8.0-openjdk
2.准备
1)下面的命令实现永久关闭SELinux
[root@elk ~]# sed -i 's/^SELINUX=.*/#&/;s/^SELINUXTYPE=.*/#&/;/SELINUX=.*/a SELINUX=disabled' /etc/sysconfig/selinux
#永久修改下主机名,须要重启系统以后生效
#下面的命令实现临时关闭SELinux
[root@elk ~]# setenforce 0
setenforce: SELinux is disabled
2) 修改主机名
#临时修改
[root@elk ~]#hostname elk.server.com
#永久修改
[root@elk ~]# vi /etc/sysconfig/network
localhost.localdomain ------》把这行修改为下面的
elk.server.com #修改为你本身的主机名
#添加域名
[root@elk ~]#cat /etc/hosts
192.168.10.243 elk.server.com
3)关闭防火墙
#临时关闭
[root@elk ~]# iptables -F
或者
[root@elk ~]# service iptables stop
4) 同步时间
ntpdate -u ntp.api.bz
#建立安装目录
[root@elk yum.repos.d]#mkdir -pv /data/application/
#编译并进行安装
[root@elk ~]# tar zxf redis-3.0.5.tar.gz && cd redis-3.0.5
[root@elk redis-3.0.5]# make PREFIX=/data/application/redis-3.0.5 install
#建立配置文件目录
[root@elk redis-3.0.5]#mkdir /data/application/redis-3.0.5/{etc,run,log}
#修改redis.conf
[root@elk redis-3.0.5]#cp /data/application/redis-3.0.5/redis.conf etc/
[root@elk redis-3.0.5]#vi /data/application/redis-3.0.5/redis.conf
修改如下几项:
daemonize yes #后台模式运行
pidfile /data/application/redis-3.0.5/run/redis.pid #redis的pid
bind 0.0.0.0 #这里根据本身的ip填写
port 6379#端口
logfile "/data/application/redis-3.0.5/log/redis.log" #log存放位置
dir /data/application/redis-3.0.5
#启动redis
执行下面的命令
[root@elk~]#/ data/application/redis-3.0.5/bin/redis-server /data/application/redis-3.0.5/etc/redis.conf
#查看是否启动成功
[root@elk ~]# lsof -i:6379
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
redis-ser 2736 root 4u IPv4 26198 0t0 TCP 192.168.10.243:6379 (LISTEN)
#测试redis
[root@localhost ~]# /data/application/redis-3.0.5/bin/redis-cli -h 192.168.10.243
192.168.10.243:6379> ping
PONG
#出现PONG,证实可使用
注意:
es(elasticsearch)版本2.x以上须要JDK 1.8以上
运行es不能使用root用来来运行
es目录必须指定一个普通用户和组(受权)
es对内存和CPU的消耗比较高
es使用的端口看开放iptables:9200,9300等
es配置其余插件实现资源等可视化监控
es的版本和插件之间版本要匹配
es集群配置,第一节点配置好scp到其余节点便可(修改配置文件)
#建立elk用户
[root@localhost application]# adduser -s /bin/bash -c 'elk' -m -d /home/elk elk
注:
从2.0开始不能用root用户启动须要elk用户启动
#解压
[root@elk elk_pack]# tar zxvf elasticsearch-6.01.tar.gz -C /data/application/
注:
Elasticsearch是不须要编译的,解压就可使用
备份配置文件
[root@elk ~]# cp /data/application/elasticsearch-6.01/config/elasticsearch.yml{,.ori}
#找到如下几行修改
[root@elk config]# vi elasticsearch.yml
path.data: /data/shuju ----》存放数据路径
path.logs: /data/logs -----》日志路径
network.host: 0.0.0.0 -----》根据本身的ip修改
http.port: 9200
bootstrap.memory_lock: false
bootstrap.system_call_filter: false
#建立los,shuju
[root@elk config]#mkdir /data/{shuju,logs}
#修改elasticsearch权限
[root@elk ~]#chown -R elk.elk /data/application/elasticsearch-6.0.1./
[root@elk ~]#chown -R elk.elk /data/{shuju,logs}
[root@elk ~]# su – elk
#在前台显示下效果
[elk@elk ~]$/data/application/elasticsearch-6.0.1/bin/elasticsearch
#测试是否成功
[root@elk ~]# curl 192.168.10.243:9200
{
"name" : "z8htm2J",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "wEbF7BwgSe-0vFyHb1titQ",
"version" : {
"number" : "6.0.1",
"build_hash" : "3adb13b",
"build_date" : "2017-03-23T03:31:50.652Z",
"build_snapshot" : false,
"lucene_version" : "6.4.1"
},
"tagline" : "You Know, for Search"
}
启动elasticsearch出现以下错误
1.问题:最大线程数,打开的过低,须要增长线程数
max number of threads [1024] for user [elasticsearch] likely toolow, increase to at least [2048]
解决:
vi /etc/security/limits.d/90-nproc.conf
* soft nproc 2048
2.问题:打开虚拟内存的个数太少须要增长
max virtual memory areas vm.max_map_count [65530] likely toolow, increase to at least [262144]
解决:
[root@elk ~]#vi /etc/sysctl.conf
vm.max_map_count=655360
[root@elk ~]#sysctl -p
注:
vm.max_map_count文件容许max_map_count限制虚拟内存的数量
3.max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
#临时修改
[root@elk ~]# ulimit -SHn 65536
注:
-S 设置软件资源限制
-H 设置硬件资源限制
-n 设置内核能够同时能够打开文件描述符
[root@elk ~]# ulimit -n
65536
注:
修改这个缘由,启动elasticsearch 会出现这个状况too many open files,致使启动失败
#永久修改
#在文件最后添加
[root@elk ~]# vi /etc/security/limits.conf
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
注:
文件格式:username|@groupname type resource limit
分为3中类型type(有 soft,hard 和 -)
soft是指当前系统生效的设置值
hard 系统设置的最大值
[if !supportLists]- [endif]同时设置了soft和hard的值
nofile - 打开文件的最大数目
noproc - 进程的最大数目
soft<=hard soft的限制不能比hard限制高
#须要重启系统才会生效
#解压
[root@elk elk_pack]# tar zxvf logstash-6.0.1tar.gz -C /data/application/
注:
Logstash是不须要编译的,解压就可使用
#测试可否使用
[root@elk ~]# /data/application/logstash-6.0.1/bin/logstash -e 'input { stdin { } } output {stdout {} }'
Sending Logstash's logs to /data/application/logstash-5.2.0/logs which is now configured via log4j2.properties
The stdin plugin is now waiting for input:
[2017-04-12T11:54:10,457][INFO ][logstash.pipeline ] Starting pipeline {"id"=>"main", "pipeline.workers"=>1, "pipeline.batch.size"=>125, "pipeline.batch.delay"=>5, "pipeline.max_inflight"=>125}
[2017-04-12T11:54:10,481][INFO ][logstash.pipeline ] Pipeline main started
[2017-04-12T11:54:10,563][INFO ][logstash.agent ] Successfully started Logstash API endpoint {:port=>9600}
hello world ---->输入hell world,随便输入什么,能输出就证实可使用
2017-04-12T03:54:40.278Z localhost.localdomain hello world ---->输出hello world
/data/application/logstash-5.2.0/vendor/bundle/jruby/1.9/gems/logstash-patterns-core-4.0.2/patterns
#解压
[root@elk elk_pack]# tar zxvf kibana-6.0.1-linux-x86_64.tar.gz -C /data/application/
注:
Kibana是不须要编译的,解压就可使用
#修改配置kibana.yml文件
#cd kibana这个目录
[root@elk ~]# cd /data/application/kibana-6.0.1/config/
#找到如下几行修改
[root@elk config]# egrep -v "^$|^[#]" kibana.yml
server.port: 5601 #kibana的端口
server.host: "0.0.0.0" #访问kibana的ip地址
elasticsearch.url: "http://192.168.10.243:9200" #elasticsearch的ip地址
kibana.index: ".kibana" #建立索引
#测试是否启动成功
[root@192 ~]# /data/application/kibana-6.0.1/bin/kibana
log [06:22:02.940] [info][status][plugin:kibana@5.2.0] Status changed from uninitialized to green - Ready
log [06:22:03.106] [info][status][plugin:elasticsearch@5.2.0] Status changed from uninitialized to yellow - Waiting for Elasticsearch
log [06:22:03.145] [info][status][plugin:console@5.2.0] Status changed from uninitialized to green - Ready
log [06:22:03.193] [warning] You're running Kibana 5.2.0 with some different versions of Elasticsearch. Update Kibana or Elasticsearch to the same version to prevent compatibility issues: v5.3.0 @ 192.168.10.243:9200 (192.168.201.135)
log [06:22:05.728] [info][status][plugin:timelion@5.2.0] Status changed from uninitialized to green - Ready
log [06:22:05.744] [info][listening] Server running at http://192.168.10.243:5601
log [06:22:05.746] [info][status][ui settings] Status changed from uninitialized to yellow - Elasticsearch plugin is yellow
log [06:22:08.263] [info][status][plugin:elasticsearch@5.2.0] Status changed from yellow to yellow - No existing Kibana index found
log [06:22:09.446] [info][status][plugin:elasticsearch@5.2.0] Status changed from yellow to green - Kibana index ready
log [06:22:09.447] [info][status][ui settings] Status changed from yellow to green – Ready
#证实启动成功
#查看port
[root@elk shuju]# lsof -i:5601
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 4690 root 13u IPv4 40663 0t0 TCP 192.168.10.243:esmagent (LISTEN)
经过web访问
http://192.168.10.243:5601
1)安装nginx
安装依赖包
[root@www ~]# yum -y install gcc gcc-c++ make libtool zlib zlib-devel pcre pcre-devel openssl openssl-devel
下载nginx的源码包:http://nginx.org/download
[root@www ~]# tar zxf nginx-1.10.2.tar.gz
[root@www ~]# cd nginx-1.10.2/
[root@www ~]# groupadd www#添加www组
[root@www ~]# useradd -g www www -s /sbin/nologin#建立nginx运行帐户www并加入到www组,不容许www用户直接登陆系统
[root@www nginx-1.10.2]# ./configure --prefix=/usr/local/nginx1.10 --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre --with-http_ssl_module --with-http_gzip_static_module --user=www --group=www
[root@www nginx-1.10.2]# make&& make install
修改日志类型为json
[root@rocketmq-nameserver2 soft]# vim nginx/conf/nginx.conf
#添加以下内容
log_format json '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"url":"$uri",'
'"referer":"$http_referer",'
'"agent":"$http_user_agent",'
'"status":"$status"}';
access_log logs/access.log json;
2)安装部署Filebeat
tar xf filebeat-6.0.1-linux-x86_64.tar.gz
cd filebeat-6.0.1-linux-x86_64
编写收集文件:
filebeat.prospectors:
- input_type: log
paths:
- /var/log/*.log
- input_type: log
paths:
- /aebiz/soft/nginx/logs/*.log
encoding: utf-8
document_type: my-nginx-log
scan_frequency: 10s
harvester_buffer_size: 16384
max_bytes: 10485760
tail_files: true
output.redis:
enabled: true
hosts: ["192.168.10.243"]
port: 6379
key: filebeat
db: 0
worker: 1
timeout: 5s
max_retries: 3
启动
[root@~ filebeat-6.0.1-linux-x86_64]# ./filebeat -c filebeat2.yml
后台启动
[root@~ filebeat-6.0.1-linux-x86_64]# nohup ./filebeat -c filebeat2.yml &
[root@bogon config]# vim 02-logstash.conf
input {
redis {
host => "192.168.10.243"
port => "6379"
data_type => "list"
key => "filebeat"
type => "redis-input"
}
}
filter {
json {
source => "message"
remove_field => "message"
}
}
output {
elasticsearch {
hosts => ["192.168.10.243:9200"]
index => "logstash-nginx-%{+YYYY.MM.dd}"
document_type => "nginx"
# template => "/usr/local/logstash-2.3.2/etc/elasticsearch-template.json"
workers => 1
}
}
启动前检查 -t 参数
[root@bogon config]# /data/application/logstash-6.0.1/bin/logstash -t -f /data/application/logstash-6.0.1/config/02-logstash.conf
Configuration OK[2018-05-06T14:11:26,442][INFO ][logstash.runner ] Using config.test_and_exit mode. Config Validation Result: OK. Exiting Logstash
启动
[root@bogon config]# /data/application/logstash-6.0.1/bin/logstash-f /data/application/logstash-6.0.1/config/02-logstash.conf
[2018-05-06T14:12:06,694][INFO ][logstash.agent ] Successfully started Logstash API endpoint {:port=>9600}[2018-05-06T14:12:07,317][WARN ][logstash.outputs.elasticsearch] You are using a deprecated config setting "document_type" set in elasticsearch. Deprecated settings will continue to work, but are scheduled for removal from logstash in the future. Document types are being deprecated in Elasticsearch 6.0, and removed entirely in 7.0. You should avoid this feature If you have any questions about this, please visit the #logstash channel on freenode irc. {:name=>"document_type", :plugin=>["192.168.10.243:9200"], index=>"logstash-nginx-%{+YYYY.MM.dd}", document_type=>"nginx", workers=>1, id=>"a8773a7416ee72eecc397e30d8399a5da417b6c3dd2359fc706229ad8186d12b">} ..........
后台启动
[root@bogon config]# nohup /data/application/logstash-6.0.1/bin/logstash -t -f /data/application/logstash-6.0.1/config/02-logstash.conf &
查看redis是否收到filebeat发送key为filebeat的数据
[root@elk config]# /data/application/redis-3.0.5/bin/redis-cli
127.0.0.1:6379> keys *
(empty list or ) #个人为空,数据是被logstash取走了
启动顺序由左到右Elasticsearch-àKibana--àLogstash
启动es
[root@elk config]# su - elk
[elk@elk ~]$ /data/application/elasticsearch-6.0.1/bin/elasticsearch
启动kibana
[root@elk etc]# /data/application/kibana-6.0.1-linux-x86_64/bin/kibana
启动logstah
[root@bogon config]# /dahta/application/logstash-6.0.1/bin/logstash -f /data/application/logstash-6.0.1/config/02-logstash.conf
客户端启动filebeat
[root@rocketmq-nameserver2 filebeat-6.0.1-linux-x86_64]# ./filebeat -c filebeat2.yml