KubernetesSREPrometheusGrafanaOpenTelemetry可观测性
场景
某电商平台Kubernetes集群在生产环境出现间歇性延迟和错误,用户反馈页面加载缓慢,但CPU和内存指标正常。SRE团队需要快速定位根因。
症状
- 应用响应时间从200ms飙升至2s
- 错误率从0.1%升至5%
- 集群资源利用率较低(CPU 30%,内存50%)
- 日志中无明显异常
诊断
现有监控仅覆盖基础设施指标,缺乏分布式追踪和业务层上下文。需引入OpenTelemetry实现请求级追踪,Prometheus采集指标,Grafana统一可视化。
部署命令
1. 部署OpenTelemetry Collector
# otel-collector-daemonset.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: otel-collector
namespace: observability
spec:
selector:
matchLabels:
app: otel-collector
template:
metadata:
labels:
app: otel-collector
spec:
containers:
- name: otel-collector
image: otel/opentelemetry-collector-contrib:0.88.0
ports:
- containerPort: 4318
name: otlp-http
- containerPort: 4317
name: otlp-grpc
- containerPort: 8888
name: metrics
volumeMounts:
- name: config
mountPath: /etc/otelcol-contrib/config.yaml
subPath: config.yaml
volumes:
- name: config
configMap:
name: otel-collector-config
2. ConfigMap配置
apiVersion: v1
kind: ConfigMap
metadata:
name: otel-collector-config
namespace: observability
data:
config.yaml: |
receivers:
otlp:
protocols:
grpc:
http:
processors:
batch:
timeout: 1s
send_batch_size: 1024
exporters:
otlp:
endpoint: "jaeger-collector.observability:4317"
tls:
insecure: true
prometheus:
endpoint: "0.0.0.0:8889"
namespace: "app"
service:
pipelines:
traces:
receivers: [otlp]
processors: [batch]
exporters: [otlp]
metrics:
receivers: [otlp]
processors: [batch]
exporters: [prometheus]
3. 配置Prometheus抓取
# prometheus-scrape-config.yaml
scrape_configs:
- job_name: 'otel-collector'
static_configs:
- targets: ['otel-collector.observability:8889']
- job_name: 'kubernetes-pods'
kubernetes_sd_configs:
- role: pod
relabel_configs:
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
action: keep
regex: true
- source_labels: [__address__, __meta_kubernetes_pod_annotation_prometheus_io_port]
action: replace
regex: ([^:]+)(?::\d+)?;(\d+)
replacement: $1:$2
target_label: __address__
4. 部署Jaeger(用于追踪存储)
kubectl create namespace observability
helm repo add jaegertracing https://jaegertracing.github.io/helm-charts
helm install jaeger jaegertracing/jaeger -n observability --set storage.backend=elasticsearch --set storage.elasticsearch.host=elasticsearch-master.observability
5. 部署Grafana
helm repo add grafana https://grafana.github.io/helm-charts
helm install grafana grafana/grafana -n observability --set datasources."datasources\.yaml".apiVersion=1 --set datasources."datasources\.yaml".datasources[0].name=Prometheus --set datasources."datasources\.yaml".datasources[0].type=prometheus --set datasources."datasources\.yaml".datasources[0].url=http://prometheus-server.observability --set datasources."datasources\.yaml".datasources[0].access=proxy --set datasources."datasources\.yaml".datasources[0].isDefault=true
风险控制
- 采样率:设置合理的采样(如10%),避免全量追踪导致性能开销。
- 高基数标签:避免在追踪或指标中加入用户ID等唯一值标签。
- 配置变更:先于测试环境验证,再推送到生产。
- 资源限制:为Collector设置CPU/内存限制,防止OOM。
回滚
- 删除DaemonSet:
kubectl delete -f otel-collector-daemonset.yaml - 移除Prometheus抓取配置,重启Prometheus。
- 删除Jaeger和Grafana helm chart:
helm delete jaeger -n observability && helm delete grafana -n observability - 恢复应用的原有监控配置。
验证
- 追踪:访问Jaeger UI,搜索请求,查看Span详情。
- 指标:在Prometheus中查询
app_request_duration_seconds。 - 仪表盘:在Grafana中导入预定义仪表盘,确认延迟和错误率下降。
- 端到端:手动发送测试请求,观察追踪和指标数据实时更新。
何时提交OpsGlobal工单
- 部署Collector后无数据上报。
- Prometheus无法抓取目标。
- 集群资源消耗异常增高。
- 需要专家协助优化采样策略或设计定制仪表盘。
适用场景
适合正在处理 Observability、Kubernetes, SRE, Prometheus, Grafana 相关问题的团队,用于快速建立排查路径和交付标准。
问题背景
本文通过一个实际场景,介绍如何使用Prometheus、Grafana和OpenTelemetry为Kubernetes集群构建端到端可观测性,涵盖症状、诊断、部署命令、风险控制、回滚和验证。
排查步骤
先确认影响范围和最近变更,再收集日志、配置、指标和链路数据,最后按风险从低到高执行修复。
命令示例
示例命令请替换为你的真实资源名,并使用环境变量保存账号、密码、token 等敏感信息。
风险说明
生产环境操作前需要确认备份、权限边界、变更窗口和回滚路径,避免扩大故障影响。
回滚方案
保留原配置和发布版本;如修复后指标异常,立即回退配置、镜像或数据库变更并复核日志。
交付清单
问题定位记录、关键命令、修复步骤、验证结果、后续优化建议。
遇到类似技术问题?
如果你的服务器、K8s、Docker、CI/CD、数据库或监控系统出现类似问题,可以提交日志和配置文件,我们帮你远程诊断。