[ELK教學]最新CentOS ElasticSearch、Logstash、Kibana、Filebeat ,快速安裝(照著貼上就對了)

Derek Wu
10 min readJul 5, 2019

--

(2020/10/23 更新) ELK 升級到最新7.9.2版本啦! 為了研究使用7.9.2新的Alert and Action報警的功能,就大膽生上去了。Kibana的UI整個大升級! 功能也更豐富了。設定檔部分有小小的修改,想升級的可以照著指令安裝升級,有無法執行的問題可以留言告訴我喔! 升級官方文件

(前情提要) 我都用 vim 開啟檔案,如果沒有裝 可以改 vi 開啟,或是 "yum install vim" 安裝,其實滿好用的,所有localhost都要改成自己的ip喔

正文

ELK 是Elasticsearch(資料庫)、Logstash(資料解析)、Kibana(資料視覺化)三個工具的縮寫,處理大量資料的好夥伴。通常會搭配Beats(資料蒐集)、Kafka(資料緩衝處理)一起服用。

我開始用ELK到現在超過兩年了,這篇文章目的是紀錄一下ELK Stack分析、安裝部署、數據監控、可視化、自動刪除資料等功能。也希望可以節省大家搭建ELK的速度,時間花在更重的事上。過程遇到的困難就讓我來解決吧。基本上複製貼上就可以,有ELK問題或指教歡迎在下面留言,如果文章有幫助到你的話也幫我拍幾下手喔~

ELK+Beats

版本:

  • Filebeats — v7.7.0
  • Elasticsearch / Logstash / Kibana — v7.9.2

環境: CentOS 7

需求: Java

FIlebeats 安裝

要抓資料的機器下安裝:

# cd /home/lnmp   安裝檔的地方可以自己選擇

預設安裝路徑: /usr/local/

# curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.7.0-linux-x86_64.tar.gz
# tar xzvf filebeat-7.7.0-linux-x86_64.tar.gz
# mv filebeat-7.7.0-linux-x86_64 /usr/local/filebeat
# cd /usr/local/filebeat

設定檔

# vim filebeat.yml

Config

#複製貼上下面內容 (localhost改成自己的IP)
name: localhost
output:
logstash:
enabled: true
hosts:
- localhost:5044
index: "localhost"
filebeat.inputs:
- type: log
paths:
- /usr/local/nginx/logs/access.log
tags: ["access"]
#開啟debug模式
logging.level: debug
logging.selectors: [publish]
logging.to_files: true
logging.files:
path: /var/log/filebeat
name: filebeat-localhost

啟動

# /usr/local/filebeat/filebeat -c /usr/local/filebeat/filebeat.yml &

Logstash 安裝

# cd /home/lnmp# wget https://artifacts.elastic.co/downloads/logstash/logstash-7.9.2.tar.gz
# tar xzvf logstash-7.9.2.tar.gz
# mv logstash-7.9.2 /usr/local/logstash
# cd /usr/local/logstash

設定檔

# vim config/logstash.conf

Config

#複製貼上下面內容 (localhost改成自己的IP)input {
beats {
port => 5044
}
}
filter {
if "access" in [tags]{ #可以根據自訂Tag產生不同檔案
json {
source => "message"
}
mutate {
add_field => { "[@metadata][tags]" => "nginx-access-logs"}
}
}
}
output {
elasticsearch {
hosts => "localhost:9200"
manage_template => false
index => "%{[@metadata][beat]}-%{[@metadata][tags]}-%{+YYYY.MM.dd}"
}
}

debug模式

# vim config/logstash.yml

加入下面這段

 log.level: debug
path.logs: /var/log/logstash

啟動/重啟

  • -- config.reload.automatic 設定檔更動時自動重新載入,超好用的指令,這樣就不用因為更動設定檔重啟了!
# kill -9 $(ps aux | grep logstash | awk '{print $2}') && /usr/local/logstash/bin/logstash -f /usr/local/logstash/config/logstash.conf --config.reload.automatic &

Elasticserach安裝

# cd /home/lnmp# wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.9.2-linux-x86_64.tar.gz
# tar xzvf elasticsearch-7.9.2-linux-x86_64.tar.gz
# mv elasticsearch-7.9.2 /usr/local/elasticsearch
# adduser elastic
# chown elastic:elastic -R /usr/local/elasticsearch/
# cd /usr/local/elasticsearch

設定檔

# vim config/elasticsearch.yml

Config

#複製貼上下面內容 (localhost改成自己的IP)node.name: node-1
network.host: localhost
cluster.initial_master_nodes: ["node-1"]

解決 Memery問題 / max file descriptors too low問題

# vim /etc/sysctl.conf 追加以下内容:
vm.max_map_count=655360
儲存後,執行:
# sysctl -p
# vim /etc/security/limits.conf 追加下面這段:
elastic soft nofile 65536
elastic hard nofile 65536
儲存後,執行:
# su elastic -c 'ulimit -Hn'
>>65536 //成功

啟動 Elasticsearch

# su elastic -c "/usr/local/elasticsearch/bin/elasticsearch &"
//Elastic不建議用root啟動

訪問/查看index

https://localhost:9200/_cat/indices?v

curl -XGET localhost:9200/_xpack?pretty

Elastic Query 指南

*啟動有問題 最後可以檢查 startup.options檔案

Kibana安裝

# wget https://artifacts.elastic.co/downloads/kibana/kibana-7.9.2-linux-x86_64.tar.gz
# tar xzvf kibana-7.9.2-linux-x86_64.tar.gz
# mv kibana-7.9.2-linux-x86_64 /usr/local/kibana
# cd /usr/local/kibana

設定檔

# vim config/kibana.yml

Config

#複製貼上下面內容 (localhost改成自己的IP)server.host: "localhost"
elasticsearch.hosts: ["http://localhost:9200"]

啟動

# /usr/local/kibana/bin/kibana --allow-root

訪問 : http://localhost:5601/

最新版Kibana功能都收在左上方的"三"裡面,讓入口變成簡潔的畫面

看到上面的畫面恭喜你就完成啦~

如果要多伺服器、建立叢集又是另外一個故事了….

如果有讓你看完這篇文,可以幫我拍手 1–10 下
如果覺得這文章對你有幫助,可以幫我拍手 10–30 下
如果覺得想看到更多關於學習筆記的文章,可以幫我拍手 30–50 下
讓我知道也記得 Follow我 DerekWu
更歡迎你在下方留言,我很樂意與你討論聊天或回答問題!

--

--

Derek Wu

B.S. in Math, 4 years Recsys engineer exp, currently pursuing M.S. in Computer Science. 數學系畢業,美國交換學生,四年演算法&系統工程師經驗,開發推薦系統。目前研究生在讀中, Work hard, play hard!