docker-compose部署grafa+ prometheus监控

16 阅读2分钟

一、目录结构

1.1、请按照如下结构新建好目录和文件

/rmq
├── docker-compose.yml
├── grafa/
  └── data/
├── prometheus/
  └── prometheus.yml
└── mysql_exporter/
  └── .my.cnf

1.2、为docker添加目录的读写权限

chmod 777 -R ./*

docker-compose.yml

prometheus查看exporter列表 http://{IP}:9090/targets

grafana默认登录账号admin和密码admin

services:
  prometheus:
    image: prom/prometheus:latest
    container_name: prometheus
    volumes:
      - /etc/localtime:/etc/localtime:ro # 同步宿主机时间到容器
      - ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
    ports:
      - 9090:9090
    restart: always
    networks:
      - dev_net

  grafana:
    image: grafana/grafana:latest
    container_name: grafana
    ports:
      - 3000:3000
    volumes:
      - /etc/localtime:/etc/localtime:ro # 同步宿主机时间到容器
      - ./grafana/data:/var/lib/grafana
    restart: always
    networks:
      - dev_net

  redis_exporter:
    image: oliver006/redis_exporter:latest
    container_name: redis_exporter
    ports:
      - 9121:9121
    volumes:
      - /etc/localtime:/etc/localtime:ro # 同步宿主机时间到容器
    environment:
      - REDIS_ADDR=redis://:redis123@redis:6379
    restart: always
    networks:
      - dev_net

  node_exporter:
    image: prom/node-exporter:latest
    container_name: node_exporter
    ports:
      - 9100:9100
    volumes:
      - /etc/localtime:/etc/localtime:ro # 同步宿主机时间到容器
    restart: always
    networks:
      - dev_net
  
  mysqld_exporter:
    image: prom/mysqld-exporter:latest
    container_name: mysqld_exporter
    volumes:
      - /etc/localtime:/etc/localtime:ro # 同步宿主机时间到容器
      - ./mysql_exporter/.my.cnf:/.my.cnf:ro
    ports:
      - 9104:9104
    restart: always
    networks:
      - dev_net
networks:
  dev_net:
    external: true

prometheus.yml

scrape_configs:
  - job_name: 'redis-exporter'
    static_configs:
      - targets: ['192.168.124.11:9121']
  - job_name: 'node-exporter'
    static_configs:
      - targets: ['192.168.124.11:9100']
  - job_name: 'mysqld-exporter'
    static_configs:
      - targets: ['192.168.124.11:9104']

.my.cnf

[client]
# 这里使用的容器名称。所有容器都指定在同一 network 中,docker 允许处在同一网络环境的容器使用容器名称项目访问。另外,容器启动时会随机分配 IP,所以这里使用容器名称最合适。当然也可以容器启动时,指定的 IP
host=mysql
port=3306
# mysql 用户名
user=root
# mysql 用户密码
password=mysql123

grafa常用模板

  • redis监控模板:763 或 11835
  • 系统监控模板:1860
  • mysql监控模板:7362 或 14057