跳至主要內容

Prometheus安装


Prometheus安装

使用docker-compose部署Prometheus+Grafana监控系统。

1. 前提

操作系统:CentOS7.9 容器服务:部署docker、docker-compose基础服务

2. 安装

步骤1:准备容器配置文件

version: '3.7'
services:
  node-exporter:
    image: prom/node-exporter:latest
    ports:
      - "9100:9100"
    networks:
      - prom

  prometheus:
    image: prom/prometheus:latest
    volumes:
      - type: bind
        source: ./prometheus/prometheus.yml
        target: /etc/prometheus/prometheus.yml
        read_only: true
      - type: volume
        source: prometheus
        target: /prometheus
    ports:
      - "9090:9090"
    networks:
      - prom

  grafana:
    depends_on:
      - prometheus
    image: grafana/grafana:latest
    volumes:
      - type: volume
        source: grafana
        target: /var/lib/grafana
    ports:
      - "3000:3000"
    networks:
      - prom

volumes:
  prometheus:
    driver: local
    driver_opts:
      type: none
      o: bind
      device: /opt/dmgeo/prom/prometheus/data
  grafana:
    driver: local
    driver_opts:
      type: none
      o: bind
      device: /opt/dmgeo/prom/grafana

networks:
  prom:
    driver: bridge

步骤2:准备服务配置文件

prometheus.yml:

global:
  scrape_interval:     15s
  evaluation_interval: 15s

rule_files:
  - "*rules.yml"

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
    - targets: ['prometheus:9090']

  - job_name: 'node'
    static_configs:
    - targets: ['node-exporter:9100','192.168.123.12:9100','192.168.123.13:9100']

步骤3:启动服务

docker-compose up -d

步骤4:访问服务

Prometheus:

  • 访问地址:http://IP:9090,其中IP为服务器IP地址。

Grafana:

  • 访问地址:http://IP:3000,其中IP为服务器IP地址。
  • 初始账号:admin,初始密码:admin

3. 高级配置

1)Prometheus配置文件热加载

前提:Prometheus已经添加了启动参数:--web.enable-lifecycle 修改配置文件后,热加载方式是发送POST请求:

  • Linux shell命令:curl -X POST http://172.18.18.197:9090/-/reload
  • Windows终端命令:curl -Method POST http://172.18.18.197:9090/-/reload

其中,172.18.18.197:9090为Prometheus访问地址。

2)Grafana代理配置

如果grafana部署在内网,有外网访问需求的话,需要配置nginx反向代理,其中包括websocket相关配置,nginx配置文件如下:

map $http_upgrade $connection_upgrade {
  default upgrade;
  '' close;
}

# grafana
server {
    listen       8300;
    server_name  localhost;

    location / {
        proxy_set_header Host $http_host;
        proxy_pass http://172.18.18.197:3000/;
    }

    # Proxy Grafana Live WebSocket connections.
    location /api/live {
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header Host $http_host;
        proxy_pass http://172.18.18.197:3000/;
  }
}

其中: 8300:代理端口,根据实际情况设置; http://172.18.18.197:3000/:内网grafana的访问地址,根据实际情况填写。