HiHuo
首页
博客
手册
工具
关于
首页
博客
手册
工具
关于
  • 网络架构师学习手册

    • 网络架构师学习教程
    • 基础篇

      • 第1章 网络模型与数据流转
      • 第2章 以太网与二层通信
      • 第3章 IP路由与三层转发
      • 第4章 TCP与可靠传输
      • 第5章 应用层协议
    • Linux网络栈

      • 第6章 数据包接收路径
      • 第7章 多核网络优化
      • 第8章 Netfilter与防火墙
      • 第9章 流量控制与QoS
    • 虚拟网络

      • 第10章 Network Namespace基础
      • 第11章 Bridge与互联
      • 第12章 VXLAN与Overlay网络
      • 第13章 OVS与SDN
    • Kubernetes网络

      • 第14章 CNI模型与实现
      • 第15章 kube-proxy与Service实现
      • 第16章 CoreDNS与服务发现
      • 第17章 NetworkPolicy与安全隔离
      • 第18章 Calico网络深度解析
      • 第19章 Cilium与eBPF网络
    • 网络架构

      • 第20章 网络设备与拓扑设计
      • 第21章 网络容量规划与计算
      • 第22章 负载均衡架构设计
      • 第23章 高可用网络架构
      • 第24章 网络安全架构
    • 性能调优

      • 第25章 系统级网络调优
      • 第26章 故障排查方法论
      • 第27章 生产环境案例分析
    • 前沿技术

      • 第28章 eBPF深度实践
      • 第29章 ServiceMesh与边车代理
      • 第30章 网络技术趋势与未来展望
    • 附录

      • 附录A:命令速查手册
      • 附录B:排错决策树
      • 附录C:学习资源
      • 附录D:技能图谱

第19章 Cilium与eBPF网络

学习目标

  • 深入理解Cilium的eBPF网络架构
  • 掌握Cilium的安装配置和高级功能
  • 了解eBPF在网络加速中的应用
  • 能够使用Hubble进行网络可观测性分析

前置知识

  • 第18章:Calico深度解析
  • 第28章:eBPF深度实践
  • 第8章:Netfilter与防火墙

19.1 Cilium概述

19.1.1 什么是Cilium

Cilium是基于eBPF的云原生网络和安全解决方案,提供高性能的网络连接、负载均衡和网络安全功能。

核心特性:

  • 基于eBPF的高性能数据平面
  • 透明的网络加密
  • 网络可观测性(Hubble)
  • 服务网格集成
  • 网络策略和安全

19.1.2 Cilium架构

┌─────────────────────────────────────────────────────────────┐
│                    Cilium Architecture                      │
├─────────────────────────────────────────────────────────────┤
│  Cilium Agent (cilium-agent)                              │
├─────────────────────────────────────────────────────────────┤
│  eBPF Programs (XDP, TC, CGROUP)                          │
├─────────────────────────────────────────────────────────────┤
│  Linux Kernel (eBPF Runtime)                              │
├─────────────────────────────────────────────────────────────┤
│  Hubble (Observability)                                   │
└─────────────────────────────────────────────────────────────┘

19.1.3 eBPF在网络中的应用

1. XDP(eXpress Data Path)

  • 网络数据包处理
  • 高性能DDoS防护
  • 负载均衡

2. TC(Traffic Control)

  • 网络策略实施
  • 流量整形
  • 包过滤

3. CGROUP

  • 容器网络隔离
  • 资源限制
  • 安全策略

19.2 Cilium安装与配置

19.2.1 使用Helm安装

添加Cilium Helm仓库:

# 添加Helm仓库
helm repo add cilium https://helm.cilium.io/
helm repo update

# 查看可用版本
helm search repo cilium/cilium

安装Cilium:

# 安装Cilium
helm install cilium cilium/cilium \
  --namespace kube-system \
  --set global.kubeProxyReplacement=strict \
  --set global.k8sServiceHost=192.168.1.100 \
  --set global.k8sServicePort=6443

# 验证安装
kubectl get pods -n kube-system -l k8s-app=cilium

19.2.2 使用Cilium CLI

安装Cilium CLI:

# 下载Cilium CLI
curl -L --remote-name-all https://github.com/cilium/cilium-cli/releases/latest/download/cilium-linux-amd64.tar.gz
tar xzvf cilium-linux-amd64.tar.gz
sudo mv cilium /usr/local/bin/

# 验证安装
cilium version

使用Cilium CLI:

# 检查Cilium状态
cilium status

# 检查连接性
cilium connectivity test

# 查看节点信息
cilium node list

19.3 eBPF网络功能

19.3.1 网络连接

查看网络连接:

# 查看Cilium端点
cilium endpoint list

# 查看网络策略
cilium policy get

# 查看服务
cilium service list

网络连接测试:

# 测试Pod间连通性
cilium connectivity test

# 测试特定Pod
cilium connectivity test --test-name=pod-to-pod

19.3.2 负载均衡

查看负载均衡配置:

# 查看服务负载均衡
cilium service list

# 查看负载均衡统计
cilium bpf lb list

# 查看负载均衡健康检查
cilium bpf lb list --verbose

19.3.3 网络策略

创建网络策略:

# cilium-network-policy.yaml
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: "80"
        protocol: TCP

应用网络策略:

# 应用策略
kubectl apply -f cilium-network-policy.yaml

# 查看策略状态
cilium policy get

# 测试策略效果
cilium connectivity test --test-name=pod-to-pod

19.4 Hubble可观测性

19.4.1 安装Hubble

使用Helm安装Hubble:

# 安装Hubble
helm install hubble cilium/cilium \
  --namespace kube-system \
  --set global.hubble.enabled=true \
  --set global.hubble.relay.enabled=true \
  --set global.hubble.ui.enabled=true

# 验证安装
kubectl get pods -n kube-system -l k8s-app=hubble-ui

配置Hubble访问:

# 端口转发
kubectl port-forward -n kube-system svc/hubble-ui 12000:80

# 访问Hubble UI
# http://localhost:12000

19.4.2 Hubble功能

查看网络流:

# 使用Hubble CLI
hubble observe

# 查看特定Pod的流
hubble observe --pod nginx-xxx

# 查看网络策略相关流
hubble observe --verdict DROPPED

网络拓扑:

# 查看网络拓扑
hubble observe --follow --format=json | jq 'select(.event_type == "Trace")'

# 查看服务依赖
hubble observe --follow --format=json | jq 'select(.event_type == "Trace" and .l7 != null)'

19.5 实验:Cilium网络分析

19.5.1 实验环境准备

创建测试应用:

# test-app.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: frontend
  labels:
    app: frontend
spec:
  replicas: 2
  selector:
    matchLabels:
      app: frontend
  template:
    metadata:
      labels:
        app: frontend
    spec:
      containers:
      - name: nginx
        image: nginx:1.20
        ports:
        - containerPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: backend
  labels:
    app: backend
spec:
  replicas: 2
  selector:
    matchLabels:
      app: backend
  template:
    metadata:
      labels:
        app: backend
    spec:
      containers:
      - name: nginx
        image: nginx:1.20
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: backend-service
spec:
  selector:
    app: backend
  ports:
  - port: 80
    targetPort: 80
  type: ClusterIP

19.5.2 实验1:网络连通性分析

步骤1:部署应用

# 部署应用
kubectl apply -f test-app.yaml

# 查看Pod状态
kubectl get pods -o wide

步骤2:分析网络配置

# 查看Cilium端点
cilium endpoint list

# 查看网络连接
cilium connectivity test

# 查看服务配置
cilium service list

步骤3:测试连通性

# 测试Pod间通信
kubectl exec -it frontend-pod -- wget -qO- http://backend-service

# 查看网络流
hubble observe --pod frontend-pod

19.5.3 实验2:网络策略测试

步骤1:创建网络策略

# cilium-policy.yaml
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: "80"
        protocol: TCP

步骤2:应用策略

# 应用策略
kubectl apply -f cilium-policy.yaml

# 查看策略状态
cilium policy get

# 测试策略效果
cilium connectivity test --test-name=pod-to-pod

19.5.4 实验3:Hubble可观测性

步骤1:启动Hubble UI

# 端口转发
kubectl port-forward -n kube-system svc/hubble-ui 12000:80

# 访问Hubble UI
# http://localhost:12000

步骤2:分析网络流

# 查看实时网络流
hubble observe --follow

# 查看特定Pod的流
hubble observe --pod frontend-pod --follow

# 查看被拒绝的流
hubble observe --verdict DROPPED --follow

19.6 高级功能

19.6.1 网络加密

启用网络加密:

# cilium-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: cilium-config
  namespace: kube-system
data:
  encryption:
    enabled: "true"
    type: "wireguard"

验证加密:

# 查看加密状态
cilium status

# 查看加密统计
cilium bpf encrypt list

19.6.2 服务网格集成

启用服务网格:

# cilium-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: cilium-config
  namespace: kube-system
data:
  enable-hubble: "true"
  enable-hubble-grpc: "true"
  enable-hubble-metrics: "true"
  hubble-metrics:
    - "dns"
    - "drop"
    - "tcp"
    - "flow"
    - "port-distribution"
    - "icmp"
    - "http"

19.6.3 性能优化

eBPF性能调优:

# cilium-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: cilium-config
  namespace: kube-system
data:
  bpf-map-dynamic-size-ratio: "0.0025"
  bpf-policy-map-max: "16384"
  bpf-lb-map-max: "65536"
  bpf-lb-acceleration: "native"
  bpf-lb-dsr-dispatch: "opt"
  bpf-lb-dsr-l4-xlate: "opt"

19.7 故障排查

19.7.1 常见问题诊断

问题1:Cilium启动失败

# 检查Cilium状态
cilium status

# 查看Cilium日志
kubectl logs -n kube-system -l k8s-app=cilium

# 检查eBPF支持
cilium status --verbose

问题2:网络连通性问题

# 运行连接性测试
cilium connectivity test

# 查看网络流
hubble observe --follow

# 检查网络策略
cilium policy get

问题3:性能问题

# 查看eBPF程序
cilium bpf prog list

# 查看eBPF映射
cilium bpf map list

# 查看性能统计
cilium metrics list

19.7.2 调试工具

# 使用Cilium CLI调试
cilium debuginfo

# 使用Hubble调试
hubble observe --follow --format=json

# 使用eBPF工具
cilium bpf prog list
cilium bpf map list

19.8 排错清单

19.8.1 Cilium组件检查

  • [ ] Cilium Agent是否正常运行
  • [ ] eBPF程序是否正确加载
  • [ ] 网络接口是否正确配置
  • [ ] 网络策略是否正确应用
  • [ ] Hubble是否正常工作

19.8.2 网络连通性检查

  • [ ] Pod网络接口是否正确创建
  • [ ] 网络连接是否正常
  • [ ] 负载均衡是否正常工作
  • [ ] 网络策略是否生效
  • [ ] 加密是否正常工作

19.8.3 性能问题检查

  • [ ] eBPF程序是否优化
  • [ ] 内存使用是否正常
  • [ ] CPU使用率是否过高
  • [ ] 网络延迟是否正常
  • [ ] 吞吐量是否满足要求

19.9 延伸阅读

  • Cilium Documentation
  • eBPF Documentation
  • Hubble Documentation
  • Cilium eBPF Programs

下一章:第20章 设备与拓扑

返回目录:README

Prev
第18章 Calico网络深度解析