Grafana Promtail Promtail Architecture

- 아래 그림에서 Promtail 에 해당하는 부분에 해당함
- Prometheus Promtail는 Grafana에서 제공하는 오픈소스 기반의 로그 수집기로 대상서버에 설치하여 Loki가 설치된 서버로 전송한다.
protail 대신에 flunt-bit를 사용하여 구성도 가능하다.

테스트 수행한 환경

- 인터넷가능한 환경, Rocky Linux 8.6, Docker가 구성된 환경

1. Promtail docker-compose 구동을 위한 스크립트 준비

전체파일 구성

tree
├── create.sh
├── delete.sh
├── docker-compose.yml
├── log.sh
├── promtail
│   └── promtail.yaml
├── start.sh
└── stop.sh

1) Promtail의 설정 샘플

주요설정 설명

- promtail.yaml: Promtail에서 사용하는 config 작성, 컨테이너 로그를 수집하여 label명 변경 및 multiline처리하는 샘플설정

mkdir promtail
vi  promtail/promtail.yaml

# https://grafana.com/docs/loki/latest/clients/promtail/configuration/
# https://docs.docker.com/engine/api/v1.41/#operation/ContainerList
server:
  http_listen_port: 9080
  grpc_listen_port: 0

positions:
  filename: /tmp/positions.yaml

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

scrape_configs:
  - job_name: flog_scrape
    docker_sd_configs:
      - host: unix:///var/run/docker.sock
        refresh_interval: 5s
        filters:
          - name: label
            values: ["logging=promtail"]
    relabel_configs:
      - source_labels: ['__meta_docker_container_name']
        regex: '/(.*)'
        target_label: 'container'
      - source_labels: ['__meta_docker_container_log_stream']
        target_label: 'logstream'
      - source_labels: ['__meta_docker_container_label_logging_jobname']
        target_label: 'job'
    pipeline_stages:
      - cri: {}
      - multiline:
          firstline: ^\d{4}-\d{2}-\d{2} \d{1,2}:\d{2}:\d{2},\d{3}
          max_wait_time: 3s
      # https://grafana.com/docs/loki/latest/clients/promtail/stages/json/
      - json:
          expressions:
            #message: message
            level: level
            #output: 'message'

- docker-compose.yml 설정 샘플

vi  docker-compose.yml

version: '3.7'

# monitor-net이름으로 이미 구성한 경우에는 networks부분은 주석처리
networks:
  monitor-net:
    driver: bridge

services:
  
  promtail:
#    image:  grafana/promtail:2.7.4
    image:  grafana/promtail:latest
    container_name: promtail
    volumes:
      - ./promtail/promtail.yaml:/etc/promtail/docker-config.yaml
      - /var/lib/docker/containers:/var/lib/docker/containers:ro
      - /var/run/docker.sock:/var/run/docker.sock
    command: -config.file=/etc/promtail/docker-config.yaml
    depends_on:
      - loki
    networks:
      - monitor-net

2) docker 컨테이너 생성,삭제,구동,중지,로그조회 등 스크립트 작성

cat > create.sh <<EOF
#!/usr/bin/bash
docker-compose -f docker-compose.yml up -d
docker-compose -f docker-compose.yml logs -f
EOF

cat > delete.sh <<EOF
#!/usr/bin/bash
docker-compose -f docker-compose.yml down -v
EOF

cat > start.sh <<EOF
#!/usr/bin/bash
docker-compose -f docker-compose.yml start
EOF

cat > stop.sh <<EOF
#!/usr/bin/bash
docker-compose -f docker-compose.yml stop
EOF

cat > log.sh <<EOF
#!/usr/bin/bash
docker-compose -f docker-compose.yml logs -f
EOF

chmod +x *.sh

2. Promtail 정상구동여부 확인

### 구동하기
./create.sh 

### 도커컨테이너 정상구동여부 확인
docker ps
CONTAINER ID        IMAGE                       COMMAND                  CREATED             STATUS              PORTS                    NAMES
2eb3475e7ee2   grafana/promtail:latest     "/usr/bin/promtail -…"   10 minutes ago   Up 10 minutes                                               promtail

## promtail 로그조회
docker logs promtail
level=info ts=2023-04-19T11:36:16.027284541Z caller=promtail.go:133 msg="Reloading configuration file" md5sum=42c33b075d67a62ff1f12020cbd260a6
level=info ts=2023-04-19T11:36:16.032321968Z caller=server.go:323 http=[::]:9080 grpc=[::]:35181 msg="server listening on addresses"
level=info ts=2023-04-19T11:36:16.037214244Z caller=main.go:174 msg="Starting Promtail" version="(version=2.8.0, branch=HEAD, revision=90888a0cc)"
level=warn ts=2023-04-19T11:36:16.037336243Z caller=promtail.go:265 msg="enable watchConfig"
level=info ts=2023-04-19T11:36:21.032927326Z caller=target_group.go:128 msg="added Docker target" containerID=80c2a646ab3369424b275c3c46e36505cce4c10d6cf047a9e5732337e419f87c

3. 구동화면 예시

- Grafana 데이터소스 -> Loki 선택 후 조회한 화면 예시

댓글남기기