目录结构

docker-compose教程:docker-compose部署grafana + promtail + loki_容器

vim docker-compose-grafana-promtail-loki.yml

# docker-compose配置参考:https://github.com/grafana/loki/blob/master/production/docker-compose.yaml
version: "3"

# 网桥loki -> 方便相互通讯
networks:
  loki:

services:
  # Loki日志系统
  loki:
    image: grafana/loki:latest
    container_name: grafana_promtail_loki_demo_loki
    restart: unless-stopped
    ports:
      - "3100:3100"
    volumes:
      - ./grafana_promtail_loki/loki:/etc/loki
    command: -config.file=/etc/loki/loki-local-config.yaml
    networks:
      - loki

  # Loki官方支持的日志采集端
  promtail:
    image: grafana/promtail:latest
    container_name: grafana_promtail_loki_demo_promtail
    restart: unless-stopped
    volumes:
      - ./grafana_promtail_loki/logs:/var/logs
      - ./grafana_promtail_loki/promtail:/etc/promtail
    command: -config.file=/etc/promtail/promtail-docker-config.yaml
    networks:
      - loki

  # 用于UI展示
  grafana:
    image: grafana/grafana:latest
    container_name: grafana_promtail_loki_demo_grafana
    restart: unless-stopped
    ports:
      - "3000:3000"
    volumes:
      - "./grafana_promtail_loki/grafana/data:/var/lib/grafana"
      #      - "./grafana_promtail_loki/grafana/config:/etc/grafana"
      - "./grafana_promtail_loki/grafana/log:/var/log/grafana"
    environment:
      GF_EXPLORE_ENABLED: "true"
    networks:
      - loki

配置文件

vim grafana_promtail_loki/loki

auth_enabled: false

server:
  http_listen_port: 3100

ingester:
  lifecycler:
    address: 127.0.0.1
    ring:
      kvstore:
        store: inmemory
      replication_factor: 1
    final_sleep: 0s
  chunk_idle_period: 5m
  chunk_retain_period: 30s

schema_config:
  configs:
    - from: 2018-04-15
      store: boltdb
      object_store: filesystem
      schema: v9
      index:
        prefix: index_
        period: 168h

storage_config:
  boltdb:
    directory: /tmp/loki/index

  filesystem:
    directory: /tmp/loki/chunks

limits_config:
  enforce_metric_name: false
  reject_old_samples: true
  reject_old_samples_max_age: 168h

chunk_store_config:
  max_look_back_period: 0

table_manager:
  chunk_tables_provisioning:
    inactive_read_throughput: 0
    inactive_write_throughput: 0
    provisioned_read_throughput: 0
    provisioned_write_throughput: 0
  index_tables_provisioning:
    inactive_read_throughput: 0
    inactive_write_throughput: 0
    provisioned_read_throughput: 0
    provisioned_write_throughput: 0
  retention_deletes_enabled: false
  retention_period: 0

vim promtail/promtail-docker-config.yaml

server:
  http_listen_port: 0
  grpc_listen_port: 0

positions:
  filename: /etc/promtail/positions.yaml   # 游标记录上一次同步位置
  sync_period: 10s #10秒钟同步一次

clients:
  - url: http://loki:3100/api/prom/push

scrape_configs:
  - job_name: test-log
    entry_parser: raw
    static_configs:
      - targets:
          - localhost
        labels:
          job: varlogs
          app: varlogs
          __path__: /var/logs/*log

启动及访问

sudo docker-compose -f docker-compose-grafana-promtail-loki.yml -p grafana_promtail_loki up -d

当使用 -p 选项指定了项目名称后,在启动容器时,Docker Compose 会在容器名称前添加该项目名称的前缀。

例如,在你提供的命令中,使用了 -p grafana_promtail_loki 指定了项目名称为 “grafana_promtail_loki”。如果你在 docker-compose.yml 文件中定义了一个名为 app 的服务,那么在使用 docker-compose up 启动容器后,该容器的名称将会是 “grafana_promtail_loki_app”。

通过添加项目名称的前缀,Docker Compose 可以确保在同一主机上运行多个项目时,每个项目内的容器名称是唯一的,避免了命名冲突。

请注意,项目名称前缀仅适用于容器的名称,不会影响容器内部的主机名或其他配置。

访问地址:http://127.0.0.1:3000 默认登录账号密码:admin/admin


links:

https://gitee.com/zhengqingya/docker-compose/tree/master/Windows/grafana_promtail_loki