Skip to content

Docker Compose 部署 Prometheus Grafana 系统监控系统

简介

部署

拉取代码

shell
git clone -b master https://github.com/stefanprodan/dockprom prom
# 拉取代码

创建 cadday 密码

shell
docker run --rm caddy caddy hash-password --plaintext 'youtpasswd'
# 获取 hash 密码
JDJhJDE0JHJmeldKeDB0NjJnY0tjQk5wZXVYaC50QjdSbi9aVlVZeFYyV012UkI2YklMaHhTdi5LSkxx

配置 docker-compose.yml

volumes

创建数据目录

shell
cd prom
mkdir prometheus_data grafana_data
# 创建数据目录

修改docker-compose.yml中顶级卷配置到指定目录用于存储数据

yml
volumes:
    prometheus_data:
      driver: local
      driver_opts:
        type: 'none'
        o: 'bind'
        device: '/root/prom/prometheus_data'
    grafana_data:
      driver: local
      driver_opts:
        type: 'none'
        o: 'bind'
        device: '/root/prom/grafana_data'

配置 grafana 密码

yml
    environment:
      - GF_SECURITY_ADMIN_USER=${ADMIN_USER:-admin}
      - GF_SECURITY_ADMIN_PASSWORD=${ADMIN_PASSWORD:-yourpasswd}
      - GF_USERS_ALLOW_SIGN_UP=false

配置 caddy 密码

yml
    environment:
      - ADMIN_USER=${ADMIN_USER:-admin}
      - ADMIN_PASSWORD=${ADMIN_PASSWORD:-admin}
      - ADMIN_PASSWORD_HASH=${ADMIN_PASSWORD_HASH:-JDJhJDE0JHJmeldKeDB0NjJnY0tjQk5wZXVYaC50QjdSbi9aVlVZeFYyV012UkI2YklMaHhTdi5LSkxx}

配置 prometheus.yml

?> 使用file_sd_configs配置独立配置文件路径,方便随时修改配置而不需要重启容器,并且方便批量分类管理.

创建独立配置目录

./prometheus创建jobs目录,创建独立配置文件:

  • nodeexporter.yml
  • cadvisor.yml
  • prometheus.yml
  • pushgateway.yml

1

独立配置文件

参考示例./prometheus/jobs/cadvisor.yml

yml
- targets: 
  - "cadvisor:8080"
  labels: 
    hostname: Server
- targets: 
  - "127.127.127.127:8080"
  labels: 
    hostname: Server1
- targets: 
  - "127.127.127.128:8080"
  labels: 
    hostname: Server2
- targets: 
  - "127.127.127.129:8080"
  labels: 
    hostname: Server3

prometheus.yml 配置

参考修改独立配置文件路径 ./prometheus/prometheus.yml

yml
scrape_configs:
  - job_name: 'nodeexporter'
    file_sd_configs:
      - files:
        - "/etc/prometheus/jobs/nodeexporter.yml"
        refresh_interval: 5s

  - job_name: 'cadvisor'
    file_sd_configs:
      - files:
        - "/etc/prometheus/jobs/cadvisor.yml"
        refresh_interval: 5s

  - job_name: 'prometheus'
    file_sd_configs:
      - files:
        - "/etc/prometheus/jobs/prometheus.yml"
        refresh_interval: 5s

  - job_name: 'pushgateway'
    file_sd_configs:
      - files:
        - "/etc/prometheus/jobs/pushgateway.yml"
        refresh_interval: 5s

客户端配置

docker-compose

!> 防火墙需开放以下相关端口

docker-compose.yml参考示例

yml
version: '3.2'
services:
  nodeexporter:
    image: prom/node-exporter:v1.1.2
    container_name: nodeexporter
    volumes:
      - /proc:/host/proc:ro
      - /sys:/host/sys:ro
      - /:/rootfs:ro
    ports:
      - 9100:9100
    environment:
      TZ: Asia/Shanghai
    command:
      - '--path.procfs=/host/proc'
      - '--path.rootfs=/rootfs'
      - '--path.sysfs=/host/sys'
      - '--collector.filesystem.ignored-mount-points=^/(sys|proc|dev|host|etc)($$|/)'
    labels:
      org.label-schema.group: "monitoring"
    restart: always
    

  cadvisor:
    image: gcr.io/cadvisor/cadvisor:v0.39.0
    container_name: cadvisor
    volumes:
      - /:/rootfs:ro
      - /var/run:/var/run:rw
      - /sys:/sys:ro
      - /var/lib/docker:/var/lib/docker:ro
      #- /cgroup:/cgroup:ro #doesn't work on MacOS only for Linux
    ports:
      - 8080:8080
    environment:
      TZ: Asia/Shanghai
    labels:
      org.label-schema.group: "monitoring"
    restart: always