Docker日志管理–docker部署安裝ELK

Docker logs

對(duì)于一個(gè)運(yùn)行的容器,Docker 會(huì)將日志發(fā)送到 容器的 標(biāo)準(zhǔn)輸出設(shè)備(STDOUT)和標(biāo)準(zhǔn)錯(cuò)誤設(shè)備(STDERR),STDOUT 和 STDERR 實(shí)際上就是容器的控制臺(tái)終端。

舉個(gè)例子,用下面的命令運(yùn)行 httpd 容器:

[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'

因?yàn)槲覀冊(cè)趩?dòng)日志的時(shí)候沒(méi)有用-d 參數(shù),httpd 容器以前臺(tái)方式啟動(dòng),日志會(huì)直接打印在當(dāng)前的終端窗口。

如果加上-d 參數(shù)以后臺(tái)方式運(yùn)行容器,我們就看不到輸出的日志了。

[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能夠打印出自容器啟動(dòng)以來(lái)完整的日志,并且-f 參數(shù)可以繼續(xù)打印出新產(chǎn)生的日志,效果上與 Linux 命令tail -f 一樣。

logging driver

Docker 提供了多種日志機(jī)制幫助用戶從運(yùn)行的容器中提取日志信息。這些機(jī)制被稱作 logging driver。

Docker 的默認(rèn) logging driver 是json-file。

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

jason-file會(huì)將容器的日志保存在 json 文件中,Docker 負(fù)責(zé)格式化其內(nèi)容并輸出到 STDOUT 和 STDERR。

我們可以在 Host 的容器目錄中找到這個(gè)文件,器路徑為 /var/lib/docker/containers/<contariner ID>/<contariner ID>-json.log

比如我們可以查看前面 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介紹

在開源的日志管理方案中,最出名的莫過(guò)于 ELK 了。ELK 是三個(gè)軟件的合稱:Elasticsearch、Logstash、Kibana。

Elasticsearch
一個(gè)近乎實(shí)時(shí)查詢的全文搜索引擎。Elasticsearch 的設(shè)計(jì)目標(biāo)就是要能夠處理和搜索巨量的日志數(shù)據(jù)。

Logstash
讀取原始日志,并對(duì)其進(jìn)行分析和過(guò)濾,然后將其轉(zhuǎn)發(fā)給其他組件(比如 Elasticsearch)進(jìn)行索引或存儲(chǔ)。Logstash 支持豐富的 Input 和 Output 類型,能夠處理各種應(yīng)用的日志。

Kibana
一個(gè)基于 JavaScript 的 Web 圖形界面程序,專門用于可視化 Elasticsearch 的數(shù)據(jù)。Kibana 能夠查詢 Elasticsearch 并通過(guò)豐富的圖表展示結(jié)果。用戶可以創(chuàng)建 Dashboard 來(lái)監(jiān)控系統(tǒng)的日志。

接下來(lái)討論如何用 ELK 這組黃金搭檔來(lái)監(jiān)控 Docker 容器的日志。

ELK日志處理流程

下圖展示了 Docker 部署環(huán)境下典型的 ELK 日志處理流程:

image

Logstash 負(fù)責(zé)從各個(gè) Docker 容器中提取日志,Logstash將日志轉(zhuǎn)發(fā)到 Elasticsearch 進(jìn)行索引和保存,Kibana 分析和可視化數(shù)據(jù)。

部署ELK

第一步:安裝

調(diào)整內(nèi)存4個(gè)G以上,內(nèi)核數(shù)4個(gè)G以上

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

我們使用的是 sebp/elk 這個(gè)現(xiàn)成的 image,里面已經(jīng)包含了整個(gè) ELK stack。容器啟動(dòng)后 ELK 各組件將分別監(jiān)聽如下端口:

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

加-d選項(xiàng),否則有時(shí)起不來(lái)

如果出現(xiàn)下面的錯(cuò)誤

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

執(zhí)行下面的命令

sysctl -w vm.max_map_count=262144

第二步:瀏覽器訪問(wèn)

先訪問(wèn)一下 Kibana http://[Host IP]:5601/ 看看效果

image

當(dāng)前 Kibana 沒(méi)有可顯示的數(shù)據(jù),因?yàn)楫?dāng)前 Elasticsearch 還沒(méi)有任何日志數(shù)據(jù)。

接下來(lái)的工作就是將 Docker 的日志導(dǎo)入 ELK

filebeat

前面我們已經(jīng)知道 Docker 會(huì)將容器日志記錄到 /var/lib/docker/containers/<contariner ID>/<contariner ID>-json.log,那么只要我們能夠?qū)⒋宋募l(fā)送給 ELK 就可以實(shí)現(xiàn)日志管理。

要實(shí)現(xiàn)這一步其實(shí)不難,因?yàn)?ELK 提供了一個(gè)配套小工具 Filebeat,它能將指定路徑下的日志文件轉(zhuǎn)發(fā)給 ELK。同時(shí) Filebeat 很聰明,它會(huì)監(jiān)控日志文件,當(dāng)日志更新時(shí),F(xiàn)ilebeat 會(huì)將新的內(nèi)容發(fā)送給 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 可能已經(jīng)有了更新的版本,請(qǐng)參考最新的安裝文檔 https://www.elastic.co/guide/en/beats/filebeat/current/filebeat-installation.html

注意:有時(shí)在創(chuàng)建index的時(shí)候會(huì)提示Couldn’t find any Elasticsearch data

可以刪除yum remove filebeat,然后重新下載配置即可

第二步:配置filebeat

Filebeat 的配置文件為 /etc/filebeat/filebeat.yml,我們需要告訴 Filebeat 兩件事:

  1. 監(jiān)控哪些日志文件?
  2. 將日志發(fā)送到哪里?

首先回答第一個(gè)問(wèn)題。

image

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

/var/lib/docker/containers//.log 是所有容器的日志文件。

/var/log/*.log 是 Host 操作系統(tǒng)的 log。

接下來(lái)告訴 Filebeat 將這些日志發(fā)送給 ELK。

Filebeat 可以將日志發(fā)送給 Elasticsearch 進(jìn)行索引和保存;也可以先發(fā)送給 Logstash 進(jìn)行分析和過(guò)濾,然后由 Logstash 轉(zhuǎn)發(fā)給 Elasticsearch。

為了不引入過(guò)多的復(fù)雜性,我們這里將日志直接發(fā)送給 Elasticsearch。

下面的內(nèi)容不需要任何的修改

image

第三步:?jiǎn)?dòng)filebeat

>[root@ken ~]# systemctl start filebeat

第四步:首先需要配置一個(gè)index pattern即告訴 Kibana 查詢和分析 Elasticsearch 中的哪些日志。

image

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

image

Time-field name 選擇 @timestamp。

點(diǎn)擊 Create 創(chuàng)建 index pattern。

image

第五步:查看日志

點(diǎn)擊 Kibana 左側(cè)Discover 菜單,便可看到容器和 syslog 日志信息。

image

第六步:測(cè)試

下面我們啟動(dòng)一個(gè)新的容器,該容器將向控制臺(tái)打印信息,模擬日志輸出。

[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 頁(yè)面馬上就能看到 busybox 的日志。

image

Kibana 也提供了強(qiáng)大的查詢功能,比如輸入關(guān)鍵字busybox 能搜索出所有匹配的日志條目。

image

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容