可观测性与稳定性 / 系统课时
组合 DaemonSet 与 Gateway 拓扑
组合 DaemonSet 与 Gateway 拓扑
这节课把节点级 Collector Agent 和集中式 Gateway 组合成两层拓扑:应用先就近进入 DaemonSet,再由 Gateway 统一处理并出网。重点不是多部署一层,而是让入口、故障域和扩缩容边界都可预测。
学习目标
- 区分 DaemonSet Agent 与 Gateway 的职责和故障域
- 配置 Agent 到 Gateway 的 OTLP 转发、队列与重试
- 判断 Gateway 扩容以及 tail sampling 对路由方式的要求
把两层职责拆开
DaemonSet Agent 跟随节点存在,适合做节点附近才能完成的工作:接收应用 OTLP、采集节点或容器信号、补充 Kubernetes 资源属性,并用较小批次快速转发。它的配置应尽量轻,避免在每个节点重复执行高成本策略。
Gateway 作为独立的 Deployment 或 StatefulSet 提供稳定的集群入口,负责集中式过滤、采样、凭据管理、较大的发送队列以及对外导出。这样节点数量增长时 Agent 随节点增长,而 Gateway 可以按总遥测吞吐独立扩容。

如果一个节点上的 Agent 失效,主要影响该节点的遥测;如果 Gateway 整层不可用,则会影响整个集群的集中出口。因此两层都要有队列与重试,但 Gateway 通常需要更大的容量和更长的后端故障吸收窗口。
配置 Agent 做就近接入与转发
以下完整 Collector 配置面向包含 k8sattributes 处理器的 Kubernetes 或 Contrib 发行版。Agent 先做内存保护,再补充 Kubernetes 属性、批处理,最后通过集群 DNS 把三类信号转发给 Gateway。
receivers:
otlp:
protocols:
grpc:
endpoint: 0.0.0.0:4317
processors:
memory_limiter:
check_interval: 1s
limit_mib: 256
k8sattributes:
passthrough: false
batch:
timeout: 1s
send_batch_size: 512
exporters:
otlp/gateway:
endpoint: otel-gateway.observability.svc.cluster.local:4317
tls:
insecure: true
sending_queue:
enabled: true
queue_size: 2048
retry_on_failure:
enabled: true
max_elapsed_time: 30s
service:
pipelines:
traces:
receivers: [otlp]
processors: [memory_limiter, k8sattributes, batch]
exporters: [otlp/gateway]
metrics:
receivers: [otlp]
processors: [memory_limiter, k8sattributes, batch]
exporters: [otlp/gateway]
logs:
receivers: [otlp]
processors: [memory_limiter, k8sattributes, batch]
exporters: [otlp/gateway]k8sattributes 需要 Collector ServiceAccount 具备读取所需 Kubernetes 元数据的 RBAC 权限。0.0.0.0 只是接收地址;生产环境还要通过 NetworkPolicy、节点入口方式或认证限制谁能访问 4317 端口。
把配置保存为 <PATH>/agent.yaml 后,可先做语法与组件验证:
otelcol-contrib validate --config=<PATH>/agent.yaml >/dev/null && echo "agent config valid"预期输出:
agent config valid如果实际发行版的二进制名不是 otelcol-contrib,替换为对应 Collector 命令即可;<PATH> 的实际值也会不同。
配置 Gateway 集中处理与出网
Gateway 接收 Agent 的 OTLP 流量,再做统一批处理并发送到后端。下面配置故意不加入 tail sampling,先建立一个可水平扩容、无跨实例状态依赖的基础 Gateway。
receivers:
otlp:
protocols:
grpc:
endpoint: 0.0.0.0:4317
processors:
memory_limiter:
check_interval: 1s
limit_mib: 1024
batch:
timeout: 5s
send_batch_size: 2048
exporters:
otlp/backend:
endpoint: backend.example.com:4317
tls:
insecure: false
headers:
authorization: "Bearer ${env:BACKEND_TOKEN}"
sending_queue:
enabled: true
queue_size: 8192
retry_on_failure:
enabled: true
max_elapsed_time: 5m
service:
pipelines:
traces:
receivers: [otlp]
processors: [memory_limiter, batch]
exporters: [otlp/backend]
metrics:
receivers: [otlp]
processors: [memory_limiter, batch]
exporters: [otlp/backend]
logs:
receivers: [otlp]
processors: [memory_limiter, batch]
exporters: [otlp/backend]验证时先提供真实的后端令牌环境变量,再检查配置:
export BACKEND_TOKEN=<VALUE>
otelcol-contrib validate --config=<PATH>/gateway.yaml >/dev/null && echo "gateway config valid"预期输出:
gateway config valid这里的队列容量只是起点,应该结合峰值吞吐、可接受的后端中断时间和 Collector 内存限制压测后调整,不能把大队列当成无限缓冲。
固定流量入口与扩缩容边界
应用如果必须进入“本节点 Agent”,就要使用能保证本地性的入口,例如节点 IP 配合 Agent 的 hostPort,或由部署系统注入明确的本地 Collector 地址。把应用直接指向一个普通的 Agent Service 可能被负载均衡到其他节点,从而失去节点级故障隔离和来源关联的确定性。
Agent 到 Gateway 则应使用稳定的 Kubernetes Service 名称。没有状态型处理时,Gateway 多副本可以由 Service 分流;Agent 数量通常由节点数决定,Gateway 副本数则由 CPU、内存、队列长度和整体吞吐决定。
对指标做累积转增量等有状态处理时也要确认同一时间序列的单写者与稳定路由,不能只看 Gateway 是否“能横向扩容”。
按故障域验收拓扑
部署完成后,先验证 DaemonSet 已覆盖目标节点、Gateway Deployment 已就绪,并确认 Gateway Service 存在:
kubectl -n observability rollout status daemonset/otel-agent --timeout=120s >/dev/null && echo "agent ready"
kubectl -n observability rollout status deployment/otel-gateway --timeout=120s >/dev/null && echo "gateway ready"
kubectl -n observability get service otel-gateway >/dev/null && echo "gateway service ready"预期输出:
agent ready
gateway ready
gateway service ready最终验收应能解释三种故障:单个 Agent 故障只影响对应节点,单个 Gateway 副本故障由 Service 和剩余副本承接,后端短暂不可用由发送队列与重试吸收。只有这三条边界都清楚,两层拓扑才真正比“所有 Collector 混在一起”更容易运维。