1. # Kubernetes CNI Cilium
  2. # 时间:20210908-[20:00-22:00]
  3. # 主题:Cilium Native Routing
  4. # 文档:
  5. # 作业:Cilium Native Routing
  1. # FAQ:

INSTALL GUIDE WITH KUBEADM

  1. ### 先挂载bpf:
  2. #Mount bpf as a service:
  3. cat <<EOF | sudo tee /etc/systemd/system/sys-fs-bpf.mount
  4. [Unit]
  5. Description=Cilium BPF mounts
  6. Documentation=https://docs.cilium.io/
  7. DefaultDependencies=no
  8. Before=local-fs.target umount.target
  9. After=swap.target
  10. [Mount]
  11. What=bpffs
  12. Where=/sys/fs/bpf
  13. Type=bpf
  14. Options=rw,nosuid,nodev,noexec,relatime,mode=700
  15. [Install]
  16. WantedBy=multi-user.target
  17. EOF
  18. systemctl enable sys-fs-bpf.mount
  19. systemctl restart sys-fs-bpf.mount
  20. ###
  21. # 鉴于安装cilium在网上有非常多的教程,但是又没有一个完全或是具体的说明,加上版本的差异,造成挪过来以出现不可用的情况,加上CIlium又处于一个快速发展的阶段,所以,我们建议安装相关的手册以官网为主,其他网站内容为辅的手段进行学习。
  22. # 文档使用方式介绍:
  23. https://docs.cilium.io/en/v1.10/ ---> https://docs.cilium.io/en/v1.10/gettingstarted/#gs-install ---> https://docs.cilium.io/en/v1.10/gettingstarted/k8s-install-advanced/ ---> https://docs.cilium.io/en/v1.10/gettingstarted/k8s-install-kubeadm/ ---> https://docs.cilium.io/en/v1.10/gettingstarted/kubeproxy-free/#kubeproxy-free # 到这里我们就正式找到cilium安装过程without kube-proxy的guide。
  24. # 1. Note。版本说明:
  25. Cilium’s kube-proxy replacement depends on the Host-Reachable Services feature, therefore a v4.19.57, v5.1.16, v5.2.0 or more recent Linux kernel is required. Linux kernels v5.3 and v5.8 add additional features that Cilium can use to further optimize the kube-proxy replacement implementation.
  26. Note that v5.0.y kernels do not have the fix required to run the kube-proxy replacement since at this point in time the v5.0.y stable kernel is end-of-life (EOL) and not maintained anymore on kernel.org. For individual distribution maintained kernels, the situation could differ. Therefore, please check with your distribution.
  27. [root@k8s-1 ~]# uname -a # 我们此时推荐使用的Linux kernel version
  28. Linux k8s-1 5.11.10-1.el7.elrepo.x86_64 #1 SMP Thu Mar 25 09:56:28 EDT 2021 x86_64 x86_64 x86_64 GNU/Linux
  29. # 2. Quick Start:
  30. # 2.1: kubeadm init --skip-phases=addon/kube-proxy ## 使用kubeadm安装kubernetes cluster的时候,skip kube-proxy 的 addons。
  31. # 2.2:如果系统中已经有了kube-proxy,如何处理:
  32. For existing installations with kube-proxy running as a DaemonSet, remove it by using the following commands below. Careful: Be aware that this will break existing service connections. It will also stop service related traffic until the Cilium replacement has been installed:
  33. $ kubectl -n kube-system delete ds kube-proxy
  34. $ # Delete the configmap as well to avoid kube-proxy being reinstalled during a kubeadm upgrade (works only for K8s 1.19 and newer)
  35. $ kubectl -n kube-system delete cm kube-proxy
  36. $ # Run on each node:
  37. $ iptables-restore <(iptables-save | grep -v KUBE)
  38. # 3. 使用HELM安装Cilium:[此时使用HELM 3]
  39. 添加 Cilium的repo:
  40. helm repo add cilium https://helm.cilium.io/
  41. # 4.使用helm安装cilium:
  42. generate the required YAML files and deploy them:
  43. 这里官网建议我们直接使用helm 去install cilium,我们这里建议使用helm template去生成相应的helm template,这样可以修改其中的部分字段。(当然这些需要修改的字段我们也可以在helm install的 时候特殊指定。不过通常我们还是习惯使用helm template生成template,然后再修改相应的字段,然后再使用kubectl apply -f cilium-xx.yaml)
  44. # helm template cilium cilium/cilium --version 1.10 --namespace kube-system --set kubeProxyReplacement=strict --set k8sServiceHost=192.168.2.21 --set k8sServicePort=6443
  45. # 5.由于我们这里是使用Native Routeing的方案,所以,我们需要做必要的修改:
  46. https://docs.cilium.io/en/v1.10/concepts/networking/routing/ # 参考这里Native Routing模块。
  47. # Disadvantages of the model #目前看来Cilium中认为使用Tunnel的弊端只有这里。
  48. # MTU Overhead
  49. Due to adding encapsulation headers, the effective MTU available for payload is lower than with native-routing (50 bytes per network packet for VXLAN). This results in a lower maximum throughput rate for a particular network connection. This can be largely mitigated by enabling jumbo frames (50 bytes of overhead for each 1500 bytes vs 50 bytes of overhead for each 9000 bytes). # 使用巨型帧:9000-50 来解决这个tunnel模式的mtu的overhead的问题。
  50. # 6.Native Routing:
  51. The native routing datapath is enabled with tunnel: disabled and enables the native packet forwarding mode. The native packet forwarding mode leverages the routing capabilities of the network Cilium runs on instead of performing encapsulation.
  52. # Cilium automatically enables IP forwarding in the Linux kernel when native routing is configured.
  53. In order to run the native routing mode, the network connecting the hosts on which Cilium is running on must be capable of forwarding IP traffic using addresses given to pods or other workloads.
  54. The Linux kernel on the node must be aware on how to forward packets of pods or other workloads of all nodes running Cilium. This can be achieved in two ways:
  55. The node itself does not know how to route all pod IPs but a router exists on the network that knows how to reach all other pods. In this scenario, the Linux node is configured to contain a default route to point to such a router. This model is used for cloud provider network integration. See Google Cloud, AWS ENI, and Azure IPAM for more details.
  56. Each individual node is made aware of all pod IPs of all other nodes and routes are inserted into the Linux kernel routing table to represent this. If all nodes share a single L2 network, then this can be taken care of by enabling the option auto-direct-node-routes: true. Otherwise, an additional system component such as a BGP daemon must be run to distribute the routes. See the guide Using kube-router to run BGP on how to achieve this using the kube-router project.
  57. Configuration
  58. The following configuration options must be set to run the datapath in native routing mode:
  59. tunnel: disabled: Enable native routing mode.
  60. native-routing-cidr: x.x.x.x/y: Set the CIDR in which native routing can be performed.
  61. The following configuration options must be set to run the datapath in native routing mode:
  62. 1.tunnel: disabled: Enable native routing mode.
  63. # tunnel: disabled
  64. 2.native-routing-cidr: x.x.x.x/y: Set the CIDR in which native routing can be performed.
  65. # native-routing-cidr: "10.0.0.0/16"
  66. # If all nodes share a single L2 network, then this can be taken care of by enabling the option auto-direct-node-routes: true. Otherwise, an additional system component such as a BGP daemon must be run to distribute the routes. See the guide Using kube-router to run BGP on how to achieve this using the kube-router project.
  67. 3.# 开启auto-direct-node-routes:true
  68. root@k8s-1:/home/cilium# cilium-agent -h| grep auto-direct-node-routes
  69. --auto-direct-node-routes Enable automatic L2 routing between nodes
  70. 4.# enable-endpoint-routes:false # 设置为flase
  71. root@k8s-1:/home/cilium# cilium-agent -h| grep enable-endpoint-routes
  72. --enable-endpoint-routes Use per endpoint routes instead of routing via cilium_host
  73. # ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— #
  74. [root@k8s-1 ~]# kubectl get pods -o wide -A
  75. NAMESPACE NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  76. default cni-bd8jc 1/1 Running 0 24h 10.0.1.20 k8s-2 <none> <none>
  77. default cni-vdb9g 1/1 Running 0 24h 10.0.0.235 k8s-1 <none> <none>
  78. kube-system cilium-h5bhb 1/1 Running 0 24h 192.168.2.21 k8s-1 <none> <none>
  79. kube-system cilium-kmjjr 1/1 Running 0 24h 192.168.2.22 k8s-2 <none> <none>
  80. kube-system cilium-operator-6c8777f4fc-cg7jr 1/1 Running 0 24h 192.168.2.21 k8s-1 <none> <none>
  81. kube-system cilium-operator-6c8777f4fc-z8hhx 1/1 Running 0 24h 192.168.2.22 k8s-2 <none> <none>
  82. kube-system coredns-7f89b7bc75-kx96m 1/1 Running 0 4d1h 10.0.0.13 k8s-1 <none> <none>
  83. kube-system coredns-7f89b7bc75-pm9hw 1/1 Running 0 4d1h 10.0.0.69 k8s-1 <none> <none>
  84. kube-system etcd-k8s-1 1/1 Running 1 4d1h 192.168.2.21 k8s-1 <none> <none>
  85. kube-system kube-apiserver-k8s-1 1/1 Running 2 4d1h 192.168.2.21 k8s-1 <none> <none>
  86. kube-system kube-controller-manager-k8s-1 1/1 Running 1 4d1h 192.168.2.21 k8s-1 <none> <none>
  87. kube-system kube-scheduler-k8s-1 1/1 Running 1 4d1h 192.168.2.21 k8s-1 <none> <none>
  88. [root@k8s-1 ~]#
  89. [root@k8s-1 ~]# kubectl -nkube-system exec -it cilium-4krnr bash
  90. kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
  91. root@k8s-1:/home/cilium#
  92. # 查询Cilium的信息:
  93. # https://docs.cilium.io/en/latest/operations/performance/tuning/#ebpf-host-routing
  94. root@k8s-2:/home/cilium# cilium-agent -h | grep install-no-conntrack-iptables-rules
  95. --install-no-conntrack-iptables-rules Install Iptables rules to skip netfilter connection tracking on all pod traffic. This option is only effective when Cilium is running in direct routing and full KPR mode. Moreover, this option cannot be enabled when Cilium is running in a managed Kubernetes environment or in a chained CNI setup.
  96. root@k8s-1:/home/cilium# cilium status
  97. KVStore: Ok Disabled
  98. Kubernetes: Ok 1.20 (v1.20.5) [linux/amd64]
  99. Kubernetes APIs: ["cilium/v2::CiliumClusterwideNetworkPolicy", "cilium/v2::CiliumEndpoint", "cilium/v2::CiliumNetworkPolicy", "cilium/v2::CiliumNode", "core/v1::Namespace", "core/v1::Node", "core/v1::Pods", "core/v1::Service", "discovery/v1beta1::EndpointSlice", "networking.k8s.io/v1::NetworkPolicy"]
  100. KubeProxyReplacement: Strict [ens33 192.168.2.21 (Direct Routing)]
  101. Cilium: Ok 1.10.3 (v1.10.3-4145278)
  102. NodeMonitor: Listening for events on 128 CPUs with 64x4096 of shared memory
  103. Cilium health daemon: Ok
  104. IPAM: IPv4: 4/254 allocated from 10.0.0.0/24,
  105. BandwidthManager: Disabled
  106. Host Routing: BPF # Enhance Feature # https://docs.cilium.io/en/latest/operations/performance/tuning/#ebpf-host-routing
  107. # Host Routing: BPF # https://cilium.io/blog/2021/05/11/cni-benchmark#breaking-the-rules-ebpf-host-routing
  108. Masquerading: BPF [ens33] 10.0.0.0/24 [IPv4: Enabled, IPv6: Disabled]
  109. Controller Status: 30/30 healthy
  110. Proxy Status: OK, ip 10.0.0.156, 0 redirects active on ports 10000-20000
  111. Hubble: Ok Current/Max Flows: 4095/4095 (100.00%), Flows/s: 8.40 Metrics: Disabled
  112. Encryption: Disabled
  113. Cluster health: 2/2 reachable (2021-08-31T12:54:01Z)
  114. root@k8s-1:/home/cilium#
  115. #3.下载Cilium 二进制文件:
  116. wget https://github.com/cilium/cilium-cli/releases/download/v0.8.6/cilium-linux-amd64.tar.gz

20210908-Cilium Native-Routing - 图1
Cilium Native Routing

HOST-Routing-[BPF]

20210908-Cilium Native-Routing - 图2

  1. # https://cilium.io/blog/2021/05/11/cni-benchmark
  2. eBPF host-routing allows to bypass all of the iptables and upper stack overhead in the host namespace as well as some of the context-switching overhead when traversing through the Virtual Ethernet pairs.
  3. # Ingress: Network packets are picked up as early as possible from the network device facing the network and delivered directly into the network namespace of the Kubernetes Pod.
  4. # Egress: On the egress side, the packet still traverses the veth pair, is picked up by eBPF and delivered directly to the external facing network interface. The routing table is consulted directly from eBPF so this optimization is entirely transparent and compatible with any other services running on the system providing route distribution. For information on how to enable this feature, see eBPF Host-Routing in the tuning guide.
  5. Calico eBPF is applying some of the same bypasses to iptables but as we'll learn later on, is not quite identical. However, it proves that the most impact can by made by bypassing slow kernel subsystems such as iptables.

Requirements on the network

  1. # 参考官网:
  2. # https://docs.cilium.io/en/v1.10/concepts/networking/routing/
  3. In order to run the native routing mode, the network connecting the hosts on which Cilium is running on must be capable of forwarding IP traffic using addresses given to pods or other workloads.
  4. The Linux kernel on the node must be aware on how to forward packets of pods or other workloads of all nodes running Cilium. This can be achieved in two ways:
  5. The node itself does not know how to route all pod IPs but a router exists on the network that knows how to reach all other pods. In this scenario, the Linux node is configured to contain a default route to point to such a router. This model is used for cloud provider network integration. See Google Cloud, AWS ENI, and Azure IPAM for more details.
  6. Each individual node is made aware of all pod IPs of all other nodes and routes are inserted into the Linux kernel routing table to represent this. If all nodes share a single L2 network, then this can be taken care of by enabling the option auto-direct-node-routes: true. Otherwise, an additional system component such as a BGP daemon must be run to distribute the routes. See the guide Using kube-router to run BGP on how to achieve this using the kube-router project.
  7. Configuration
  8. The following configuration options must be set to run the datapath in native routing mode:
  9. # 1.tunnel: disabled: Enable native routing mode.
  10. # 2.native-routing-cidr: x.x.x.x/y: Set the CIDR in which native routing can be performed.
  11. # 3.If all nodes share a single L2 network, then this can be taken care of by enabling the option auto-direct-node-routes: true

同节点Pod之间通信

  1. # env 描述:
  2. [root@k8s-2 ~]# kubectl get pods -o wide | grep k8s-2
  3. cc 1/1 Running 0 16m 10.0.1.211 k8s-2 <none> <none>
  4. cni-dq6rz 1/1 Running 0 17m 10.0.1.73 k8s-2 <none> <none>
  5. [root@k8s-2 ~]#
  6. 在 5.10 内核以后,Cilium 新增了 eBPF Host-Routing 功能,该功能更加速了 eBPF 数据面性能,新增了 bpf_redirect_peer 和 bpf_redirect_neigh 两个 redirect 方式,bpf_redirect_peer 可以理解成 bpf_redirect 的升级版,其将数据包直接送到 veth pair Pod 里面接口 eth0 上,而不经过宿主机的 lxc 接口,这样实现的好处是少进入一次 cpu backlog queue 队列,该特性引入后,路由模式下,Pod -> Pod 性能接近 Node -> Node 性能,同时 Cilium 数据面路径发生了较大的变化。
  7. # https://www.infoq.cn/article/p9vg2g9t49kpvhrckfwu
  8. 1. 除了 host->Pod 外,所有路径都是经过接口跳着走的,最大变化是不再经过 Kernel 转发处理,也意味着来回流量路径不再经过 kernel 的 Netfilter 框架,kernel tc 等模块,大大提升了转发性能;
  9. 2.redirect_peer 特性专用于 veth pair 类型接口,因为流量被直接重定向到 Pod 里面的接口,所以在宿主机 lxc 口上无法抓到进入 Pod 的流量,因 tcpdump 抓包点在 tc ingress hook 之前,所以可以抓到出 Pod 未经过 eBPF 处理的流量。
  10. 我们在HOST NS中抓包:
  11. [root@k8s-2 ~]# kubectl exec -it cc -- ip a
  12. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
  13. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  14. inet 127.0.0.1/8 scope host lo
  15. valid_lft forever preferred_lft forever
  16. 10: eth0@if11: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
  17. link/ether 16:ca:13:c3:7c:8f brd ff:ff:ff:ff:ff:ff link-netnsid 0
  18. inet 10.0.1.211/32 scope global eth0
  19. valid_lft forever preferred_lft forever
  20. [root@k8s-2 ~]# kubectl exec -it cni-dq6rz -- ip a
  21. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
  22. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  23. inet 127.0.0.1/8 scope host lo
  24. valid_lft forever preferred_lft forever
  25. 8: eth0@if9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
  26. link/ether e6:73:63:ca:dd:4c brd ff:ff:ff:ff:ff:ff link-netnsid 0
  27. inet 10.0.1.73/32 scope global eth0
  28. valid_lft forever preferred_lft forever
  29. [root@k8s-2 ~]# ip a
  30. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
  31. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  32. inet 127.0.0.1/8 scope host lo
  33. valid_lft forever preferred_lft forever
  34. inet6 ::1/128 scope host
  35. valid_lft forever preferred_lft forever
  36. 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq state UP group default qlen 1000
  37. link/ether 00:0c:29:e2:bf:86 brd ff:ff:ff:ff:ff:ff
  38. inet 192.168.2.22/24 brd 192.168.2.255 scope global noprefixroute ens33
  39. valid_lft forever preferred_lft forever
  40. inet6 fe80::a9cd:74a4:47fc:9fec/64 scope link noprefixroute
  41. valid_lft forever preferred_lft forever
  42. 3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
  43. link/ether 02:42:75:f5:10:87 brd ff:ff:ff:ff:ff:ff
  44. inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
  45. valid_lft forever preferred_lft forever
  46. 4: cilium_net@cilium_host: <BROADCAST,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
  47. link/ether b2:6e:6f:c8:53:fc brd ff:ff:ff:ff:ff:ff
  48. inet6 fe80::b06e:6fff:fec8:53fc/64 scope link
  49. valid_lft forever preferred_lft forever
  50. 5: cilium_host@cilium_net: <BROADCAST,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
  51. link/ether 52:9b:a0:7a:60:f9 brd ff:ff:ff:ff:ff:ff
  52. inet 10.0.1.123/32 scope link cilium_host
  53. valid_lft forever preferred_lft forever
  54. inet6 fe80::509b:a0ff:fe7a:60f9/64 scope link
  55. valid_lft forever preferred_lft forever
  56. 7: lxc_health@if6: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
  57. link/ether f2:bf:2f:84:81:fb brd ff:ff:ff:ff:ff:ff link-netnsid 0
  58. inet6 fe80::f0bf:2fff:fe84:81fb/64 scope link
  59. valid_lft forever preferred_lft forever
  60. 9: lxc59b348afd5c1@if8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
  61. link/ether c2:63:ff:dd:de:bc brd ff:ff:ff:ff:ff:ff link-netnsid 1
  62. inet6 fe80::c063:ffff:fedd:debc/64 scope link
  63. valid_lft forever preferred_lft forever
  64. 11: lxc23801e340e9a@if10: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
  65. link/ether 1a:1a:f0:a8:a5:bf brd ff:ff:ff:ff:ff:ff link-netnsid 2
  66. inet6 fe80::181a:f0ff:fea8:a5bf/64 scope link
  67. valid_lft forever preferred_lft forever
  68. [root@k8s-2 ~]#
  69. [root@k8s-2 ~]# kubectl exec -ti cc -- ping 10.0.1.73
  70. PING 10.0.1.73 (10.0.1.73): 56 data bytes
  71. 64 bytes from 10.0.1.73: seq=0 ttl=63 time=0.123 ms
  72. ^C
  73. --- 10.0.1.73 ping statistics ---
  74. 1 packets transmitted, 1 packets received, 0% packet loss
  75. round-trip min/avg/max = 0.123/0.123/0.123 ms
  76. [root@k8s-2 ~]#
  77. 抓包结果:
  78. [root@k8s-2 ~]# tcpdump -ne -i lxc23801e340e9a
  79. tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
  80. listening on lxc23801e340e9a, link-type EN10MB (Ethernet), capture size 262144 bytes
  81. 22:16:16.371807 16:ca:13:c3:7c:8f > 1a:1a:f0:a8:a5:bf, ethertype IPv4 (0x0800), length 98: 10.0.1.211 > 10.0.1.73: ICMP echo request, id 19968, seq 0, length 64
  82. 22:16:21.607147 16:ca:13:c3:7c:8f > 1a:1a:f0:a8:a5:bf, ethertype ARP (0x0806), length 42: Request who-has 10.0.1.123 tell 10.0.1.211, length 28
  83. 22:16:21.607205 1a:1a:f0:a8:a5:bf > 16:ca:13:c3:7c:8f, ethertype ARP (0x0806), length 42: Reply 10.0.1.123 is-at 1a:1a:f0:a8:a5:bf, length 28
  84. ^C
  85. 3 packets captured
  86. 3 packets received by filter
  87. 0 packets dropped by kernel
  88. [root@k8s-2 ~]# tcpdump -ne -i lxc59b348afd5c1
  89. tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
  90. listening on lxc59b348afd5c1, link-type EN10MB (Ethernet), capture size 262144 bytes
  91. 22:16:16.371876 e6:73:63:ca:dd:4c > c2:63:ff:dd:de:bc, ethertype IPv4 (0x0800), length 98: 10.0.1.73 > 10.0.1.211: ICMP echo reply, id 19968, seq 0, length 64
  92. 22:16:21.607134 e6:73:63:ca:dd:4c > c2:63:ff:dd:de:bc, ethertype ARP (0x0806), length 42: Request who-has 10.0.1.123 tell 10.0.1.73, length 28
  93. 22:16:21.607181 c2:63:ff:dd:de:bc > e6:73:63:ca:dd:4c, ethertype ARP (0x0806), length 42: Reply 10.0.1.123 is-at c2:63:ff:dd:de:bc, length 28
  94. # 2.redirect_peer 特性专用于 veth pair 类型接口,因为流量被直接重定向到 Pod 里面的接口,所以在宿主机 lxc 口上无法抓到进入 Pod 的流量,因 tcpdump 抓包点在 tc ingress hook 之前,所以可以抓到出 Pod 未经过 eBPF 处理的流量。
  95. # 解释一下这句话所要表达的内容:什么叫做:只能抓到出去的包,而不能抓不到进来的包:tcpdump hook 要比 TC的hook更加靠前一些。
  96. 就以 10.0.1.211所在Pod为例举例说明:
  97. 10.0.1.211 对应的eth0@if11 的 if11 形成的veth pair lxc23801e340e9a@if10
  98. 此时在lxc23801e340e9a上抓包:可以看到:
  99. # 1. 从10.0.1.211 的pod去ping 10.0.1.73
  100. [root@k8s-2 ~]# kubectl exec -ti cc -- ping 10.0.1.73
  101. PING 10.0.1.73 (10.0.1.73): 56 data bytes
  102. 64 bytes from 10.0.1.73: seq=0 ttl=63 time=0.123 ms
  103. [root@k8s-2 ~]# tcpdump -ne -i lxc23801e340e9a
  104. tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
  105. listening on lxc23801e340e9a, link-type EN10MB (Ethernet), capture size 262144 bytes
  106. 22:16:16.371807 16:ca:13:c3:7c:8f > 1a:1a:f0:a8:a5:bf, ethertype IPv4 (0x0800), length 98: 10.0.1.211 > 10.0.1.73: ICMP echo request, id 19968, seq 0, length 64
  107. 此时可以看到只有从pod 10.0.1.211 上出来的ICMP Request的数据包,而没有ICMP的Replay。
  108. 此时:
  109. # redirect_peer 特性专用于 veth pair 类型接口,因为流量被直接重定向到 Pod 里面的接口,所以在宿主机 lxc 口上无法抓到进入 Pod 的流量,因 tcpdump 抓包点在 tc ingress hook 之前,所以可以抓到出 Pod 未经过 eBPF 处理的流量。
  110. 这里需要明确的是:需要弄清楚这个,tcpdump和tc生效的位置。这个对于理解次模型非常重要!!!

20210908-Cilium Native-Routing - 图3
image.png

跨节点通信

from-container BPF 程序分析

  1. # env:
  2. ^C[root@k8s-1 ~]# kubectl get pods -o wide
  3. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  4. cni-2hmps 1/1 Running 0 29s 10.0.0.4 k8s-1 <none> <none>
  5. cni-hm9m7 1/1 Running 0 29s 10.0.1.64 k8s-2 <none> <none>
  6. [root@k8s-1 ~]#
  7. [root@k8s-1 ~]#
  8. [root@k8s-1 ~]#
  9. [root@k8s-1 ~]# kubectl exec -it cni-2hmps -- ping 10.0.1.64
  10. PING 10.0.1.64 (10.0.1.64): 56 data bytes
  11. 64 bytes from 10.0.1.64: seq=0 ttl=62 time=0.681 ms
  12. 64 bytes from 10.0.1.64: seq=1 ttl=62 time=0.594 ms
  13. ^C
  14. --- 10.0.1.64 ping statistics ---
  15. 2 packets transmitted, 2 packets received, 0% packet loss
  16. round-trip min/avg/max = 0.594/0.637/0.681 ms
  17. [root@k8s-1 ~]#
  18. 在HOST NS中查看veth pair的tc信息:
  19. [root@k8s-2 ~]# tc filter show dev lxc23801e340e9a ingress
  20. filter protocol all pref 1 bpf chain 0
  21. filter protocol all pref 1 bpf chain 0 handle 0x1 bpf_lxc.o:[from-container] direct-action not_in_hw tag e6d16ab039dd3408
  22. [root@k8s-2 ~]# tc filter show dev lxc59b348afd5c1 ingress
  23. filter protocol all pref 1 bpf chain 0
  24. filter protocol all pref 1 bpf chain 0 handle 0x1 bpf_lxc.o:[from-container] direct-action not_in_hw tag c8417a60698f6d32
  25. [root@k8s-2 ~]#
  26. ##1.0#####################
  27. # 1.BPF 代码都在 Cilium 源代码的 bpf/ 目录中。为避免本文过长,将只贴出核心调用栈。
  28. 大致调用栈如下:
  29. __section("from-container")
  30. handle_xgress // bpf/bpf_lxc.c
  31. |-validate_ethertype(skb, &proto)
  32. |-switch (proto) {
  33. case ETH_P_IP:
  34. tail_handle_ipv4 // bpf/bpf_lxc.c
  35. |-handle_ipv4_from_lxc // bpf/bpf_lxc.c
  36. |-if dst is k8s Service
  37. | lb4_local()
  38. | |-ct_create4
  39. | |-lb4_lookup_backend
  40. | |-lb4_xlate
  41. |
  42. |-policy_can_egress4()
  43. |
  44. |-if tunnel
  45. | encap vxlan
  46. | else // direct routing, pass to kernel stack (continue normal routing)
  47. | ipv4_l3() // dec TTL, set src/dst MAC
  48. | asm_set_seclabel_identity(skb); // set identity to skb
  49. |-return TC_ACT_OK;
  50. }
  51. 所做的事情:
  52. 1.对包进行验证,并提取出 L3 proto(协议类型)。
  53. 2.如果 L3 proto 是 IPv4,调用 tail_handle_ipv4() 进行处理。
  54. 3.tail_handle_ipv4() 进一步调用 handle_ipv4_from_lxc(),后者完成:
  55. 3.1:Service 负载均衡,即从 Service 的后端 Pod 中选择一个合适的,这里假设选择 NODE2 上的 POD4。
  56. 3.2:创建或更新连接跟踪(CT)记录。
  57. 3.3:执行 DNAT,将包的 dst_ip 由 ServiceIP 改成 POD4_IP。
  58. 3.4:进行容器出向(egress)安全策略验证,这里假设没有安全策略,因此能通过验证。
  59. 3.5:对包进行封装,或者通过主机进行路由,本文只看直接路由的情况。
  60. 在送回协议栈之前,调用 ipv4_l3() 设置 TTL、MAC 地址等:
  61. int
  62. ipv4_l3(struct __ctx_buff *ctx, int l3_off, __u8 *smac, __u8 *dmac, struct iphdr *ip4)
  63. {
  64. ipv4_dec_ttl(ctx, l3_off, ip4));
  65. if (smac)
  66. eth_store_saddr(ctx, smac, 0);
  67. eth_store_daddr(ctx, dmac, 0);
  68. return CTX_ACT_OK;
  69. }
  70. 以上假设都成立的情况下,BPF 程序最后返回 TC_ACK_OK,这个包就进入内核协议 栈继续处理了。
  71. ##1.0#####################
  72. ##2.0#####################
  73. 此时数据包进入HOST NS中:
  74. eBPF host-routing allows to bypass all of the iptables and upper stack overhead in the host namespace as well as some of the context-switching overhead when traversing through the Virtual Ethernet pairs. Network packets are picked up as early as possible from the network device facing the network and delivered directly into the network namespace of the Kubernetes Pod. On the egress side, the packet still traverses the veth pair, is picked up by eBPF and delivered directly to the external facing network interface. The routing table is consulted directly from eBPF so this optimization is entirely transparent and compatible with any other services running on the system providing route distribution. For information on how to enable this feature, see eBPF Host-Routing in the tuning guide.
  75. 此时查询HOST NS中的路由表

HOST NS 路由查询[报文出节点[k8s-1]方向]

  1. # env:
  2. [root@k8s-1 ~]# kubectl get pods -o wide
  3. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  4. cni-bhq9h 1/1 Running 0 60m 10.0.0.53 k8s-1 <none> <none>
  5. cni-dq6rz 1/1 Running 0 60m 10.0.1.73 k8s-2 <none> <none>
  6. [root@k8s-1 ~]#
  7. [root@k8s-1 ~]# route -n
  8. Kernel IP routing table
  9. Destination Gateway Genmask Flags Metric Ref Use Iface
  10. 0.0.0.0 192.168.2.1 0.0.0.0 UG 100 0 0 ens33
  11. 10.0.0.0 10.0.0.76 255.255.255.0 UG 0 0 0 cilium_host
  12. 10.0.0.76 0.0.0.0 255.255.255.255 UH 0 0 0 cilium_host
  13. 10.0.1.0 192.168.2.22 255.255.255.0 UG 0 0 0 ens33 # 匹配到此条目
  14. 172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0
  15. 192.168.2.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
  16. [root@k8s-1 ~]#
  17. # 查看ens33的tc 信息:
  18. [root@k8s-1 ~]# tc filter show dev ens33 egress
  19. filter protocol all pref 1 bpf chain 0
  20. filter protocol all pref 1 bpf chain 0 handle 0x1 bpf_netdev_ens33.o:[to-netdev] direct-action not_in_hw tag 776e35350adbd7a0
  21. [root@k8s-1 ~]#
  22. # to-netdev BPF 程序分析
  23. 调用栈如下,
  24. __section("to-netdev")
  25. to_netdev
  26. |-policy_clear_mark
  27. |-src_id = resolve_srcid_ipv4
  28. | |-lookup_ip4_remote_endpoint
  29. | |-ipcache_lookup4
  30. |-ipv4_host_policy_egress(src_id)
  31. |-policy_can_egress4
  32. |-ret = ct_lookup4()
  33. |-switch (ret) {
  34. case CT_NEW : ct_create4(); break;
  35. case CT_ESTABLISHED:
  36. case CT_RELATED :
  37. case CT_REPLY : break;
  38. default : ret = DROP; break;
  39. }
  40. return ret;
  41. 粗略地说,对于我们这个 case,这段 BPF 其实并不会做什么实际的事情,程序最后返回 TC_ACK_OK 放行。

报文进入另外一个节点[k8s-2]

  1. // kernel source tree, 5.11
  2. ixgbe_poll
  3. |-ixgbe_clean_rx_irq
  4. |-if support XDP offload
  5. | skb = ixgbe_run_xdp()
  6. |-skb = ixgbe_construct_skb()
  7. |-ixgbe_rx_skb
  8. |-napi_gro_receive
  9. |-napi_skb_finish(dev_gro_receive(skb))
  10. |-netif_receive_skb_internal
  11. |-if generic XDP
  12. | |-if do_xdp_generic() != XDP_PASS
  13. | return NET_RX_DROP
  14. |-__netif_receive_skb(skb)
  15. |-__netif_receive_skb_one_core
  16. |-__netif_receive_skb_core(&pt_prev)
  17. |-for tap in taps:
  18. | deliver_skb
  19. |-sch_handle_ingress // net/core/dev.c
  20. |-tcf_classify // net/sched/cls_api.c
  21. |-for tp in tps:
  22. tp->classify
  23. |-cls_bpf_classify // net/sched/cls_bpf.c
  24. 大致过程:
  25. 1.网卡收包
  26. 如果网卡支持 XDP offload,并且有 XDP 程序,就会执行 XDP 程序。我们这里没有启 用 XDP。
  27. 2.创建 skb。
  28. 3.GRO,对分片的包进行重组。
  29. Generic XDP 处理:如果网卡不支持 XDP offload,那 XDP 程序会从 step 2 延后到这里执行。
  30. 4.Tap 处理(此处没有)。
  31. TC ingress 处理,支持包括 BPF 在内的 TC 程序。
  32. 其中的 sch_handle_ingress() 会进入 TC ingress hook 执行处理。
  33. 查看加载的 BPF 程序
  34. 查看 ingress 方向加载的 BPF:
  35. [root@k8s-2 ~]# tc filter show dev ens33 ingress
  36. filter protocol all pref 1 bpf chain 0
  37. filter protocol all pref 1 bpf chain 0 handle 0x1 bpf_netdev_ens33.o:[from-netdev] direct-action not_in_hw tag 697bc2349ac2ea82
  38. [root@k8s-2 ~]#
  39. from-netdev BPF 程序分析
  40. 调用栈:
  41. __section("from-netdev")
  42. from_netdev
  43. |-handle_netdev
  44. |-validate_ethertype
  45. |-do_netdev
  46. |-identity = resolve_srcid_ipv4() // 从 ctx 中提取 src identity
  47. |-ctx_store_meta(CB_SRC_IDENTITY, identity) // 将 identity 存储到 ctx->cb[CB_SRC_IDENTITY]
  48. |-ep_tail_call(ctx, CILIUM_CALL_IPV4_FROM_LXC) // 尾调用到 endpoint BPF
  49. |
  50. |------------------------------
  51. |
  52. __section_tail(CILIUM_MAP_CALLS, CILIUM_CALL_IPV4_FROM_LXC)
  53. tail_handle_ipv4_from_netdev
  54. |-tail_handle_ipv4
  55. |-handle_ipv4
  56. |-ep = lookup_ip4_endpoint()
  57. |-ipv4_local_delivery(ctx, ep)
  58. |-tail_call_dynamic(ctx, &POLICY_CALL_MAP, ep->lxc_id);
  59. 主要逻辑:
  60. 1.调用 handle_netdev() 处理将从宿主机进入 Cilium 管理的网络的流量,具体事情:
  61. 1.1解析这个包所属的 identity(Cilium 依赖 identity 做安全策略),并存储到包的结构体中。
  62. 对于 direct routing 模式,从 ipcache 中根据 IP 查询 identity。
  63. 对于 tunnel 模式,直接从 VxLAN 头中携带过来了。
  64. 1.2尾调用到 tail_handle_ipv4_from_netdev()。
  65. 2.tail_handle_ipv4_from_netdev() 进一步调用 tail_handle_ipv4(),后者再调用 handle_ipv4()。handle_ipv4() 做的事情:
  66. 2.1查找 dst_ip 对应的 endpoint(即 对端节点的POD)。
  67. 2.2调用 ipv4_local_delivery() 执行处理,这个函数会根据 endpoint id 直接尾调用到 endpoint (对端节点的POD) 的 BPF 程序。 ## 所以lxc0xxxx 就没有egress的filter。
  68. # to-container BPF 程序分析
  69. 这次尾调用到达的是 to-container BPF 程序。调用栈:
  70. __section("to-container")
  71. handle_to_container // bpf/bpf_lxc.c
  72. |-inherit_identity_from_host(skb, &identity) // -> bpf/lib/identity.h
  73. |-tail_ipv4_to_endpoint // bpf/bpf_lxc.c
  74. |-ipv4_policy // bpf/bpf_lxc.c
  75. |-policy_can_access_ingress // bpf/lib/policy.h
  76. |-__policy_can_access // bpf/lib/policy.h
  77. |-if p = map_lookup_elem(l3l4_key); p // L3+L4 policy
  78. | return TC_ACK_OK
  79. |-if p = map_lookup_elem(l4only_key); p // L4-Only policy
  80. | return TC_ACK_OK
  81. |-if p = map_lookup_elem(l3only_key); p // L3-Only policy
  82. | return TC_ACK_OK
  83. |-if p = map_lookup_elem(allowall_key); p // Allow-all policy
  84. | return TC_ACK_OK
  85. |-return DROP_POLICY; // DROP
  86. 所做的事情也很清楚:
  87. 提取包的 src identity 信息,这个信息此时已经在包的元数据里面了。
  88. 调用 tail_ipv4_to_endpoint(),这个函数会进一步调用 ipv4_policy() 执行 容器入向(ingress)安全策略检查。
  89. 如果包没有被策略拒绝,就会被转发到 lxc00dd 的对端,即 POD的网卡 eth0。

HOST-Routing