docker elk

Docker logs

 

對於一個運行的容器,Docker 會將日誌發送到 容器的 標準輸出設備(STDOUT)和標準錯誤設備(STDERR),STDOUT 和 STDERR 實際上就是容器的控制檯終端。html

舉個例子,用下面的命令運行 httpd 容器:web

 
[root@host1 ~]# docker run -p 80:80 httpd Unable to find image 'httpd:latest' locally latest: Pulling from library/httpd 5e6ec7f28fb7: Pull complete 566e675a8212: Pull complete ef5a8026039b: Pull complete 22ecb0106557: Pull complete 91cc511c603e: Pull complete Digest: sha256:44daa8e932a32ab6e50636d769ca9a60ad412124653707e5ed59c0209c72f9b3 Status: Downloaded newer image for httpd:latest AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.12. Set the 'ServerName' directive globally to suppress this message AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.12. Set the 'ServerName' directive globally to suppress this message [Mon Jan 28 08:55:25.168252 2019] [mpm_event:notice] [pid 1:tid 140652308325568] AH00489: Apache/2.4.38 (Unix) configured -- resuming normal operations [Mon Jan 28 08:55:25.182578 2019] [core:notice] [pid 1:tid 140652308325568] AH00094: Command line: 'httpd -D FOREGROUND'
 

 

由於咱們在啓動日誌的時候沒有用-d 參數,httpd 容器之前臺方式啓動,日誌會直接打印在當前的終端窗口。docker

若是加上-d 參數之後臺方式運行容器,咱們就看不到輸出的日誌了。json

[root@host1 ~]# docker run -d -p 80:80 httpd 98d1fe5f1d074c345f578ef7767e8b2543977e61bb241f5629509c54502a7218

 

這種狀況下,查看容器日誌推薦的方法是用docker logs命令。瀏覽器

 
[root@host1 ~]# docker logs -f 98d1fe5f1d07 AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.12. Set the 'ServerName' directive globally to suppress this message AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.12. Set the 'ServerName' directive globally to suppress this message [Mon Jan 28 08:56:09.352502 2019] [mpm_event:notice] [pid 1:tid 139863955322048] AH00489: Apache/2.4.38 (Unix) configured -- resuming normal operations [Mon Jan 28 08:56:09.353740 2019] [core:notice] [pid 1:tid 139863955322048] AH00094: Command line: 'httpd -D FOREGROUND'
 

docker logs可以打印出自容器啓動以來完整的日誌,而且-f 參數能夠繼續打印出新產生的日誌,效果上與 Linux 命令tail -f 同樣。dom

 

logging driver

 

Docker 提供了多種日誌機制幫助用戶從運行的容器中提取日誌信息。這些機制被稱做 logging driver。curl

Docker 的默認 logging driver 是json-file。ide

[root@host1 ~]# docker info | grep 'Logging Driver' Logging Driver: json-file

 

jason-file會將容器的日誌保存在 json 文件中,Docker 負責格式化其內容並輸出到 STDOUT 和 STDERR。工具

咱們能夠在 Host 的容器目錄中找到這個文件,器路徑爲 /var/lib/docker/containers/<contariner ID>/<contariner ID>-json.logpost

 

好比咱們能夠查看前面 httpd 容器 json 格式的日誌文件。

能夠看到下面四條日誌記錄

 
[root@host1 ~]# cat /var/lib/docker/containers/c633eb9e8ddd2f4fe134ebfdc157398d97c281552c8ae357f4f4879f8dc6483b/c633eb9e8ddd2f4fe134ebfdc157398d97c281552c8ae357f4f4879f8dc6483b-json.log {"log":"AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.12. Set the 'ServerName' directive globally to suppress this message\n","stream":"stderr","time":"2019-01-28T09:08:22.784008521Z"} {"log":"AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.12. Set the 'ServerName' directive globally to suppress this message\n","stream":"stderr","time":"2019-01-28T09:08:22.792051336Z"} {"log":"[Mon Jan 28 09:08:22.792515 2019] [mpm_event:notice] [pid 1:tid 139740890547392] AH00489: Apache/2.4.38 (Unix) configured -- resuming normal operations\n","stream":"stderr","time":"2019-01-28T09:08:22.798741115Z"} {"log":"[Mon Jan 28 09:08:22.793583 2019] [core:notice] [pid 1:tid 139740890547392] AH00094: Command line: 'httpd -D FOREGROUND'\n","stream":"stderr","time":"2019-01-28T09:08:22.798792809Z"}
 

 

ELK介紹

 

在開源的日誌管理方案中,最出名的莫過於 ELK 了。ELK 是三個軟件的合稱:Elasticsearch、Logstash、Kibana。

Elasticsearch
一個近乎實時查詢的全文搜索引擎。Elasticsearch 的設計目標就是要可以處理和搜索巨量的日誌數據。

Logstash
讀取原始日誌,並對其進行分析和過濾,而後將其轉發給其餘組件(好比 Elasticsearch)進行索引或存儲。Logstash 支持豐富的 Input 和 Output 類型,可以處理各類應用的日誌。

Kibana
一個基於 JavaScript 的 Web 圖形界面程序,專門用於可視化 Elasticsearch 的數據。Kibana 可以查詢 Elasticsearch 並經過豐富的圖表展現結果。用戶能夠建立 Dashboard 來監控系統的日誌。

接下來討論如何用 ELK 這組黃金搭檔來監控 Docker 容器的日誌。

 

ELK日誌處理流程

 

下圖展現了 Docker 部署環境下典型的 ELK 日誌處理流程:

 

Logstash 負責從各個 Docker 容器中提取日誌,Logstash將日誌轉發到 Elasticsearch 進行索引和保存,Kibana 分析和可視化數據。

 

部署ELK

 

第一步:安裝

調整內存4個G以上,內核數4個G以上

[root@host1 ~]# docker run -p 5601:5601 -p 9200:9200 -p 5044:5044 -d --name elk sebp/elk

咱們使用的是 sebp/elk 這個現成的 image,裏面已經包含了整個 ELK stack。容器啓動後 ELK 各組件將分別監聽以下端口:

5601 – Kibana web 接口
9200 – Elasticsearch JSON 接口
5044 – Logstash 日誌接收接口

 

加-d選項,不然有時起不來

 

若是出現下面的錯誤

max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

執行下面的命令

sysctl -w vm.max_map_count=262144

 

第二步:瀏覽器訪問

先訪問一下 Kibana http://[Host IP]:5601/ 看看效果

 

當前 Kibana 沒有可顯示的數據,由於當前 Elasticsearch 尚未任何日誌數據。

 

接下來的工做就是將 Docker 的日誌導入 ELK

 

filebeat

 

前面咱們已經知道 Docker 會將容器日誌記錄到 /var/lib/docker/containers/<contariner ID>/<contariner ID>-json.log,那麼只要咱們可以將此文件發送給 ELK 就能夠實現日誌管理。

要實現這一步其實不難,由於 ELK 提供了一個配套小工具 Filebeat,它能將指定路徑下的日誌文件轉發給 ELK。同時 Filebeat 很聰明,它會監控日誌文件,當日志更新時,Filebeat 會將新的內容發送給 ELK。

 

第一步:安裝filebeat

[root@ken ~]# curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.2.0-x86_64.rpm

[root@ken ~]# rpm -vi filebeat-6.5.4-x86_64.rpm

Filebeat 可能已經有了更新的版本,請參考最新的安裝文檔 https://www.elastic.co/guide/en/beats/filebeat/current/filebeat-installation.html

 

注意:有時在建立index的時候會提示Couldn’t find any Elasticsearch data

能夠刪除yum remove filebeat,而後從新下載配置便可

 

第二步:配置filebeat

Filebeat 的配置文件爲 /etc/filebeat/filebeat.yml,咱們須要告訴 Filebeat 兩件事:

  1. 監控哪些日誌文件?
  2. 將日誌發送到哪裏?

首先回答第一個問題。

 

 

在 paths 中咱們配置了兩條路徑:

/var/lib/docker/containers/*/*.log 是全部容器的日誌文件。

/var/log/*.log 是 Host 操做系統的 log。

接下來告訴 Filebeat 將這些日誌發送給 ELK。

Filebeat 能夠將日誌發送給 Elasticsearch 進行索引和保存;也能夠先發送給 Logstash 進行分析和過濾,而後由 Logstash 轉發給 Elasticsearch。

爲了避免引入過多的複雜性,咱們這裏將日誌直接發送給 Elasticsearch。

下面的內容不須要任何的修改

 

第三步:啓動filebeat

[root@ken ~]# systemctl start filebeat

 

第四步:首先須要配置一個index pattern即告訴 Kibana 查詢和分析 Elasticsearch 中的哪些日誌。

 

指定 index pattern 爲 filebeat-*,這與 Elasticsearch 中的 index一致。

 

Time-field name 選擇 @timestamp。

 

點擊 Create 建立 index pattern。

 

第五步:查看日誌

點擊 Kibana 左側Discover 菜單,即可看到容器和 syslog 日誌信息。

 

第六步:測試

下面咱們啓動一個新的容器,該容器將向控制檯打印信息,模擬日誌輸出。

[root@ken ~]# docker run busybox sh -c 'while true; do echo "This is a log message from container busybox!"; sleep 10; done;' This is a log message from container busybox! This is a log message from container busybox!

 

刷新 Kibana 頁面立刻就能看到 busybox 的日誌。

 

Kibana 也提供了強大的查詢功能,好比輸入關鍵字busybox 能搜索出全部匹配的日誌條目。

 

Continue Reading

相關文章
相關標籤/搜索