日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Prometheus+Grafana監(jiān)控Docker容器和Linux主機

作者:GottdesKrieges 更新時間: 2022-07-02 編程語言

Prometheus+Grafana監(jiān)控Docker容器和Linux主機

  • Prometheus概述
  • Prometheus部署
  • Prometheus配置文件
    • prometheus.yml
    • scrape_configs
  • 使用Prometheus監(jiān)控docker容器
    • 使用cAdvisor采集監(jiān)控數(shù)據(jù)(被監(jiān)控端)
    • 修改prometheus配置文件
    • 使用Grafana可視化監(jiān)控數(shù)據(jù)
  • 使用Prometheus監(jiān)控Linux主機
    • 安裝node exporter(被監(jiān)控端)
    • 修改prometheus配置文件
    • 使用Grafana可視化監(jiān)控數(shù)據(jù)

Prometheus概述

Prometheus發(fā)布于2016年,比zabbix晚了4年。雖然其社區(qū)支持暫時不如后者完善,但是對容器的支持更好,不僅支持swarm原生群集,還支持kubernetes容器集群的監(jiān)控。

Prometheus的特點包括:

  • 多維數(shù)據(jù)模型:由度量名稱和鍵值對標識的時間序列數(shù)據(jù);
  • PromQL:一種靈活的查詢語言,可以利用多維數(shù)據(jù)完成復雜查詢;
  • 不依賴分布式存儲,單個服務器節(jié)點可直接工作;
  • 基于HTTP的pull方式采集時間序列數(shù)據(jù);
  • 支持通過PushGateway組件推送時間序列數(shù)據(jù);
  • 通過服務發(fā)現(xiàn)(service discovery)或靜態(tài)配置發(fā)現(xiàn)目標;
  • 多種圖形模式及儀表盤支持(grafana)。

架構
Prometheus+Grafana的架構如下:

在這里插入圖片描述

組件

主要組件 功能
Prometheus Server 收集指標和存儲時間序列數(shù)據(jù),并提供查詢接口
ClientLibrary 客戶端庫
Push Gateway 短期存儲指標數(shù)據(jù),主要用于臨時性任務
Exporters 采集已有的第三方服務監(jiān)控指標并暴露metrics
Alertmanager 處理告警
Web UI 簡單的Web控制臺

實例和作業(yè)
實例(Instance)和作業(yè)(Job)是Prometheus中的兩個重要概念。實例是指可以抓取的目標(即被監(jiān)控端,通常是單個的進程),而作業(yè)則是指具有相同目標的實例的集合。

Prometheus部署

Prometheus有四種常見的部署方法:

  • 使用預編譯的二進制文件(pre-compiled binaries)安裝;
  • 通過源碼編譯(from source);
  • 使用docker部署;
  • 通過配置管理系統(tǒng)安裝:比如Ansible、Chef、Puppet和SaltStack。

下面主要介紹第三種方法(最簡單最方便),其它方法參見 https://prometheus.io/docs/prometheus/latest/installation/

使用docker部署
需要準備配置文件prometheus.yml(見后面的內容)。

$ docker run -d -p 9090:9090 prom/prometheus  # 最簡單的運行命令
# 運行并綁定掛載主機上的配置文件prometheus.yml
$ docker run -d --name=prometheus -p 9090:9090 \
-v /path/to/prometheus.yml:/etc/prometheus/prometheus.yml \  
prom/prometheus
# 訪問http://主機IP:9090

Prometheus配置文件

prometheus.yml

Prometheus的配置語言用YAML語言編寫,其格式一般如下:

# global關鍵字用于定義全局參數(shù)
global:
  # 定義從被監(jiān)控目標采集數(shù)據(jù)的頻率,默認為1分鐘
  [ scrape_interval: <duration> | default = 1m ]

  # 定義數(shù)據(jù)采集請求的超時時間,默認為10秒
  [ scrape_timeout: <duration> | default = 10s ]

  # 評估規(guī)則(告警檢測)的頻率,默認為1分鐘
  [ evaluation_interval: <duration> | default = 1m ]

  # 跟外部系統(tǒng)(federation, remote storage, Alertmanager)通信時向任意時間序列或告警添加的標簽
  external_labels:
    [ <labelname>: <labelvalue> ... ]

  # 記錄PromQL查詢語句的文件。重新加載配置會重新打開此文件
  [ query_log_file: <string> ]

# rule_files定義了告警規(guī)則文件列表
rule_files:
  [ - <filepath_glob> ... ]

# scrape_configs定義了監(jiān)控數(shù)據(jù)抓取的配置列表
scrape_configs:
  [ - <scrape_config> ... ]

# alerting定義了與Alertmanager相關的告警配置
alerting:
  alert_relabel_configs:
    [ - <relabel_config> ... ]
  alertmanagers:
    [ - <alertmanager_config> ... ]

# remote_write和remote_read定義了遠程讀寫的相關配置
remote_write:
  [ - <remote_write> ... ]

remote_read:
  [ - <remote_read> ... ]

scrape_configs

下面主要介紹scrape_configs的配置參數(shù),它定義了監(jiān)控目標(target)的集合并描述了從監(jiān)控目標采集數(shù)據(jù)的相關參數(shù)。監(jiān)控目標可以通過static_configs關鍵字配置,也可以通過使用服務發(fā)現(xiàn)機制來動態(tài)地發(fā)現(xiàn)。使用relabel_configs關鍵字可以在采集數(shù)據(jù)前對任意監(jiān)控目標及其標簽(label)進行進一步的修改。

# 數(shù)據(jù)采集作業(yè)的名稱
job_name: <job_name>

# 數(shù)據(jù)采集的頻率,默認為對應的全局配置
[ scrape_interval: <duration> | default = <global_config.scrape_interval> ]

# 數(shù)據(jù)采集的超時時間,默認為對應的全局配置
[ scrape_timeout: <duration> | default = <global_config.scrape_timeout> ]

# 從監(jiān)控目標獲取指標的HTTP資源路徑
[ metrics_path: <path> | default = /metrics ]

# 當采集的監(jiān)控數(shù)據(jù)的標簽與服務器端配置的不一樣時,如果honor_labels設置為false,則以服務器端為準,否則以采集數(shù)據(jù)的標簽為準
[ honor_labels: <boolean> | default = false ]

# honor_timestamps設置為true時,使用采集的監(jiān)控數(shù)據(jù)的時間戳
[ honor_timestamps: <boolean> | default = true ]

# 監(jiān)控數(shù)據(jù)采集請求使用的協(xié)議,默認為http
[ scheme: <scheme> | default = http ]

# 可選的HTTP URL參數(shù)
params:
  [ <string>: [<string>, ...] ]

# 使用配置的用戶名和密碼設置每個數(shù)據(jù)采集請求的Authorization頭部標簽。其中password與password_file配置互斥
basic_auth:
  [ username: <string> ]
  [ password: <secret> ]
  [ password_file: <string> ]

# 使用bear token設置每個數(shù)據(jù)采集請求的Authorization頭部標簽。與bearer_token_file配置互斥
[ bearer_token: <secret> ]

# 使用bear token設置每個數(shù)據(jù)采集請求的Authorization頭部標簽。與bearer_token配置互斥
[ bearer_token_file: <filename> ]

# 數(shù)據(jù)采集請求的TLS配置(安全傳輸層協(xié)議)
tls_config:
  [ <tls_config> ]

# 可選的代理URL.
[ proxy_url: <string> ]

# Azure服務發(fā)現(xiàn)的配置清單
azure_sd_configs:
  [ - <azure_sd_config> ... ]

# Consul服務發(fā)現(xiàn)的配置清單
consul_sd_configs:
  [ - <consul_sd_config> ... ]

# DigitalOcean服務發(fā)現(xiàn)的配置清單
digitalocean_sd_configs:
  [ - <digitalocean_sd_config> ... ]

# Docker Swarm服務發(fā)現(xiàn)的配置清單
dockerswarm_sd_configs:
  [ - <dockerswarm_sd_config> ... ]

# DNS服務發(fā)現(xiàn)的配置清單
dns_sd_configs:
  [ - <dns_sd_config> ... ]

# EC2服務發(fā)現(xiàn)的配置清單
ec2_sd_configs:
  [ - <ec2_sd_config> ... ]

# Eureka服務發(fā)現(xiàn)的配置清單
eureka_sd_configs:
  [ - <eureka_sd_config> ... ]

# file服務發(fā)現(xiàn)的配置清單
file_sd_configs:
  [ - <file_sd_config> ... ]

# GCE服務發(fā)現(xiàn)的配置清單
gce_sd_configs:
  [ - <gce_sd_config> ... ]

# Hetzner服務發(fā)現(xiàn)的配置清單
hetzner_sd_configs:
  [ - <hetzner_sd_config> ... ]

# Kubernetes服務發(fā)現(xiàn)的配置清單
kubernetes_sd_configs:
  [ - <kubernetes_sd_config> ... ]

# Marathon服務發(fā)現(xiàn)的配置清單
marathon_sd_configs:
  [ - <marathon_sd_config> ... ]

# AirBnB's Nerve服務發(fā)現(xiàn)的配置清單
nerve_sd_configs:
  [ - <nerve_sd_config> ... ]

# OpenStack服務發(fā)現(xiàn)的配置清單
openstack_sd_configs:
  [ - <openstack_sd_config> ... ]

# Zookeeper Serverset服務發(fā)現(xiàn)的配置清單
serverset_sd_configs:
  [ - <serverset_sd_config> ... ]

# Triton服務發(fā)現(xiàn)的配置清單
triton_sd_configs:
  [ - <triton_sd_config> ... ]

# 為當前作業(yè)靜態(tài)配置的監(jiān)控目標清單.
static_configs:
  [ - <static_config> ... ]

# 監(jiān)控目標relabel重新設定標簽配置
relabel_configs:
  [ - <relabel_config> ... ]

# 指標relabel配置清單
metric_relabel_configs:
  [ - <relabel_config> ... ]

# 監(jiān)控數(shù)據(jù)采樣的數(shù)目限制,默認為0表示無限制
[ sample_limit: <int> | default = 0 ]

# 數(shù)據(jù)采集的監(jiān)控目標的個數(shù)限制,默認為0表示無限制
[ target_limit: <int> | default = 0 ]

其他參數(shù)的配置參見 https://prometheus.io/docs/prometheus/latest/configuration/configuration/

詳細配置可以參考示例:https://github.com/prometheus/prometheus/blob/release-2.24/documentation/examples/prometheus-dockerswarm.yml。

使用Prometheus監(jiān)控docker容器

常用的監(jiān)控指標有內存、CPU、磁盤、網絡。一般使用Google開源的cAdvisor (Container Advisor)來收集正在運行的容器資源信息和性能信息(參見 https://github.com/google/cadvisor)。

使用cAdvisor采集監(jiān)控數(shù)據(jù)(被監(jiān)控端)

使用Docker部署cadvisor。

$ docker run -d --volume=/:/rootfs:ro \
--volume=/var/run:/var/run:ro --volume=/sys:/sys:ro \
--volume=/var/lib/docker/:/var/lib/docker:ro \
--volume=/dev/disk/:/dev/disk:ro \
--publish=8080:8080 --detach=true \
--name=cadvisor google/cadvisor:latest
# 訪問http://主機IP:8080

cAdvisor不負責存儲采集到的監(jiān)控數(shù)據(jù),因此只能查看實時數(shù)據(jù)。cAdvisor為Prometheus提供的數(shù)據(jù)采集接口為http://被監(jiān)控主機IP:8080/metrics

修改prometheus配置文件

在prometheus.yml的scrape_configs中添加監(jiān)控作業(yè)。

- job_name: "my_docker"
  static_configs:
  - targets: ['被監(jiān)控主機IP:8080']

然后重啟容器:docker restart prometheus。可以在http://主機IP:9090/targets 頁面可以查看prometheus監(jiān)控的目標。在http://主機IP:9090/graph 頁面可以查看監(jiān)控參數(shù)的簡單圖形化展示。

使用Grafana可視化監(jiān)控數(shù)據(jù)

Grafana則是一個開源的度量分析和可視化系統(tǒng)(https://grafana.com/docs/)。它比Prometheus自帶的圖形化展示功能更強大。

Docker部署Grafana

$ docker run -d --name=grafana -p 3000:3000 grafana/grafana
# 訪問http://主機IP:3000,默認用戶名和密碼都是admin,登錄并修改密碼

添加數(shù)據(jù)源
在添加數(shù)據(jù)源中選擇Prometheus,在HTTP下的URL欄中粘貼http://Prometheus主機IP:9090并保存。

創(chuàng)建儀表盤(dashboard)
在新建儀表盤(New dashboard)中點擊右側的導入儀表盤(Import dashboard),輸入并搜索193(對應儀表盤名稱為docker monitoring),在顯示的儀表盤選項(Options)中選擇數(shù)據(jù)源為Prometheus,最后點擊導入即可。

在這里插入圖片描述

https://grafana.com/grafana/dashboards 上可以發(fā)現(xiàn)更多官方和社區(qū)提供的儀表盤。

使用Prometheus監(jiān)控Linux主機

Prometheus監(jiān)控Linux主機需要在被監(jiān)控目標上安裝Node Exporter。在目標主機上執(zhí)行以下腳本來部署node exporter。
(參見 https://prometheus.io/docs/guides/node-exporter/#monitoring-linux-host-metrics-with-the-node-exporter

安裝node exporter(被監(jiān)控端)

#!/bin/bash

wget https://github.com/prometheus/node_exporter/releases/download/v0.17.0/node_exporter-0.17.0.linux-amd64.tar.gz

tar zxf node_exporter-0.17.0.linux-amd64.tar.gz
mv node_exporter-0.17.0.linux-amd64 /usr/local/node_exporter

cat << EOF > /usr/lib/systemd/system/node_exporter.service
[Unit]
Description=https://prometheus.io

[Service]
Restart=on-failure
ExecStart=/usr/local/node_exporter/node_exporter

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable node_exporter
systemctl restart node_exporter

安裝完成以后使用ps -ef | grep node_exporter查看是否正常運行。Node exporter默認會使用9100端口,為Prometheus提供的數(shù)據(jù)采集接口為http://被監(jiān)控主機IP:9100/metrics

修改prometheus配置文件

在prometheus.yml的scrape_configs中添加監(jiān)控作業(yè)。

- job_name: "my_linux"
  static_configs:
  - targets: ['被監(jiān)控主機IP:9100']

然后重啟容器:docker restart prometheus。可以在http://主機IP:9090/targets 頁面可以查看prometheus監(jiān)控的目標。在http://主機IP:9090/graph 頁面可以查看監(jiān)控參數(shù)的簡單圖形化展示。

使用Grafana可視化監(jiān)控數(shù)據(jù)

Grafana提供的9276儀表盤可以很好地可視化Linux主機基礎監(jiān)控信息。參照上文中Grafana可視化容器監(jiān)控的儀表盤導入操作。

在這里插入圖片描述

原文鏈接:https://blog.csdn.net/Sebastien23/article/details/113645177

欄目分類
最近更新