Prometheus部署监控容器

Prometheus搭建实例

    1. Prometheus Server

         Prometheus Server 本身也将以容器的方式运行在 host 192.168.11.133 上

    2. Exporter

         Prometheus 有很多现成的 Exporter,完整列表请参考 https://prometheus.io/docs/instrumenting/exporters/

        使用:

                Node Exporter   负责收集 host 硬件和操作系统数据

                cAdvisor            负责收集容器数据

    3.Grafana

        显示多维数据,Grafana 本身也将以容器方式运行在 host 192.168.11.133 上

 

1.安装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)($|/)"


#Node Exporter 启动后,将通过 9100 提供 host 的监控数据
http://192.168.11.133:9100/metrics 
node exporter安装

 

2.安装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



[root@node3 ~]# mount -o remount,rw '/sys/fs/cgroup'
[root@node3 ~]# ln -s /sys/fs/cgroup/cpu,cpuacct /sys/fs/cgroup/cpuacct,cpu
cAdvisor安装

 

3.选择一台主机安装Prometheus Server

docker run -d -p 9090:9090 \
-v /root/prometheus.yml:/etc/prometheus/prometheus.yml \
--name prometheus \
--net=host \
prom/prometheus
Prometheus server安装
global:
  scrape_interval:     15s # By default, scrape targets every 15 seconds.

  # Attach these labels to any time series or alerts when communicating with
  # external systems (federation, remote storage, Alertmanager).
  external_labels:
    monitor: 'codelab-monitor'

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

    # Override the global default and scrape targets from this job every 5 seconds.
    scrape_interval: 5s

    static_configs:
      - targets: ['localhost:9090','localhost:8080','localhost:9100','192.168.11.134:8080','192.168.11.134:9100']
prometheus.yml

 static_configs:   指定从哪些 exporter 抓取数据.这里指定了两台 host 上的 Node Exporter 和 cAdvisor

 

 

   所有 Target 的 State 都是 UP,说明 Prometheus Server 能够正常获取监控数据

 

4.安装Grafana  选择和Prometheus Server同一台主机

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

-e "GF_SECURITY_ADMIN_PASSWORD=secret 指定了 Grafana admin用户密码 secret
grafana安装

 

5.使用Grafana展示Prometheus Server的数据

    

1.添加data source

    

2.下载dashboard

    访问  https://grafana.com/dashboards?dataSource=prometheus&search=docker  下载这些现成的 Dashboard 每个Dashboard就是一个json文件

    

3.添加dashboard

     

     

    

docker启动prometheus异常

           open /prometheus/queries.active: permission denied  prometheus镜像中默认包含nobody用户,docker run的时候默认用nobody用户来做为启动用户

          

 

          docker run -it -u user_name --name container_name -d image_name /bin/bash

          docker run -u root --privileged=true  prometheus

posted @ 2018-09-14 14:47  不懂123  阅读(1202)  评论(0编辑  收藏  举报