?
? ????? PHP ??? ???? ??? ?? ??
Prometheus 是一個開源系統(tǒng)監(jiān)控和警報工具包。您可以將 Docker 配置為 Prometheus 目標。本主題向您展示如何配置 Docker,設置 Prometheus 作為 Docker 容器運行,并使用 Prometheus 監(jiān)控 Docker 實例。
警告:可用指標和這些指標的名稱正在積極開發(fā)中,并可能隨時更改。
目前,您只能監(jiān)控 Docker 本身。您目前無法使用 Docker 目標監(jiān)控您的應用程序。
要將 Docker 守護程序配置為 Prometheus 目標,您需要指定metrics-address
。最好的方法是通過daemon.json
默認情況下位于以下位置之一的。如果該文件不存在,請創(chuàng)建它。
Linux:/etc/docker/daemon.json
Windows 服務器:C:\ProgramData\docker\config\daemon.json
適用于 Mac 的 Docker for Mac / Docker:單擊工具欄中的 Docker 圖標,選擇 Preferences,然后選擇 Daemon。點擊高級。
如果該文件當前為空,請粘貼以下內(nèi)容:
{ "metrics-addr" : "127.0.0.1:9323", "experimental" : true}
如果文件不為空,請?zhí)砑舆@兩個鍵,確保生成的文件是有效的JSON。請注意,除了最后一行,每行都以逗號(,
)結(jié)尾。
保存文件,或者在 Docker for Mac 或 Docker for Windows 的情況下,保存配置。重新啟動 Docker。
Docker 現(xiàn)在在端口9323上公開 Prometheus 兼容指標。
在此示例中,Prometheus 在同一主機上作為 Docker 容器運行。
復制以下配置文件之一并將其保存到/tmp/prometheus.yml
。這是一個普通存儲 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']
接下來,使用此配置啟動單個副本 Prometheus 服務。
$ 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
驗證 Docker 目標是否在http:// localhost:9090 / targets / 中列出。
如果您使用 Docker for Mac 或 Docker for Windows,則無法直接訪問端點 URL。
創(chuàng)建一個圖。點擊 Prometheus UI 中的圖表鏈接。從“ 執(zhí)行”按鈕右側(cè)的組合框中選擇一個度量標準,然后單擊“ 執(zhí)行”。以下屏幕截圖顯示了圖表engine_daemon_network_actions_seconds_count
。
上圖顯示了一個相當閑置的 Docker 實例。如果您正在運行活動工作負載,則您的圖形可能看起來不同
為了使圖表更有趣,可以通過啟動一項服務來創(chuàng)建一些網(wǎng)絡操作,其中包括10個任務,它們可以不停地 ping Docker(您可以將 ping 目標更改為任何您喜歡的任務):
$ docker service create \ --replicas 10 \ --name ping_service \ alpine ping docker.com
等待幾分鐘(默認刮取間隔為15秒)并重新加載圖形。
準備就緒后,請停止并刪除ping_service
服務,以免無緣無故地淹沒有ping的主機。
$ docker service remove ping_service
等待幾分鐘,你會發(fā)現(xiàn)圖表回落到閑置狀態(tài)。
閱讀 Prometheus 文檔
設置一些警報