loki的docker服务发现

简介

目前我的大部分的服务都是docker部署在一台机器上的,然后docker-compose启动,为啥不用k8s呢,因为不想太复杂

操作

首先搭建一个loki,我loki的数据都是存储在minio上的,下面是docker-compose.yaml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
version: "3"
services: 
  loki:
    image: "grafana/loki:2.5.0"
    container_name: "loki"
    restart: "always"
    ports: 
      - "3100:3100"
    volumes: 
      - "./:/mnt/config"
      - "/etc/localtime:/etc/localtime"
      - "./data:/loki"
    command:
      - "-config.file=/mnt/config/loki-config.yaml"

然后是loki-config.yaml

 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
auth_enabled: false

server:
  http_listen_port: 3100
  grpc_listen_port: 9096

common:
  path_prefix: /tmp/loki
  storage:
    filesystem:
      chunks_directory: /tmp/loki/chunks
      rules_directory: /tmp/loki/rules
  replication_factor: 1
  ring:
    instance_addr: 127.0.0.1
    kvstore:
      store: inmemory

schema_config:
  configs:
    - from: 2020-10-24
      store: boltdb-shipper
      object_store: aws
      schema: v11
      index:
        prefix: index_
        period: 24h

ruler:
  alertmanager_url: http://localhost:9093

storage_config:
  aws:
    s3: https://user:[email protected]/loki
    s3forcepathstyle: true
  boltdb_shipper:
    active_index_directory: /loki/boltdb-shipper-active
    cache_location: /loki/boltdb-shipper-cache
    cache_ttl: 1h
    shared_store: s3

对于minio只要配置

1
2
3
4
5
6
7
8
9
storage_config:
  aws:
    s3: https://user:[email protected]/loki
    s3forcepathstyle: true
  boltdb_shipper:
    active_index_directory: /loki/boltdb-shipper-active
    cache_location: /loki/boltdb-shipper-cache
    cache_ttl: 1h
    shared_store: s3

就好了url最后的loki就是bucket的名字

之后就是promtail的docker-compose.yaml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
version: "3"
services: 
  promtail:
    image: "grafana/promtail:2.5.0"
    container_name: "promtail"
    restart: "always"
    volumes: 
      - "./:/mnt/config"
      - "/etc/localtime:/etc/localtime"
      - "/var/run/docker.sock:/var/run/docker.sock"
    command:
      - "-config.file=/mnt/config/promtail-config.yaml"

因为要使用docker的服务发现所以docker.sock也要挂载进来

之后就是promtail-config.yaml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
server:
  http_listen_port: 9080
  grpc_listen_port: 0

positions:
  filename: /mnt/config/positions.yaml

clients:
  - url: https://loki.bboysoul.cn/loki/api/v1/push

scrape_configs:
- job_name: 10.10.100.10-docker
  docker_sd_configs:
    - host: unix:///var/run/docker.sock
      refresh_interval: 5s
  relabel_configs:
    - source_labels: ['__meta_docker_container_name']
      regex: '/(.*)'
      target_label: 'container'

下面是loki的地址,个人习惯所有带有端口的都使用nginx反向代理下

1
2
clients:
  - url: https://loki.bboysoul.cn/loki/api/v1/push

job的配置如下,这里我只relable了container name,这样只要在grafana里弥漫搜索

{container="gitea"}

就可以搜索到对应容器的日志了

1
2
3
4
5
6
7
8
9
scrape_configs:
- job_name: 10.10.100.10-docker
  docker_sd_configs:
    - host: unix:///var/run/docker.sock
      refresh_interval: 5s
  relabel_configs:
    - source_labels: ['__meta_docker_container_name']
      regex: '/(.*)'
      target_label: 'container'

当然positions.yaml最好挂载出来,不然重启容器之后positions.yaml就清空了

欢迎关注我的博客 www.bboy.app

Have Fun