EKL
开源实时日志分析ELK平台能够完美的解决我们上述的问题,ELK由ElasticSearch、Logstash和Kiabana三个开源工具组成:
1)ElasticSearch是一个基于Lucene的开源分布式搜索服务器。它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是第二流行的企业搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。 
在elasticsearch中,所有节点的数据是均等的。
2)Logstash是一个完全开源的工具,它可以对你的日志进行收集、过滤、分析,支持大量的数据获取方法,并将其存储供以后使用(如搜索)。说到搜索,logstash带有一个web界面,搜索和展示所有日志。一般工作方式为c/s架构,client端安装在需要收集日志的主机上,server端负责将收到的各节点日志进行过滤、修改等操作在一并发往elasticsearch上去。
3)Kibana 是一个基于浏览器页面的Elasticsearch前端展示工具,也是一个开源和免费的工具,Kibana可以为 Logstash 和 ElasticSearch 提供的日志分析友好的 Web 界面,可以帮助您汇总、分析和搜索重要数据日志。
官网下载相关的安装包
https://www.elastic.co/cn/downloads/
elasticsearch-6.7.0.tar.gz
logstash-6.7.0.tar.gz
kibana-6.7.0-linux-x86_64.tar.gz
这里两台主机
master   10.121.63.208   muhe222
master   10.121.63.240   muhe221
两台机器上都需要配置elasticsearch
1、elasticsearch配置
master-slave模式:
master收集到日志后,会把一部分数据碎片到salve上(随机的一部分数据);同时,master和slave又都会各自做副本,并把副本放到对方机器上,这样就保证了数据不会丢失。
如果master宕机了,那么客户端在日志采集配置中将elasticsearch主机指向改为slave,就可以保证ELK日志的正常采集和web展示。
 
elasticsearch相关基础知识
index: es里的index相当于一个数据库。
type: 相当于数据库里的一个表。
id: 唯一,相当于主键。
node:节点是es实例,一台机器可以运行多个实例,但是同一台机器上的实例在配置文件中要确保http和tcp端口不同(下面有讲)。
cluster:代表一个集群,集群中有多个节点,其中有一个会被选为主节点,这个主节点是可以通过选举产生的,主从节点是对于集群内部来说的。
shards:代表索引分片,es可以把一个完整的索引分成多个分片,这样的好处是可以把一个大的索引拆分成多个,分布到不同的节点上,构成分布式搜索。分片的数量只能在索引创建前指定,并且索引创建后不能更改。
replicas:代表索引副本,es可以设置多个索引的副本,副本的作用一是提高系统的容错性,当个某个节点某个分片损坏或丢失时可以从副本中恢复。二是提高es的查询效率,es会自动对搜索请求进行负载均衡。 
muhe222@muhe222:~/soft/elasticsearch-6.7.0$ tar -zxvf elasticsearch-6.7.0.tar.gz muhe222@muhe222:~/soft/elasticsearch-6.7.0/config$ vi elasticsearch.yml cluster.name: Es6.7_Cluster node.name: node-muhe222 network.host: 10.121.63.208 http.port: 9200 discovery.zen.ping.unicast.hosts: ["10.121.63.208", "10.121.63.240"] discovery.zen.minimum_master_nodes: 2 bootstrap.memory_lock: false bootstrap.system_call_filter: false #elasticsearch配置允许跨域访问,这样head插件可以访问es http.cors.enabled: true http.cors.allow-origin: "*"
//启动ES
muhe222@muhe222:~/soft/elasticsearch-6.7.0/bin$ ./elasticsearch
......
ERROR: [2] bootstrap checks failed
[1]: max number of threads [2048] for user [muhe222] is too low, increase to at least [4096]
[2]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
错误解决:
max number of threads
$ vim /etc/security/limits.conf
* soft nproc 65536
* hard nproc 65536
* soft nofile 65536
* hard nofile 65536
$ vim /etc/security/limits.d/90-nproc.conf 
soft nproc 2048
vm.max_map_count
$ vi /etc/sysctl.conf
vm.max_map_count=262144
sysctl -p
system call filters
$ vim config/elasticsearch.yml
bootstrap.memory_lock: false
bootstrap.system_call_filter: false
#elasticsearch配置允许跨域访问,这样head插件可以访问es
http.cors.enabled: true
http.cors.allow-origin: "*"
./elasticsearch
在另外一台机器上也类似的配置并启动 ./elasticsearch
访问 http://10.121.63.208:9200/
{ "name" : "node-muhe222", "cluster_name" : "Es6.7_Cluster", "cluster_uuid" : "jDBxMf_uQbWI7fsv45flFA", "version" : { "number" : "6.7.0", "build_flavor" : "default", "build_type" : "tar", "build_hash" : "8453f77", "build_date" : "2019-03-21T15:32:29.844721Z", "build_snapshot" : false, "lucene_version" : "7.7.0", "minimum_wire_compatibility_version" : "5.6.0", "minimum_index_compatibility_version" : "5.0.0" }, "tagline" : "You Know, for Search" }
或者其它方式访问
muhe222@muhe222:~/soft/elasticsearch-6.7.0/config$ curl -X GET http://muhe222:9200 { "name" : "node-muhe222", "cluster_name" : "Es6.7_Cluster", "cluster_uuid" : "jDBxMf_uQbWI7fsv45flFA", "version" : { "number" : "6.7.0", "build_flavor" : "default", "build_type" : "tar", "build_hash" : "8453f77", "build_date" : "2019-03-21T15:32:29.844721Z", "build_snapshot" : false, "lucene_version" : "7.7.0", "minimum_wire_compatibility_version" : "5.6.0", "minimum_index_compatibility_version" : "5.0.0" }, "tagline" : "You Know, for Search" }
按照相同的步骤启动各个节点,控制台显示启动成功之后,访问http://10.121.63.208:9200/_cat/nodes,若配置的节点都在,则集群部署成功
10.121.63.208 30 70 2 0.05 0.14 0.07 mdi - node-muhe222
10.121.63.240 31 90 2 0.16 0.26 0.19 mdi * node-muhe221
elasticsearch-head
Elasticsearch的插件,默认都是安装在plugins文件夹中的,head插件原来也是需要与Elasticsearch合为一体的。在5.5.x版本以后,head插件就作为一个独立的插件独立了出来。
下载elasticsearch-head-master.zip  https://github.com/mobz/elasticsearch-head
由于head插件本质上还是一个nodejs的工程,因此需要安装Node.js环境。具体步骤请参考https://www.cnblogs.com/muhe221/articles/10626011.html
需要注意的是elasticsearch-head-master.zip解压后的内容不能放在elasticsearch的plugins目录下(另外一说:得删除plugins目录下的elasticsearch-head-master.zip就不会报错),否则会报以下错误
java.lang.IllegalArgumentException: property [elasticsearch.version] is missing for plugin [head]”
安装NPM后需要安装grunt
#npm 配置代理 muhe222@muhe222:~/soft$ npm config set registry http://rnd-mirrors.huawei.com/npm-registry/ #这里安装 grunt-cli muhe222@muhe222:~/soft$ npm install -g grunt-cli #Grunt和 Grunt 插件是通过 npm 安装并管理的,npm是 Node.js 的包管理器。 muhe222@muhe222:~/soft$ grunt -V grunt-cli v1.3.2
修改Gruntfile.js 增加一行"hostname: '0.0.0.0',"
muhe222@muhe222:~/soft/elasticsearch-head-master$ vi Gruntfile.js connect: { server: { options: { port: 9100, base: '.', keepalive: true, hostname: '*' //新增 } } }
muhe222@muhe222:~/soft/elasticsearch-head-master$ npm install // cd 到该目录执行
npm install执行可能会有如下错误信息:
npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! phantomjs-prebuilt@2.1.16 install: `node install.js` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the phantomjs-prebuilt@2.1.16 install script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above. npm ERR! A complete log of this run can be found in: npm ERR! /home/muhe222/.npm/_logs/2019-03-30T06_51_24_829Z-debug.log
#通过以下命令解决
muhe222@muhe222:~/soft/elasticsearch-head-master$ npm install phantomjs-prebuilt@2.1.16 --ignore-scripts # 另外npm install时有这个警告,暂时不用理会它 npm WARN elasticsearch-head@0.0.0 license should be a valid SPDX license expression npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.7 (node_modules/chokidar/node_modules/fsevents): npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.7: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
错误信息:[node-muhe221] failed to send join request to master [{node-muhe222}{kzNrtBjwQb24VmLxTEpeYQ}{noXPEHk6Q9STTF22QDRRVA}{10.121.63.208}{10.121.63.208:9300}
需要将elasticsearch-6.7.0/data下的文件清空
muhe222@muhe222:~/soft/elasticsearch-head-master$ grunt server    #  最后启动nodejs
Running "connect:server" (connect) task
Waiting forever...
Started connect web server on http://localhost:9100
使用http://localhost:9100访问出现正确界面并显示"集群健康值: green (0 of 0)"则说明elasticsearch-head配置正确
安装Ik中文分词器elasticsearch-analysis-ik
Elasticsearch中自带了一种分词器standard,对英文的分词搜索支持很完善,但是对于中文的分词不太完美。
对于中文的分词搜索,Elasticsearch自带的分词器会将中文分成单个字来进行搜索,无法达到分词的效果,因此我们需要一个更加完美的分词器IK。
https://github.com/medcl/elasticsearch-analysis-ik/releases/elasticsearch-analysis-ik-6.7.0.zip
解压到~/soft/elasticsearch-6.7.0/plugins/ik并重新启动elasticsearch
用浏览器打开 http://localhost:9100
elasticsearch:
URL:http://10.121.63.208:9200/
elasticsearch-head:
http://10.121.63.208:9100/
查询:http://10.121.63.208:9200/ x1/x2/x3 #选择post { "name": "cm", "password":"123456" } 提交申请 结果: { "_index": "x1", "_type": "x2", "_id": "x3", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 0, "_primary_term": 1 }
修改内容为 {} GET 提交申请 结果: { "_index": "x1", "_type": "x2", "_id": "x3", "_version": 1, "_seq_no": 0, "_primary_term": 1, "found": true, "_source": { "name": "cm", "password": "123456" } }
或者使用curl命令:
muhe222@muhe222:~/soft$ curl -XPOST http://10.121.63.208:9200/a/b/c -H 'Content-Type:application/json' -d' { "id" : 3, "username" : "caoming", "description" : "for test" }' 结果: { "_index":"a", "_type":"b", "_id":"c", "_version":1, "result":"created", "_shards": {"total":2, "successful":1, "failed":0}, "_seq_no":0, "_primary_term":1 }
查询结果
muhe222@muhe222:~/soft/elasticsearch-6.7.0/plugins/ik$ curl -XGET "http://10.121.63.208:9200/a/b/c" { "_index": "a", "_type": "b", "_id": "c", "_version": 1, "_seq_no": 0, "_primary_term": 1, "found": true, "_source": { "id": 3, "username": "caoming", "description": "for test" } }
安装Kibana
只需要在master上安装即可
muhe222@muhe222:~/soft/kibana-6.7.0/config$ vi kibana.yml
#配置本机ip
server.host: "192.168.252.129"
#配置es集群url
在启动elasticsearch和elastic-haed后启动kibana muhe222@muhe222:~/soft/kibana-6.7.0/bin$ ./kibana
Elasticsearch中的监控设置xpack.monitoring.collection.enabled(可以不设置,默认为false)
在[6.3.0]版本上增加的,设置为true以启用监控数据收集。当这个设置是false(默认值)时,Elasticsearch的监控数据没有收集并且所有的其他来源的监控数据像是Kibana,Beats,Logstash都被忽略了。
elasticsearch.url: "http://192.168.252.129:9200" 如果能访问则说明配置成功
“Dev Tools”=>输入“GET _cat/health?v” 点击执行可以查看相关状态
epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent 1554109641 09:07:21 Es6.7_Cluster green 2 2 38 19 0 0 0 0 - 100.0%
                    
                
                
            
        
浙公网安备 33010602011771号