Leif160519的blog Leif160519的blog

——————

目录
Docker监控普罗米修斯
/      

Docker监控普罗米修斯

主机名称IP地址安装组件
DockerMachine192.168.116.1311. Prometheus Server(普罗米修斯监控主服务器 )
2. Node Exporter (收集Host硬件和操作系统信息)
3. cAdvisor (负责收集Host上运行的容器信息)
4. Grafana (展示普罗米修斯监控界面)
Docker01192.168.116.1321. Node Exporter (收集Host硬件和操作系统信息)
2. cAdvisor (负责收集Host上运行的容器信息)
Docker02192.168.116.1331. Node Exporter (收集Host硬件和操作系统信息)
2. cAdvisor (负责收集Host上运行的容器信息)

1.安装Node Exporter 来收集硬件信息
所有节点运行以下命令安装Node Exporter 容器

docker run -d -p 9100:9100 \
  -v "/proc:/host/proc" \
  -v "/sys:/host/sys" \
  -v "/:/rootfs" \
  --net=host \
  prom/node-exporter \
  --path.procfs /host/proc \
  --path.sysfs /host/sys \
  --collector.filesystem.ignored-mount-points "^/(sys|proc|dev|host|etc)($|/)"

注意,这里我们使用了--net=host,这样 Prometheus Server 可以直接与 Node Exporter 通信
image.png

  1. Node Exporter 启动后,将通过 9100 提供 host 的监控数据。在浏览器中通过 http://192.168.116.131:9100/metrics 测试一下。
    image.png

  2. 安装cAdvisor 来收集容器信息 所有节点运行以下命令来安装cAdvisor

docker run \
  --volume=/:/rootfs:ro \
  --volume=/var/run:/var/run:rw \
  --volume=/sys:/sys:ro \
  --volume=/var/lib/docker/:/var/lib/docker:ro \
  --publish=8080:8080 \
  --detach=true \
  --name=cadvisor \
  --net=host \
  google/cadvisor:latest

注意,这里我们使用了 --net=host,这样 Prometheus Server 可以直接与 cAdvisor 通信。

  1. cAdvisor 启动后,将通过 8080 提供 host 的监控数据。在浏览器中通
    在浏览器中通过 http://192.168.116.132:8080/metrics 测试一下。
    image.png

5.在Dockermachine服务器上安装普罗米修斯服务
安装之前需要将普罗米修斯的启动文件创建好, 在/root目录下创建prometheus.yml文件文件内容如下:主要修改targets部分

# 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).
# Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"
# 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','localhost:8080','localhost:9100','192.168.116.133:8080','192.168.116.133:9100','192.168.116.132:8080','192.168.116.132:9100']

指定从哪些 exporter 抓取数据。这里指定了两台 host 上的 Node Exporter 和 cAdviso

另外 localhost:9090 就是 Prometheus Server 自己,可见 Prometheus 本身也会收集自己的监控数据。同样地,我们也可以通

  1. 运行以下命令安装普罗米修斯服务
docker run -d -p 9090:9090 \
  -v /root/prometheus.yml:/etc/prometheus/prometheus.yml \
  --name prometheus \
  --net=host \
  prom/prometheus

注意,这里我们使用了--net=host,这样 Prometheus Server 可以直接与 Exporter 和 Grafana 通信。
prometheus.yml 是 Prometheus Server 的配置文件

  1. 安装完毕后浏览器中打开http://192.168.116.131:9090 点击菜单 status ----> target,状态显示为up,说明普罗米修斯服务能够正常获取监控数据,点击endpoint可以查看收集的详细信息由于没有安装UI 服务器Granfana所以,都是枯燥的字母
    image.png

  2. 在DockerMachine上运行Grafana

docker run -d -i -p 3000:3000 \
  -e "GF_SERVER_ROOT_URL=http://grafana.server.name”  \
  -e "GF_SECURITY_ADMIN_PASSWORD=secret" \
  --net=host \
  grafana/grafana

注意,这里我们使用了 --net=host,这样 Grafana 可以直接与 Prometheus Server 通信。
-e "GF_SECURITY_ADMIN_PASSWORD=secret 指定了 Grafana admin用户密码 secret

  1. 现在查看各个Host上都运行了那些容器 Dockermachine: 共4个 Grafana,Prometheus, cadvisor, node-exporter
    image.png
    Docker01: 共2个 cAdvisor,node-exporter
    image.png
    Docker02:共2个 cAdvisor, node-exporter
    image.png

  2. Grafana启动后,在浏览器中打开http://192.168.116.131:3000 登录界面好炫啊----尤其是这种骚蓝背景,登录用admin 密码为刚创建Grafana时的secret
    image.png

  3. 真是逼格满满,并且很人性化,按照向导操作添加DataSource
    image.png

  4. 添加普罗米修斯服务器,重点看框部分,其它默认即可
    image.png
    点击Save & Test 如果一切顺利该DataSource可以正常工作了,也就是说Grafana可以正常跟Prometheus正常通信了,name怎么展示数据呢,答案是通过dashboard.
    image.png

  5. 自己手工创建dashboard有点困难,可以借助开元的力量访问 https://grafana.com/dashboards?dataSource=prometheus&search=docker,将会看到很多用于监控 Docker 的 Dashboard。
    image.png

  6. 有些dashboard可以下载后直接导入,而有些需要修改后再导入,需要看dashboard的overview
    image.png

  7. 例如导入Docker and system monitoring dashboard
    image.png

  8. 美好的画面立刻呈现了
    image.png

17.部署完成了,基本的dashboard可以通过开元网站下载,若果结合本地环境还还得需要深入研究,另外普罗米修斯强大的告警功能在这里受限于自己knowledge待下回分解了。
详见 http://docs.grafana.org/alerting/rules/


“The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.” – Tom Cargill

标  题Docker监控普罗米修斯
作  者Leif160519
出  处https://github.icu/articles/2019/10/08/1570520164756.html
关于博主:坐标南京,运维工程师,如有问题探讨可以直接下方留言。
声援博主:如果您觉得文章对您有帮助,可以评论、订阅、收藏。您的鼓励是博主的最大动力!