1

我有一些 kubernetes 应用程序记录到文件而不是 stdout/stderr,我使用 Promtail 边车收集它们。但是由于边车使用“localhost”目标执行,所以我没有一个kubernetes_sd_config可以为我将 pod 元数据应用于标签的工具。所以我坚持静态地声明我的标签。

# ConfigMap
apiVersion: v1
kind: ConfigMap
metadata:
  labels:
    app: promtail
  name: sidecar-promtail
data:
  config.yml: |
    client:
      url: http://loki.loki.svc.cluster.local:3100/loki/api/v1/push
      backoff_config:
        max_period: 5m
        max_retries: 10
        min_period: 500ms
      batchsize: 1048576
      batchwait: 1s
      external_labels: {}
      timeout: 10s
    positions:
      filename: /tmp/promtail-positions.yaml
    server:
      http_listen_port: 3101
    target_config:
      sync_period: 10s
    scrape_configs:
    - job_name: sidecar-logs
      static_configs:
        - targets:
          - localhost
          labels:
            job: sidecar-logs
            __path__: "/sidecar-logs/*.log"

----
# Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: test-logger
spec:
  selector:
    matchLabels:
      run: test-logger
  template:
    metadata:
      labels:
        run: test-logger
    spec:
      volumes:
        - name: nfs
          persistentVolumeClaim:
            claimName: nfs-claim
        - name: promtail-config
          configMap:
            name: sidecar-promtail
      containers:
        - name: sidecar-promtail
          image: grafana/promtail:2.1.0
          volumeMounts:
            - name: nfs
              mountPath: /sidecar-logs
            - mountPath: /etc/promtail
              name: promtail-config
        - name: simple-logger         
          image: foo/simple-logger
          volumeMounts:
            - name: nfs
              mountPath: /logs

根据父 pod 的元数据标记收集的日志的最佳方法是什么?

4

1 回答 1

1

您可以执行以下操作:

在 sidecar 容器中,将需要的 pod 名称、节点名称等信息暴露为环境变量,然后'-config.expand-env'在 promtail 配置文件中添加开启环境扩展的标志,例如:

...
- name: sidecar-promtail
  image: grafana/promtail:2.1.0
  # image: grafana/promtail:2.4.1 # use this one if environment expansion is not available in 2.1.0
  args:
    # Enable environment expansion in promtail config file
    - '-config.expand-env'
  env:
    - name: NODE_NAME
      valueFrom:
        fieldRef:
          fieldPath: spec.nodeName
    - name: POD_NAME
      valueFrom:
        fieldRef:
          fieldPath: metadata.name
...

然后在您的 configMap 中,在您的 static_config 标签中添加环境变量,如下所示:

...
    scrape_configs:
    - job_name: sidecar-logs
      static_configs:
        - targets:
          - localhost
          labels:
            job: sidecar-logs
            pod: ${POD_NAME}
            node_name: ${NODE_NAME}
            __path__: "/sidecar-logs/*.log"
...
于 2022-01-06T19:55:54.293 回答