Docker Swarm 容器的 Promtail 配置和设置

问题描述 投票:0回答:1

我有一个 promtail 和 docker compose 配置和设置,工作正常,但是当我尝试对 docker swarm 集群执行相同操作时,日志由于某种原因没有显示

我在网上搜索了有关使用 promtail 进行 docker swarm 工作配置和设置的文档,不幸的是我找不到任何帮助

所以我希望有人愿意帮助我指出正确的方向

这是 docker swarm 中 promtail 容器的错误

level=error ts=2024-11-21T00:52:59.569451224Z caller=client.go:430 component=client host=loki:3100 msg="final error sending batch" status=400 tenant= error="server returned HTTP status 400 Bad Request (400): error at least one label pair is required per stream"

level=error ts=2024-11-21T00:53:10.069885261Z caller=client.go:430 component=client host=loki:3100 msg="final error sending batch" status=400 tenant= error="server returned HTTP status 400 Bad Request (400): error at least one label pair is required per stream"

这是我正在使用的 promtail 配置

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: system
    static_configs:
    - targets:
        - localhost
      labels:
        job: varlogs
        # __path__: /var/log/*log
        __path__: /var/log/!(auth.log)*log

  - 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 swarm 服务

x-logging:
  &default-logging
  driver: "json-file"
  options:
    max-size: "1m"
    max-file: "1"
    tag: "{{.Name}}"

services:

  app-1:
    image: "app-1:v0.1"
    networks:
      - apps
    env_file:
      - ./env/.env.app-1
    deploy:
      mode: replicated
      replicas: 2
      placement:
        constraints:
          - node.labels.node != node-1
    labels:
      logging: "promtail"
      logging_jobname: "containerlogs"
    logging: *default-logging

networks:
  apps:
    external: true

但是当我检查 loki 时,我在 loki 仪表板中看不到

container
logstream
标签,就像 docker compose 中的标签与上述配置相同

我做错了什么,我需要做什么来解决这个问题,以便我可以看到

container
logstream
标签来过滤到我想要查看日志的容器日志?

docker swarm 标签未显示在 loki 中

enter image description here

docker compose 工作正常,标签显示在 loki 中

enter image description here

docker-compose docker-swarm grafana-loki promtail
1个回答
0
投票

我觉得奇怪的是你正在从 /var/log/* 读取日志,这是在 Linux 上写入日志的地方,但不是主要写入容器日志的地方。

在 swarm 中写入日志时,做两件事很重要:

首先,在 json-log 元数据中包含

com.docker.*
标签。这需要将以下内容添加到您的 daemon.json 中:

"log-driver": "json-file",
  "log-opts": {
    "labels-regex": "^com.docker.+",
  }

然后,我使用以下 promtail 配置。

server:
  http_listen_address: 0.0.0.0
  http_listen_port: 9080

positions:
  filename: /tmp/positions.yaml

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

scrape_configs:

- job_name: containers
  static_configs:
  - targets:
      - localhost
    labels:
      job: containerlogs
      __path__: /var/lib/docker/containers/*/*log

  pipeline_stages:
  - json:
      expressions:
        log: log
        stream: stream
        time: time
        tag: attrs.tag
        compose_project: attrs."com.docker.compose.project"
        compose_service: attrs."com.docker.compose.service"
        stack_name: attrs."com.docker.stack.namespace"
        swarm_service_name: attrs."com.docker.swarm.service.name"
        swarm_task_name: attrs."com.docker.swarm.task.name"
  - regex:
      expression: "^/var/lib/docker/containers/(?P<container_id>.{12}).+/.+-json.log$"
      source: filename
  - timestamp:
      format: RFC3339Nano
      source: time
  - labels:
      stream:
      container_id:
      tag:
      compose_project:
      compose_service:
      stack_name:
      swarm_service_name:
      swarm_task_name:
  - output:
      source: log

这使用

docker compose
docker service
和/或
docker stack
命令添加的标签来识别 compose 或 swarm 服务名称等。

© www.soinside.com 2019 - 2024. All rights reserved.