ELK 架構(gòu)之 Elasticsearch 和 Kibana 安裝配置

閱讀目錄:

1. ELK Stack 簡介

2. 環(huán)境準(zhǔn)備

3. 安裝 Elasticsearch

4. 安裝 Kibana

5. Kibana 使用

6. Elasticsearch 命令

最近在開發(fā)分布式服務(wù)追蹤,使用 Spring Cloud Sleuth Zipkin + Stream + RabbitMQ 中間件,默認(rèn)使用內(nèi)存存儲數(shù)據(jù),但這樣應(yīng)用于生產(chǎn)環(huán)境,就不太合適了。

最終我采用的方案:服務(wù)追蹤數(shù)據(jù)使用 RabbitMQ 進行采集 + 數(shù)據(jù)存儲使用 Elasticsearch + 數(shù)據(jù)展示使用 Kibana。

這篇文章主要記錄 Elasticsearch 和 Kibana 環(huán)境的配置,以及采集服務(wù)追蹤數(shù)據(jù)的顯出處理。

1. ELK Stack 簡介

ELK 是三個開源軟件的縮寫,分別為:Elasticsearch、Logstash 以及 Kibana,它們都是開源軟件。不過現(xiàn)在還新增了一個 Beats,它是一個輕量級的日志收集處理工具(Agent),Beats 占用資源少,適合于在各個服務(wù)器上搜集日志后傳輸給 Logstash,官方也推薦此工具,目前由于原本的 ELK Stack 成員中加入了 Beats 工具所以已改名為 Elastic Stack。

根據(jù) Google Trend 的信息顯示,Elastic Stack 已經(jīng)成為目前最流行的集中式日志解決方案。

Elastic Stack 包含:

Elasticsearch?是個開源分布式搜索引擎,提供搜集、分析、存儲數(shù)據(jù)三大功能。它的特點有:分布式,零配置,自動發(fā)現(xiàn),索引自動分片,索引副本機制,restful 風(fēng)格接口,多數(shù)據(jù)源,自動搜索負(fù)載等。詳細(xì)可參考?Elasticsearch 權(quán)威指南

Logstash?主要是用來日志的搜集、分析、過濾日志的工具,支持大量的數(shù)據(jù)獲取方式。一般工作方式為 c/s 架構(gòu),client 端安裝在需要收集日志的主機上,server 端負(fù)責(zé)將收到的各節(jié)點日志進行過濾、修改等操作在一并發(fā)往 Elasticsearch 上去。

Kibana?也是一個開源和免費的工具,Kibana 可以為 Logstash 和 ElasticSearch 提供的日志分析友好的 Web 界面,可以幫助匯總、分析和搜索重要數(shù)據(jù)日志。

Beats?在這里是一個輕量級日志采集器,其實 Beats 家族有 6 個成員,早期的 ELK 架構(gòu)中使用 Logstash 收集、解析日志,但是 Logstash 對內(nèi)存、cpu、io 等資源消耗比較高。相比 Logstash,Beats 所占系統(tǒng)的 CPU 和內(nèi)存幾乎可以忽略不計。

ELK Stack (5.0版本之后)--> Elastic Stack == (ELK Stack + Beats)。

目前 Beats 包含六種工具:

Packetbeat: 網(wǎng)絡(luò)數(shù)據(jù)(收集網(wǎng)絡(luò)流量數(shù)據(jù))

Metricbeat: 指標(biāo)(收集系統(tǒng)、進程和文件系統(tǒng)級別的 CPU 和內(nèi)存使用情況等數(shù)據(jù))

Filebeat: 日志文件(收集文件數(shù)據(jù))

Winlogbeat: windows 事件日志(收集 Windows 事件日志數(shù)據(jù))

Auditbeat:審計數(shù)據(jù)(收集審計日志)

Heartbeat:運行時間監(jiān)控(收集系統(tǒng)運行時的數(shù)據(jù))

ELK 簡單架構(gòu)圖:

2. 環(huán)境準(zhǔn)備

服務(wù)器環(huán)境:Centos 7.0(目前單機,后續(xù)再部署集群)

Elasticsearch 和 Logstash 需要 Java 環(huán)境,Elasticsearch 推薦的版本為 Java 8,安裝教程:確定穩(wěn)定的 Spring Cloud 相關(guān)環(huán)境版本

另外,我們需要修改下服務(wù)器主機信息:

[root@node1 ~]# vi /etc/hostnamenode1[root@node1 ~]# vi /etc/hosts192.168.0.11 node1127.0.0.1? node1 localhost localhost.localdomain localhost4 localhost4.localdomain4::1? ? ? ? node1 localhost localhost.localdomain localhost6 localhost6.localdomain6

注意:我之前安裝 Elasticsearch 和 Kibana 都是最新版本(6.x),但和 Spring Cloud 集成有些問題,所以就采用了 5.x 版本(具體 5.6.9 版本)

3. 安裝 Elasticsearch

運行以下命令將 Elasticsearch 公共 GPG 密鑰導(dǎo)入 rpm:

[root@node1 ~]# rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch

在/etc/yum.repos.d/目錄中,創(chuàng)建一個名為elasticsearch.repo的文件,添加下面配置:

[elasticsearch-5.x]name=Elasticsearch repositoryfor5.x packagesbaseurl=https://artifacts.elastic.co/packages/5.x/yumgpgcheck=1gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearchenabled=1autorefresh=1type=rpm-md

Elasticsearch 源創(chuàng)建完成之后,通過 makecache 查看源是否可用,然后通過 yum 安裝 Elasticsearch:

[root@node1 ~]# yum makecache && yum install elasticsearch -y

修改配置(啟動地址和端口):

[root@node1 ~]# vi /etc/elasticsearch/elasticsearch.ymlnetwork.host: node1# 默認(rèn)localhost,自定義為iphttp.port: 9200

要將 Elasticsearch 配置為在系統(tǒng)引導(dǎo)時自動啟動,運行以下命令:

[root@node1 ~]# sudo /bin/systemctl daemon-reload[root@node1 ~]# sudo /bin/systemctl enable elasticsearch.service

Elasticsearch 可以按如下方式啟動和停止:

[root@node1 ~]# sudo systemctl start elasticsearch.service[root@node1 ~]# sudo systemctl stop elasticsearch.service

列出 Elasticsearch 服務(wù)的日志:

[root@node1 ~]# sudo journalctl --unit elasticsearch-- Logs begin at 三 2018-05-09 10:13:46 CEST, end at 三 2018-05-09 10:53:53 CEST. --5月 09 10:53:43 node1 systemd[1]: [/usr/lib/systemd/system/elasticsearch.service:8] Unknown lvalue'RuntimeDirectory'insection'Service'5月 09 10:53:43 node1 systemd[1]: [/usr/lib/systemd/system/elasticsearch.service:8] Unknown lvalue'RuntimeDirectory'insection'Service'5月 09 10:53:48 node1 systemd[1]: Starting Elasticsearch...5月 09 10:53:48 node1 systemd[1]: Started Elasticsearch.5月 09 10:53:48 node1 elasticsearch[2908]:which: no javain(/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin)5月 09 10:53:48 node1 elasticsearch[2908]: could not find java;setJAVA_HOME or ensure java isinPATH5月 09 10:53:48 node1 systemd[1]: elasticsearch.service: main process exited, code=exited, status=1/FAILURE5月 09 10:53:48 node1 systemd[1]: Unit elasticsearch.service entered failed state.

出現(xiàn)了錯誤,具體信息是未找到JAVA_HOME環(huán)境變量,但我們明明已經(jīng)配置過了。

解決方式(參考資料:https://segmentfault.com/q/1010000004715131):

[root@node1 ~]# vi /etc/sysconfig/elasticsearchJAVA_HOME=/usr/local/java

重新啟動:

sudo systemctl restart elasticsearch.service

或者通過systemctl命令,查看 Elasticsearch 啟動狀態(tài):

[root@node1 ~]# systemctl status elasticsearch.serviceelasticsearch.service - Elasticsearch? Loaded: loaded (/usr/lib/systemd/system/elasticsearch.service; enabled)? Active: active (running) since 一 2018-05-14 05:13:45 CEST; 4h 5min ago? ? Docs: http://www.elastic.co? Process: 951 ExecStartPre=/usr/share/elasticsearch/bin/elasticsearch-systemd-pre-exec (code=exited, status=0/SUCCESS) Main PID: 953 (java)? CGroup: /system.slice/elasticsearch.service? ? ? ? ? └─953 /usr/local/java/bin/java -Xms2g -Xmx2g -XX:+UseConcMarkSweepGC -XX:CMSInitiatingO...5月 14 05:13:45 node1 systemd[1]: Started Elasticsearch.

發(fā)現(xiàn) Elasticsearch 已經(jīng)成功啟動。

查看 Elasticsearch 信息:

[root@node1 ~]# curl -XGET 'http://node1:9200/?pretty'{"name":"AKmrtMm","cluster_name":"elasticsearch","cluster_uuid":"r7lG3UBXQ-uTLHInJxbOJA","version": {"number":"5.6.9","build_hash":"877a590","build_date":"2018-04-12T16:25:14.838Z","build_snapshot":false,"lucene_version":"6.6.1"},"tagline":"You Know, for Search"}

4. 安裝 Kibana

運行以下命令將 Elasticsearch 公共 GPG 密鑰導(dǎo)入 rpm:

[root@node1 ~]# rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch

在/etc/yum.repos.d/目錄中,創(chuàng)建一個名為kibana.repo的文件,添加下面配置:

[kibana-5.x]name=Kibana repositoryfor5.x packagesbaseurl=https://artifacts.elastic.co/packages/5.x/yumgpgcheck=1gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearchenabled=1autorefresh=1type=rpm-md

安裝 Kibana:

[root@node1 ~]# yum makecache && yum install kibana -y

修改配置(地址和端口,以及 Elasticsearch 的地址,注意server.host只能填寫服務(wù)器的 IP 地址):

[root@node1 ~]# vi /etc/kibana/kibana.yml# Kibana is served by a back end server. This setting specifies the port to use.server.port: 5601# Specifies the address to which the Kibana server will bind. IP addresses and host names are both valid values.# The default is 'localhost', which usually means remote machines will not be able to connect.# To allow connections from remote users, set this parameter to a non-loopback address.server.host:"192.168.0.11"# The Kibana server's name.? This is used for display purposes.server.name:"kibana-server"# The URL of the Elasticsearch instance to use for all your queries.elasticsearch.url:"http://node1:9200"# 配置kibana的日志文件路徑,不然默認(rèn)是messages里記錄日志logging.dest: /var/log/kibana.log

創(chuàng)建日志文件:

[root@node1 ~]# touch /var/log/kibana.log; chmod 777 /var/log/kibana.log

要將 Kibana 配置為在系統(tǒng)引導(dǎo)時自動啟動,運行以下命令:

[root@node1 ~]# sudo /bin/systemctl daemon-reload[root@node1 ~]# sudo /bin/systemctl enable kibana.service

Kibana 可以如下啟動和停止

[root@node1 ~]# sudo systemctl start kibana.service[root@node1 ~]# sudo systemctl stop kibana.service

查看啟動日志:

[root@node1 ~]# sudo journalctl --unit kibana5月 09 11:14:48 node1 systemd[1]: Starting Kibana...5月 09 11:14:48 node1 systemd[1]: Started Kibana.

然后瀏覽器訪問:http://node1:5601

初次使用時,會讓你配置一個默認(rèn)的 index,也就是你至少需要關(guān)聯(lián)一個 Elasticsearch 里的 Index,可以使用 pattern 正則匹配。

注意:如果 Elasticsearch 中沒有數(shù)據(jù)的話,你是無法創(chuàng)建 Index 的。

如果 Spring Cloud Sleuth Zipkin + Stream + RabbitMQ 配置正確的話(以后再詳細(xì)說明),服務(wù)追蹤的數(shù)據(jù)就已經(jīng)存儲在 Elasticsearch 中了。

5. Kibana 使用

創(chuàng)建zipkin:*索引(*匹配后面所有字符):

然后就可以查看服務(wù)追蹤的數(shù)據(jù)了:

也可以創(chuàng)建自定義儀表盤:

6. Elasticsearch 命令

創(chuàng)建索引:

$ curl -XPUT'http://node1:9200/twitter'

查看 Index 索引列表:

$ curl -XGET http://node1:9200/_cat/indices

yellow open twitter k1KnzWyYRDeckjt7GASh8w 5 1 1 0 5.1kb 5.1kb

yellow open .kibana 8zJGQkq8TwC4s3JJLMX44g 1 1 1 0? 4kb? 4kb

yellow open student iZPqPcwrQbifGOfE9DQYvg 5 1 0 0? 955b? 955b

添加 Document 數(shù)據(jù):

$ curl -XPUT'http://node1:9200/twitter/tweet/1'-d'{

? ? "user" : "kimchy",

? ? "post_date" : "2009-11-15T14:12:12",

? ? "message" : "trying out Elastic Search"

}'

獲取 Document 數(shù)據(jù):

$ curl -XGET'http://node1:9200/twitter/tweet/1'{"_index":"twitter","_type":"tweet","_id":"1","_version":1,"found":true,"_source":{"user":"kimchy","post_date":"2009-11-15T14:12:12","message":"trying out Elastic Search"}}%

查詢zipkin索引下面的數(shù)據(jù):

$ curl -XGET'http://node1:9200/zipkin:*/_search'

參考資料:

集中式日志系統(tǒng) ELK 協(xié)議棧詳解

ELK+Filebeat搭建實時日志分析平臺

如何在CentOS 7上安裝Elasticsearch,Logstash和Kibana(ELK堆棧)

Spring Cloud Sleuth進階實戰(zhàn)

How To Install Elasticsearch, Logstash, and Kibana (ELK Stack) on CentOS 7

如何在 CentOS 7 上安裝 Elastic Stack

搭建ELK日志分析平臺(下)—— 搭建kibana和logstash服務(wù)器推薦

搭建ELK日志分析平臺(上)—— ELK介紹及搭建 Elasticsearch 分布式集群推薦

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容