Cilium网络可观测性:指标、日志、追踪集成
还在为Kubernetes网络流量黑盒而烦恼?Cilium提供了完整的网络可观测性解决方案,通过指标、日志和追踪的深度集成,让您对容器网络流量了如指掌。本文将带您全面掌握Cilium的可观测性能力,从基础监控到高级追踪,构建完整的网络可视化体系。
可观测性架构全景
Cilium的可观测性架构基于三大支柱构建,为不同层级的网络流量提供全面洞察:
核心组件功能对比
| 组件 | 监控层级 | 数据格式 | 主要用途 |
|---|---|---|---|
| Cilium Agent | L3/L4 | Prometheus | 系统状态监控 |
| Hubble | L4/L7 | 流日志/指标 | 网络流量分析 |
| Envoy Proxy | L7 | Prometheus | 应用层协议监控 |
指标监控体系
Cilium Agent指标配置
Cilium Agent提供丰富的系统级指标,通过Prometheus进行采集:
# Helm安装配置示例
helm install cilium cilium/cilium \
--namespace kube-system \
--set prometheus.enabled=true \
--set operator.prometheus.enabled=true \
--set prometheus.port=9962 \
--set operator.prometheus.port=9963
关键指标类别
端点管理指标
# 端点状态监控
cilium_endpoint_state{state="ready"} # 就绪端点数量
cilium_endpoint_state{state="waiting-for-identity"} # 等待身份端点
endpoint_regenerations_total{outcome="success"} # 端点重建成功次数
网络策略指标
# 策略执行统计
policy_endpoint_enforcement_status # 端点策略执行状态
policy_change_total{outcome="success"} # 策略变更成功次数
policy_implementation_delay # 策略部署延迟
数据路径指标
# 数据包处理统计
drop_count_total{reason="policy_denied",direction="ingress"} # 策略拒绝数据包
forward_count_total{direction="egress"} # 转发数据包数量
bpf_map_ops_total{operation="lookup",outcome="success"} # BPF映射操作统计
Hubble指标体系
Hubble提供网络流量层面的深度指标:
# Hubble指标启用配置
helm upgrade cilium cilium/cilium \
--set hubble.enabled=true \
--set hubble.metrics.enabled="{dns,drop,tcp,flow,port-distribution,icmp,httpV2}" \
--set hubble.metrics.enableOpenMetrics=true \
--set hubble.metrics.port=9965
Hubble核心指标
| 指标类型 | 描述 | 标签维度 |
|---|---|---|
hubble_flows_processed_total | 处理的流总数 | verdict, protocol |
hubble_tcp_flags_total | TCP标志位统计 | flag, direction |
hubble_dns_queries_total | DNS查询统计 | query_type, rcode |
hubble_http_requests_total | HTTP请求统计 | method, status_code |
日志收集与分析
Hubble流日志导出
Hubble支持将网络流日志导出到文件,便于日志收集系统处理:
# 静态日志导出配置
helm install cilium cilium/cilium \
--set hubble.enabled=true \
--set hubble.export.static.enabled=true \
--set hubble.export.static.filePath="/var/run/cilium/hubble/events.log" \
--set hubble.export.fileMaxSizeMb=50 \
--set hubble.export.fileMaxBackups=10
动态日志配置
对于需要多维度日志分析的场景,可使用动态导出配置:
# 动态流日志配置示例
hubble:
export:
dynamic:
enabled: true
config:
content:
- name: "security-audit"
filePath: "/var/run/cilium/hubble/security.log"
fieldMask: ["time", "verdict", "source", "destination", "drop_reason_desc"]
includeFilters:
- verdict: ["DROPPED", "ERROR"]
- name: "application-traffic"
filePath: "/var/run/cilium/hubble/app.log"
fieldMask: ["time", "source.namespace", "source.pod_name", "l7"]
includeFilters:
- event_type: [{"type": 4}] # L7事件
日志过滤策略
# 使用Hubble CLI生成过滤器
hubble observe --verdict DROPPED --verdict ERROR --print-raw-filters
# 输出: {"verdict":["DROPPED","ERROR"]}
hubble observe --not --namespace kube-system --print-raw-filters
# 输出: {"source_pod":["kube-system/"]},{"destination_pod":["kube-system/"]}
L7协议可见性
启用应用层监控
要实现HTTP、DNS等L7协议的可见性,需要配置Cilium网络策略:
apiVersion: "cilium.io/v2"
kind: CiliumNetworkPolicy
metadata:
name: "l7-visibility"
spec:
endpointSelector:
matchLabels:
"k8s:io.kubernetes.pod.namespace": default
egress:
- toPorts:
- ports:
- port: "53"
protocol: ANY
rules:
dns:
- matchPattern: "*"
- toEndpoints:
- matchLabels:
"k8s:io.kubernetes.pod.namespace": default
toPorts:
- ports:
- port: "80"
protocol: TCP
- port: "8080"
protocol: TCP
rules:
http: [{}]
安全敏感信息处理
Hubble提供敏感信息过滤功能,保护隐私数据:
# 安全配置选项
--hubble-redact-enabled=true
--hubble-redact-http-urlquery=true
--hubble-redact-http-userinfo=true
--hubble-redact-kafka-apikey=true
--hubble-redact-http-headers-allow="content-type,user-agent"
追踪与OpenMetrics集成
Exemplars支持
Cilium支持OpenMetrics格式,实现指标与追踪的关联:
# 启用OpenMetrics和Exemplars
helm upgrade cilium cilium/cilium \
--set hubble.metrics.enableOpenMetrics=true \
--set hubble.metrics.enabled="{httpV2:exemplars=true;labelsContext=source_ip,source_namespace,source_workload,destination_ip,destination_namespace,destination_workload,traffic_direction}"
追踪数据关联
通过Exemplars,可以将网络指标与分布式追踪关联:
# HTTP指标示例(包含追踪上下文)
hubble_http_requests_total{method="GET",status="200"} 1.0 # {trace_id="abc123",span_id="def456"}
hubble_tcp_connections_total{status="established"} 5.0 # {trace_id="xyz789",span_id="uvw012"}
可视化仪表板
Grafana监控部署
部署完整的监控栈,包含Prometheus和Grafana:
# 部署监控示例栈
kubectl apply -f https://raw.githubusercontent.com/cilium/cilium/master/examples/kubernetes/addons/prometheus/monitoring-example.yaml
# 访问Grafana
kubectl -n cilium-monitoring port-forward service/grafana 3000:3000
核心监控仪表板
Cilium提供多个预配置的Grafana仪表板:
- 网络流量仪表板:实时显示L3/L4层流量统计
- HTTP监控仪表板:应用层协议性能分析
- 策略执行仪表板:网络安全策略执行情况
- 端点健康仪表板:容器端点状态监控
性能优化与最佳实践
指标采集优化
对于大规模集群,建议禁用高基数指标:
# 优化指标配置
--metrics="-cilium_node_connectivity_status -cilium_node_connectivity_latency_seconds"
--prometheus.scrape-interval=30s
日志存储策略
# 日志轮转配置
hubble.export.fileMaxSizeMb: 100 # 单个文件最大100MB
hubble.export.fileMaxBackups: 7 # 保留7个备份文件
hubble.export.fileCompress: true # 启用压缩
安全审计配置
# 安全审计专用配置
- name: "security-audit"
filePath: "/var/run/cilium/hubble/security-audit.log"
fieldMask: ["time", "verdict", "source.ip", "destination.ip", "l4", "drop_reason_desc"]
includeFilters:
- verdict: ["DROPPED", "ERROR"]
- drop_reason_desc: ["POLICY_DENIED", "AUTH_REQUIRED"]
故障排查与诊断
常见问题诊断
| 症状 | 可能原因 | 解决方案 |
|---|---|---|
| 指标缺失 | Prometheus配置错误 | 检查annotation和service配置 |
| L7数据不可见 | 未启用L7策略 | 配置CiliumNetworkPolicy |
| 日志文件不增长 | 过滤器配置过严 | 调整include/exclude过滤器 |
| 性能影响 | 指标基数过高 | 禁用不必要的指标标签 |
监控健康检查
# 检查指标端点
curl http://localhost:9962/metrics | grep cilium_endpoint_state
# 验证Hubble指标
curl http://localhost:9965/metrics | grep hubble_flows_processed_total
# 检查日志文件
kubectl exec -n kube-system ds/cilium -- ls -la /var/run/cilium/hubble/
总结
Cilium的网络可观测性体系提供了从数据链路层到应用层的全方位监控能力。通过指标、日志和追踪的深度集成,运维团队可以获得:
- 实时可视化:Grafana仪表板提供直观的网络状态展示
- 深度诊断:Hubble流日志支持细粒度的故障排查
- 安全审计:完整的网络流量记录满足合规要求
- 性能分析:多维度指标帮助识别性能瓶颈
无论是日常运维监控、安全事件调查还是性能优化,Cilium的可观测性功能都能提供强大的支持,让Kubernetes网络不再是一个黑盒子。
下一步行动建议:
- 从基础指标监控开始,逐步启用L7可见性
- 配置日志导出,集成到现有的日志管理平台
- 探索OpenMetrics和分布式追踪的高级功能
- 根据业务需求定制监控仪表板和告警规则
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考



