快速创建一个可用的 prometheus 和 grafana 进行测试, 并将数据保留在当前的目录中, 在重启之后数据不会丢失:

  1. 创建一个目录.

    mkdir /opt/monitor
    mkdir /opt/monitor/grafana
    mkdir /opt/monitor/grafana_data
    mkdir /opt/monitor/prometheus
    mkdir /opt/monitor/prometheus_data
    touch /opt/monitor/docker-compose.yaml
  2. 创建docker-compose 文件

    ---
    version: "3"
    services:
      prometheus:
        container_name: prometheus
        image: reg.liarlee.site/docker.io/prom/prometheus:latest
        restart: always
        network_mode: host
        environment:
          - TZ=Asia/Shanghai
        volumes:
          # - /opt/monitor/prometheus/prometheus.yaml:/etc/prometheus/prometheus.yml
          - /opt/monitor/prometheus_data:/prometheus
        command:
          - '--config.file=/etc/prometheus/prometheus.yml'
          - '--storage.tsdb.path=/prometheus'
          - '--web.console.libraries=/usr/share/prometheus/console_libraries'
          - '--web.console.templates=/usr/share/prometheus/consoles'
          - '--storage.tsdb.retention.time=90d'
      grafana:
        container_name: grafana
        image: reg.liarlee.site/docker.io/grafana/grafana-oss:main-ubuntu
        restart: always
        network_mode: host
        environment:
          - TZ=Asia/Shanghai
        volumes:
          - /opt/monitor/grafana_data:/var/lib/grafana
          - /opt/monitor/grafana/datasource:/etc/grafana/provisioning/datasources
          # - /opt/monitor/grafana/grafana.ini:/etc/grafana/grafana.ini
          - /etc/localtime:/etc/localtime:ro
        user: '472'
  3. 准备基础配置文件

    docker compose up -d 
    docker cp grafana:/etc/grafana/grafana.ini /opt/monitor/grafana/grafana.ini
    docker cp prometheus:/etc/prometheus/prometheus.yml /opt/monitor/prometheus/prometheus.yaml
    
    chown -R 472:472 /opt/monitor/grafana_data
    chown -R 472:472 /opt/monitor/grafana
    
    chown -R nobody:nobody /opt/monitor/prometheus_data
    chown -R nobody:nobody /opt/monitor/prometheus
    
    docker compose down --remove-orphans
  4. 准备prometheus 作为默认的Datasource

    touch /opt/monitor/grafana/datasource/datasource.yml
    ---
    apiVersion: 1
    
    datasources:
    - name: Prometheus
      type: prometheus
      url: http://localhost:9090 
      isDefault: true
      access: proxy
      editable: true
  5. 修改配置文件中需要的参数, 取消配置文件中的注释, 然后重启即可。

    docker compose down --remove-orphans && docker compose up -d