ELK 套件搭建
http://blog.****.net/a464057216/article/category/6135397 (ELK安装配置系列教程)
http://www.cnblogs.com/onetwo/p/6059231.html (ELK日志分析平台搭建全过程)
http://blog.chinaunix.net/uid-23916356-id-5764256.html (ELK5.4.0部署参考文档 )
http://zhengmingjing.blog.51cto.com/1587142/d-6 (ELK+Filebeat+Nginx集中式日志解决方案)
http://blog.chinaunix.net/uid-26168435-id-5759443.html (CentOS7部署ELK5.2 (yum安装))
http://www.cnblogs.com/NextNight/p/6826621.html (ELK5.3.1从单机到分布式的安装与使用)
https://kibana.logstash.es/content/ (ELK stack 中文指南)
http://blog.****.net/q15150676766/article/category/7042899 (ELK学习笔记)
http://blog.****.net/napoay/article/category/6507166 (Elasticsearch 教程)
http://www.cnblogs.com/ljhdo/category/754111.html (Elasticsearch 教程入门8篇)
http://www.cnblogs.com/zlslch/category/972313.html (ELK集群搭建)
http://www.cnblogs.com/zlslch/category/950999.html (ELK概念学习系列)
http://www.54tianzhisheng.cn/2017/10/15/ElasticSearch-cluster-health-metrics/ (ElasticSearch 集群监控 )
https://elasticsearch.cn/topic/Elastic%E6%97%A5%E6%8A%A5 (Elastic中午社区日报)
ELK是三个开源工具组成,简单解释如下:
Elasticsearch是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等。
Logstash是一个完全开源的工具,它可以对你的日志进行收集、过滤,并将其存储供以后使用(如,搜索)。
Kibana 也是一个开源和免费的工具,它可以为 Logstash 和 ElasticSearch 提供的日志分析友好的 Web 界面,可以帮助您汇总、分析和搜索重要数据日志。
场景分析:
日志主要包括系统日志、应用程序日志和安全日志等等。运维人员和开发人员可以通过日志了解服务器软硬件信息、检查配置过程中的错误及错误发生的原因。经常分析日志可以了解服务器的负荷,性能安全性,从而及时采取措施纠正错误。
通常,日志被分散的储存不同的设备上。如果你管理数十上百台服务器,你还在使用依次登录每台机器的传统方法查阅日志。这样是不是感觉很繁琐和效率低下。当务之急我们使用集中化的日志管理,例如:开源的syslog,将所有服务器上的日志收集汇总。
集中化管理日志后,日志的统计和检索又成为一件比较麻烦的事情,一般我们使用grep、awk和wc等Linux命令能实现检索和统计,但是对于要求更高的查询、排序和统计等要求和庞大的机器数量依然使用这样的方法难免有点力不从心。
这里采用开源实时日志分析ELK平台能够完美的解决我们上述的问题,当然也还有别的平台或者工具可以使用,这里只讨论ELK,官方网站:https://www.elastic.co
截止目前elk官网提供最新稳定版本为5.5.0
需要实现效果:
1、系统messages日志通过本地beat方式(数据不做任何处理)导入到elasticsearch中,最后通过kibana可以查询。
2、Apache访问日志通过远程beat方式(数据经过处理)导入到elasticsearch中,通过kibana可以搜索日志中任何一个字段展示,也可以组合模糊查询索。 也就是说将apache日志按json格式存储到elasticsearch中。
主要注意事项:
3、关闭防火墙和selinux。
4、elk为了统一都使用tar包安装所有软件。用yum安装特别是logstash会遇到很多坑。
5、搭建过程不难,难的是各个项目互相调试,难的是elk的高级使用方法。
安装步骤:
1、Elk三个节点安装jdk,jdk可以去oracle官方网站下载,版本号可以和我的不一样
ELK 5.0 必须用jdk1.8 以上版本。jdk安装看
http://yjph83.iteye.com/blog/2097789
2、安装elasticsearch节点
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.5.0.tar.gz
tar zxvf elasticsearch-5.5.0.tar.gz
mv elasticsearch-5.5.0 /usr/local/elasticsearch
cd /usr/local/elasticsearch/config
备份elasticsearch默认配置文件,以防修改出错
cp elasticsearch.yml elasticsearch.yml.default
官网上的很多种安装方法说明:官网安装说明
elasticsearch.yml 的配置说明:ElasticSearch集群配置详解
编辑后如下:
cluster.name: mych-es-cluster
node.name: node-1
path.data: /usr/local/elasticsearch/data #此目录必须大空间
path.logs: /usr/local/elasticsearch/log
network.host: 10.129.4.46
http.port: 9200
#--下边2行一般可以不需要,是用于2台es以上的集群才启用
discovery.zen.ping.unicast.hosts: ["10.129.4.46"] #用,分开的“” 所有节点IP
discovery.zen.minimum_master_nodes: 1 #集群可做master的最小节点数
## 以下两项设置es5.x版本的head插件可以访问es
http.cors.enabled: true
http.cors.allow-origin: "*"
添加elasticsearch用户,tar包启动必须使用普通用户运
[[email protected] config]# useradd es #(elasticsearch 的简写)
[[email protected] config]# chown -R es:es /usr/local/elasticsearch
用户名可以随便取:我也可以设置为es
修改vim /etc/sysctl.conf,此处是修改elasticsearch 的虚拟内存,添加如下内容:
vm.max_map_count = 655360
并执行 sysctl -p /etc/sysctl.conf
修改打开vim /etc/security/limits.conf文件,修改打开文件句柄数
* soft nproc 65536
* hard nproc 65536
* soft nofile 65536
* hard nofile 65536
此时,我们就基本配置完成,则需要切换到es用户下,启动elasticsearch;
su - es
cd /usr/local/elasticsearch
bin/elasticsearch
注意:第一次启动需要一些时间,因为需要做一些初始化动作,如果没启动成功请查系elasticsearch的相关日志解决。注意上面只是前台启动调试,在后台需要加&,需要重新启动。
或者后台启动用如下命令:
[[email protected] ~]$ /usr/local/elasticsearch/bin/elasticsearch -d
-d 就表示后台运行;也可以直接 将-d 换成 & ;
停止用 jps 查看elasticsearch 进程号,用kill -9 进程号 停止
我遇到几个问题:
ERROR: [2] bootstrap checks failed
[1]: max number of threads [1024] for user [es] is too low, increase to at least [2048]
[2]: system call filters failed to install; check the logs and fix your configuration or disable system call filters at your own risk
解决方案:
针对【1】 只需要如下执行:
vim /etc/security/limits.d/90-nproc.conf
* soft nproc 2048 # 此行1024修改为2048
针对【2】 则
在elasticsearch.yml中在Memory下面配置:
bootstrap.memory_lock: false
bootstrap.system_call_filter: false
常见问题:ElasticSearch 5.x 安装部署常见错误或问题
查看端口是否开启
[[email protected] config]# netstat -an | more
tcp 0 0 ::ffff:10.129.4.46:9200 :::* LISTEN
tcp 0 0 ::ffff:10.129.4.46:9300 :::* LISTEN
可以查看到9200 和 9300 2个端口已经启用。即表示elasticsearch安装成功。
通过访问本机测试:
[[email protected] config]# curl http://10.129.4.46:9200
{
"name" : "node-1",
"cluster_name" : "mych-es-cluster",
"cluster_uuid" : "W4TPOu5hT8q4WXHpiE6ddQ",
"version" : {
"number" : "5.5.0",
"build_hash" : "260387d",
"build_date" : "2017-06-30T23:16:05.735Z",
"build_snapshot" : false,
"lucene_version" : "6.6.0"
},
"tagline" : "You Know, for Search"
}
[[email protected] config]#
elasticsearch基本的API格式 curl -X请求方法(大写) IP地址 :port/ES中定义的Index/ES中定义的type/document的ID。注意:elasticsearch的增加是用的PUT方法,更新是POST方法,这和一般的REST规范有所不同。
3、安装logstash节点
直接将logstash-5.5.0.tar.gz 解压并移动到/usr/local/目录下:
[[email protected] home]# tar -zxvf logstash-5.5.0.tar.gz
[[email protected] home]# mv logstash-5.5.0 /usr/local/logstash/
Logstash流水线有两个必须要素:input和output,filter要素是可选的。input插件读入数据,filter插件按照配置调整数据格式,output插件将数据写到目的地。
下面通过最简单的Logstash管道测试一下logstash是否安装成功:
[[email protected] ~]# cd /usr/local/logstash/
[[email protected] logstash]# ./bin/logstash -e "input{stdin{}} output{stdout{}}"
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
Sending Logstash's logs to /usr/local/logstash/logs which is now configured via log4j2.properties
[2017-07-18T09:39:36,551][INFO ][logstash.setting.writabledirectory] Creating directory {:setting=>"path.queue", :path=>"/usr/local/logstash/data/queue"}
[2017-07-18T09:39:36,556][INFO ][logstash.setting.writabledirectory] Creating directory {:setting=>"path.dead_letter_queue", :path=>"/usr/local/logstash/data/dead_letter_queue"}
[2017-07-18T09:39:36,578][INFO ][logstash.agent ] No persistent UUID file found. Generating new UUID {:uuid=>"29cf823c-a6ae-4af3-a241-9b806b381010", :path=>"/usr/local/logstash/data/uuid"}
[2017-07-18T09:39:36,715][INFO ][logstash.pipeline ] Starting pipeline {"id"=>"main", "pipeline.workers"=>8, "pipeline.batch.size"=>125, "pipeline.batch.delay"=>5, "pipeline.max_inflight"=>1000}
[2017-07-18T09:39:36,753][INFO ][logstash.pipeline ] Pipeline main started
The stdin plugin is now waiting for input:
[2017-07-18T09:39:36,839][INFO ][logstash.agent ] Successfully started Logstash API endpoint {:port=>9600}
此时表示安装成功,但是log4j2 的日志配置文件没有找到,我们直接输入:
hello word!
2017-07-18T01:39:49.222Z localhost.localdomain hello word!
输入Ctrl+D则会退出Logstash。-e参数是的Logstash可以直接在命令行模式配置Logstash的管道配置,使用-f参数可以指定Logstash配置文件路径,使用-h参数可以查看更多的命令行帮助信息。
实际使用中Logstash一般配合Elasticsearch和Filebeat使用
Logstash使用beats input plugin接收来自beat的数据(适配所有遵循beats框架的beat),然后使用Elasticsearch output plugin向Elasticsearch发送数据,比如在/usr/local/logstash/config目录下添加配置文件beats-input.conf:
input {
beats { port => 5044 type => "logs" } }再添加如下配置文件output.conf:
output { elasticsearch { hosts => "10.129.4.46:9200" index => "%{[@metadata][beat]}-%{+YYYY.MM.dd}" document_type => "%{[@metadata][type]}" } }
可以将beats-input.conf和output.conf 的内容拷贝到一个文件 logstash_test.conf中,则我们可以用如下命令启动:
[[email protected] logstash]# bin/logstash -f config/logstash_test.conf &
[2017-07-18T14:27:39,024][INFO ][logstash.pipeline ] Starting pipeline {"id"=>"main", "pipeline.workers"=>8, "pipeline.batch.size"=>125, "pipeline.batch.delay"=>5, "pipeline.max_inflight"=>1000}
[2017-07-18T14:27:40,657][INFO ][logstash.inputs.beats ] Beats inputs: Starting input listener {:address=>"0.0.0.0:5044"}
[2017-07-18T14:27:40,738][INFO ][logstash.pipeline ] Pipeline main started
[2017-07-18T14:27:41,038][INFO ][logstash.agent ] Successfully started Logstash API endpoint {:port=>9600}
即表示启动成功!后台运行程序则:nohup bin/logstash -f config/logstash_test.conf &
当然,需要在Beat的配置文件中指定将收集到的数据发送给Logstash,下面以使用Filebeat收集日志为例讲解Logstash与Filebeat配合的方法。
4、安装Filebeat
[[email protected] home]# tar -zxvf filebeat-5.5.0-linux-x86_64.tar.gz
[[email protected] home]# mv filebeat-5.5.0-linux-x86_64 /usr/local/filebeat
[[email protected] logstash]# cd /usr/local/filebeat
[[email protected] filebeat]# cp filebeat.yml filebeat.yml.default
filebeat安装目录下一般都有 filebeat.xxx.template.json这个文件可以导入到es中作为es的模板;
curl -XPUT http://10.129.11.86:9200/_template/filebeat?pretty [email protected]/usr/local/filebeat/filebeat.template.json
注意: 因为es一旦生产索引后,只能动态增加字段类型,而不能修改原索引的类型;故要修改新索引的类型等属性,必须修改 该模板并重新覆盖。
编辑 filebeat.yml
- input_type: log
# Paths that should be crawled and fetched. Glob based paths.
paths:
- /var/log/club.changhong.com.log #这里用club.changhong.com.log作为我们的日志输入项
然后配置Filebeat向Logstash输出,需要在output字段将向Elasticsearch输出的配置注释掉,然后将向Logstash的输出的配置去掉注释。比如:
output.logstash:
# The Logstash hosts
hosts: ["localhost:5044"]
我们启动filebeat:
[[email protected] filebeat]# ./filebeat &
为了不让一些执行信息输出到前台(控制台),我们还会加上刚才提到的>/dev/null 2>&1命令来丢弃所有的输出:
nohup ./filebeat -e -c filebeat.yml >/dev/null 2>&1 &
-c:配置文件位置
-e:关闭日志输出
/dev/null 2>&1 不清楚请看这里
查看logs目录下的日志:
再查看进程:
ps -ef|grep filebeat
root 12511 16196 14 15:08 pts/4 00:00:01 ./filebeat
es 12643 900 0 15:08 pts/5 00:00:00 grep filebeat
表示filebeat启动成功!
5、安装kibana
同上边一样,解压kibana的安装包;
移动到指定目录下:
备份kibana.yml
[[email protected] config]# tar -zxvf kibana-5.5.0-linux-x86_64.tar.gz
[[email protected] config]# mv kibana-5.5.0-linux-x86_64 /opt/kibana
[[email protected] config]# cd /opt/kibana
[[email protected] config]# cp kibana.yml ./kibana.yml.default
vim config/kibana.yml 编辑填写如下内容
server.port: 5601
server.host: "10.129.4.46"
elasticsearch.url: "http://10.129.4.46:9200"
保存退出, 再用如下命令后台启动:
[[email protected] kibana]# nohup ./bin/kibana &
[2] 704
[[email protected] kibana]# log [09:35:48.269] [info][status][plugin:[email protected]] Status changed from uninitialized to green - Ready
log [09:35:48.380] [info][status][plugin:[email protected]] Status changed from uninitialized to yellow - Waiting for Elasticsearch
log [09:35:48.433] [info][status][plugin:[email protected]] Status changed from uninitialized to green - Ready
log [09:35:48.508] [info][status][plugin:[email protected]] Status changed from uninitialized to green - Ready
log [09:35:48.789] [info][status][plugin:[email protected]] Status changed from uninitialized to green - Ready
log [09:35:48.795] [info][listening] Server running at http://10.129.4.46:5601
log [09:35:48.798] [info][status][ui settings] Status changed from uninitialized to yellow - Elasticsearch plugin is yellow
log [09:35:48.839] [info][status][plugin:[email protected]] Status changed from yellow to green - Kibana index ready
log [09:35:48.841] [info][status][ui settings] Status changed from yellow to green - Ready
地址栏输入:ip:端口 进行验证。OK
可以通过如下命令查查:
[[email protected] ~]# lsof -i :5601
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 5036 root 12u IPv4 35586466 0t0 TCP 10.129.4.46:esmagent (LISTEN)
如果没安装lsof 命令,则 yum install lsof 即可;
停止则 kill -9 PID # PID 就是lsof 命令下的PID
以上就表示基本跑通了,但是我们实际应用还有许多的调优等;
1、将logstash_test.conf 文件汇总添加
input {
beats {
port => 5044
type => "logs"
}
}
filter {
grok {
match => {"message" => "%{COMBINEDAPACHELOG} %{QS:x_forwarded_for}" }
}
date {
match => [ "timestamp" , "dd/MMM/YYYY:HH:mm:ss Z" ]
}
geoip {
source => "clientip"
}
}
output {
elasticsearch {
hosts => "10.129.4.46:9200"
index => "logstash-custom-%{+YYYY.MM.dd}"
document_type => "%{type}"
flush_size => 20000
idle_flush_time => 10
sniffing => true
template_overwrite => true
}
}
但是,我启动kibana 时报错:
最后在 elasticsearch.yml 配置文件中添加以下2行配置重启搞定;
http.cors.enabled: true
http.cors.allow-origin: "*"
es集群配置一般有如下属性选择:
network.host: <hostname> , _local_ cluster.name: <clustername> node.name: <nodename> node.master: true node.data: true discovery.zen.ping.multicast.enabled: false discovery.zen.ping.unicast.hosts: [ <host1>, <host2>, <host3>] discovery.zen.minimum_master_nodes: 2 http.cors.enabled: true http.cors.allow-origin: "*" index.number_of_shards: 3 index.number_of_replicas: 1 index.translog.durability: async index.translog.flush_threshold_size: 1g index.refresh_interval: 30s bootstrap.mlockall: true threadpool.bulk.type: fixed threadpool.bulk.queue_size: 5000