1.1 平臺環境:
OS:CentOS release 6.4(Final)
ElasticSearch:6.3.2
Logstash:6.3.2
Kibana:6.3.2
JRE:1.8php
注:因爲Logstash的運行依賴於Java環境, 而Logstash 1.5以上版本不低於java 1.7,所以推薦使用最新版本的Java。由於咱們只須要Java的運行環境,因此能夠只安裝JRE,不過這裏我依然使用JDKjava
1.2 ELK下載:https://www.elastic.co/downloads/node
cd /data/package/ wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.3.2.tar.gz wget https://artifacts.elastic.co/downloads/logstash/logstash-6.3.2.tar.gz wget https://artifacts.elastic.co/downloads/kibana/kibana-6.3.2-linux-x86_64.tar.gz
1.3 安裝準備linux
#配置iptables,保證內網之間能夠互通 [root@Elk_Server]# iptables -F;iptables -I INPUT -s 192.168.0.0/16 -j ACCEPT [root@Elk_Server]# service iptables save;service iptables restart #關閉selinux [root@Elk_Server]# setenforce 0 [root@Elk_Server]# vim /etc/sysconfig/selinux SELINUX=disabled
1.4 時間同步git
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime yum install ntpdate -y;ntpdate time.windows.com echo '01 00 * * * ntpdate time.windows.com' >>/etc/crontab
1.5 配置yum源github
#導入公鑰 [root@Elk_Server ~]# rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch [root@Elk_Server ~]# vim /etc/yum.repos.d/elasticsearch.repo [elasticsearch-6.x] name=Elasticsearch repository for 6.x packages baseurl=https://artifacts.elastic.co/packages/6.x/yum gpgcheck=1 gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch enabled=1 autorefresh=1 type=rpm-md
1.6 安裝JAVA 1.8.0_151, 請見 http://qiangsh.blog.51cto.com/3510397/1771748正則表達式
這個其實也是ELK中的核心,啓動的時候必定要注意,由於es不能夠進行root帳戶啓動,因此你還須要開啓一個elsearch帳戶。**mongodb
2.1.1 yum方式安裝數據庫
[root@Elk_Server ~]# sudo yum install elasticsearch -y
2.1.2 源碼安裝(本文爲編譯安裝配置) npm
#將解壓後的文件複製三份 tar zxvf elasticsearch-6.3.2.tar.gz -C /usr/local/ mv /usr/local/elasticsearch-6.3.2 /usr/local/es-master cp -rf /usr/local/es-master /usr/local/es-data1 cp -rf /usr/local/es-master /usr/local/es-data2 groupadd elasticsearch useradd elasticsearch -g elasticsearch
2.2 建立elasticsearch數據目錄
mkdir -p /data{,1,2}/elasticsearch/data mkdir -p /data{,1,2}/elasticsearch/logs chown -R elasticsearch:elasticsearch /data{,1,2}/elasticsearch
2.3 編輯Elasticsearch集羣配置文件
cp /usr/local/es-master/config/elasticsearch.yml /usr/local/es-master/config/elasticsearch.yml-bak #建立軟鏈接 mkdir /etc/elasticsearch/ ln -s /usr/local/es-master/config/elasticsearch.yml /etc/elasticsearch/es-master.yml ln -s /usr/local/es-data1/config/elasticsearch.yml /etc/elasticsearch/es-data1.yml ln -s /usr/local/es-data2/config/elasticsearch.yml /etc/elasticsearch/es-data2.yml
主節點配置:
vim /etc/elasticsearch/es-master.yml # ======================== Elasticsearch Configuration ========================= # cluster.name: elk-cluster node.name: node-master node.attr.rack: r1 # ----------------------------------- Paths ------------------------------------ #設置data存放的路徑爲/data/elasticsearch/data path.data: /data/elasticsearch/data #設置logs日誌的路徑爲/data/elasticsearch/logs path.logs: /data/elasticsearch/logs # ---------------------------------- Network ----------------------------------- #本機地址 network.host: 192.168.6.108 #開啓監聽的端口爲9200 http.port: 9200 #tcp通信端口 transport.tcp.port: 9330 #是否做爲主機 node.master: true #是否做爲數據節點 node.data: false node.max_local_storage_nodes: 3 #在刪除索引時,是否須要明確指定名稱.該值爲false時,則可經過正則或_all刪除: action.destructive_requires_name: true # --------------------------------- Discovery ---------------------------------- #集羣信息,默認的通信接口是9300 discovery.zen.ping.unicast.hosts: ["192.168.6.108:9330", "192.168.6.108:9331","192.168.6.108:9332"] #在新集羣搭建初期,總會出現某幾個節點與其餘節點通訊異常致使節點頻繁加入、退出集羣。這個過程是自動執行的。經過配置discovery.zen.ping_timeout來控制節點加入某個集羣或者開始選舉的響應時間(默認3s)。 discovery.zen.ping_timeout: 60s #這個關閉了自動建立索引。爲的也是安全考慮,不然即便是內網,也有不少掃描程序,一旦開啓,掃描程序會自動給你建立不少索引。 action.auto_create_index: false # ---------------------------------- Gateway ----------------------------------- # # Block initial recovery after a full cluster restart until N nodes are started: #設置集羣中N個節點啓動時進行數據恢復,默認爲1。 gateway.recover_after_nodes: 2 #設置初始化數據恢復進程的超時時間,默認是5分鐘。 gateway.recover_after_time: 3m #設置集羣中節點的數量,默認爲2,一旦這N個節點啓動,就會當即進行數據恢復。 gateway.expected_nodes: 3 # ---------------------------------- Various ----------------------------------- #開啓跨域訪問支持,默認爲false http.cors.enabled: true #跨域訪問容許的域名地址,(容許全部域名)以上使用正則 http.cors.allow-origin: /.*/
數據1節點配置:
vim /etc/elasticsearch/es-data1.yml # ======================== Elasticsearch Configuration ========================= # cluster.name: elk-cluster node.name: node-data1 node.attr.rack: r1 # ----------------------------------- Paths ------------------------------------ #設置data存放的路徑爲/dat1/elasticsearch/data path.data: /data1/elasticsearch/data #設置logs日誌的路徑爲/data1/elasticsearch/logs path.logs: /data1/elasticsearch/logs # ---------------------------------- Network ----------------------------------- #本機地址 network.host: 192.168.6.108 #開啓監聽的端口爲9201 http.port: 9201 #tcp通信端口 transport.tcp.port: 9331 #是否做爲主機 node.master: false #是否做爲數據節點 node.data: true node.max_local_storage_nodes: 3 #在刪除索引時,是否須要明確指定名稱.該值爲false時,則可經過正則或_all刪除: action.destructive_requires_name: true # --------------------------------- Discovery ---------------------------------- #集羣信息,默認的通信接口是9300 discovery.zen.ping.unicast.hosts: ["192.168.6.108:9330", "192.168.6.108:9331","192.168.6.108:9332"] discovery.zen.ping_timeout: 60s action.auto_create_index: false # ---------------------------------- Gateway ----------------------------------- # # Block initial recovery after a full cluster restart until N nodes are started: #設置集羣中N個節點啓動時進行數據恢復,默認爲1。 gateway.recover_after_nodes: 2 #設置初始化數據恢復進程的超時時間,默認是5分鐘。 gateway.recover_after_time: 3m gateway.expected_nodes: 3 # ---------------------------------- Various ----------------------------------- #開啓跨域訪問支持,默認爲false http.cors.enabled: true #跨域訪問容許的域名地址,(容許全部域名)以上使用正則 http.cors.allow-origin: /.*/
數據2節點配置:
vim /etc/elasticsearch/es-data2.yml # ======================== Elasticsearch Configuration ========================= # cluster.name: elk-cluster node.name: node-data2 node.attr.rack: r1 # ----------------------------------- Paths ------------------------------------ #設置data存放的路徑爲/data2/elasticsearch/data path.data: /data2/elasticsearch/data #設置logs日誌的路徑爲/data2/elasticsearch/logs path.logs: /data2/elasticsearch/logs # ---------------------------------- Network ----------------------------------- #本機地址 network.host: 192.168.6.108 #開啓監聽的端口爲9202 http.port: 9202 #tcp通信端口 transport.tcp.port: 9332 #是否做爲主機 node.master: false #是否做爲數據節點 node.data: true node.max_local_storage_nodes: 3 #在刪除索引時,是否須要明確指定名稱.該值爲false時,則可經過正則或_all刪除: action.destructive_requires_name: true # --------------------------------- Discovery ---------------------------------- #集羣信息,默認的通信接口是9300 discovery.zen.ping.unicast.hosts: ["192.168.6.108:9330", "192.168.6.108:9331","192.168.6.108:9332"] discovery.zen.ping_timeout: 60s #這個關閉了自動建立索引。爲的也是安全考慮,不然即便是內網,也有不少掃描程序,一旦開啓,掃描程序會自動給你建立不少索引。 action.auto_create_index: false # ---------------------------------- Gateway ----------------------------------- # # Block initial recovery after a full cluster restart until N nodes are started: #設置集羣中N個節點啓動時進行數據恢復,默認爲1。 gateway.recover_after_nodes: 2 #設置初始化數據恢復進程的超時時間,默認是5分鐘。 gateway.recover_after_time: 3m gateway.expected_nodes: 3 # ---------------------------------- Various ----------------------------------- #開啓跨域訪問支持,默認爲false http.cors.enabled: true #跨域訪問容許的域名地址,(容許全部域名)以上使用正則 http.cors.allow-origin: /.*/
使用外網搭建集羣時需注意:
network.host須要修改成0.0.0.0,同時要暴露你的外網地址,代碼以下:
network.host: 0.0.0.0 network.publish_host: xx.xx.xx.xx
discovery.zen.ping.unicast.hosts修改成各個服務器的外網地址和通信端口便可。
2.4 安裝head插件
head插件可以生成集羣的統計數據,並提供瀏覽器查詢,同時還能對elasticsearch索引進行結構化查詢。
cd /usr/local/es-master/ mkdir head &&cd head git clone https://github.com/mobz/elasticsearch-head.git cd elasticsearch-head/ #install node v7.x curl --silent --location https://rpm.nodesource.com/setup_7.x | bash - sudo yum install nodejs npm config set registry=http://registry.npm.taobao.org npm install ##運行緩慢 npm install -g grunt-cli ##修改配置文件 [root@Elk_Server elasticsearch-head]# vim Gruntfile.js connect: { server: { options: { port: 9100, hostname: '192.168.6.108', ##添加這行,冒號後面有空格 base: '.', keepalive: true } } }
2.5 系統參數調整
##修改es三個節點的JVM運行內存,這個能夠根據須要更改,可是-Xms和-Xmx的值必須同樣,否則啓動報錯 [root@Elk_Server ]# vim /usr/local/es-master/config/jvm.options [root@Elk_Server ]# vim /usr/local/es-data1/config/jvm.options [root@Elk_Server ]# vim /usr/local/es-data2/config/jvm.options ------------------------------------------------------------------------- -Xms2g -Xmx2g ##修改Linux最大打開文件數 vim /etc/security/limits.conf * soft nofile 655350 ##在末尾添加 * hard nofile 655350 ##這一步須要重啓linux使配置生效 reboot [root@Elk_Server~]# ulimit -n 655350 ##修改sysctl.conf文件 [root@Elk_Server ~]# vim /etc/sysctl.conf vm.max_map_count=655350 [root@Elk_Server ~]# sysctl -p
2.6 將ES的head插件啓動
cd /usr/local/es-master/head/elasticsearch-head && grunt server & [3] 5946 ##會開啓9100端口。
2.7 啓動Elasticsearch(ES只能使用普通用戶啓動)
chown -R elasticsearch:elasticsearch /usr/local/es-* chown -R elasticsearch:elasticsearch /data{,1,2}/elasticsearch ##切換到普通用戶啓動elasticsearch su - elasticsearch nohup /usr/local/es-master/bin/elasticsearch & # -d或&之後臺的方式進行啓動Elasticsearch [1] 6202 ##啓動成功是會開啓9200,9300兩個端口的。 ##傳輸端口爲9300接受http請求的端口爲9200 ##在瀏覽器打開能夠利用head插件查看elasticsearch的集羣及索引狀況
2.8 主節點啓動好後,用一樣的方法啓動子節點就能夠了。
若是報錯,解決方式:https://blog.51cto.com/qiangsh/2152670
2.9 驗證啓動
ps axu |grep elasticsearch 方法一: curl 'http://192.168.6.108:9200/_search?pretty' ----- { "took" : 9, "timed_out" : false, "_shards" : { "total" : 0, "successful" : 0, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 0, "max_score" : 0.0, "hits" : [ ] } } 方法二: //或者經過瀏覽器訪問:http://192.168.6.108:9200/
#檢查es版本信息 curl -u <user>:<passwd> http://192.168.6.108:9200 #此時觀察ES集羣狀態: curl http://192.168.6.108:9200/_cluster/health?pretty #觀察集羣內各索引狀態: curl http://192.168.6.108:9200/_cat/indices?pretty #查詢elasticsearch節點狀態: curl -XGET http://192.168.6.108:9200/_cat/shards |grep UNASSIGNED #查看節點列表 http://IP:9200/_cat/nodes?v curl 'IP:9200/_cat/nodes?v' #列出全部索引及存儲大小 http://IP:9200/_cat/indices?v curl 'IP:9200/_cat/indices?v'--查詢全部索引及數據大小 #建立索引 建立索引名爲XX,默認會有5個分片,1個索引 curl -XPUT 'IP:9200/XX?pretty' #添加一個類型 curl -XPUT 'IP:9200/XX/external/2?pretty' -d ' { "gwyy": "John" }' #更新一個類型 curl -XPOST 'IP:9200/XX/external/1/_update?pretty' -d ' { "doc": {"name": "Jaf"} }' #刪除指定索引 curl -XDELETE 'IP:9200/_index?pretty'
Logstash的功能以下:
其實它就是一個收集器而已,咱們須要爲它指定Input和Output(固然Input和Output能夠爲多個)。因爲咱們須要把Java代碼中Log4j的日誌輸出到ElasticSearch中,所以這裏的Input就是Log4j,而Output就是ElasticSearch。
3.1 安裝Logstash
tar zxvf logstash-6.3.2.tar.gz -C /usr/local/ mv /usr/local/logstash-6.3.2 /usr/local/logstash ln -s /usr/local/logstash/bin/logstash /usr/bin/ mkdir -p /data/logs/logstash
3.2 驗證安裝是否成功,從下圖中能夠看到,當前開啓的是9600端口
logstash -e 'input { stdin { } } output { elasticsearch { hosts => ["192.168.6.108:9200"] } stdout { codec => rubydebug }}' ###等待片刻後出現「The stdin plugin is now waiting for input:」,輸入「test」回車,返回下面標準的輸出。 ----- [2018-07-31T07:34:23,968][INFO ][logstash.agent ] Successfully started Logstash API endpoint {:port=>9600} test { "@version" => "1", "host" => "Elk_Server", "message" => "test", "@timestamp" => 2018-07-31T07:34:40.422Z }
3.3 logstash配置
##接下來是重點,也是ELK的難點吧,就是將日誌進行分析,使用正則匹配過濾日誌中想要的內容。
[root@Elk_Server logstash]# cat /data/logs/test.log 120.79.189.51 - - [10/Jun/2018:12:58:12 +0800] "POST /wp-cron.php?doing_wp_cron=1528606692.3628709316253662109375 HTTP/1.1" 499 0 "-" "WordPress/4.8; http://120.79.189.51"
##這是日誌中的一行,logstash的正則匹配默認是匹配一行,以回車符做爲分隔符。
##固然若是日誌是json格式的那麼就沒有須要處理直接輸出便可。
#咱們到config目錄中新建一個logstash.conf配置。 [root@Elk_Server logstash]# vim /etc/logstash/conf.d/logstash.conf input { file { path => "/data/logs/test.log" type => "test.log" start_position => "beginning" } } filter { grok { match => { "message" => "(?<ip_addr>\d+?\.\d+?\.\d+?\.\d+?)\s-\s-\s\[(?<log_date>\d+?/\w+?/\d+?):(?<log_time>\d+?:\d+?:\d+?)\s+" } } } output { stdout {codec => rubydebug} }
##這是logstash的日誌分析文件,總共分爲三個大部分,input、flter、output,其中input是輸入日誌,這裏選擇的是file文件,path是文件的路徑,type是文件的類型,這個能夠自定義,主要用來區分每一個日誌,start_position是設定爲beginning是指從文件的開頭開始讀取。
##flter是處理日誌的部分,使用grok這個強大的組件進行信息過濾,對於日誌的正則匹配最總會以json的格式輸出,因此正則匹配的格式是(?<字段名>正則表達式過濾將要匹配的內容)在每一個括號內建立過濾的內容,將不須要的內容放在括號外,能夠一直作匹配知道日誌結束,這裏我只是簡單匹配日誌前面的ip地址和日期時間。
針對正則匹配字段詳細說明:
##正則匹配字段經過「(?<字段名>正則表達式)」來定義,咱們看到「(?<ip_addr>\d+?.\d+?.\d+?.\d+?」匹配了日誌中的IP地址,而後將不須要的內容放在括號外面匹配,接着是日期和時間,使用一樣的方式,而後不須要的內容放在外面由於這裏我只匹配了前面的一點內容,若是有須要提取,能夠一直匹配,知道這一行結束,logstash默認只會匹配日誌以行做爲分割,下一行又是新的一次匹配了。
#output是處理輸出部分,這裏我只是簡單輸出到終端,先實驗正則匹配沒問題後,將輸出指向elasticsearch。
[root@Elk_Server logstash]# logstash -r -f /etc/logstash/conf.d/logstash.conf .............. { "path" => "/data/logs/test.log", "host" => "Elk_Server", "type" => "test.log", "log_date" => "10/Jun/2018", "@version" => "1", "log_time" => "12:58:12", "@timestamp" => 2018-07-31T09:45:16.247Z, "ip_addr" => "120.79.189.51", "message" => "120.79.189.51 - - [10/Jun/2018:12:58:12 +0800] \"POST /wp-cron.php?doing_wp_cron=1528606692.3628709316253662109375 HTTP/1.1\" 499 0 \"-\" \"WordPress/4.8; http://120.79.189.51\"" }
接下來將日誌輸出到elasticsearch,修改output裏面的內容便可。
output { elasticsearch { ##指定輸出到ES action => "index" ##創建索引 hosts => "192.168.6.108:9200" ##ES地址端口 index => "test_log-%{+yyyy.MM.dd}" ##索引的名稱 } stdout {codec => rubydebug} }
3.4 驗證配置,並報告錯誤
logstash -f /etc/logstash/conf.d/logstash.conf --config.test_and_exit \\或者 logstash -t -f /etc/logstash/conf.d/logstash.conf
3.5 服務啓動
nohup logstash -f /etc/logstash/conf.d/logstash.conf --config.reload.automatic -l /data/logs/logstash & \\或者 nohup logstash -r -f /etc/logstash/conf.d/logstash.conf -l /data/logs/logstash &
--config.reload.automatic選項啓用自動配置從新加載,這樣您每次修改配置文件時都沒必要中止並從新啓動Logstash。
3.6 結合filebeat案例
日誌內容以下:
2018-12-20 17:40:50.665 INFO 1 --- [Thread-422] f.s.j.e.common.iml.AbstractJobWorker : no task in app f.s.job.executor.social.users's job 0 2018-12-22 11:23:55.947 INFO -- [cluster-ClusterId{value='5xxx001145200', description='null'}-dds-xxxc9e41.mongodb.rds.aliyuncs.com:3717] org.mongodb.xxx.cluster : Server xxx:3717 is no longer a member of the replica set. Removing from client view of cluster.
filebeat配置文件:
#=========================== Filebeat inputs ============================= filebeat.inputs: - type: log enabled: true paths: - /data/log/*.log fields: app: tomcat type: all fields_under_root: true #multiline.pattern: '^\[' multiline.pattern: '^\d+\-\d+\-\d+\s+\d+\:\d+\:\d+' multiline.negate: true multiline.match: after multiline.max_lines: 200 multiline.timeout: 10s #tags: ["filebeat"] #exclude_lines: ['^\d+\-\d+\-\d+\s+\d+\:\d+\:\d+\s+DEBUG'] #include_lines: ['^ERR', '^WARN'] #----------------------------- Logstash output -------------------------------- output.logstash: # enabled: true hosts: ["127.0.0.1:5044"] # index => "filebeat-%{+YYYY.MM.dd}" # worker: 4 # bulk_max_size: 1024 # compression_level: 6 # loadbalance: false # backoff.max: 120s
logstash配置文件:
[root@Elk_Server logstash]# vim /etc/logstash/conf.d/logstash.conf input { beats { host => '0.0.0.0' port => "5044" } } filter { grok { match => { "message" => ["%{TIMESTAMP_ISO8601:date}\s+(?:%{LOGLEVEL:loglevel}|%{LOGLEVEL:loglevel}\s+\d) \-+ \[%{NOTSPACE:servicename}\] %{JAVALOGMESSAGE:message}", "%{TIMESTAMP_ISO8601:date}\s+(?:%{LOGLEVEL:loglevel}|%{LOGLEVEL:loglevel}\s+\d) \-+ \[%{NOTSPACE:servicename}\{%{DATA:description}\}-%{DATA:db}\] %{JAVALOGMESSAGE:message}"] } overwrite => [ "message" ] } #geoip { # source => "remote_addr" # target => "geoip" # database => "/opt/GeoLite2-City.mmdb" # add_field => ["[geoip][coordinates]", "%{[geoip][longitude]}"] # add_field => ["[geoip][coordinates]", "%{[geoip][latitude]}"] #} date { locale => "en" match => ["date", "dd/MMM/yyyy:HH:mm:ss Z"] } } output { elasticsearch { hosts => [ "192.168.6.108:9200" ] index => "logstash-%{type}-%{+YYYY.MM.dd}" } # stdout {codec => rubydebug} }
4.1 安裝Kibana
tar zxvf kibana-6.3.2-linux-x86_64.tar.gz -C /usr/local/ mv /usr/local/kibana-6.3.2-linux-x86_64 /usr/local/kibana
4.2 配置Kibana
cd /usr/local/kibana/ vim config/kibana.yml #把如下注釋放開,使配置起做用。 ----- server.port: 5601 server.host: "192.168.6.108" elasticsearch.url: "http://192.168.6.108:9200" kibana.index: ".kibana"
4.3 啓動Kibana並進行測試訪問,從日誌中能夠看出,當前開了5601端口
./bin/kibana #後臺啓動 nohup /usr/local/kibana/bin/kibana & ----- log [03:30:39.833] [info][status][plugin:kibana@6.2.4] Status changed from uninitialized to green - Ready log [03:30:39.882] [info][status][plugin:elasticsearch@6.2.4] Status changed from uninitialized to yellow - Waiting for Elasticsearch log [03:30:40.026] [info][status][plugin:timelion@6.2.4] Status changed from uninitialized to green - Ready log [03:30:40.033] [info][status][plugin:console@6.2.4] Status changed from uninitialized to green - Ready log [03:30:40.037] [info][status][plugin:metrics@6.2.4] Status changed from uninitialized to green - Ready log [03:30:40.061] [info][listening] Server running at http://192.168.30.211:5601 log [03:30:40.117] [info][status][plugin:elasticsearch@6.2.4] Status changed from yellow to green - Ready
4.4 查看啓動沒有報錯,能夠經過192.168.6.108:5601在瀏覽器進行訪問了。
①點擊Discover出現如下界面,在logstash日誌分析文件裏面有創建了一個索引,索引的名稱是test_log-${日期}這樣的形式,它是 Elasticsearch 中的一個索引名稱開頭。Kibana 會自動檢測在 Elasticsearch 中是否存在該索引名稱。
②按照註釋配置,而後點擊Next step,在第二頁 選擇@timestamp點擊create建立
③建立完成以後,能夠看到如下一個界面,紅框內是 自動生成的域,也能夠理解爲 跟數據庫中的字段相似,其中有一個message字段,就是咱們想要的日誌信息。
④再次點擊Discover出現如下界面,能夠看到默認搜索的是最後15分鐘的日誌,能夠經過點擊設置搜索的時間範圍.
⑤能夠點擊右側域的add設置須要顯示的字段
添加完成以後,日誌顯示以下:
⑥也能夠查看請求體和響應體