eBPF 与 Cilium 实战:告别 iptables,拥抱云原生网络

iptables 是 Linux 网络的老兵,但在大规模 Kubernetes 集群中,它的性能瓶颈和运维复杂度已经难以接受。eBPF 和 Cilium 正在彻底改变这一局面。

iptables 的问题

在一个有 1000 个 Service 的集群中,iptables 规则可能超过 10 万条。每个数据包都要线性遍历这些规则,延迟随规则数量线性增长。

# 查看当前 iptables 规则数量
iptables -L | wc -l

# 大集群中这个数字可能是 50000+

eBPF 的革命

eBPF(extended Berkeley Packet Filter)允许在内核中安全运行自定义程序,无需修改内核代码:

传统方式:数据包 → 内核网络栈 → iptables 规则链 → 目标
eBPF 方式:数据包 → eBPF 程序(XDP/TC hook)→ 直接转发

核心优势:

  • O(1) 查找:用哈希表替代线性规则链
  • 内核态执行:零用户态切换开销
  • 动态加载:无需重启内核即可更新逻辑
  • 可观测性:在内核层采集精确的网络数据

安装 Cilium

# 使用 Helm 安装
helm repo add cilium https://helm.cilium.io/
helm repo update

helm install cilium cilium/cilium \
  --namespace kube-system \
  --set kubeProxyReplacement=true \
  --set k8sServiceHost=<API_SERVER_IP> \
  --set k8sServicePort=6443 \
  --set hubble.relay.enabled=true \
  --set hubble.ui.enabled=true

# 验证安装
cilium status --wait

kubeProxyReplacement=true 让 Cilium 完全替代 kube-proxy,用 eBPF 处理所有 Service 流量。

Cilium NetworkPolicy

Cilium 扩展了标准 NetworkPolicy,支持 L7 策略:

# 标准 K8s NetworkPolicy(L3/L4)
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-api
spec:
  podSelector:
    matchLabels:
      app: backend
  ingress:
  - from:
    - podSelector:
        matchLabels:
          app: frontend
    ports:
    - port: 8080
# Cilium NetworkPolicy(L7,HTTP 方法级别控制)
apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
  name: allow-api-get-only
spec:
  endpointSelector:
    matchLabels:
      app: backend
  ingress:
  - fromEndpoints:
    - matchLabels:
        app: frontend
    toPorts:
    - ports:
      - port: "8080"
        protocol: TCP
      rules:
        http:
        - method: GET          # 只允许 GET 请求
          path: "/api/v1/.*"   # 只允许特定路径

Hubble:网络可观测性

Hubble 是 Cilium 内置的可观测性平台,基于 eBPF 采集数据,零侵入:

# 安装 Hubble CLI
HUBBLE_VERSION=$(curl -s https://raw.githubusercontent.com/cilium/hubble/master/stable.txt)
curl -L --remote-name-all \
  https://github.com/cilium/hubble/releases/download/$HUBBLE_VERSION/hubble-linux-amd64.tar.gz
tar xzvf hubble-linux-amd64.tar.gz
mv hubble /usr/local/bin/

# 开启端口转发
cilium hubble port-forward &

# 实时观察流量
hubble observe --follow

# 过滤特定 Pod 的流量
hubble observe --pod production/api-server --follow

# 查看 HTTP 请求详情
hubble observe --protocol http --follow

# 查看被拒绝的流量(排查网络策略问题)
hubble observe --verdict DROPPED --follow

Hubble UI 提供可视化的服务依赖图,访问:

cilium hubble ui
# 自动打开浏览器,展示实时流量拓扑

Tetragon:运行时安全

Tetragon 是基于 eBPF 的运行时安全工具,可以检测容器内的异常行为:

# 检测容器内执行 shell 命令
apiVersion: cilium.io/v1alpha1
kind: TracingPolicy
metadata:
  name: detect-shell-exec
spec:
  kprobes:
  - call: "sys_execve"
    syscall: true
    args:
    - index: 0
      type: "string"
    selectors:
    - matchBinaries:
      - operator: In
        values:
        - "/bin/sh"
        - "/bin/bash"
      matchActions:
      - action: Sigkill   # 直接杀掉进程
# 实时监控安全事件
tetra getevents -o compact --pods nginx

性能对比

在 1000 节点集群的基准测试中:

指标kube-proxy (iptables)Cilium (eBPF)
Service 转发延迟~0.8ms~0.1ms
规则更新时间秒级毫秒级
CPU 开销高(规则遍历)低(哈希查找)
最大 Service 数~5000无明显上限

eBPF 和 Cilium 代表了 Kubernetes 网络的未来方向,现在是时候在生产集群中认真评估迁移了。

← 返回文章列表