Prometheus监控安装和使用

使用 Docker 安装 Prometheus

  • 基础环境

    • 192.168.2.230(部署Prometheus,Grafana)
    • 192.168.2.231
    • 192.168.2.232
  • 新建目录和配置文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    mkdir -p /home/config/prometheus
    cat > prometheus.yml<<EOF
    global:
    scrape_interval: 10s
    evaluation_interval: 10s

    scrape_configs:
    - job_name: prometheus
    static_configs:
    - targets: ['localhost:9090']
    labels:
    instance: prometheus

    - job_name: linux
    static_configs:
    - targets: ['192.168.2.230:9100','192.168.2.231:9100','192.168.2.232:9100']

    - job_name: docker
    static_configs:
    - targets: ['192.168.2.230:9101','192.168.2.231:9101','192.168.2.232:9101']

    - job_name: mysql
    static_configs:
    - targets: ['192.168.2.230:9104']

    EOF
  • 安装 Prometheus

    1
    2
    3
    4
    5
    6
    7
    docker run -d \
    --name prometheus \
    -p 9090:9090 \
    -v /home/config/prometheus:/etc/prometheus \
    --restart=always \
    quay.io/prometheus/prometheus
    firewall-cmd --zone=public --add-port=9090/tcp --permanent && firewall-cmd --reload
  • 安装 grafana

    1
    2
    3
    4
    5
    6
    docker run -d \
    --name=grafana \
    -p 3000:3000 \
    --restart=always \
    grafana/grafana
    firewall-cmd --zone=public --add-port=3000/tcp --permanent && firewall-cmd --reload
  • 安装 node-exporter

    1
    2
    3
    4
    5
    6
    7
    8
    9
    docker run -d \
    --name node-exporter \
    -p 9100:9100 \
    -v /proc:/host/proc \
    -v /sys:/host/sys \
    -v /:/rootfs \
    --restart=always \
    prom/node-exporter
    firewall-cmd --zone=public --add-port=9100/tcp --permanent && firewall-cmd --reload
  • 安装 cadvisor

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    docker run -d \
    --name cadvisor \
    -p 9101:8080 \
    -v /:/rootfs:ro \
    -v /var/run:/var/run:rw \
    -v /sys:/sys:ro \
    -v /var/lib/docker/:/var/lib/docker:ro \
    -v /dev/disk/:/dev/disk:ro \
    --restart=always \
    google/cadvisor
    firewall-cmd --zone=public --add-port=9101/tcp --permanent && firewall-cmd --reload
  • 安装 mysql-exporter

    • 新增用户

      1
      2
      3
      CREATE USER 'exporter'@'%' IDENTIFIED BY '********';
      GRANT PROCESS, REPLICATION CLIENT ON *.* TO 'exporter'@'%';
      GRANT SELECT ON `performance\_schema`.* TO 'exporter'@'%';
    • 添加监控容器

      1
      2
      3
      4
      5
      6
      7
      docker run -d \
      --name mysql-exporter \
      -p 9104:9104 \
      -e DATA_SOURCE_NAME="user:password@(url:port)/database" \
      --restart=always \
      prom/mysqld-exporter
      firewall-cmd --zone=public --add-port=9104/tcp --permanent && firewall-cmd --reload