?
This document uses PHP Chinese website manual Release
Prometheus 是一個(gè)開源系統(tǒng)監(jiān)控和警報(bào)工具包。您可以將 Docker 配置為 Prometheus 目標(biāo)。本主題向您展示如何配置 Docker,設(shè)置 Prometheus 作為 Docker 容器運(yùn)行,并使用 Prometheus 監(jiān)控 Docker 實(shí)例。
警告:可用指標(biāo)和這些指標(biāo)的名稱正在積極開發(fā)中,并可能隨時(shí)更改。
目前,您只能監(jiān)控 Docker 本身。您目前無(wú)法使用 Docker 目標(biāo)監(jiān)控您的應(yīng)用程序。
要將 Docker 守護(hù)程序配置為 Prometheus 目標(biāo),您需要指定metrics-address
。最好的方法是通過(guò)daemon.json
默認(rèn)情況下位于以下位置之一的。如果該文件不存在,請(qǐng)創(chuàng)建它。
Linux:/etc/docker/daemon.json
Windows 服務(wù)器:C:\ProgramData\docker\config\daemon.json
適用于 Mac 的 Docker for Mac / Docker:?jiǎn)螕艄ぞ邫谥械?Docker 圖標(biāo),選擇 Preferences,然后選擇 Daemon。點(diǎn)擊高級(jí)。
如果該文件當(dāng)前為空,請(qǐng)粘貼以下內(nèi)容:
{ "metrics-addr" : "127.0.0.1:9323", "experimental" : true}
如果文件不為空,請(qǐng)?zhí)砑舆@兩個(gè)鍵,確保生成的文件是有效的JSON。請(qǐng)注意,除了最后一行,每行都以逗號(hào)(,
)結(jié)尾。
保存文件,或者在 Docker for Mac 或 Docker for Windows 的情況下,保存配置。重新啟動(dòng) Docker。
Docker 現(xiàn)在在端口9323上公開 Prometheus 兼容指標(biāo)。
在此示例中,Prometheus 在同一主機(jī)上作為 Docker 容器運(yùn)行。
復(fù)制以下配置文件之一并將其保存到/tmp/prometheus.yml
。這是一個(gè)普通存儲(chǔ) Prometheus 配置文件,除了在文件底部添加 Docker 作業(yè)定義之外。Docker for Mac 和 Docker for Windows 需要稍微不同的配置。
Docker for Linux
Docker for Mac 或 Windows
# my global config global: scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute. evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute. # scrape_timeout is set to the global default (10s). # Attach these labels to any time series or alerts when communicating with # external systems (federation, remote storage, Alertmanager). external_labels: monitor: 'codelab-monitor'# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.rule_files: # - "first.rules" # - "second.rules"# A scrape configuration containing exactly one endpoint to scrape:# Here it's Prometheus itself.scrape_configs: # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config. - job_name: 'prometheus' # metrics_path defaults to '/metrics' # scheme defaults to 'http'. static_configs: - targets: ['localhost:9090'] - job_name: 'docker' # metrics_path defaults to '/metrics' # scheme defaults to 'http'. static_configs: - targets: ['localhost:9323']
# my global config global: scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute. evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute. # scrape_timeout is set to the global default (10s). # Attach these labels to any time series or alerts when communicating with # external systems (federation, remote storage, Alertmanager). external_labels: monitor: 'codelab-monitor'# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.rule_files: # - "first.rules" # - "second.rules"# A scrape configuration containing exactly one endpoint to scrape:# Here it's Prometheus itself.scrape_configs: # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config. - job_name: 'prometheus' # metrics_path defaults to '/metrics' # scheme defaults to 'http'. static_configs: - targets: ['localhost:9090'] - job_name: 'docker' # metrics_path defaults to '/metrics' # scheme defaults to 'http'. static_configs: - targets: ['192.168.65.1:9323']
接下來(lái),使用此配置啟動(dòng)單個(gè)副本 Prometheus 服務(wù)。
$ docker service create --replicas 1 --name my-prometheus \ --mount type=bind,source=/tmp/prometheus.yml,destination=/etc/prometheus/prometheus.yml \ --publish 9090:9090/tcp \ prom/prometheus
驗(yàn)證 Docker 目標(biāo)是否在http:// localhost:9090 / targets / 中列出。
如果您使用 Docker for Mac 或 Docker for Windows,則無(wú)法直接訪問(wèn)端點(diǎn) URL。
創(chuàng)建一個(gè)圖。點(diǎn)擊 Prometheus UI 中的圖表鏈接。從“ 執(zhí)行”按鈕右側(cè)的組合框中選擇一個(gè)度量標(biāo)準(zhǔn),然后單擊“ 執(zhí)行”。以下屏幕截圖顯示了圖表engine_daemon_network_actions_seconds_count
。
上圖顯示了一個(gè)相當(dāng)閑置的 Docker 實(shí)例。如果您正在運(yùn)行活動(dòng)工作負(fù)載,則您的圖形可能看起來(lái)不同
為了使圖表更有趣,可以通過(guò)啟動(dòng)一項(xiàng)服務(wù)來(lái)創(chuàng)建一些網(wǎng)絡(luò)操作,其中包括10個(gè)任務(wù),它們可以不停地 ping Docker(您可以將 ping 目標(biāo)更改為任何您喜歡的任務(wù)):
$ docker service create \ --replicas 10 \ --name ping_service \ alpine ping docker.com
等待幾分鐘(默認(rèn)刮取間隔為15秒)并重新加載圖形。
準(zhǔn)備就緒后,請(qǐng)停止并刪除ping_service
服務(wù),以免無(wú)緣無(wú)故地淹沒(méi)有ping的主機(jī)。
$ docker service remove ping_service
等待幾分鐘,你會(huì)發(fā)現(xiàn)圖表回落到閑置狀態(tài)。
閱讀 Prometheus 文檔
設(shè)置一些警報(bào)