Loki’s docker service discovery

20220424-1.png

Introduction

At present, most of my services are deployed by docker on one machine, and then docker-compose is started. Why not use k8s, because I don’t want to be too complicated

operate

First build a loki, my loki data is stored on the minio, the following is 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"

Then loki-config.yaml

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
twenty one
twenty two
twenty three
twenty four
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

For minio just configure

 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

Okay, the loki at the end of the url is the name of the bucket

After that is the 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"

Because docker’s service discovery is to be used, docker.sock must also be mounted

After that is 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'

The following is the address of loki. Personally, all those with ports are used under the nginx reverse proxy.

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

The configuration of the job is as follows, here I only relable the container name, so as long as the search is diffused in grafana

{container="gitea"}

You can search for the logs of the corresponding container.

 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'

Of course, positions.yaml is best to be mounted, otherwise positions.yaml will be emptied after restarting the container

Welcome to my blog www.bboy.app

Have Fun

This article is reprinted from: https://www.bboy.app/2022/04/24/loki%E7%9A%84docker%E6%9C%8D%E5%8A%A1%E5%8F%91%E7%8E%B0 /
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment