---
# System prepended metadata

title: Docker 監控 Container cAdvisor & Prometheus
tags: [Docker]

---

# Docker 監控 Container cAdvisor & Prometheus
先建立一個專屬的network，因為本人使用Windows諸多狀況下Container都會選擇吃docker自己的ip根本機ip不同
```
docker network create --subnet=172.34.0.0/24 dockernetwork
```
## 建立cAdvisor Docker-Compose
```
cadvisor:
    image: google/cadvisor
    container_name: cadvisor
    volumes:
      - /:/rootfs:ro
      - /var/run:/var/run:rw
      - /sys:/sys:ro
      - /var/lib/docker/:/var/lib/docker:ro
    ports:
      - "8080:8080"
    restart: always 
    networks:
      mynetwork:
        ipv4_address: 172.34.0.2
```
## 建立Prometheus Docker-Compose
設置prometheus.yml
```
global:
  scrape_interval: 15s 
  evaluation_interval: 15s
alerting:
  alertmanagers:
    - static_configs:
        - targets:
rule_files:
scrape_configs:
  - job_name: "prometheus"
    static_configs:
      - targets: ["localhost:9090"]
  - job_name: "cadvisor"
    scrape_interval: 10s
    metrics_path: '/metrics'
    static_configs:
      - targets: ["172.34.0.3:8080"]
        labels:
          group: 'cadvisor'
```
建立Compose
```
prometheus:
    image: prom/prometheus
    container_name: prometheus
    ports:
      - "9091:9090"
    volumes:
      - ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
    restart: always 
    networks:
      mynetwork:
        ipv4_address: 172.34.0.3

//此處因為本機Windows中9090已被占用且無法更改，所以映射到9091
```

## 第一次測試
完整docker-compose
```
version: '3'
services:
  cadvisor:
    image: google/cadvisor
    container_name: cadvisor
    volumes:
      - /:/rootfs:ro
      - /var/run:/var/run:rw
      - /sys:/sys:ro
      - /var/lib/docker/:/var/lib/docker:ro
    ports:
      - "8080:8080"
    restart: always 
    networks:
      mynetwork:
        ipv4_address: 172.34.0.2

  prometheus:
    image: prom/prometheus
    container_name: prometheus
    ports:
      - "9091:9090"
    volumes:
      - ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
    restart: always 
    networks:
      mynetwork:
        ipv4_address: 172.34.0.3

networks:
  mynetwork:
    external:
      name: dockernetwork
```
進入docker-compose資料夾後
```
docker-compose up -d
```
檢視prometheus是否有找到prometheus跟cadvisor，若失敗請確認設定中的ip或確認Container是否開啟。
![螢幕擷取畫面 2023-11-08 100726.png](https://hackmd.io/_uploads/ByyOpwOXp.png)
## 建立Grafana Docker Compose
```
  grafana:
    image: grafana/grafana:8.2.7
    container_name: grafana
    ports:
      - "3000:3000"
    networks:
      mynetwork:
        ipv4_address: 172.34.0.4
```

## 最後測試
先登入Grafana 帳號密碼預設都是admin

#### 設定DataSource
![螢幕擷取畫面 2023-11-08 114301.png](https://hackmd.io/_uploads/ryRyEK_X6.png)

#### 開始建立DashBored
可以參考我建立針對Shadowsocks做偵測是否開啟的模板，用下方GitHub中的檔案來操作
[https://github.com/willie891031/Docker-Monitor-Container](https://)
使用DashBored.json在import時輸入
![螢幕擷取畫面 2023-11-08 143741.png](https://hackmd.io/_uploads/H1Uphid7p.png)

#### 在Grfana中設定SMTP Server
直接用Docker Desktop來編輯即可，檔案位置在./etc/grafana/grafana.ini 找到SMTP
```
#################################### SMTP / Emailing ##########################
[smtp]
enabled = true
host = smtp.gmail.com:465
user = your email
# If the password contains # or ; you have to wrap it with triple quotes. Ex """#password;"""
password = your email Password
cert_file = 
key_file =
skip_verify = true
from_address = your email
from_name = Grafana
# EHLO identity in SMTP dialog (defaults to instance_name)
ehlo_identity =
```

#### 設定Gmail
關閉「兩步驟驗證」並開啟「低安全性應用程式存取權」
![螢幕擷取畫面 2023-11-08 144714.png](https://hackmd.io/_uploads/HkXb1hd7p.png)

#### 建立Notification Channel
依照下圖設定Addresses填入要接收通知的Mail，若有多個Email需要通知請以;符號間隔
![螢幕擷取畫面 2023-11-08 151729.png](https://hackmd.io/_uploads/rJbXI3OQp.png)


按下Send Test ，便會送出測試郵件
![螢幕擷取畫面 2023-11-08 145159.png](https://hackmd.io/_uploads/SkbXl2dX6.png)

#### 設定警報(Alert)
編輯個別的Pannel來建立Alert，若使用我的模板那可以看到已建立好的Alert，可以加以修改
![螢幕擷取畫面 2023-11-08 145313.png](https://hackmd.io/_uploads/ryJugh_7a.png)

#### 收到Alert Mail
最後當然是為了收到這封郵件來得知Docker Container狀況
![螢幕擷取畫面 2023-11-08 145553.png](https://hackmd.io/_uploads/Bycb-2_Q6.png)
基本上到這就算初步完成了，提醒監控的Container符合異常條件會有Alert Mail 脫離異常也會有一封 OK Mail來提醒管理者。

## 番外篇
### MySQL Exporter 監測MySQL資料
1. 下載 prom/mysqld-exporter
```
docker pull prom/mysqld-exporter
```
2. 在MySQL中新增監測帳號，請使用root帳號進行指令
```
CREATE USER 'monitor_admin'@'localhost' IDENTIFIED BY 'Password';
GRANT PROCESS, REPLICATION CLIENT ON *.* TO 'exporter'@'localhost';
```
3. 建立docker-compose
```
mysqld-exporter:
    image: prom/mysqld-exporter
    container_name: mysql_exporter
    ports:
      - "9104:9104"
    command:
      - "--mysqld.username=<monitor_user>:<Password>"
      - "--mysqld.address=mysql ip:port"
    restart: always 
    networks:
      mynetwork:
        ipv4_address: 172.34.0.3
```
4. 在 Prometheus.yml 加入設定
```
- job_name: 'mysql-exporter'
    static_configs:
      - targets: ['172.34.0.3:9104']
```
5. 運行Compose，並查看Prometheus是否有識別出mysqld-exporter
![螢幕擷取畫面 2023-11-10 170904](https://hackmd.io/_uploads/H1VH7ujQp.png)

6. 至Grafana添加MySQL DashBored (ID:7362)
![螢幕擷取畫面 2023-11-10 171038](https://hackmd.io/_uploads/S1ziQOoX6.png)



參考文件：
[https://ej-feng.medium.com/grafana-alert-%E5%91%8A%E8%AD%A6%E4%BD%BF%E7%94%A8-email-%E9%80%9A%E7%9F%A5%E7%9A%84%E7%94%A8%E6%B3%95-8e62c208457d](https://)
[https://sectools.tw/docker-monitor/](https://)