一、環境信息以及安裝前准備
1、組件介紹
*Filebeat是一個日志文件托運工具,在你的服務器上安裝客戶端后,filebeat會監控日志目錄或者指定的日志文件,追蹤讀取這些文件(追蹤文件的變化,不停的讀) Kafka是一種高吞吐量的分布式發布訂閱消息系統,它可以處理消費者規模的網站中的所有動作流數據(暫時不用) *Logstash是一根具備實時數據傳輸能力的管道,負責將數據信息從管道的輸入端傳輸到管道的輸出端;與此同時這根管道還可以讓你根據自己的需求在中間加上濾網,Logstash提供里很多功能強大的濾網以滿足你的各種應用場景 *ElasticSearch它提供了一個分布式多用戶能力的全文搜索引擎,基於RESTful web接口 *Kibana是ElasticSearch的用戶界面
在實際應用場景下,為了滿足大數據實時檢索的場景,利用Filebeat去監控日志文件,將Kafka作為Filebeat的輸出端,Kafka實時接收到Filebeat后以Logstash作為輸出端輸出,到Logstash的數據也許還不是我們想要的格式化或者特定業務的數據,這時可以通過Logstash的一些過了插件對數據進行過濾最后達到想要的數據格式以ElasticSearch作為輸出端輸出,數據到ElasticSearch就可以進行豐富的分布式檢索了
2、高級架構
Elastic
的技術架構可以簡單,也可以高級,它是很具有擴展性的,最簡單的技術架構就是使用Beats
進行數據的收集,Beats
是一種抽象的稱呼,具體的可以是使用FileBeat
收集數據源為文件的數據或者使用TopBeat
來收集系統中的監控信息,可以說類似Linux
系統中的TOP
命令,當然還有很多的Beats
的具體實現,再使用logstash
進行數據的轉換和導入到Elasticsearch
中,最后使用Kibana
進行數據的操作以及數據的可視化等操作。
當然,在生產環境中,我們的數據可能在不同的地方,例如關系型數據庫Postgre
,或者MQ
,再或者Redis
中,我們可以統一使用Logstash
進行數據的轉換,同時,也可以根據數據的熱度不同將ES
集群架構為一種冷溫熱架構,利用ES
的多節點,將一天以內的數據稱謂熱數據,讀寫頻繁,就存放在ES
的熱節點中,七天以內的數據稱之為溫數據,就是偶爾使用的數據存放在溫節點中,將極少數會用到的數據存放在冷節點中。

3、本次架構圖
4、環境准備
主機角色,配置(內存不要低於8核16G):
5、軟件版本下載
官網下載地址,個人建議最好再官網下載,之前在其他網站下載es,一直報錯,一直解決不了,在官網上下載下來的就一點問題都沒有:(如果下載過慢,建議使用迅雷下載,有會員就更好啦)
elastic https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.8.0.rpm kibana https://artifacts.elastic.co/downloads/kibana/kibana-6.8.0-x86_64.rpm logstash https://artifacts.elastic.co/downloads/logstash/logstash-6.8.0.rpm filebeat https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.8.0-x86_64.rpm
破解的x-pack和license的包下載地址:https://pan.baidu.com/s/1f9plvnhdbeSroOdW2G1YIg 提取碼:1v2g
6、安裝前提:
所有插件安裝之前必須安裝jdk1.8,並且需要配置環境變量,這邊我就不一一介紹了。
二、ES集群安裝配置
2.1、安裝
說明:3台機器一起安裝
[root@es-node1 config]#rpm -ivh elasticsearch-6.8.0.rpm
2.2、配置elasticsearch
#配置JVM參數,最大不要超過32G,並且留一半內存給操作系統,默認是1g [root@es-node1 config]# vim jvm.options -Xms4g -Xmx4g #配置elastic信息,其他節點需要修改node.name和network.host的值 [root@es-node1 config]# cd /etc/elasticsearch [root@es-node1 config]# cp elasticsearch.yml elasticsearch.yml-bak [root@es-node1 config]# vim elasticsearch.yml [root@es-node1 config]# grep "^[a-z]" elasticsearch.yml cluster.name: my-es node.name: node-76 path.data: /data/es #日志收集的目錄 path.logs: /var/log/elasticsearch #es自己的日志,如有報錯可在這邊查看 network.host: 172.9.201.76 http.port: 9200 discovery.zen.ping.unicast.hosts: ["172.9.201.76", "172.9.201.77","172.9.201.78"] discovery.zen.minimum_master_nodes: 2
設置打開文件描述符、進程數、內存限制
[root@es-node1 config]# vim /etc/security/limits.conf * soft nofile 65536 * hard nofile 131072 * soft nproc 2048 * hard nproc 4096 * soft memlock unlimited * hard memlock unlimited
配置內核參數
[root@es-node1 config]# vim /etc/sysctl.conf net.ipv4.tcp_max_tw_buckets = 6000 net.ipv4.ip_local_port_range = 1024 65000 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_tw_recycle = 1 net.ipv4.tcp_fin_timeout = 10 net.ipv4.tcp_syncookies = 1 net.core.netdev_max_backlog = 262144 net.ipv4.tcp_max_orphans = 262144 net.ipv4.tcp_max_syn_backlog = 262144 net.ipv4.tcp_timestamps = 0 net.ipv4.tcp_synack_retries = 1 net.ipv4.tcp_syn_retries = 1 net.ipv4.tcp_keepalive_time = 30 net.ipv4.tcp_mem= 786432 2097152 3145728 net.ipv4.tcp_rmem= 4096 4096 16777216 net.ipv4.tcp_wmem= 4096 4096 16777216
創建相關目錄及授權
[root@es-node1 ~]# mkdir -pv /data/es/ mkdir: 已創建目錄 "/data" mkdir: 已創建目錄 "/data/es/" [root@es-node1 ~]# chown -R elastic:elastic /data/es/
啟動elasticsearch,三台機器一起啟動
[root@es-node1 ~]# systemctl start elasticsearch
檢查服務是否正常:
[elastic@es-node1 elasticsearch]$ netstat -tnlp Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 172.9.201.76:9200 0.0.0.0:* LISTEN 2072/java #9200是http協議的RESTful接口 tcp 0 0 172.9.201.76:9300 0.0.0.0:* LISTEN 2072/java #9300是tcp通訊端口,集群間和TCPClient都走的它 tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN - tcp6 0 0 :::22 :::* LISTEN -
在瀏覽器中訪問: http://172.9.201.76:9200
name "node-76" cluster_name "my-es" cluster_uuid "FhxctUHqTz6eJZCkDuXwPQ" version number "6.8.0" build_flavor "default" build_type "rpm" build_hash "65b6179" build_date "2019-05-15T20:06:13.172855Z" 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"
查看是否是集群:
curl -XGET 'http://172.9.201.76:9200/_cat/nodes' //隨意一台es中可執行,也可更換其中的 ip(這里可22或23)
三、安裝並配置Kibana
3.1、安裝kibana
[root@shtw-kibana01 ~]# rpm -ivh kibana-6.8.0-x86_64.rpm
3.2、配置kibana
說明:雖然我們搭建了三台es的集群,但是我們在kibana.yml的elasticsearch.hosts只能配置一台es的主機,所以我們這邊一般配置master這一台。
[root@shtw-kibana01 ~]# cd /etc/kibana [root@shtw-kibana01 ~]# cp kibana.yml kibana.yml-bak [root@shtw-kibana01 ~]# vim kibana.yml server.port: 5601 #監聽的端口 server.host: "172.9.201.83" #監聽的地址 elasticsearch.hosts: ["http://172.9.201.76:9200"] #elasticsearch訪問的URL地址
3.3、啟動kibana
[root@shtw-kibana01 ~]# systemctl start kibana
我們可以看到啟動后服務監聽的地址,訪問這個URL:172.9.201.83:5601
kibana主頁面,大致意思就是告訴我們kibana可以做哪些事
點擊Monitoring標簽可以開啟監控設置,並且我們的license是basic版本的(即基礎版)
3.4、漢化kibana
7.x版本官方自帶漢化資源文件(位於kibana目錄下的node_modules/x-pack/plugins/translations/translations/
目錄,所以我們6.8.0是自帶漢化目錄的,接下來我們要做的是:
[root@shtw-kibana01 translations]# cd /usr/share/kibana/node_modules/x-pack/plugins/translations [root@shtw-kibana01 translations]# cp -r translations /usr/share/kibana/src/legacy/core_plugins/kibana [root@shtw-kibana01 translations]# cd /etc/kibana/kibana.yml #修改配置文件 i18n.locale: "zh-CN" #默認是en
四、安裝破解版x-pack
強烈說明:本教程只供交流學習使用,切勿運用到商業用途。如有需要請購買官方正版授權
由於在elasticsearch在6.3版本之后x-pack是默認安裝好的,所以不再需要用戶自己去安裝
1、替換x-pack包,並重啟elasticsearch服務
[root@shtw-esnode01 x-pack-core]# cd /usr/share/elasticsearch/modules/x-pack-core [root@shtw-esnode01 x-pack-core]# cp x-pack-core-6.8.0.jar x-pack-core-6.8.0.jar.20191206.bak [root@shtw-esnode01 x-pack-core]#rz -y #選擇下載在windows桌面上的已經破解好的x-pack-core-6.8.0.jar
2、替換完畢之后需要新增elasticsearch.yml配置
[root@shtw-esnode01 elasticsearch]# cd /etc/elasticsearch [root@shtw-esnode01 elasticsearch]# vim elasticsearch.yml xpack.security.enabled: false #新增關閉x-pack的配置文件
重啟elasticsearch服務,並把license.json上傳到服務器(三台es集群機器都執行如下操作,不然無法破解)
[elastic@es-node1 elasticsearch]$ curl -XPUT -u elastic 'http://172.9.201.76:9200/_xpack/license' -H "Content-Type: application/json" -d @license.json Enter host password for user 'elastic': #密碼為:change {"acknowledged":true,"license_status":"valid"}
在kibana上查看license
Platinum表示白金版 過期時間為2099 年12月31日,但是我們暫時還不能使用x-pack,因為白金版的x-pack需要SSL進行數據傳輸,所以我們需要先配置SSL
五、配置SSL並啟用x-pack
配置SSL之后,只要在master的es機器上配置用戶名密碼之后,其他的兩台es機器也具有相同的用戶名密碼,配置步驟如下
1、創建證書
[elastic@es-node1 elasticsearch]$ cd /usr/share/elasticsearch/bin [elastic@es-node1 bin]$ ./elasticsearch-certgen
2、解壓證書
[elastic@es-node1 bin]$ mkdir /tmp/cert [elastic@es-node1 bin]$ mv cert.zip /tmp/cert/ [elastic@es-node1 bin]$ cd /tmp/cert/ [elastic@es-node1 cert]$ unzip cert.zip Archive: cert.zip creating: ca/ inflating: ca/ca.crt inflating: ca/ca.key creating: elasticsearch/ inflating: elasticsearch/elasticsearch.crt inflating: elasticsearch/elasticsearch.key [elastic@es-node1 cert]$ ll 總用量 8 drwxrwxr-x 2 elastic elastic 34 9月 20 13:47 ca -rw------- 1 elastic elastic 5157 9月 20 13:47 cert.zip drwxrwxr-x 2 elastic elastic 56 9月 20 13:47 elasticsearch [elastic@es-node1 cert]$ mv ca/* /etc/elasticsearch/ [elastic@es-node1 cert]$ mv elasticsearch/* /etc/elasticsearch/ [elastic@es-node1 cert]$ cd /etc/elasticsearch/ [elastic@es-node1 config]$ ll -rct #最后四個 總用量 48 -rw-rw---- 1 elastic elastic 0 9月 19 16:15 users_roles -rw-rw---- 1 elastic elastic 0 9月 19 16:15 users -rw-rw---- 1 elastic elastic 197 9月 19 16:15 roles.yml -rw-rw---- 1 elastic elastic 473 9月 19 16:15 role_mapping.yml -rw-rw---- 1 elastic elastic 6380 9月 19 16:15 log4j2.properties -rw-rw---- 1 elastic elastic 2942 9月 19 16:15 jvm.options -rw-r----- 1 elastic elastic 2853 9月 19 16:15 elasticsearch.yml-bak -rw-rw---- 1 elastic elastic 207 9月 19 16:20 elasticsearch.keystore -rw-rw---- 1 elastic elastic 2905 9月 20 13:27 elasticsearch.yml -rw-rw-r-- 1 elastic elastic 1671 9月 20 13:57 ca.key -rw-rw-r-- 1 elastic elastic 1200 9月 20 13:57 ca.crt -rw-rw-r-- 1 elastic elastic 1675 9月 20 13:57 elasticsearch.key -rw-rw-r-- 1 elastic elastic 1237 9月 20 13:57 elasticsearch.crt
將證書拷貝到其他節點,放入 /etc/elasticsearch 目錄下
[elastic@es-node1 config]$ scp *.crt *.key 172.9.201.77:/etc/elasticsearch/ [elastic@es-node1 config]$ scp *.crt *.key 172.9.201.78:/etc/elasticsearch/
3、配置SSL,其他節點相同配置
[elastic@es-node1 config]$ vim elasticsearch.yml [elastic@es-node1 config]$ tail elasticsearch.yml # ---------------------------------- Various ----------------------------------- # # Require explicit names when deleting indices: # #action.destructive_requires_name: true xpack.security.enabled: true #之前配置為false注意刪除或者修改 xpack.security.transport.ssl.enabled: true xpack.ssl.key: elasticsearch.key xpack.ssl.certificate: elasticsearch.crt xpack.ssl.certificate_authorities: ca.crt
重啟elasticsearch服務,這個時候發現登錄用戶還是不行,不要着急,這邊還需要配置kibana使用用戶名密碼登錄
4、創建elk的集群的相關用戶
注意的是:這邊只需要在master的es主機上去創建,因為是master的es主機創建的ssl證書。
[elastic@es-node1 bin]$ cd /usr/share/elasticsearch/bin [elastic@es-node1 bin]$ ./elasticsearch-setup-passwords -h #查看命令幫助 Sets the passwords for reserved users Commands -------- auto - Uses randomly generated passwords #主要命令選項,表示系統將使用隨機字符串設置密碼 interactive - Uses passwords entered by a user #主要命令選項,表示使用用戶輸入的字符串作為密碼 Non-option arguments: command Option Description ------ ----------- -h, --help show help -s, --silent show minimal output -v, --verbose show verbose output [elastic@es-node1 bin]$ ./elasticsearch-setup-passwords auto #為了演示效果,這里我們使用系統自動創建 Initiating the setup of passwords for reserved users elastic,kibana,logstash_system,beats_system. The passwords will be randomly generated and printed to the console. Please confirm that you would like to continue [y/N]y #選擇y Changed password for user kibana #kibana角色和密碼 PASSWORD kibana = 4VXPRYIVibyAbjugK6Ok Changed password for user logstash_system #logstash角色和密碼 PASSWORD logstash_system = 2m4uVdSzDzpt9OEmNin5 Changed password for user beats_system #beast角色和密碼 PASSWORD beats_system = O8VOzAaD3fO6bstCGDyQ Changed password for user elastic #elasticsearch角色和密碼 PASSWORD elastic = 1TWVMeN8tiBy917thUxq
可以先將以上用戶和密碼保存下來,具體的用戶介紹見最后,這邊最好保存在有道雲筆記中,方便記錄。
配置kibana添加elasticsearch用戶認證:
[root@kb-node1 ~]# vim /etc/kibana/kibana.yml [root@kb-node1 ~]# grep "^elastic" kibana.yml elasticsearch.username: "elastic" elasticsearch.password: "1TWVMeN8tiBy917thUxq" #就是上一步創建的elastic的賬號和密碼
重啟kibana,重啟后打開kibana web頁面:
可以使用用戶名和密碼登錄了(使用之前創建的用戶名和密碼登錄即可),這里我們選擇elastic用戶登錄
六、安裝logstash以及配置
1、安裝logstash
[root@shtw-logstash01 ~]# rpm -ivh logstash-6.8.0.rpm
2、配置logstash
[root@shtw-logstash01 logstash]# cd /etc/logstash [root@shtw-logstash01 logstash]# vim logstash.yml path.data: /data/logstash #配置數據路徑 http.host: "172.9.201.82" #配置主機名 path.logs: /var/log/logstash
3、配置conf文件
說明:這邊有一個大坑,如果你不在output中根據if去判斷,則所有采集的日志,就會采集到各個index文件中,那樣就會所有的index的內容就會是一樣的,所以我們根據tags來判斷。
[root@shtw-logstash01 conf.d]# cd /etc/logstash/conf.d [root@shtw-logstash01 conf.d]# vim service_5044.conf # Sample Logstash configuration for creating a simple # Beats -> Logstash -> Elasticsearch pipeline. #采集filebeat過來的日志 input { beats { port => 5044 } file { path=> [ "/usr/local/tomcat/logs/catalina.out"] } } #過濾條件 filter{ if "beats_input_codec_plain_applied" in [tags]{ mutate{ remove_tag => ["beats_input_codec_plain_applied"] #刪除beats_input_codec_plain_applied的tags } } } #輸出到es output { if "workflow2" in [tags] { #tags是在filebeat客戶端設置的 elasticsearch { hosts => ["http://10.10.201.76:9200","http://10.10.201.77:9200","http://10.10.201.78:9200"] #輸出的3台es集群 index => "workflow2-%{+YYYY.MM.dd}" #index名 user => "elastic" #es的用戶名密碼 password => "2GyYwGHHHMA7udFTozo4" } } else if "simpleworkflow" in [tags] { elasticsearch { hosts => ["http://10.10.201.76:9200","http://10.10.201.77:9200","http://10.10.201.78:9200"] index => "simpleworkflow-%{+YYYY.MM.dd}" user => "elastic" password => "2GyYwGHHHMA7udFTozo4" } } stdout { codec => json_lines } }
注意的是:這邊可以創建多個conf文件,每個文件的端口號必須不一致。
啟動logstash:
[root@shtw-logstash01 ~]# systemctl start logstash
七、安裝filebeat
在需要采集日志的地方安裝filebeat
1、安裝filebeta
[root@shtw-k2workflow01 ~]# rpm -ivh filebeat-6.8.0-x86_64.rpm
2、配置filebeat
[root@shtw-k2workflow01 ~]# cd /etc/filebeat [root@shtw-k2workflow01 filebeat]# cp filebeat.yml filebeat.yml-bak [root@shtw-k2workflow01 filebeat]# vim filebeat.yml filebeat.inputs: - type: log enabled: true #默認是false,要改成true的,不然的話,采集不到日志 paths: - /usr/local/tomcat/logs/catalina.out #日志的路徑 #- /var/log/*.log #- c:\programdata\elasticsearch\logs\* filebeat.config.modules: path: ${path.config}/modules.d/*.yml reload.enabled: false setup.template.settings: index.number_of_shards: 3 # 用於在logstash中的output中做判斷,不然采集的日志都輸出到一起去了 tags: ["workflow2"] setup.kibana: output.logstash: # The Logstash hosts hosts: ["10.10.201.82:5044"] #傳送到logstash的主機,並且對應端口號 processors: - add_host_metadata: ~ - add_cloud_metadata: ~
3、啟動filebeat
[root@shtw-k2workflow01 filebeat]# systemctl start filebeat
等5s中去kibana中去查看索引,如圖:
4、創建索引
5、查看采集的日志
搭建完畢,elk的路還是很長的,才走完萬里長征第一步。