Skip to content

1. docker-compose方式

1.1 创建目录

bash
mkdir -p /data/monitor/prometheus/{data,config}
mkdir -p /data/monitor/prometheus/{data,config}

1.2 创建配置文件

yaml
global:
  scrape_interval:     15s # 每15s采集一次数据
  evaluation_interval: 15s # 每15s做一次告警检测


scrape_configs:
  # 配置prometheus服务本身
  - job_name: prometheus
    metrics_path: /prometheus/metrics/  #由于在nginx上面做了目录代理,则这里需要添加路径
    static_configs:
      - targets: ['172.18.110.129:19090']
        labels:
          instance: prometheus
global:
  scrape_interval:     15s # 每15s采集一次数据
  evaluation_interval: 15s # 每15s做一次告警检测


scrape_configs:
  # 配置prometheus服务本身
  - job_name: prometheus
    metrics_path: /prometheus/metrics/  #由于在nginx上面做了目录代理,则这里需要添加路径
    static_configs:
      - targets: ['172.18.110.129:19090']
        labels:
          instance: prometheus

1.3 创建docker-compose

vi docker-compose.yml

yaml
version: '3.8'
services:
  prometheus:
    image: prom/prometheus:v2.55.1
    container_name: prometheus
    restart: always
    ports:
      - "19090:9090"
    volumes:
      - /data/monitor/prometheus/config:/etc/prometheus
      - /data/monitor/prometheus/data:/prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--web.enable-lifecycle'
      - "--storage.tsdb.retention.time=10d"
      - "--storage.tsdb.no-lockfile"
      - "--storage.tsdb.min-block-duration=2h"
      - "--storage.tsdb.max-block-duration=2h"
      - "--web.console.libraries=/etc/prometheus/console_libraries"
      - "--web.console.templates=/etc/prometheus/consoles"
      - "--web.external-url=prometheus" #nginx代理需要,否则访问时的单独加上graph
version: '3.8'
services:
  prometheus:
    image: prom/prometheus:v2.55.1
    container_name: prometheus
    restart: always
    ports:
      - "19090:9090"
    volumes:
      - /data/monitor/prometheus/config:/etc/prometheus
      - /data/monitor/prometheus/data:/prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--web.enable-lifecycle'
      - "--storage.tsdb.retention.time=10d"
      - "--storage.tsdb.no-lockfile"
      - "--storage.tsdb.min-block-duration=2h"
      - "--storage.tsdb.max-block-duration=2h"
      - "--web.console.libraries=/etc/prometheus/console_libraries"
      - "--web.console.templates=/etc/prometheus/consoles"
      - "--web.external-url=prometheus" #nginx代理需要,否则访问时的单独加上graph
  • 启动
bash
docker-compose up -d
docker-compose up -d
  • 关闭
docker-compose down
docker-compose down
  • 动态更新配置
bash
curl -v --request POST 'http://172.18.110.129:19090/prometheus/-/reload'
curl -v --request POST 'http://172.18.110.129:19090/prometheus/-/reload'

1.4 nginx代理

bash
   location ^~  /prometheus/ {
        proxy_pass     http://172.18.110.129:19090/prometheus/;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	proxy_http_version 1.1;
	proxy_set_header Connection "Keep-Alive";
	proxy_set_header Host $http_host;
	proxy_set_header Upgrade $http_upgrade;
	proxy_set_header Connection $connection_upgrade;
   }
   location ^~  /prometheus/ {
        proxy_pass     http://172.18.110.129:19090/prometheus/;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	proxy_http_version 1.1;
	proxy_set_header Connection "Keep-Alive";
	proxy_set_header Host $http_host;
	proxy_set_header Upgrade $http_upgrade;
	proxy_set_header Connection $connection_upgrade;
   }
  • 效果

image-20241210143545183

2. docker方式

  • 创建目录
mkdir -p /data/prometheus
mkdir -p /data/prometheus
  • 拉取官方镜像
docker pull prom/prometheus

docker pull grafana/grafana

docker pull prom/node-exporter

docker pull oliver006/redis_exporter
docker pull prom/prometheus

docker pull grafana/grafana

docker pull prom/node-exporter

docker pull oliver006/redis_exporter
bash
docker run -d -p 9090:9090 \ 
--net=host \
-v /data/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \ 
-v /data/prometheus: \ 
prom/prometheus \ 
--config.file="/etc/prometheus/prometheus.yml" \ 
--storage.tsdb.path="/data/prometheus"
docker run -d -p 9090:9090 \ 
--net=host \
-v /data/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \ 
-v /data/prometheus: \ 
prom/prometheus \ 
--config.file="/etc/prometheus/prometheus.yml" \ 
--storage.tsdb.path="/data/prometheus"