4. 部署 OpenTelemetry Collector:Docker 部署、Kubernetes 部署、配置文件结构、Pipeline 配置
OpenTelemetry Collector 是数据管道的核心组件,负责接收、处理、导出遥测数据。本章将覆盖 Collector 的两种主流部署方式、配置文件的结构化设计以及 Pipeline 的声明式配置。掌握这些内容后,你可以在任何环境中快速搭建生产级的数据采集层。
4.1 部署方式对比
根据基础设施类型,Collector 通常以 Docker 容器 或 Kubernetes DaemonSet/Deployment 形式运行。下表从运维维度对比两种方式的关键差异:
| 维度 | Docker 部署 | Kubernetes 部署 |
|---|---|---|
| 适用场景 | 单机、VM、开发环境 | 容器化集群、微服务架构 |
| 资源管理 | 手动端口映射、卷挂载 | 自动调度、HPA、ConfigMap |
| 配置更新 | 重启容器或挂载新配置 | 滚动更新 ConfigMap + 热加载 |
| 服务发现 | 需外部 DNS 或静态配置 | 原生 Kubernetes API 发现 |
| 推荐模式 | Agent 模式(单进程) | Agent(DaemonSet)+ Gateway(Deployment) |
生产建议: 在 Kubernetes 环境中,始终采用 Agent + Gateway 双层架构。Agent 以 DaemonSet 运行在每个节点上,负责采集节点级指标和日志;Gateway 以 Deployment 运行,负责数据聚合、过滤、批处理以及导出到后端。
4.2 Docker 部署 Collector
使用 Docker 部署 Collector 是最直接的入门方式。以下步骤演示如何快速启动一个完整的 Collector 实例:
- 拉取镜像:
docker pull otel/opentelemetry-collector-contrib:latest - 准备配置文件:创建
otel-collector-config.yaml - 启动容器:挂载配置文件并映射端口
docker run -d \
--name otel-collector \
-p 4317:4317 \
-p 4318:4318 \
-p 8888:8888 \
-p 8889:8889 \
-v $(pwd)/otel-collector-config.yaml:/etc/otel-collector-config.yaml \
otel/opentelemetry-collector-contrib:latest \
--config=/etc/otel-collector-config.yaml
端口说明:
- 4317:gRPC 接收 OTLP 数据
- 4318:HTTP 接收 OTLP 数据
- 8888:Prometheus 指标暴露端口(Collector 自身指标)
- 8889:健康检查端点
4.3 Kubernetes 部署 Collector
在 Kubernetes 中,推荐使用 OpenTelemetry Operator 或手动编写 YAML 清单。以下是一个典型的 DaemonSet 部署示例:
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: otel-collector-agent
namespace: observability
spec:
selector:
matchLabels:
app: otel-collector-agent
template:
metadata:
labels:
app: otel-collector-agent
spec:
containers:
- name: otel-collector
image: otel/opentelemetry-collector-contrib:latest
args:
- "--config=/conf/otel-collector-config.yaml"
ports:
- containerPort: 4317
name: otlp-grpc
- containerPort: 4318
name: otlp-http
- containerPort: 8888
name: metrics
volumeMounts:
- name: collector-config
mountPath: /conf
volumes:
- name: collector-config
configMap:
name: otel-collector-config
注意: 如果使用 DaemonSet 采集节点日志,需要额外挂载宿主机
/var/log 目录。同时,确保 hostNetwork: true 或正确配置网络策略,避免端口冲突。
4.4 配置文件结构
Collector 的配置文件采用 YAML 格式,由四个顶级区块组成。理解每个区块的作用是配置 Pipeline 的前提。
每个区块的职责:
- receivers:定义如何接收数据,例如
otlp、prometheus、jaeger、filelog。 - processors:对数据进行处理,包括批处理、采样、属性修改、过滤等。
- exporters:定义数据输出目标,例如
otlp、prometheusremotewrite、logging。 - extensions:提供 Collector 自身的运维能力,如健康检查、性能分析。
- service.pipelines:将 receivers、processors、exporters 串联成完整的数据流。
4.5 Pipeline 配置详解
Pipeline 是 Collector 的核心抽象。每个 Pipeline 对应一种信号类型(traces / metrics / logs),并声明数据从接收到导出的完整路径。以下是一个生产级 Pipeline 配置示例:
receivers:
otlp:
protocols:
grpc:
endpoint: 0.0.0.0:4317
http:
endpoint: 0.0.0.0:4318
processors:
batch:
timeout: 1s
send_batch_size: 1024
memory_limiter:
check_interval: 1s
limit_mib: 512
spike_limit_mib: 128
attributes:
actions:
- key: environment
value: "production"
action: upsert
exporters:
otlp:
endpoint: "backend-collector.observability:4317"
tls:
insecure: true
logging:
loglevel: debug
service:
pipelines:
traces:
receivers: [otlp]
processors: [memory_limiter, batch, attributes]
exporters: [otlp, logging]
metrics:
receivers: [otlp]
processors: [memory_limiter, batch]
exporters: [otlp]
logs:
receivers: [otlp]
processors: [memory_limiter, batch]
exporters: [otlp]
关键配置解读:
- memory_limiter:防止 Collector 内存溢出,必须放在 Processor 列表首位。
- batch:合并数据包,提高导出效率,降低后端压力。
- attributes:统一添加环境标签,便于后续数据路由和过滤。
- logging exporter:调试阶段开启
debug级别,生产环境建议关闭或设为info。
4.6 Pipeline 数据流可视化
下图展示了一个完整的 traces Pipeline 数据流,从应用 SDK 发送 OTLP 数据开始,经过 Collector 内部处理,最终导出到后端存储:
4.7 配置最佳实践
生产环境 Checklist:
- 始终启用
memory_limiter并设置合理的limit_mib(建议为容器内存限制的 60-70%)。 - 使用
batchprocessor 减少网络开销,timeout建议 1-5s。 - 为每个 Pipeline 添加
loggingexporter 并设为info级别,便于故障排查。 - 在 Kubernetes 中通过
ConfigMap管理配置,利用 Collector 的--config热加载能力实现无重启更新。 - 为不同信号类型(traces / metrics / logs)创建独立的 Pipeline,避免互相影响。