在Collector处理器中,如何通过resource processor注入k8s.namespace.name标签?

解读

国内一线互联网与金融客户把 Zabbix 6.x/7.x 与 OpenTelemetry Collector 混部在同一条可观测链路上,用 Collector 做协议转换、标签补全,再回写 Zabbix Trapper 或 HTTP Agent。
“注入 k8s.namespace.name” 的本质是:在 Collector 的 pipeline 里,利用 k8sattributes processor(社区常叫 resource processor)把 Pod 所在命名空间写进 Resource 标签,后续所有 Metrics/Traces/Logs 都会携带该标签,Zabbix 端通过预处理步骤即可直接映射到主机宏或模板标签,实现按 Namespace 的告警分派、仪表盘聚合与权限隔离。
面试官重点考察:

  1. 是否知道 k8sattributes processor 才是官方推荐的“resource 级”补全手段,而不是 attributes processor;
  2. 能否给出最小可用配置,并说明 RBAC、downwardAPI、labels 路径三要素;
  3. 是否理解注入后如何在 Zabbix 侧消费(模板变量、LLD 宏、标签过滤)。

知识点

  1. OpenTelemetry Collector 架构:Receiver → Processor → Exporter;Processor 又分 attributes、resource、k8sattributes 等。
  2. k8sattributes processor 通过 list-watch 本地缓存 Pod/Node/Service 元数据,支持把任意元数据字段提取到 Resource 属性。
  3. 关键配置字段:
    • filter → node_from_env: K8S_NODE_NAME
    • extract → metadata: [k8s.namespace.name]
    • pod_association → source: connection.ip / resource_attribute: k8s.pod.ip
  4. 依赖:Collector 所在 Pod 需配置
    • serviceAccount + ClusterRole(get/list/watch pods、nodes、namespaces)
    • downwardAPI 把 “status.podIP” 注入环境变量,供 pod_association 匹配。
  5. Zabbix 7.x 已支持 OTEL 数据模型,Trapper 类型 item 可直接读取 tag 值;若走 Prometheus exporter 中转,则通过 {__meta_kubernetes_namespace} 重新标签。

答案

  1. 部署前提
    a. 用 DaemonSet 方式运行 Collector,确保与业务 Pod 同节点。
    b. 创建专用 ServiceAccount zabbix-collector,绑定 ClusterRole:
    rules:

    • apiGroups: [""]
      resources: ["pods", "nodes", "namespaces"]
      verbs: ["get", "list", "watch"]
      c. 在 DaemonSet 的 env 中注入:
    • name: K8S_NODE_NAME
      valueFrom:
      fieldRef: fieldPath: spec.nodeName
    • name: POD_IP
      valueFrom:
      fieldRef: fieldPath: status.podIP
  2. 编写 collector.yaml(核心片段)

receivers:
  prometheus:
    config:
      scrape_configs:
      - job_name: 'myapp'
        kubernetes_sd_configs:
        - role: pod
        relabel_configs:
        - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
          action: keep
          regex: true

processors:
  k8sattributes:
    auth_type: serviceAccount
    passthrough: false
    filter:
      node_from_env: K8S_NODE_NAME
    extract:
      metadata:
        - k8s.namespace.name
        - k8s.pod.name
        - k8s.node.name
    pod_association:
      - sources:
          - from: connection
            name: ip
          - from: resource_attribute
            name: k8s.pod.ip
  resource:
    # 若需要二次 rename,可在此统一加前缀
    attributes:
      - key: namespace
        from_attribute: k8s.namespace.name
        action: upsert

exporters:
  zabbix:
    endpoint: http://zabbix-server:10051
    # 官方 OTEL exporter 或自定义 HTTP Agent
    headers:
      X-OTEL-Namespace: ${namespace}

service:
  pipelines:
    metrics:
      receivers: [prometheus]
      processors: [k8sattributes, resource, batch]
      exporters: [zabbix]
  1. 验证
    a. 启动后查看 Collector 自身指标 otelcol_processor_k8sattributes_updated,若 >0 说明缓存同步成功。
    b. 在 Zabbix Server 用 zabbix_get -s 127.0.0.1 -k otel.metric[namespace=test,metric=cpu_usage] 能拿到带 namespace 标签的数据。
    c. 在前端 Templates → Macros 添加 {$NAMESPACE} = {{ITEM.TAGS.namespace}},即可按命名空间做告警路由。

拓展思考

  1. 多集群场景:给 k8sattributes processor 增加 cluster_name 配置项,或在 resource 里强制写入 cluster="sh-prod",避免不同集群同名 namespace 冲突。
  2. 高基问题:若集群 Pod 数量 >10k,需开启 processor 的 filter::namespace_regex 做分片,或部署多套 Collector,按 namespace 做 Shard。
  3. 安全加固:
    • 为 Collector 的 ServiceAccount 加 namespace 范围限制,仅监控指定 NS;
    • 开启 opa-gatekeeper 策略,禁止 Pod 直接携带 k8s.namespace.name 标签,防止伪造。
  4. 与 Zabbix LLD 联动:把 namespace 作为低级别发现键,自动生成 “Namespace → Deployment → Pod” 三级主机原型,实现动态扩缩容监控。
  5. 降本方案:若只关心 namespace 维度聚合,可在 Collector 端用 transform processor 提前做指标级降采样(sum without pod),减少 Trapper 流量 60% 以上。