ELKStack简介 – 日志分析管理(都是Java架构 – 需要JDK底层)
什么是ELK?
通俗来讲,ELK是由Elasticsearch、Logstash、Kibana 三个开源软件组成的一个组合体,这三个软件当中,每个软件用于完成不同的功能,ELK又称ELKstack,官网 https://www.elastic.co/ , ELK主要优点有如下几个:
处理方式灵活:elasticsearch是实时全文索引,具有强大的搜索功能(搜索引擎 --> 百度–> lucence引擎功能)
配置相对简单:elasticsearch全部使用JSON接口,logstash使用模块配置(和Jenkins + Ansible --> 模块+插件),kibana的配置文件部分更简单
检索性能高:基于优秀的设计,虽然每次查询都是实时,但是也可以达到百亿级数据的查询秒级响应
集群线性扩展:elasticsearch和logstash都可以灵活线性扩展 – > ES本身没有单点概念 – 集群概念 – ES至少两个 – 没有主从概念 - 从谁身上都能看到所有的数据
前端操作绚丽:kibana的前端设计比较绚丽,而且操作简单
elasticsearch是一个高度可扩展全文搜索和分析引擎,基于Apache Lucene 构建,能对大容量的数据进行接近实时的存储、搜索和分析操作,可以处理大规模日志数据,比如Nginx、Tomcat、系统日志等功能。
数据收集引擎。它支持动态的从各种数据源搜集数据,并对数据进行过滤、分析、丰富、统一格式等操作,然后存储到用户指定的位置;支持普通log、自定义json格式的日志解析。
数据分析和可视化平台。通常与 Elasticsearch 配合使用,对其中数据进行搜索、分析和以统计图表的方式展示。
这里实验所使用系统CentOS 7.5.1804 x86_64,服务器信息如下。并关闭防火墙和selinux,及host绑定等。
IPAddrHostNameMem192.168.194.130node12G192.168.194.131node22Gepel源配置
[root@localhost ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo关闭防火墙
[root@localhost ~]# systemctl stop firewalld [root@localhost ~]# setenforce 0因为elasticsearch服务运行需要java环境,因此两台elasticsearch服务器需要安装java环境。
jdk包
链接: https://pan.baidu.com/s/1YXeyNrqLHAQuj7LVQwxepQ 提取码: njmf 复制这段内容后打开百度网盘手机App,操作更方便哦
两个节点都要安装
[root@localhost ~]# rpm -ivh jdk-8u131-linux-x64_.rpm两台节点都需要安装elasticsearch,使用yum安装会很慢,所以先下载下来传到服务器进行安装,官网下载地址:https://www.elastic.co/cn/downloads/past-releases#elasticsearch
elasticsearch包
链接: https://pan.baidu.com/s/15_W_Kqfu0Gvk14bFunXNGg 提取码: 4a99 复制这段内容后打开百度网盘手机App,操作更方便哦
配置elasticsearch,elk2配置一个相同的节点,通过组播进行通信,如果无法通过组播查询,修改成单播即可。
将下载的elasticsearch包上传到服务器进行安装。 安装elasticsearch
[root@localhost ~]# rpm -ivh elasticsearch-6.6.2.rpm修改配置文件 本机在集群内的节点(node1/node2)名称不一样,其他都一样
[root@localhost ~]# cd /etc/elasticsearch/ [root@localhost elasticsearch]# cp elasticsearch.yml elasticsearch.yml.bak -----备份文件 [root@localhost elasticsearch]# vim elasticsearch.yml [root@localhost elasticsearch]# cat elasticsearch.yml|egrep -v "^#|^$" cluster.name: my_1803a #ELK的集群名称,名称相同即属于是同一个集群 node.name: node-1 #本机在集群内的节点名称 path.data: /var/lib/elasticsearch #数据存放目录 path.logs: /var/log/elasticsearch #日志保存目录 #bootstrap.memory_lock: true #服务启动的时候锁定足够的内存,防止数据写入swap network.host: 192.168.194.130 #监听的IP地址 http.port: 9200 #服务监听的端口 discovery.zen.ping.unicast.hosts: ["192.168.194.130", "192.168.194.131"] #单播配置一台即可修改内存限制,内存锁定需要进行配置需要2g以上内存,否则会导致无法启动elasticsearch。
[root@localhost ~]# vim /usr/lib/systemd/system/elasticsearch.service [root@localhost ~]# cat /usr/lib/systemd/system/elasticsearch.service |sed -n 7,8p [Service] LimitMEMLOCK=infinity #无限制使用内存 [root@localhost ~]# systemctl daemon-reload [root@localhost ~]# vim /etc/elasticsearch/jvm.options [root@localhost ~]# cat /etc/elasticsearch/jvm.options |grep "^-Xm" -Xms2g #最小和最大内存限制 -Xmx2g最小和最大内存限制,为什么最小和最大设置一样大?参考:
https://www.elastic.co/guide/en/elasticsearch/reference/current/heap-size.html
[root@localhost ~]# systemctl start elasticsearch -----启动服务 [root@localhost ~]# netstat -lptnu|grep java -----查看端口状态 tcp6 0 0 192.168.194.130:9200 :::* LISTEN 3616/java tcp6 0 0 192.168.194.130:9300 :::* LISTEN 3616/java通过浏览器访问elasticsearch端口
通过shell命令获取集群状态,这里获取到的是一个json格式的返回值,例如对status进行分析,如果等于green(绿色)就是运行在=正常,等于yellow(黄色)表示副本分片丢失,red(红色)表示主分片丢失。
[root@localhost ~]# curl http://192.168.194.130:9200/_cluster/health?pretty=true { "cluster_name" : "my_1803a", "status" : "green", "timed_out" : false, "number_of_nodes" : 2, "number_of_data_nodes" : 2, "active_primary_shards" : 0, "active_shards" : 0, "relocating_shards" : 0, "initializing_shards" : 0, "unassigned_shards" : 0, "delayed_unassigned_shards" : 0, "number_of_pending_tasks" : 0, "number_of_in_flight_fetch" : 0, "task_max_waiting_in_queue_millis" : 0, "active_shards_percent_as_number" : 100.0 } [root@localhost ~]# curl http://192.168.194.131:9200/_cluster/health?pretty=true { "cluster_name" : "my_1803a", "status" : "green", "timed_out" : false, "number_of_nodes" : 2, "number_of_data_nodes" : 2, "active_primary_shards" : 0, "active_shards" : 0, "relocating_shards" : 0, "initializing_shards" : 0, "unassigned_shards" : 0, "delayed_unassigned_shards" : 0, "number_of_pending_tasks" : 0, "number_of_in_flight_fetch" : 0, "task_max_waiting_in_queue_millis" : 0, "active_shards_percent_as_number" : 100.0 }我们不可能经常通过命令来查看集群的信息,所以就使用到了插件 –head。插件是为了完成不同的功能,官方提供了一些插件但大部分是收费的,另外也有一些开发爱好者提供的插件,可以实现对elasticsearch集群的状态监控与管理配置等功能。
head:主要用来做集群管理的插件
下载地址:https://github.com/mobz/elasticsearch-head
安装npm和git
[root@localhost ~]# yum -y install npm git显示没有npm命令执行
[root@localhost ~]# yum -y install epel-release [root@localhost ~]# yum -y install npm [root@localhost ~]# npm -v 3.10.10安装elasticsearch-head插件
[root@localhost ~]# cd /usr/local/src/ [root@localhost src]# git clone git://github.com/mobz/elasticsearch-head.git [root@localhost src]# cd elasticsearch-head/ [root@localhost elasticsearch-head]# npm install grunt -save --registry=https://registry.npm.taobao.org npm(Node Package Manager)意思是 node 的包管理器,它是随着 NodeJs 安装时一起被安装的;无论是在前端还是在前端开发中都会使用到 npm 包管理器。grunt是一套前端自动化工具,一个基于nodeJs的命令行工具,一般用于: 压缩文件合并文件简单语法检查 [root@localhost elasticsearch-head]# ll node_modules/grunt #确定该目录有生成文件 总用量 20 drwxr-xr-x. 2 root root 19 4月 6 2016 bin -rw-r--r--. 1 root root 7111 4月 6 2016 CHANGELOG drwxr-xr-x. 4 root root 47 10月 20 18:48 lib -rw-r--r--. 1 root root 1592 3月 23 2016 LICENSE drwxr-xr-x. 5 root root 50 10月 20 18:48 node_modules -rw-r--r--. 1 root root 3893 10月 20 18:48 package.json -rw-r--r--. 1 root root 878 2月 12 2016 README.md [root@localhost elasticsearch-head]# npm install --registry=https://registry.npm.taobao.or #执行安装 [root@elk1 elasticsearch-head]# npm run start & #后台启动服务 [root@elk1 ~]# ss -nlt |grep 9100 LISTEN 0 128 *:9100 *:*修改elasticsearch服务配置文件,开启跨域访问支持(跨域是指跨域名的访问),然后重启elasticsearch服务
[root@localhost ~]# vim /etc/elasticsearch/elasticsearch.yml http.cors.enabled: true #最下方添加 http.cors.allow-origin: "*"为了方便管理elasticsearch-head插件,编写一个启动脚本
[root@localhost ~]# vim /usr/bin/elasticsearch-head #!/bin/bash #desc: elasticsearch-head service manager #date: 2019 data="cd /usr/local/src/elasticsearch-head/; nohup npm run start > /dev/null 2>&1 & " function START (){ eval $data && echo -e "elasticsearch-head start\033[32m ok\033[0m" } function STOP (){ ps -ef |grep grunt |grep -v "grep" |awk '{print $2}' |xargs kill -s 9 > /dev/null && echo -e "elasticsearch-head stop\033[32m ok\033[0m" } case "$1" in start) START ;; stop) STOP ;; restart) STOP sleep 3 START ;; *) echo "Usage: elasticsearch-head (start|stop|restart)" ;; esac [root@localhost ~]# chmod +x /usr/bin/elasticsearch-head
Master的职责:
统计各node节点状态信息、集群状态信息统计、索引的创建和删除、索引分配的管理、关闭node节点等 Savle的职责:
同步数据、等待机会成为Master
Logstash 是一个开源的数据收集引擎,可以水平伸缩,而且logstash是整个ELK当中拥有最多插件的一个组件,其可以接收来自不同来源的数据并同意输出到指定的且可以是多个不同目的地。官网下载地址:https://www.elastic.co/cn/downloads/past-releases#logstash
Kibana是一个通过调用elasticsearch服务器进行图形化展示搜索结果的开源项目。官网下载地址:https://www.elastic.co/cn/downloads/past-releases#kibana
说明:通过logstash收集别的日志文件,前提需要logstash用户对被收集的日志文件有读的权限并对写入的文件有写的权限