容器化日誌收集方案-EFK
本文將描述如何使用 elasticsearch、 fluentd 、 kibana構建容器化日誌收集系統
原創作者:吳鵬
介 紹elasticsearch:
一個分散式的實時文件儲存,每個欄位 可以被索引與搜尋
一個分散式實時分析搜尋引擎
能勝任上百個服務節點的擴充套件,並支援 PB 級別的結構化或者非結構化資料
kibana:
是一個針對Elasticsearch的開源分析及視覺化平臺,用來搜尋、檢視互動儲存在Elasticsearch索引中的資料。
fluentd:
是日誌收集系統,通過豐富的外掛,可以收集來自於各種系統或應用的日誌,然後根據使用者定義將日誌做分類處理。
Kafka:
是分散式的、可分割槽的、可複製的訊息系統。它提供了普通訊息系統的功能,但具有自己獨特的設計
Kafka將訊息以topic為單位進行歸納。 將向Kafka topic釋出訊息的程式成為producers. 將預訂topics並消費訊息的程式成為consumer.
如 何 安 裝安裝kafka+zookeeper
參考: ofollow,noindex" target="_blank">https://hub.docker.com/r/wurstmeister/kafka/
安裝elasticsearch+kibana:
docker run -d -v "$PWD/esdata":/usr/share/elasticsearch/data -p 9200:9200 elasticsearch docker run --name kibana -e ELASTICSEARCH_URL=http://127.0.0.1:9200 -p 5601:5601 -d kibana
安裝fluentd
1、自定義fluentd的Dockerfile
FROM fluent/fluentd:v1.2# below RUN includes plugin as examples elasticsearch is not required# you may customize including plugins as you wishRUN apk add --update --virtual .build-deps \ sudo \ build-base \ ruby-dev \ tzdata \ && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ && sudo gem install fluent-plugin-elasticsearch \ && sudo gem install fluent-plugin-kafka \ && sudo gem sources --clear-all \ && apk del .build-deps \ && rm -rf /var/cache/apk/* \ /home/fluent/.gem/ruby/2.3.0/cache/*.gem ENTRYPOINT ["fluentd", "-c", "/fluentd/etc/fluent.conf", "-p", "/fluentd/plugins"]
2、編寫fluent.conf
<system> rpc_endpoint 127.0.0.1:24444</system><source> @type forward@id forward_input</source><source> @typetail#### tail方式採集日誌 #formatnone format/^(?<all>.*)$/ path/log/log.txt pos_file /log/pos_file/httpd-access.log.pos taglog.tag</source><match log.**> @type copy<store> @type elasticsearch hosts localhost:9200 type_name elasticsearch_fluentd include_tag_key true tag_key log_name flush_interval 10s # for testing logstash_format true logstash_prefix logstash</store> <store> @typekafka brokerslocalhost:9092#zookeeperlocalhost:2181 default_topicfluent_kafka#重新整理間隔 flush_interval30 ack_timeout2000 output_data_typeattr:all</store></match><source> @type monitor_agent @id monitor_agent_input port 24220</source><match debug.**> @type stdout</match>
3、構建fluentd image:
docker build -t fluentd:test -f Dockerfile .
4、啟動fluentd
docker run -it --rm --name fluent_test -v $(pwd)/log/:/log/ -v $(pwd)/:/fluentd/ etc/ fluentd:test
說明:
配置fluentd的RPC 為了重新載入fluentd.conf檔案
參考: https://docs.fluentd.org/v1.0/articles/rpc
<system> rpc_endpoint 127.0.0.1:24444</system>
測試
增加這個檔案的日誌
echotest > $(pwd)/log/log.txt
在kibana中進行日誌檢視:
修改fluent.conf
tag_key log_name 修改為tag_key log_name_key執行curl http://127.0.0.1:24444/api/ config.reload
即可以重新載入fluent.conf配置檔案,使其生效 繼續向log.txt寫入日誌 fluentd向kafka寫入日誌進入kafka的container中執行
bin/kafka-console-consumer.sh --bootstrap-server localhost: 9092 --topic fluent_kafka --from-beginning
附上kafka基本命令
建立topic 這裡建立的topic為:bin/kafka-topics.sh --create --zookeeper zookeeper:2181 --replication-factor 1 --partitions 1 --topic 檢視topicking bin/ kafka-topics.sh --list --zookeeper zookeeper:2181掛起生產者:producerbin/ kafka-console-producer.sh --broker-list localhost:9092 --topic test掛起之後, 就可以輸入資訊:test掛起消費者: consumer:bin/kafka-console-consumer. sh --bootstrap-server localhost:9092 --topic test --from-beginning在consulmer 就可以看到生產出來的:test至此完成了基本的elk 和fluent+kafka+zookeeper的日誌系統
在kubernets中使用DaemonSet 來獲取主機日誌或docker container日誌參考:
https://docs.fluentd.org/v1.0/articles/quickstart