Cilium 容器网络与安全策略部署实战

一、背景与概述

1.1 为什么选择 Cilium

Cilium 是一款基于 eBPF 技术的开源网络和安全性解决方案,专为容器化环境设计。与传统基于 iptables 的方案相比,Cilium 具有以下优势:

  • 高性能:利用 eBPF 在内核层面处理网络流量,绕过 iptables 链,降低延迟
  • 可观测性:提供细粒度的网络流量可视化,支持 Hubble 进行流量分析
  • 安全性:支持基于身份(Identity)的 L3-L7 网络策略,实现零信任安全模型
  • 可扩展性:支持大规模集群,单节点可处理数万 Pod

1.2 适用场景

  • Kubernetes 集群网络插件(CNI)
  • 微服务间零信任网络安全
  • 多租户集群网络隔离
  • 需要深度网络可观测性的场景

二、环境准备

2.1 系统要求

组件 版本要求 说明
Linux 内核 ≥ 4.19 推荐 5.4+ 以获得完整 eBPF 支持
Kubernetes ≥ 1.19 推荐 1.25+
etcd ≥ 3.4.0 用于存储网络状态
容器运行时 containerd/CRI-O Docker 已弃用

2.2 内核 eBPF 支持检查

1
2
3
4
5
6
7
8
9
10
# 检查内核版本
uname -r

# 检查 eBPF 支持
zgrep CONFIG_BPF /proc/config.gz
zgrep CONFIG_BPF_SYSCALL /proc/config.gz
zgrep CONFIG_BPF_JIT /proc/config.gz

# 检查必要内核功能
bpftool feature list | grep -E "prog|map"

2.3 安装依赖

1
2
3
4
5
# Ubuntu/Debian
apt-get install -y linux-headers-$(uname -r) clang llvm libelf-dev

# CentOS/RHEL
yum install -y kernel-devel-$(uname -r) clang llvm elfutils-libelf-devel

三、Cilium 部署

3.1 使用 Helm 部署(推荐)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 添加 Cilium Helm 仓库
helm repo add cilium https://helm.cilium.io/
helm repo update

# 创建命名空间
kubectl create namespace cilium-system

# 部署 Cilium
helm install cilium cilium/cilium --version 1.15.0 \
--namespace cilium-system \
--set ipam.mode=kubernetes \
--set routingMode=tunnel \
--set tunnelProtocol=vxlan \
--set hubble.enabled=true \
--set hubble.relay.enabled=true \
--set hubble.ui.enabled=true \
--set operator.replicas=1

3.2 核心配置参数说明

参数 推荐值 说明
ipam.mode kubernetes 使用 K8s IPAM,与集群集成
routingMode tunnel/native tunnel 跨节点,native 同网段
tunnelProtocol vxlan/geneve VXLAN 兼容性更好
hubble.enabled true 启用可观测性
encryption.enabled true 启用节点间加密

3.3 验证部署状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 检查 Pod 状态
kubectl get pods -n cilium-system

# 检查 Cilium 节点状态
kubectl get ciliumnodes

# 检查 Cilium 状态
kubectl exec -n cilium-system -t ds/cilium -- cilium status

# 预期输出示例:
# KVStore: Ok Disabled
# Kubernetes: Ok 1.28 (v1.28.0) [linux/amd64]
# Kubernetes APIs: Ok Okay
# cilium-operator: Ok
# Hubble: Ok Current/Max Flows: 4095/4095 (100.00%)
# Cluster health: 3/3 reachable (2024-01-15T10:00:00Z)

四、网络策略配置

4.1 CiliumNetworkPolicy 基础

Cilium 支持 Kubernetes 标准 NetworkPolicy,同时提供扩展的 CiliumNetworkPolicy:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
name: allow-frontend-to-backend
namespace: default
spec:
endpointSelector:
matchLabels:
app: backend
ingress:
- fromEndpoints:
- matchLabels:
app: frontend
toPorts:
- ports:
- port: "8080"
protocol: TCP

4.2 L7 应用层策略

Cilium 支持 HTTP/gRPC/Kafka 等应用层协议策略:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
name: http-policy
namespace: default
spec:
endpointSelector:
matchLabels:
app: api-server
ingress:
- fromEndpoints:
- matchLabels:
app: web-frontend
toPorts:
- ports:
- port: "80"
protocol: TCP
rules:
http:
- method: GET
path: "/api/*"
- method: POST
path: "/api/login"

4.3 基于身份的隔离

1
2
3
4
5
6
7
8
9
10
11
apiVersion: cilium.io/v2
kind: CiliumClusterwideNetworkPolicy
metadata:
name: deny-cross-namespace
spec:
description: "默认拒绝跨命名空间流量"
endpointSelector: {}
ingress:
- fromEndpoints:
- matchLabels:
io.kubernetes.pod.namespace: "" # 仅允许同命名空间

4.4 DNS 策略

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
name: dns-egress-policy
namespace: default
spec:
endpointSelector:
matchLabels:
app: restricted-app
egress:
- toEndpoints:
- matchLabels:
k8s:io.kubernetes.pod.namespace: kube-system
k8s:k8s-app: kube-dns
toPorts:
- ports:
- port: "53"
protocol: UDP
rules:
dns:
- matchPattern: "*.internal.company.com"

五、Hubble 可观测性

5.1 启用 Hubble UI

1
2
3
4
5
6
7
# 端口转发访问 Hubble UI
kubectl port-forward -n cilium-system svc/hubble-ui 12000:80

# 或使用 NodePort/Ingress 暴露
helm upgrade cilium cilium/cilium -n cilium-system \
--set hubble.ui.service.type=NodePort \
--set hubble.ui.service.nodePort=30080

5.2 使用 Hubble CLI

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 安装 Hubble CLI
curl -L --remote-name-all \
https://github.com/cilium/hubble/releases/download/v1.15.0/hubble-linux-amd64.tar.gz
tar xzvf hubble-linux-amd64.tar.gz
sudo mv hubble /usr/local/bin/

# 配置 Hubble
kubectl port-forward -n cilium-system svc/hubble-relay 4245:80
export HUBBLE_SERVER=localhost:4245

# 查看实时流量
hubble observe --follow

# 过滤特定 Pod 流量
hubble observe --pod frontend --follow

# 查看 dropped 流量
hubble observe --type drop --follow

5.3 关键监控指标

1
2
3
4
5
6
7
8
9
# Cilium 核心指标
cilium_agent_ready
cilium_policy_max_revision
cilium_drop_count_total
cilium_forward_count_total

# Hubble 流量指标
hubble_flows_processed_total
hubble_flows_dropped_total

六、高级功能配置

6.1 节点间加密(WireGuard)

1
2
3
4
5
# Helm values
encryption:
enabled: true
type: wireguard
nodeEncryption: true

6.2 带宽管理

1
2
3
bandwidthManager:
enabled: true
bbr: true

6.3 外部集群连接(Cluster Mesh)

1
2
3
4
5
6
7
8
# 启用 Cluster Mesh
helm upgrade cilium cilium/cilium -n cilium-system \
--set cluster.enabled=true \
--set cluster.id=1 \
--set cluster.name=cluster-1

# 连接多个集群
clustermeshctl connect --context cluster-1 --dest-context cluster-2

七、故障排查

7.1 常见问题诊断

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 检查 eBPF 程序加载
cilium bpf cpus list

# 检查策略状态
cilium policy get

# 检查端点状态
cilium endpoint list

# 检查连接跟踪
cilium bpf ct list global

# 查看代理日志
kubectl logs -n cilium-system -l k8s-app=cilium --tail=100

7.2 网络连通性测试

1
2
3
4
5
# 生成网络策略测试报告
cilium policy trace --src-identity 1234 --dst-identity 5678 --dport 80

# 测试 Pod 间连通性
kubectl exec -it frontend-pod -- curl -v http://backend-service:8080

7.3 性能问题排查

1
2
3
4
5
6
7
8
9
10
# 检查 eBPF 程序执行时间
cilium bpf metrics list

# 检查丢包原因
hubble observe --type drop --since 5m | sort | uniq -c

# 常见丢包原因:
# - Policy denied: 策略拒绝
# - Invalid state: 连接状态异常
# - No route to host: 路由问题

八、最佳实践

8.1 安全加固

  1. 默认拒绝策略:为每个命名空间配置默认拒绝策略
  2. 最小权限原则:仅开放必要的端口和协议
  3. 启用加密:生产环境必须启用节点间加密
  4. 定期审计:使用 Hubble 审计网络流量

8.2 性能优化

  1. 选择合适路由模式:同网段用 native,跨网段用 tunnel
  2. 调整 BPF 映射大小:根据 Pod 数量调整 CT 表大小
  3. 启用 XDP:高吞吐场景启用 XDP 加速
  4. 监控内核资源:确保 ulimit 足够

8.3 运维建议

  1. 版本管理:定期升级 Cilium 获取安全补丁
  2. 备份策略:使用 GitOps 管理网络策略
  3. 监控告警:配置关键指标告警
  4. 文档维护:记录网络拓扑和策略设计

九、总结

Cilium 作为新一代容器网络方案,凭借 eBPF 技术提供了卓越的性能和可观测性。通过本指南,您可以:

  • 完成 Cilium 在 Kubernetes 集群的部署
  • 配置 L3-L7 多层网络策略
  • 利用 Hubble 实现网络流量可视化
  • 排查常见网络问题

在生产环境中,建议先在测试环境验证策略,逐步推广到生产集群。


参考资源: