ELK+kafka日誌系統搭建-實戰
阿新 • • 發佈:2019-02-11
日誌主要包括系統日誌、應用程式日誌和安全日誌。系統運維和開發人員可以通過日誌瞭解伺服器軟硬體資訊、檢查配置過程中的錯誤及錯誤發生的原因。經常分析日誌可以瞭解伺服器的負荷,效能安全性,從而及時採取措施糾正錯誤。
通常,日誌被分散的儲存不同的裝置上。如果你管理數十上百臺伺服器,你還在使用依次登入每臺機器的傳統方法查閱日誌。這樣是不是感覺很繁瑣和效率低下。當務之急我們使用集中化的日誌管理,例如:開源的syslog,將所有伺服器上的日誌收集彙總。
集中化管理日誌後,日誌的統計和檢索又成為一件比較麻煩的事情,一般我們使用grep、awk和wc等Linux命令能實現檢索和統計,但是對於要求更高的查詢、排序和統計等要求和龐大的機器數量依然使用這樣的方法難免有點力不從心。- Elasticsearch是個開源分散式搜尋引擎,它的特點有:分散式,零配置,自動發現,索引自動分片,索引副本機制,restful風格介面,多資料來源,自動搜尋負載等。
- Logstash是一個完全開源的工具,他可以對你的日誌進行收集、過濾,並將其儲存供以後使用(如,搜尋)。
- Kibana 也是一個開源和免費的工具,它Kibana可以為 Logstash 和 ElasticSearch 提供的日誌分析友好的 Web 介面,可以幫助您彙總、分析和搜尋重要資料日誌。
這樣可以看到logstash輸出到ES成功 系統日誌我們已經成功的收集,並且已經寫入到es叢集中,那上面的演示是logstash直接將日誌寫入到es叢集中的,這種場合我覺得如果量不是很大的話直接像上面已將將輸出output定義到es叢集即可,如果量大的話需要加上訊息佇列來緩解es叢集的壓力。前面已經提到了我這邊之前使用的是單臺redis作為訊息佇列,但是redis不能作為list型別的叢集,也就是redis單點的問題沒法解決,所以這裡我選用了kafka.但是由於資源問題現在只用一臺kafka。在搭建kafka叢集時,需要提前安裝zookeeper叢集,當然kafka已經自帶zookeeper程式只需要解壓並且安裝配置就行了 四、安裝kafka (10.10.13.15) yun install -y java1.7.0-openjdk* #wget http://apache.fayea.com/kafka/0.9.0.1/kafka_2.11-0.8.2.2.tgz #tar -zxvf kafka_2.11-0.8.2.2.tgz #mv kafka_2.11-0.8.2.2 /usr/local/kafka vi /usr/local/kafka/config/server.properties broker.id=0 listeners=PLAINTEXT://:9092 host.name=10.10.13.15 advertised.host.name=10.10.13.15 num.network.threads=3 num.io.threads=8 socket.send.buffer.bytes=102400 socket.receive.buffer.bytes=102400 socket.request.max.bytes=104857600 log.dirs=/elasticsearch/kafka-logs num.partitions=4 num.recovery.threads.per.data.dir=1 log.retention.hours=168 log.segment.bytes=1073741824 log.retention.check.interval.ms=300000 zookeeper.connect=localhost:2181 zookeeper.connection.timeout.ms=6000 #vi /usr/local/kafka/config/zookeeper.properties dataDir=/kafka/zookeeper clientPort=2181 maxClientCnxns=0 autopurge.snapRetainCount=100 autopurge.purgeInterval=12 啟動zookeeper/kafka服務,可以用netstat -tnlp看是否正常監聽2181和9092,可以設定為開機自動啟動服務 #/usr/local/kafka/bin/zookeeper-server-start.sh /usr/local/kafka/config/zookeeper.properties > /dev/null & #/usr/local/kafka/bin/kafka-server-start.sh /usr/local/kafka/config/server.properties > /dev/null & kafka常用命令(可以在bin下對不同指令碼執行--hlep查詢幫助): <>建立一個topic #bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test #注意:factor大小不能超過broker數 <>檢視當前所有topic #bin/kafka-topics.sh --list --zookeeper localhost:2181 __consumer_offsets bar foo haproxy linuxlog ((先建立linux客戶端使用topic,客戶端logstash可以輸出到該topic)) 這就是我們使用ELK收集Linux日誌 m-test-topic test-1 - marked for deletion winlog (先建立windows客戶端使用topic,客戶端logstash可以輸出到該topic) 這就是我們使用ELK收集windows日誌 winlog2 測試: 傳送訊息,這裡使用的是生產者角色
/bin/bash /usr/local/kafka/bin/kafka-console-producer.sh --broker-list 10.10.13.15:9092 --topic summer This is a messages welcome to kafka |
This is a messages welcome to kafka |