企业运维之 ELK日志分析平台(Kibana)

Posted 123坤

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了企业运维之 ELK日志分析平台(Kibana)相关的知识,希望对你有一定的参考价值。

ELK日志分析平台--kibana数据可视化介绍与配置

1. Kibana 简介

kibana 是一款开源的数据分析和可视化平台,它是 Elastic Stack 成员之一,设计用于和 Elasticsearch 协作。可以使用 Kibana 对 Elasticsearch 索引中的数据进行搜索、查看、交互操作。可以很方便的利用图表、表格及地图对数据进行多元化的分析和呈现。
Kibana 核心产品搭载了一批经典功能:柱状图、线状图、饼图、旭日图,等等。并且可以将地理数据融入任何地图,精选的时序性 UI,对 Elasticsearch 中的数据执行高级时间序列分析,利用 Graph 功能分析数据间的关系,Kibana 开发工具为开发人员提供了多种强大方法来帮助其与 Elastic Stack 进行交互。

下载官网 https://elasticsearch.cn/download/
Kibana 指南https://www.elastic.co/guide/en/kibana/7.6/index.html

将数据导入kibana
在 Kibana 中处理数据,可以:
1、使用文件数据可视化工具上传 CSV、JSON 或日志文件。
2、使用 GeoJSON 上传功能上传地理空间数据。
3、通过设置 Beats 模块索引日志、指标、事件或应用程序数据。
4、将 Kibana 与现有的 Elasticsearch 索引连接起来。

Kibana 使用索引模式来告诉它要探索哪些 Elasticsearch 索引。如果添加示例数据或运行内置教程,将免费获得一个索引模式。如果加载自己的数据,则可以在 Management 中创建索引模式。

2. Kibana 安装与配置

将下载好的软件包进行安装,并修改配置文件信息,并开启kibana。
此处的 ES 集群有三台主机,此处的 Kibana 和 Logstash 在一台主机上公存。

[root@server4 ~]# rpm -ivh kibana-7.6.1-x86_64.rpm 
[root@server4 ~]# vim /etc/kibana/kibana.yml 
  7 server.host: "172.25.25.24"	#服务监听地址
 25 server.name: "server4"
 28 elasticsearch.hosts: ["http://172.25.25.21:9200"]	#ES集群地址
 37 kibana.index: ".kibana"		#kibana在ES中创建的索引
115 i18n.locale: "zh -CN"		#默认支持中文
[root@server4 ~]# systemctl start kibana
[root@server4 ~]# netstat -antlp | grep :5601	#查看服务端口
tcp        0      0 172.25.25.24:5601       0.0.0.0:*               LISTEN      14523/node 

当正常开启之后便可连接图形界面做操作;网页访问http://172.25.25.24:5601

此处选择官方模板:

完成之后便会发现ES端相应的索引已经存在。

先创建索引:查看之前的存在 es 中的 apache 的日志数据,按下图步骤操作即可

创建可视化:

完成之后可以将其保存。

将可视化图表放在dashbload中:

再次创建可视化,选择垂直图;来做对于IP 的访问;


可以看到条形图,被不同的主机访问过的次数:


保存数据

将垂直图也加入仪表盘,此时可以在仪表盘看到添加的数据。此处要没有对垂直图进行保存便不能在仪表盘显示。

启用xpack安全验证

在配置监控时由于目前部署的 ELK 端口都是开放的,任何人都能访问,没有任何安全保障,容易受到攻击,会提示有错误需要启用 xpack 安全验证来保护 ELK,这样es都是内部运行的,不暴露外网,更安全。

查看es集群内部状态:



由于没有开启认证,此处一直报错,接下来设置认证。
参考连接:https://www.elastic.co/guide/en/elasticsearch/reference/7.6/configuring-tls.html#node-certificates

[root@server1 elasticsearch]# pwd
/usr/share/elasticsearch
[root@server1 elasticsearch]# bin/elasticsearch-certutil ca
#使用elasticsearch-certutil工具生成ca,此处设置密码为空,方便使用
[root@server1 elasticsearch]# bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12
#使用ca创建证书elastic-stack-ca.p12,设置密码为空,直接回车
[root@server1 elasticsearch]# cp elastic-certificates.p12 /etc/elasticsearch
#把证书复制到elasticsearch的配置目录
[root@server1 elasticsearch]# chown elasticsearch /etc/elasticsearch/elastic-certificates.p12
#文件拥有者改为 elasticsearch,以便elasticsearch可以使用证书,或者直接改权限大小也行

完成之后,直接将该文件复制到集群中的其他结点上。

[root@server1 elasticsearch]# scp /etc/elasticsearch/elastic-certificates.p12 server2:/etc/elasticsearch/
[root@server1 elasticsearch]# scp /etc/elasticsearch/elastic-certificates.p12 server3:/etc/elasticsearch/

添加内部信息采集参数到ES集群,每一个ES集群节点都需要操作;开启xpack安全验证指定认证的文件;重新启动 elasticsearch 服务。

[root@server1 elasticsearch]# pwd
/etc/elasticsearch
[root@server1 elasticsearch]# vim elasticsearch.yml
#添加下面的内容
 97 xpack.security.enabled: true
 98 xpack.security.transport.ssl.enabled: true
 99 xpack.security.transport.ssl.verification_mode: certificate
100 xpack.security.transport.ssl.keystore.path: /etc/elasticsearch/elastic-certificates.p12
101 xpack.security.transport.ssl.truststore.path: /etc/elasticsearch/elastic-certificates.p12
[root@server1 elasticsearch]# systemctl restart elasticsearch.service 

依次类推为其他节点也完成该操作:

[root@server2 elasticsearch]# pwd
/etc/elasticsearch
[root@server2 elasticsearch]# vim elasticsearch.yml
[root@server2 elasticsearch]# chown elasticsearch elastic-certificates.p12
[root@server2 elasticsearch]# systemctl restart elasticsearch.service 

[root@server3 elasticsearch]# pwd
/etc/elasticsearch
[root@server3 elasticsearch]# vim elasticsearch.yml
[root@server3 elasticsearch]# chown elasticsearch elastic-certificates.p12
[root@server3 elasticsearch]# systemctl restart elasticsearch.service 

ES集群重启正常后,设置用户密码:

[root@server1 bin]# pwd 
/usr/share/elasticsearch/bin
[root@server1 bin]# ./elasticsearch-setup-passwords --help
[root@server1 bin]# ./elasticsearch-setup-passwords interactive
Initiating the setup of passwords for reserved users elastic,apm_system,kibana,logstash_system,beats_system,remote_monitoring_user.
You will be prompted to enter passwords as the process progresses.
Please confirm that you would like to continue [y/N]y

Enter password for [elastic]: 
Reenter password for [elastic]: 
Enter password for [apm_system]: 
Reenter password for [apm_system]: 
Enter password for [kibana]: 
Reenter password for [kibana]: 
Enter password for [logstash_system]: 
Reenter password for [logstash_system]: 
Enter password for [beats_system]: 
Reenter password for [beats_system]: 
Enter password for [remote_monitoring_user]: 
Reenter password for [remote_monitoring_user]: 
Changed password for user [apm_system]
Changed password for user [kibana]
Changed password for user [logstash_system]
Changed password for user [beats_system]
Changed password for user [remote_monitoring_user]
Changed password for user [elastic]

这时kibana连接ES 时就需要密码,
登录进去会发现 kibana 没有正常运行,需要设置 kibana 连接 ES 的用户密码:
elasticsearch-head的设置

[root@server4 kibana]# pwd
/etc/kibana
[root@server4 kibana]# vim kibana.yml 
 46 elasticsearch.username: "kibana"
 47 elasticsearch.password: "westos"
[root@server4 kibana]# systemctl restart kibana

重启之后再次连接,登陆密码账户会获得所有权限;此时再次进入监控设置时便没有问题;

此时 elasticsearch 连接也出现了问题;

设置 Logstash 连接 ES 用户密码:
head访问: 用 docker 方式访问时没有问题,直接登陆用户和密码即可;head 需要修改文件;

[root@server1 elasticsearch]# vim  elasticsearch.yml 
 67 http.cors.allow-headers: Authorization,X-Requested-With,Content-Length,Content-Type

[root@server1 elasticsearch]# systemctl restart elasticsearch.service 

重启完成之后,此时在访问时需要添加用户和密码;http://172.25.25.24:9100/?auth_user=elastic&auth_password=westos


设置 Logstash 连接 ES 用户密码:

[root@server4 conf.d]# cat apache.conf
input 
	file 
		path => "/var/log/httpd/access_log"
		start_position => "beginning"
	

filter 
	grok 
	match =>  "message" => "%HTTPD_COMBINEDLOG" 
	

output 
	stdout 
	elasticsearch 
	        hosts => "172.25.25.21:9200"
                index => "apachelog-%+YYYY.MM.dd"
		user => "elastic"	#加入用户名和密码
		password => "westos"
	

[root@server4 conf.d]# /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/apache.conf

到 kibana 端查看数据上传成功,证明logstash采集成功.

此时再次进入设置监控: metricbeat集群信息采集插件

logstash是负责采集数据的,流向为 logstash-> ES->kibana,但是它的资源消耗很大,同时有些定制的采集指标 logstash 无法满足。所以使用轻量级的采集工具 metricbeat 来实现采集,流向为metricbeat-> ES->kibana,可以获取系统级的 CPU 使用率、内存、文件系统、磁盘 IO 和网络 IO 统计数据,还可针对系统上的每个进程获得与 top 命令类似的统计数据。同时 Metricbeat 提供多种内部模块,用于从服务中收集指标,例如 Apache、nginx、MongoDB、mysql、PostgreSQL、Prometheus、Redis 等等。


在所有ES集群节点均需要安装 metricbeat 的操作

[root@server1 ~]# ls
elasticsearch-7.6.1-x86_64.rpm  metricbeat-7.6.1-x86_64.rpm
[root@server1 ~]# rpm -ivh metricbeat-7.6.1-x86_64.rpm 
[root@server1 modules.d]# pwd
/etc/metricbeat/modules.d
[root@server1 modules.d]# ls
activemq.yml.disabled             etcd.yml.disabled            nats.yml.disabled
aerospike.yml.disabled            golang.yml.disabled          nginx.yml.disabled
apache.yml.disabled               googlecloud.yml.disabled     oracle.yml.disabled
appsearch.yml.disabled            graphite.yml.disabled        php_fpm.yml.disabled
aws.yml.disabled                  haproxy.yml.disabled         postgresql.yml.disabled
azure.yml.disabled                http.yml.disabled            prometheus.yml.disabled
beat-xpack.yml.disabled           jolokia.yml.disabled         rabbitmq.yml.disabled
beat.yml.disabled                 kafka.yml.disabled           redis.yml.disabled
ceph.yml.disabled                 kibana-xpack.yml.disabled    sql.yml.disabled
cockroachdb.yml.disabled          kibana.yml.disabled          stan.yml.disabled
consul.yml.disabled               kubernetes.yml.disabled      statsd.yml.disabled
coredns.yml.disabled              kvm.yml.disabled             system.yml
couchbase.yml.disabled            logstash-xpack.yml.disabled  tomcat.yml.disabled
couchdb.yml.disabled              logstash.yml.disabled        traefik.yml.disabled
docker.yml.disabled               memcached.yml.disabled       uwsgi.yml.disabled
dropwizard.yml.disabled           mongodb.yml.disabled         vsphere.yml.disabled
elasticsearch-xpack.yml.disabled  mssql.yml.disabled           windows.yml.disabled
elasticsearch.yml.disabled        munin.yml.disabled           zookeeper.yml.disabled
envoyproxy.yml.disabled           mysql.yml.disabled
[root@server1 modules.d]# metricbeat modules enable elasticsearch-xpack
Enabled elasticsearch-xpack
#启用elasticsearch-xpack模板文件
[root@server1 modules.d]# vim elasticsearch-xpack.yml 
#监控的信息;
 17   username: "elastic"
 18   password: "westos"

编辑metricbeat主配置文件,设定其将监控信息发到那去;

[root@server1 metricbeat]# vim metricbeat.yml 
 94   hosts: ["172.25.25.21:9200"]
101   username: "elastic"
[root@server1 metricbeat]# systemctl enable --now metricbeat

启动之后,便会自动识别到;一个节点完成之后,其他节点依次即可完成。

[root@server2 ~]# rpm -ivh metricbeat-7.6.1-x86_64.rpm 
[root@server2 modules.d]# pwd
/etc/metricbeat/modules.d
[root@server2 modules.d]# metricbeat modules enable elasticsearch-xpack
[root@server2 modules.d]# vim elasticsearch-xpack.yml
[root@server2 modules.d]# cd ..
[root@server2 metricbeat]# ls
fields.yml  metricbeat.reference.yml  metricbeat.yml  modules.d
[root@server2 metricbeat]# vim metricbeat.yml 
 94   hosts: ["172.25.25.22:9200"]
101   username: "elastic"
102   password: "westos"
[root@server2 metricbeat]# systemctl enable --now metricbeat.service 

当所有节点都完成时结果入下图所示:

filebeat
普通ELK架构
logstash -> es -> kibana

企业ELK架构
filebeat -> kafka/redis -> logstash -> es -> kibana

在这里还用到消息队列redis/kafka作为缓存使用。通过 logstash 搜集日志数据存入redis/kafka,再通过logstash对数据格式转化处理后储存到Elasticsearch中。

所有ES集群节点均需要安装filebeat的操作.

参考文档:
https://www.elastic.co/guide/en/beats/filebeat/7.6/filebeat-module-elasticsearch.html

[root@server1 ~]# rpm -ivh metricbeat-7.6.1-x86_64.rpm 
[root@server1 modules.d]# pwd
/etc/filebeat/modules.d
[root@server1 modules.d]# filebeat modules enable elasticsearch
Enabled elasticsearch
[root@server1 modules.d]# vim elasticsearch.yml
 11     var.paths:
 12       - /var/log/elasticsearch/*.log
 13       - /var/log/elasticsearch/*_server.json
 19     var.paths:
 20       - /var/log/elasticsearch/gc.log.[0-9]*
 21       - /var/log/elasticsearch/gc.log
 27     var.paths:
 28       - /var/log/elasticsearch/*_access.log
 29       - /var/log/elasticsearch/*_audit.json
 35     var.paths:
 36       - /var/log/elasticsearch/*_index_search_slowlog.log
 37       - /var/log/elasticsearch/*_index_indexing_slowlog.log
 38       - /var/log/elasticsearch/*_index_search_slowlog.json
 39       - /var/log/elasticsearch/*_index_indexing_slowlog.json
 46     var.paths:
 47       - /var/log/elasticsearch/*_deprecation.log
 48       - /var/log/elasticsearch/*_deprecation.json
[root@server1 modules.d]# cd ..
[root@server1 filebeat]# vim filebeat.yml
148 output.elasticsearch:
149   # Array of hosts to connect to.
150   hosts: ["172.25.25.21:9200"]
152   # Protocol - either `http` (default) or `https`.
153   #protocol: "https"
155   # Authentication credentials - either API key or username/password.
156   #api_key: "id:api_key"
157   username: "elastic"
158   password: "westos
[root@server1 filebeat]# systemctl enable --now filebeat.service 

可以通过流式传输关键字数据

ES数据备份与恢复
在ES集群节点创建 备份目录
#mkdir /es-backup
挂载共享存储到ES集群节点的备份目录:
#mount 172.25.0.3:/backup /es-backup //以nfs为例
在ES集群各节点设置:
path.repo: /es-backup //指定仓库路径
重启ES集群各节点才能生效

#systemctl restart elasticsearch //滚动重启

快照仓库需要注意的是需要在整个集群的每一台机器上挂载相同的共享文件存储目录,保证在集群里做的操作是输出到相同的地方的,并且需要具备可写权限。

滚动重启:
关闭节点时,分配过程将等待 index.unassigned.node_left.delayed_timeout一分钟(默认为一分钟),然后开始将该节点上的分片复制到集群中的其他节点,引起不必要的IO消耗,可以在关闭节点之前禁用副本分配,待节点再次加入集群后再启用。




kibana控制台

控制台插件提供一个用户界面来和 Elasticsearch 的 REST API(REST就是一种设计API的模式) 交互。控制台有两个主要部分: editor ,用来编写提交给 Elasticsearch 的请求; response 面板,用来展示请求结果的响应。

由于开启了xpack,想要输入命令时还需要用户密码,这里还有一个更加方便的方法,使用 kibana 的控制台,可以直接输入命令,不用用户密码,因为登陆 kibana 时已经输入过了。控制台实际还是后台调用了API,找 es 执行,再返回给 kibana 展示。

简单测试,输入了健康查询语句,可以看到节点的健康状况

后面还会有更多的调优和问题解决。
问题一:集群分片数太多,导致无法创建索引(默认为1000)
解决通过api修改集群单节点最大分片数。
问题二:集群单节点磁盘使用率超过90%,集群readonly;
解决:首先需要清理部分历史数据,如果不能清理,则需要在添加节点,进行reloadting;
通过api修改集群参数,使得集群可写。
问题三:集群每天早上8:00开始出现频繁的red,并且出现部分节点oom;
解决:查看是否有定时任务造成,如果有就需要通知业务部门进行采取措施进行优化;
查看是否存在同一时间点大量创建索引,这时会出现集群分配索引消耗大量内存资源导致集群节点oom(解决办法:可根据时即情况提前创建好索引,避免分配带来的压力)
问题四:集群每节点内存使用到达90%,出现单节点oom,整个集群处于瘫痪状态。
解决:此时需要进行扩容,根据实际情况进行扩容,如果单节点内存未超过32g,则只需要加大内存,如果超过最大内存则需要添加节点。
问题五:业务量大的时候,写入es的agent性能不足,导致数据丢失。
解决:尽量在消费agent前面添加一层缓存,(Kafka/redis),消费agent;
消费者可根据实际情况增加或者减少。
问题六:kafka分区数太少,导致消费agent消费性能未发挥到极致;
解决:可通过增加分区数量进行解决。
问题七:kafka持续积压,增加多个logstash进行消费并不能解决问题;
解决:查看kafka机器的cpu使用率,发现所有核心cpu使用率基本达到95%以上,需要进行扩容kafka机器,并平衡分区来解决。

以上是关于企业运维之 ELK日志分析平台(Kibana)的主要内容,如果未能解决你的问题,请参考以下文章

企业运维之 ELK日志分析平台(Elasticsearch)

企业运维之 ELK日志分析平台(Elasticsearch)

企业运维之 ELK日志分析平台(Logstash)

企业运维之 ELK日志分析平台(Logstash)

Linux企业运维——ELK日志分析平台(下)kibana数据可视化xpack安全验证轻量级metricbeat

Linux企业运维——ELK日志分析平台(下)kibana数据可视化xpack安全验证轻量级metricbeat