docker-compose快速搭建 Prometheus+Grafana監控系統

進擊的java程式設計師k發表於2019-03-29

一、說明

Prometheus負責收集資料,Grafana負責展示資料。其中採用Prometheus 中的 Exporter含:
1)Node Exporter,負責收集 host 硬體和作業系統資料。它將以容器方式執行在所有 host 上。
2)cAdvisor,負責收集容器資料。它將以容器方式執行在所有 host 上。
3)Alertmanager,負責告警。它將以容器方式執行在所有 host 上。

二、安裝docker,docker-compose

2.1 安裝docker

先安裝一個64位的Linux主機,其核心必須高於3.10,記憶體不低於1GB。在該主機上安裝Docker。

# 安裝依賴包
yum install -y yum-utils device-mapper-persistent-data lvm2
# 新增Docker軟體包源
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# 安裝Docker CE
yum install docker-ce -y
# 啟動
systemctl start docker
# 開機啟動
systemctl enable docker
# 檢視Docker資訊
docker info複製程式碼

2.2 安裝docker-compose

curl -L https://github.com/docker/compose/releases/download/1.23.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose複製程式碼

三、新增配置檔案

mkdir -p /usr/local/src/config
cd /usr/local/src/config複製程式碼

2.1 新增prometheus.yml配置檔案,

vim prometheus.yml

# 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: ['192.168.159.129:9093']
      # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  - "node_down.yml"
  # - "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'
    static_configs:
- targets: ['192.168.159.129:9090']

  - job_name: 'cadvisor'
    static_configs:
    - targets: ['192.168.159.129:8080']

  - job_name: 'node'
    scrape_interval: 8s
    static_configs:
      - targets: ['192.168.159.129:9100']複製程式碼

2.2 新增郵件告警配置檔案

新增配置檔案alertmanager.yml,配置收發郵件郵箱
vim alertmanager.yml

global:
  smtp_smarthost: 'smtp.163.com:25'  #163伺服器
  smtp_from: 'tsiyuetian@163.com'        #發郵件的郵箱
  smtp_auth_username: 'tsiyuetian@163.com'  #發郵件的郵箱使用者名稱,也就是你的郵箱
  smtp_auth_password: 'TPP***'        #發郵件的郵箱密碼
  smtp_require_tls: false        #不進行tls驗證

route:
  group_by: ['alertname']
  group_wait: 10s
  group_interval: 10s
  repeat_interval: 10m
  receiver: live-monitoring

receivers:
- name: 'live-monitoring'
  email_configs:
  - to: '1933306137@qq.com'        #收郵件的郵箱複製程式碼

2.3 新增報警規則

新增一個node_down.yml為 prometheus targets 監控
vim node_down.yml

groups:
- name: node_down
  rules:
  - alert: InstanceDown
    expr: up == 0
    for: 1m
    labels:
      user: test
    annotations:
      summary: "Instance {{ $labels.instance }} down"
      description: "{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 1 minutes."複製程式碼

四、編寫docker-compose

vim docker-compose-monitor.yml

version: '2'

networks:
    monitor:
        driver: bridge

services:
    prometheus:
        image: prom/prometheus
        container_name: prometheus
        hostname: prometheus
        restart: always
        volumes:
            - /usr/local/src/config/prometheus.yml:/etc/prometheus/prometheus.yml
            - /usr/local/src/config/node_down.yml:/etc/prometheus/node_down.yml
        ports:
            - "9090:9090"
        networks:
            - monitor

    alertmanager:
        image: prom/alertmanager
        container_name: alertmanager
        hostname: alertmanager
        restart: always
        volumes:
            - /usr/local/src/config/alertmanager.yml:/etc/alertmanager/alertmanager.yml
        ports:
            - "9093:9093"
        networks:
            - monitor

    grafana:
        image: grafana/grafana
        container_name: grafana
        hostname: grafana
        restart: always
        ports:
            - "3000:3000"
        networks:
            - monitor

    node-exporter:
        image: quay.io/prometheus/node-exporter
        container_name: node-exporter
        hostname: node-exporter
        restart: always
        ports:
            - "9100:9100"
        networks:
            - monitor

    cadvisor:
        image: google/cadvisor:latest
        container_name: cadvisor
        hostname: cadvisor
        restart: always
        volumes:
            - /:/rootfs:ro
            - /var/run:/var/run:rw
            - /sys:/sys:ro
            - /var/lib/docker/:/var/lib/docker:ro
        ports:
            - "8080:8080"
        networks:
            - monitor複製程式碼

五、啟動docker-compose

#啟動容器:
docker-compose -f /usr/local/src/config/docker-compose-monitor.yml up -d
#刪除容器:
docker-compose -f /usr/local/src/config/docker-compose-monitor.yml down
#重啟容器:
docker restart id複製程式碼

容器啟動如下:
docker-compose快速搭建 Prometheus+Grafana監控系統

prometheus targets介面如下:
docker-compose快速搭建 Prometheus+Grafana監控系統

備註:如果State為Down,應該是防火牆問題,參考下面防火牆配置。

prometheus graph介面如下:
docker-compose快速搭建 Prometheus+Grafana監控系統

備註:如果沒有資料,同步下時間。

六、防火牆配置

6.1 關閉selinux

setenforce 0
vim /etc/sysconfig/selinux複製程式碼

6.2 配置iptables

#刪除自帶防火牆
systemctl stop firewalld.service
systemctl disable firewalld.service複製程式碼
#安裝iptables
yum install -y iptables-services複製程式碼
#配置
vim /etc/sysconfig/iptables
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [24:11326]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 9090 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 8080 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3000 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 9093 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 9100 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT複製程式碼
#啟動
systemctl restart iptables.service
systemctl enable iptables.service複製程式碼

七、配置Grafana

7.1 新增Prometheus資料來源

docker-compose快速搭建 Prometheus+Grafana監控系統

7.2 配置dashboards

說明:可以用自帶模板,也可以去https://grafana.com/dashboards,下載對應的模板。

7.3 檢視資料

我從網頁下載了docker相關的模板:Docker and system monitoring,893
docker-compose快速搭建 Prometheus+Grafana監控系統
輸入893,就會載入出下面的資訊
docker-compose快速搭建 Prometheus+Grafana監控系統

匯入後去首頁檢視資料
docker-compose快速搭建 Prometheus+Grafana監控系統

八、附錄:單獨命令啟動各容器

#啟動prometheus
docker run -d -p 9090:9090 --name=prometheus \
-v /usr/local/src/config/prometheus.yml:/etc/prometheus/prometheus.yml \
-v /usr/local/src/config/node_down.yml:/etc/prometheus/node_down.yml \
prom/prometheus

# 啟動grafana
docker run -d -p 3000:3000 --name=grafana grafana/grafana

#啟動alertmanager容器
docker run -d -p 9093:9093 -v /usr/local/src/config/config.yml:/etc/alertmanager/config.yml --name alertmanager prom/alertmanager

#啟動node exporter
docker run -d \
  -p 9100:9100 \
  -v "/:/host:ro,rslave" \
  --name=node_exporter \
  quay.io/prometheus/node-exporter \
  --path.rootfs /host

#啟動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                               \
google/cadvisor:latest複製程式碼

docker-compose快速搭建 Prometheus+Grafana監控系統

相關文章