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

Cilium VXLAN环境准备

  1. # 1.安装VxLAN环境:
  2. # 1.1.Add Cilium repo:
  3. $ helm repo add cilium https://helm.cilium.io/
  4. # 1.2:.Generate the Cilium Template:
  5. helm template cilium cilium/cilium --version 1.10.3 \
  6. --namespace kube-system \
  7. --set kubeProxyReplacement=strict \
  8. --set k8sServiceHost=REPLACE_WITH_API_SERVER_IP \
  9. --set k8sServicePort=REPLACE_WITH_API_SERVER_PORT
  10. helm template cilium cilium/cilium --version 1.10 --namespace kube-system --set kubeProxyReplacement=strict --set k8sServiceHost=192.168.2.21 --set k8sServicePort=6443 > vxlan.yaml
  11. 在这个vxlan.yaml文件中我们需要修改部分参数,以适配后期的Debug:
  12. 1.4.1.
  13. # If you want to run cilium in debug mode change this value to true
  14. debug: "true"
  15. debug-verbose: "datapath"
  16. 1.4.2.
  17. # If you want cilium monitor to aggregate tracing for packets, set this level
  18. # to "low", "medium", or "maximum". The higher the level, the less packets
  19. # that will be seen in monitor output.
  20. monitor-aggregation: none
  21. 1.4.3.
  22. # Encapsulation mode for communication between nodes
  23. # Possible values:
  24. # - disabled
  25. # - vxlan (default)
  26. # - geneve
  27. tunnel: vxlan
  28. 1.4.4.
  29. # wait-bpf-mount makes init container wait until bpf filesystem is mounted
  30. wait-bpf-mount: "true"
  31. #Mount bpf as a service:
  32. cat <<EOF | sudo tee /etc/systemd/system/sys-fs-bpf.mount
  33. [Unit]
  34. Description=Cilium BPF mounts
  35. Documentation=https://docs.cilium.io/
  36. DefaultDependencies=no
  37. Before=local-fs.target umount.target
  38. After=swap.target
  39. [Mount]
  40. What=bpffs
  41. Where=/sys/fs/bpf
  42. Type=bpf
  43. Options=rw,nosuid,nodev,noexec,relatime,mode=700
  44. [Install]
  45. WantedBy=multi-user.target
  46. EOF
  47. systemctl enable sys-fs-bpf.mount
  48. systemctl restart sys-fs-bpf.mount
  49. mount | grep "/sys/fs/bpf"
  50. #none on /sys/fs/bpf type bpf (rw,nosuid,nodev,noexec,relatime,mode=700)
  51. 1.4.5:cilium status:
  52. root@k8s-2:/home/cilium# cilium status
  53. KVStore: Ok Disabled
  54. Kubernetes: Ok 1.20 (v1.20.5) [linux/amd64]
  55. 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"]
  56. KubeProxyReplacement: Strict [ens33 192.168.2.22 (Direct Routing)]
  57. Cilium: Ok 1.10.3 (v1.10.3-4145278)
  58. NodeMonitor: Listening for events on 128 CPUs with 64x4096 of shared memory
  59. Cilium health daemon: Ok
  60. IPAM: IPv4: 6/254 allocated from 10.0.1.0/24,
  61. BandwidthManager: Disabled
  62. Host Routing: BPF
  63. Masquerading: BPF [ens33] 10.0.1.0/24 [IPv4: Enabled, IPv6: Disabled]
  64. Controller Status: 35/35 healthy
  65. Proxy Status: OK, ip 10.0.1.84, 0 redirects active on ports 10000-20000
  66. Hubble: Ok Current/Max Flows: 4095/4095 (100.00%), Flows/s: 23.63 Metrics: Disabled
  67. Encryption: Disabled
  68. Cluster health: 2/2 reachable (2021-09-09T12:47:20Z)
  69. root@k8s-2:/home/cilium#
  70. # 2.cilium 环境中tc 加载ebpf的信息:
  71. root@k8s-2:/home/cilium# bpftool net show
  72. xdp:
  73. tc: # 这里可以查看到我们环境中的各个接口所加载的eBPF的Program。
  74. ens33(2) clsact/ingress bpf_netdev_ens33.o:[from-netdev] id 710
  75. ens33(2) clsact/egress bpf_netdev_ens33.o:[to-netdev] id 716
  76. cilium_net(4) clsact/ingress bpf_host_cilium_net.o:[to-host] id 704
  77. cilium_host(5) clsact/ingress bpf_host.o:[to-host] id 692
  78. cilium_host(5) clsact/egress bpf_host.o:[from-host] id 698
  79. cilium_vxlan(6) clsact/ingress bpf_overlay.o:[from-overlay] id 637
  80. cilium_vxlan(6) clsact/egress bpf_overlay.o:[to-overlay] id 642
  81. lxc_health(8) clsact/ingress bpf_lxc.o:[from-container] id 663
  82. lxcd01889e694fe(10) clsact/ingress bpf_lxc.o:[from-container] id 657
  83. lxcff60740f8e50(12) clsact/ingress bpf_lxc.o:[from-container] id 665
  84. lxcb20467fae4ac(14) clsact/ingress bpf_lxc.o:[from-container] id 661
  85. lxc9d9e27623fba(16) clsact/ingress bpf_lxc.o:[from-container] id 658
  86. flow_dissector:
  87. root@k8s-2:/home/cilium#
  88. # 3.jq环境准备:
  89. root@k8s-2:/home/cilium# apt-get update
  90. Get:1 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
  91. Get:2 http://archive.ubuntu.com/ubuntu focal InRelease [265 kB]
  92. Get:3 http://security.ubuntu.com/ubuntu focal-security/universe amd64 Packages [791 kB]
  93. Get:4 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
  94. Get:5 http://archive.ubuntu.com/ubuntu focal-backports InRelease [101 kB]
  95. Get:6 http://archive.ubuntu.com/ubuntu focal/universe amd64 Packages [11.3 MB]
  96. Get:7 http://security.ubuntu.com/ubuntu focal-security/restricted amd64 Packages [529 kB]
  97. Get:8 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages [1075 kB]
  98. Get:9 http://security.ubuntu.com/ubuntu focal-security/multiverse amd64 Packages [30.1 kB]
  99. Get:10 http://archive.ubuntu.com/ubuntu focal/multiverse amd64 Packages [177 kB]
  100. Get:11 http://archive.ubuntu.com/ubuntu focal/main amd64 Packages [1275 kB]
  101. Get:12 http://archive.ubuntu.com/ubuntu focal/restricted amd64 Packages [33.4 kB]
  102. Get:13 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [1070 kB]
  103. Get:14 http://archive.ubuntu.com/ubuntu focal-updates/multiverse amd64 Packages [33.3 kB]
  104. Get:15 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [1516 kB]
  105. Get:16 http://archive.ubuntu.com/ubuntu focal-updates/restricted amd64 Packages [575 kB]
  106. Get:17 http://archive.ubuntu.com/ubuntu focal-backports/universe amd64 Packages [6324 B]
  107. Get:18 http://archive.ubuntu.com/ubuntu focal-backports/main amd64 Packages [2668 B]
  108. Fetched 19.1 MB in 6s (3330 kB/s)
  109. Reading package lists... Done
  110. root@k8s-2:/home/cilium# apt-get install jq
  111. Reading package lists... Done
  112. Building dependency tree
  113. Reading state information... Done
  114. The following additional packages will be installed:
  115. libjq1 libonig5
  116. The following NEW packages will be installed:
  117. jq libjq1 libonig5
  118. 0 upgraded, 3 newly installed, 0 to remove and 8 not upgraded.
  119. Need to get 313 kB of archives.
  120. After this operation, 1062 kB of additional disk space will be used.
  121. Do you want to continue? [Y/n] y
  122. Get:1 http://archive.ubuntu.com/ubuntu focal/universe amd64 libonig5 amd64 6.9.4-1 [142 kB]
  123. Get:2 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 libjq1 amd64 1.6-1ubuntu0.20.04.1 [121 kB]
  124. Get:3 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 jq amd64 1.6-1ubuntu0.20.04.1 [50.2 kB]
  125. Fetched 313 kB in 2s (136 kB/s)
  126. debconf: delaying package configuration, since apt-utils is not installed
  127. Selecting previously unselected package libonig5:amd64.
  128. (Reading database ... 5525 files and directories currently installed.)
  129. Preparing to unpack .../libonig5_6.9.4-1_amd64.deb ...
  130. Unpacking libonig5:amd64 (6.9.4-1) ...
  131. Selecting previously unselected package libjq1:amd64.
  132. Preparing to unpack .../libjq1_1.6-1ubuntu0.20.04.1_amd64.deb ...
  133. Unpacking libjq1:amd64 (1.6-1ubuntu0.20.04.1) ...
  134. Selecting previously unselected package jq.
  135. Preparing to unpack .../jq_1.6-1ubuntu0.20.04.1_amd64.deb ...
  136. Unpacking jq (1.6-1ubuntu0.20.04.1) ...
  137. Setting up libonig5:amd64 (6.9.4-1) ...
  138. Setting up libjq1:amd64 (1.6-1ubuntu0.20.04.1) ...
  139. Setting up jq (1.6-1ubuntu0.20.04.1) ...
  140. Processing triggers for libc-bin (2.31-0ubuntu9.2) ...
  141. # 查看map:
  142. bpftool map list --bpffs -j | jq | egrep 'id|sys'
  143. # bpftool map list --bpffs -j | jq | egrep 'id|sys' | grep ipcache -B1
  144. # bpftool map list --bpffs -j | jq | egrep 'id|sys' | grep lxc -B1
  145. # 分别在不同的host上查询相应的Map信息:
  146. # 4.地址信息介绍:
  147. [root@k8s-1 ~]# kubectl get pods -o wide
  148. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  149. cni-4p44n 1/1 Running 1 14d 10.0.1.100 k8s-2 <none> <none>
  150. cni-kq5sq 1/1 Running 1 14d 10.0.0.254 k8s-1 <none> <none>
  151. #4.1: pod-k8s-1's:
  152. # cni-kq5sq 1/1 Running 1 14d 10.0.0.254 k8s-1 <none> <none>
  153. [root@k8s-1 ~]# kubectl exec -ti cni-kq5sq -- ip a HOST IP地址:192.168.2.21 <> c0.a8.02.15
  154. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
  155. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  156. inet 127.0.0.1/8 scope host lo
  157. valid_lft forever preferred_lft forever
  158. 11: eth0@if12: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
  159. link/ether 9e:03:09:77:7d:b0 brd ff:ff:ff:ff:ff:ff link-netnsid 0 # MAC 地址
  160. inet 10.0.0.254/32 scope global eth0 #IP地址:10.0.0.254 <> 0a.00.00.fe
  161. valid_lft forever preferred_lft forever
  162. [root@k8s-1 ~]# ip a | grep 12: -A 3
  163. 12: lxc0486be7fa43b@if11: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
  164. link/ether b6:3a:d6:e8:f2:49 brd ff:ff:ff:ff:ff:ff link-netnsid 2 #MAC地址: b6:3a:d6:e8:f2:49 <> b6:3a:d6:e8:f2:49
  165. inet6 fe80::b43a:d6ff:fee8:f249/64 scope link
  166. valid_lft forever preferred_lft forever
  167. [root@k8s-1 ~]#
  168. 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
  169. link/ether 00:0c:29:bd:fb:4a brd ff:ff:ff:ff:ff:ff # MAC地址
  170. inet 192.168.2.21/24 brd 192.168.2.255 scope global noprefixroute ens33 # IP地址
  171. valid_lft forever preferred_lft forever
  172. inet6 fe80::e222:32bb:f400:f0c3/64 scope link noprefixroute
  173. valid_lft forever preferred_lft forever
  174. # 4.2: pod-k8s-2's # HOST IP地址:192.168.2.22 <> c0.a8.02.16
  175. # cni-4p44n 1/1 Running 1 14d 10.0.1.100 k8s-2 <none> <none>
  176. [root@k8s-2 ~]# kubectl exec -it cni-4p44n -- ip a
  177. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
  178. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  179. inet 127.0.0.1/8 scope host lo
  180. valid_lft forever preferred_lft forever
  181. 11: eth0@if12: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
  182. link/ether 02:a1:1c:41:a5:0c brd ff:ff:ff:ff:ff:ff link-netnsid 0 #MAC地址
  183. inet 10.0.1.100/32 scope global eth0 #IP地址: 10.0.1.100 <> 0a.00.01.64
  184. valid_lft forever preferred_lft forever
  185. [root@k8s-2 ~]# ip a | grep 12: -A 3
  186. 12: lxcaa879877688d@if11: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
  187. link/ether ea:db:e8:e3:75:cb brd ff:ff:ff:ff:ff:ff link-netnsid 2 #MAC地址: ea:db:e8:e3:75:cb <> ea:db:e8:e3:75:cb
  188. inet6 fe80::e8db:e8ff:fee3:75cb/64 scope link
  189. valid_lft forever preferred_lft forever
  190. [root@k8s-2 ~]#
  191. #4.3:首先在k8s-1节点上查看对应的MAP信息:
  192. 首先是:ip地址信息:
  193. [root@k8s-1 ~]# kubectl -nkube-system exec -it cilium-s6vsd -- bash
  194. bpftool map list --bpffs -j | jq | egrep 'id|sys'
  195. root@k8s-1:/home/cilium# bpftool map dump id 77
  196. key:
  197. 40 00 00 00 00 00 00 01 0a 00 00 fe 00 00 00 00 <> 10.0.0.254
  198. 00 00 00 00 00 00 00 00
  199. value:
  200. 47 69 00 00 00 00 00 00 00 00 00 00 <> 本地地址信息显示为全00
  201. --------------------------------------------------------------------
  202. key:
  203. 40 00 00 00 00 00 00 01 0a 00 01 64 00 00 00 00 <> 10.0.1.100
  204. 00 00 00 00 00 00 00 00
  205. value:
  206. 47 69 00 00 c0 a8 02 16 00 00 00 00 <> 192.168.2.22
  207. # 4.4:再查看lxc信息:[veth]:
  208. [root@k8s-1 ~]# kubectl -nkube-system exec -it cilium-s6vsd -- bash
  209. bpftool map list --bpffs -j | jq | egrep 'id|sys'
  210. root@k8s-1:/home/cilium# bpftool map dump id 76
  211. root@k8s-1:/home/cilium# bpftool map dump id 76
  212. key:
  213. 0a 00 00 fe 00 00 00 00 00 00 00 00 00 00 00 00 <> 10.0.0.254
  214. 01 00 00 00
  215. value:
  216. 0c 00 00 00 00 00 c2 0d 00 00 00 00 00 00 00 00 <> c2 0d Policy ID || 0c is the interface index.
  217. 9e 03 09 77 7d b0 00 00 b6 3a d6 e8 f2 49 00 00 <> link/ether 9e:03:09:77:7d:b0 brd ff:ff:ff:ff:ff:ff link-netnsid 0 # MAC 地址
  218. <> link/ether b6:3a:d6:e8:f2:49 brd ff:ff:ff:ff:ff:ff link-netnsid 2 #MAC地址: b6:3a:d6:e8:f2:49 <> b6:3a:d6:e8:f2:49
  219. 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  220. # 4.5:查看Tunnel信息:
  221. oot@k8s-1:/home/cilium# bpftool map dump id 80
  222. key:
  223. 0a 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 <> 0a 00 01 00 --- 10.0.1.0
  224. 01 00 00 00
  225. value:
  226. c0 a8 02 16 00 00 00 00 00 00 00 00 00 00 00 00 <> c0 a8 02 16 --- 192.168.2.22
  227. 01 00 00 00
  228. Found 1 element
  229. root@k8s-1:/home/cilium#

同节点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生效的位置。这个对于理解次模型非常重要!!!

20210910-Cilium VxLAN Mode - 图120210910-Cilium VxLAN Mode - 图2

不同节点Pod通信

  1. # 0.先看一个cilium monitor -vv 的trace信息:
  2. [root@k8s-1 ~]# kubectl exec -it cni-kq5sq -- ping 10.0.1.100
  3. PING 10.0.1.100 (10.0.1.100): 56 data bytes
  4. 64 bytes from 10.0.1.100: seq=0 ttl=63 time=0.977 ms
  5. ^C
  6. --- 10.0.1.100 ping statistics ---
  7. 1 packets transmitted, 1 packets received, 0% packet loss
  8. round-trip min/avg/max = 0.977/0.977/0.977 ms
  9. [root@k8s-1 ~]#
  10. ####################################################################################################################################################################################################
  11. ------------------------------------------------------------------------------
  12. Ethernet {Contents=[..14..] Payload=[..30..] SrcMAC=9e:03:09:77:7d:b0 DstMAC=ff:ff:ff:ff:ff:ff EthernetType=ARP Length=0} # 1.发ARP请求出去
  13. CPU 02: MARK 0x0 FROM 3522 from-endpoint: 42 bytes (42 captured), state newidentity 26951->unknown, orig-ip 0.0.0.0 # 2.使用的identity是26591
  14. ------------------------------------------------------------------------------
  15. CPU 02: MARK 0x0 FROM 3522 DEBUG: 42 bytes, Delivery to ifindex 12 # 3.转发到ifindex 12的接口上
  16. Ethernet {Contents=[..14..] Payload=[..30..] SrcMAC=b6:3a:d6:e8:f2:49 DstMAC=9e:03:09:77:7d:b0 EthernetType=ARP Length=0} # 4.获得ARP的Response。Dst的MAC地址
  17. ------------------------------------------------------------------------------
  18. Ethernet {Contents=[..14..] Payload=[..86..] SrcMAC=9e:03:09:77:7d:b0 DstMAC=b6:3a:d6:e8:f2:49 EthernetType=IPv4 Length=0} # 5.完成的SrcMAC和DstMAC地址
  19. IPv4 {Contents=[..20..] Payload=[..64..] Version=4 IHL=5 TOS=0 Length=84 Id=62026 Flags=DF FragOffset=0 TTL=64 Protocol=ICMPv4 Checksum=12797 SrcIP=10.0.0.254 DstIP=10.0.1.100 Options=[] Padding=[]}
  20. ICMPv4 {Contents=[..8..] Payload=[..56..] TypeCode=EchoRequest Checksum=7898 Id=11008 Seq=0}
  21. CPU 02: MARK 0x0 FROM 3522 from-endpoint: 98 bytes (98 captured), state newidentity 26951->unknown, orig-ip 0.0.0.0 # 6.进入pod的veth的lxc接口的from-container TCHOOK.
  22. CPU 02: MARK 0x0 FROM 3522 DEBUG: Conntrack lookup 1/2: src=10.0.0.254:11008 dst=10.0.1.100:0 # 7.Conntrack 查询
  23. CPU 02: MARK 0x0 FROM 3522 DEBUG: Conntrack lookup 2/2: nexthdr=1 flags=1
  24. CPU 02: MARK 0x0 FROM 3522 DEBUG: CT verdict: New, revnat=0
  25. CPU 02: MARK 0x0 FROM 3522 DEBUG: Successfully mapped addr=10.0.1.100 to identity=26951 # 8.Dst IP map到identity
  26. CPU 02: MARK 0x0 FROM 3522 DEBUG: Conntrack create: proxy-port=0 revnat=0 src-identity=26951 lb=0.0.0.0 # 9.创建新的conntrack
  27. CPU 02: MARK 0x0 FROM 3522 DEBUG: Encapsulating to node 3232236054 (0xc0a80216) from seclabel 26951 # 10.map信息查询以后,Encapsulating到节点(0xc0a80216)-192.168.2.22
  28. ------------------------------------------------------------------------------
  29. Ethernet {Contents=[..14..] Payload=[..86..] SrcMAC=9e:03:09:77:7d:b0 DstMAC=b6:3a:d6:e8:f2:49 EthernetType=IPv4 Length=0}
  30. IPv4 {Contents=[..20..] Payload=[..64..] Version=4 IHL=5 TOS=0 Length=84 Id=62026 Flags=DF FragOffset=0 TTL=64 Protocol=ICMPv4 Checksum=12797 SrcIP=10.0.0.254 DstIP=10.0.1.100 Options=[] Padding=[]}
  31. ICMPv4 {Contents=[..8..] Payload=[..56..] TypeCode=EchoRequest Checksum=7898 Id=11008 Seq=0}
  32. CPU 02: MARK 0x0 FROM 3522 to-overlay: 98 bytes (98 captured), state new, interface cilium_vxlanidentity 26951->unknown, orig-ip 0.0.0.0 # 11.被tail call到cilium_vxlan TC,被to-overlay拦截处理
  33. CPU 02: MARK 0x0 FROM 11 DEBUG: Conntrack lookup 1/2: src=192.168.2.21:43558 dst=192.168.2.22:8472 # 12.封装完成VxLAN报文
  34. CPU 02: MARK 0x0 FROM 11 DEBUG: Conntrack lookup 2/2: nexthdr=17 flags=1
  35. CPU 02: MARK 0x0 FROM 11 DEBUG: CT verdict: New, revnat=0
  36. CPU 02: MARK 0x0 FROM 11 DEBUG: Conntrack create: proxy-port=0 revnat=0 src-identity=0 lb=0.0.0.0 # 13.创建新的contrack
  37. ------------------------------------------------------------------------------
  38. Ethernet {Contents=[..14..] Payload=[..118..] SrcMAC=00:0c:29:bd:fb:4a DstMAC=00:0c:29:e2:bf:86 EthernetType=IPv4 Length=0} # 14.k8s-1和k8s-2节点的MAC地址信息
  39. IPv4 {Contents=[..20..] Payload=[..98..] Version=4 IHL=5 TOS=0 Length=134 Id=40508 Flags= FragOffset=0 TTL=64 Protocol=UDP Checksum=22191 SrcIP=192.168.2.21 DstIP=192.168.2.22 Options=[] Padding=[]}
  40. UDP {Contents=[..8..] Payload=[..90..] SrcPort=43558 DstPort=8472(otv) Length=114 Checksum=0} # 15.payload 信息
  41. Packet has been truncated
  42. CPU 02: MARK 0x0 FROM 11 to-network: 148 bytes (128 captured), state new, orig-ip 0.0.0.0
  43. ------------------------------------------------------------------------------
  44. Ethernet {Contents=[..14..] Payload=[..118..] SrcMAC=00:0c:29:e2:bf:86 DstMAC=00:0c:29:bd:fb:4a EthernetType=IPv4 Length=0} # 16.以下进入收包过程,和封装包过程为逆过程
  45. IPv4 {Contents=[..20..] Payload=[..98..] Version=4 IHL=5 TOS=0 Length=134 Id=21873 Flags= FragOffset=0 TTL=64 Protocol=UDP Checksum=40826 SrcIP=192.168.2.22 DstIP=192.168.2.21 Options=[] Padding=[]}
  46. UDP {Contents=[..8..] Payload=[..90..] SrcPort=34421 DstPort=8472(otv) Length=114 Checksum=0}
  47. Packet has been truncated
  48. CPU 05: MARK 0x0 FROM 11 from-network: 148 bytes (128 captured), state new, interface ens33, orig-ip 0.0.0.0
  49. CPU 05: MARK 0x0 FROM 11 DEBUG: Successfully mapped addr=192.168.2.22 to identity=6
  50. ------------------------------------------------------------------------------
  51. Ethernet {Contents=[..14..] Payload=[..86..] SrcMAC=02:a1:1c:41:a5:0c DstMAC=ea:db:e8:e3:75:cb EthernetType=IPv4 Length=0}
  52. IPv4 {Contents=[..20..] Payload=[..64..] Version=4 IHL=5 TOS=0 Length=84 Id=39214 Flags= FragOffset=0 TTL=64 Protocol=ICMPv4 Checksum=51993 SrcIP=10.0.1.100 DstIP=10.0.0.254 Options=[] Padding=[]}
  53. ICMPv4 {Contents=[..8..] Payload=[..56..] TypeCode=EchoReply Checksum=9946 Id=11008 Seq=0}
  54. CPU 05: MARK 0x0 FROM 0 from-overlay: 98 bytes (98 captured), state new, interface cilium_vxlan, orig-ip 0.0.0.0
  55. CPU 05: MARK 0x0 FROM 0 DEBUG: Tunnel decap: id=26951 flowlabel=0
  56. CPU 05: MARK 0x0 FROM 0 DEBUG: Attempting local delivery for container id 3522 from seclabel 26951
  57. CPU 05: MARK 0x0 FROM 3522 DEBUG: Conntrack lookup 1/2: src=10.0.1.100:0 dst=10.0.0.254:11008
  58. CPU 05: MARK 0x0 FROM 3522 DEBUG: Conntrack lookup 2/2: nexthdr=1 flags=0
  59. CPU 05: MARK 0x0 FROM 3522 DEBUG: CT entry found lifetime=50396, revnat=0
  60. CPU 05: MARK 0x0 FROM 3522 DEBUG: CT verdict: Reply, revnat=0
  61. ------------------------------------------------------------------------------
  62. Ethernet {Contents=[..14..] Payload=[..86..] SrcMAC=b6:3a:d6:e8:f2:49 DstMAC=9e:03:09:77:7d:b0 EthernetType=IPv4 Length=0}
  63. IPv4 {Contents=[..20..] Payload=[..64..] Version=4 IHL=5 TOS=0 Length=84 Id=39214 Flags= FragOffset=0 TTL=63 Protocol=ICMPv4 Checksum=52249 SrcIP=10.0.1.100 DstIP=10.0.0.254 Options=[] Padding=[]}
  64. ICMPv4 {Contents=[..8..] Payload=[..56..] TypeCode=EchoReply Checksum=9946 Id=11008 Seq=0}
  65. CPU 05: MARK 0x0 FROM 3522 to-endpoint: 98 bytes (98 captured), state reply, interface lxc0486be7fa43bidentity 26951->26951, orig-ip 10.0.1.100, to endpoint 3522
  66. ------------------------------------------------------------------------------
  67. ####################################################################################################################################################################################################
  68. # env 介绍:
  69. [root@k8s-1 ~]# kubectl get pods -o wide
  70. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  71. cni-4p44n 1/1 Running 1 14d 10.0.1.100 k8s-2 <none> <none>
  72. cni-kq5sq 1/1 Running 1 14d 10.0.0.254 k8s-1 <none> <none>
  73. [root@k8s-1 ~]#
  74. # 使用Pod cni-kq5sq ping Pod cni-4p44n.
  75. # 首先查看Pod cni-kq5sq的路由信息:
  76. [root@k8s-1 ~]# kubectl exec -ti cni-kq5sq bash
  77. kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
  78. bash-5.1# route -n
  79. Kernel IP routing table
  80. Destination Gateway Genmask Flags Metric Ref Use Iface
  81. 0.0.0.0 10.0.0.214 0.0.0.0 UG 0 0 0 eth0
  82. 10.0.0.214 0.0.0.0 255.255.255.255 UH 0 0 0 eth0
  83. bash-5.1#
  84. 但是此时我们知道构造的GW是来自宿主机上的cilium_host的ip地址,但是MAC地址却是对应的veth的lxc的mac地址。
  85. # 查看cni-kq5sq 对应的veth的lxc接口:
  86. bash-5.1# ip a
  87. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
  88. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  89. inet 127.0.0.1/8 scope host lo
  90. valid_lft forever preferred_lft forever
  91. 11: eth0@if12: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
  92. link/ether 9e:03:09:77:7d:b0 brd ff:ff:ff:ff:ff:ff link-netnsid 0
  93. inet 10.0.0.254/32 scope global eth0
  94. valid_lft forever preferred_lft forever
  95. bash-5.1# exit
  96. exit
  97. [root@k8s-1 ~]#
  98. 对应的lxc是12.
  99. 12: lxc0486be7fa43b@if11: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
  100. link/ether b6:3a:d6:e8:f2:49 brd ff:ff:ff:ff:ff:ff link-netnsid 2
  101. inet6 fe80::b43a:d6ff:fee8:f249/64 scope link
  102. valid_lft forever preferred_lft forever
  103. # 此时查看该接口上加载的bpf program信息:
  104. root@k8s-1:/home/cilium# bpftool net show | grep 12
  105. lxc0486be7fa43b(12) clsact/ingress bpf_lxc.o:[from-container] id 659
  106. ##1.0#####################
  107. # 1.BPF 代码都在 Cilium 源代码的 bpf/ 目录中。为避免本文过长,将只贴出核心调用栈。
  108. 大致调用栈如下:
  109. __section("from-container")
  110. handle_xgress // bpf/bpf_lxc.c
  111. |-validate_ethertype(skb, &proto)
  112. |-switch (proto) {
  113. case ETH_P_IP:
  114. tail_handle_ipv4 // bpf/bpf_lxc.c
  115. |-handle_ipv4_from_lxc // bpf/bpf_lxc.c
  116. |-if dst is k8s Service
  117. | lb4_local()
  118. | |-ct_create4
  119. | |-lb4_lookup_backend
  120. | |-lb4_xlate
  121. |
  122. |-policy_can_egress4()
  123. |
  124. |-if tunnel
  125. | encap vxlan
  126. | else // direct routing, pass to kernel stack (continue normal routing)
  127. | ipv4_l3() // dec TTL, set src/dst MAC
  128. | asm_set_seclabel_identity(skb); // set identity to skb
  129. |-return TC_ACT_OK;
  130. }
  131. 所做的事情:
  132. 1.对包进行验证,并提取出 L3 proto(协议类型)。
  133. 2.如果 L3 proto 是 IPv4,调用 tail_handle_ipv4() 进行处理。
  134. 3.tail_handle_ipv4() 进一步调用 handle_ipv4_from_lxc(),后者完成:
  135. 3.1:Service 负载均衡,即从 Service 的后端 Pod 中选择一个合适的,这里假设选择 NODE2 上的 POD4。
  136. 3.2:创建或更新连接跟踪(CT)记录。
  137. 3.3:执行 DNAT,将包的 dst_ip 由 ServiceIP 改成 POD4_IP。
  138. 3.4:进行容器出向(egress)安全策略验证,这里假设没有安全策略,因此能通过验证。
  139. 3.5:对包进行封装,或者通过主机进行路由,本文只看直接路由的情况。
  140. 在送回协议栈之前,调用 ipv4_l3() 设置 TTL、MAC 地址等:
  141. int
  142. ipv4_l3(struct __ctx_buff *ctx, int l3_off, __u8 *smac, __u8 *dmac, struct iphdr *ip4)
  143. {
  144. ipv4_dec_ttl(ctx, l3_off, ip4));
  145. if (smac)
  146. eth_store_saddr(ctx, smac, 0);
  147. eth_store_daddr(ctx, dmac, 0);
  148. return CTX_ACT_OK;
  149. }
  150. 以上假设都成立的情况下,BPF 程序最后返回 TC_ACK_OK,这个包就进入内核协议 栈继续处理了

eBPF Packet Processing Egress[k8s-1]

  1. root@k8s-1:/home/cilium# bpftool net show
  2. xdp:
  3. tc:
  4. ens33(2) clsact/ingress bpf_netdev_ens33.o:[from-netdev] id 703
  5. ens33(2) clsact/egress bpf_netdev_ens33.o:[to-netdev] id 709
  6. cilium_net(4) clsact/ingress bpf_host_cilium_net.o:[to-host] id 697
  7. cilium_host(5) clsact/ingress bpf_host.o:[to-host] id 685
  8. cilium_host(5) clsact/egress bpf_host.o:[from-host] id 691
  9. cilium_vxlan(6) clsact/ingress bpf_overlay.o:[from-overlay] id 637
  10. cilium_vxlan(6) clsact/egress bpf_overlay.o:[to-overlay] id 642 # match this tc filter
  11. lxc_health(8) clsact/ingress bpf_lxc.o:[from-container] id 657
  12. lxcccc40ac18a81(10) clsact/ingress bpf_lxc.o:[from-container] id 663
  13. lxc0486be7fa43b(12) clsact/ingress bpf_lxc.o:[from-container] id 659
  14. lxc245b8cb25654(14) clsact/ingress bpf_lxc.o:[from-container] id 661
  15. flow_dissector:
  16. root@k8s-1:/home/cilium#
  17. 1.Packet’s header information is passed to the handle_xgress, defined in bpf/bpf_lxc.c, where its Ethertype is checked.
  18. 2.All IPv4 packets are dispatched to handle_ipv4_from_lxc via an intermediate tail_handle_ipv4 function.
  19. 3.Most of the packet processing decisions are made inside handle_ipv4_from_lxc. At some point the execution flow reaches this part of the function where destination IP lookup is triggered.
  20. 4.The lookup_ip4_remote_endpoint function is defined inside bpf/lib/eps.h and uses IPCACHE eBPF map to look up information about a remote endpoint:
  21. #define lookup_ip4_remote_endpoint(addr) \
  22. ipcache_lookup4(&IPCACHE_MAP, addr, V4_CACHE_KEY_LEN)
  23. ipcache_lookup4(struct bpf_elf_map *map, __be32 addr, __u32 prefix)
  24. {
  25. struct ipcache_key key = {
  26. .lpm_key = { IPCACHE_PREFIX_LEN(prefix), {} },
  27. .family = ENDPOINT_KEY_IPV4,
  28. .ip4 = addr,
  29. };
  30. key.ip4 &= GET_PREFIX(prefix);
  31. return map_lookup_elem(map, &key);
  32. }
  33. # To simulate a map lookup, we can use bpftool map lookup command and point it at a pinned location of the IPCACHE map. The key is based on the ipcache_key struct with destination IP 10.0.1.100
  34. # bpftool map lookup pinned /sys/fs/bpf/tc/globals/cilium_ipcache key hex 40 00 00 00 00 00 00 01 0a 00 01 64 00 00 00 00 00 00 00 00 00 00 00 00
  35. root@k8s-1:/home/cilium# bpftool map lookup pinned /sys/fs/bpf/tc/globals/cilium_ipcache key hex 40 00 00 00 00 00 00 01 0a 00 01 64 00 00 00 00 00 00 00 00 00 00 00 00
  36. key:
  37. 40 00 00 00 00 00 00 01 0a 00 01 64 00 00 00 00
  38. 00 00 00 00 00 00 00 00
  39. value:
  40. 47 69 00 00 c0 a8 02 16 00 00 00 00
  41. root@k8s-1:/home/cilium#
  42. 从上边的信息我们可以看到:
  43. Dst_IP:c0 a8 02 16 --- 192.168.2.22
  44. 1.此时一旦这个OuterIP查询完成,此时就可以进入VxLAN的数据报文封装。
  45. 2.封装过后的VxLAN数据包,就直接在:https://github.com/cilium/cilium/blob/v1.9.1/bpf/lib/encap.h#L153 的逻辑下redirect到VXLAN的egress的interface上。
  46. 3.此时在外部网络的交换或是路由下送到Dst_IP:192.168.2.22所在的主机上。完成数据包出域的过程。

eBPF Packet Processing Ingress[k8s-2]

  1. # 此时由刚才转发过来的数据包,此时触发cilium_vxlan(6) clsact/ingress bpf_overlay.o:[from-overlay] id 637
  2. root@k8s-1:/home/cilium# bpftool net show
  3. xdp:
  4. tc:
  5. ens33(2) clsact/ingress bpf_netdev_ens33.o:[from-netdev] id 703
  6. ens33(2) clsact/egress bpf_netdev_ens33.o:[to-netdev] id 709
  7. cilium_net(4) clsact/ingress bpf_host_cilium_net.o:[to-host] id 697
  8. cilium_host(5) clsact/ingress bpf_host.o:[to-host] id 685
  9. cilium_host(5) clsact/egress bpf_host.o:[from-host] id 691
  10. cilium_vxlan(6) clsact/ingress bpf_overlay.o:[from-overlay] id 637 # match this tc filter
  11. cilium_vxlan(6) clsact/egress bpf_overlay.o:[to-overlay] id 642
  12. lxc_health(8) clsact/ingress bpf_lxc.o:[from-container] id 657
  13. lxcccc40ac18a81(10) clsact/ingress bpf_lxc.o:[from-container] id 663
  14. lxc0486be7fa43b(12) clsact/ingress bpf_lxc.o:[from-container] id 659
  15. lxc245b8cb25654(14) clsact/ingress bpf_lxc.o:[from-container] id 661
  16. 2.This time it’s the from-overlay program located inside bpf/bpf_overlay.c.
  17. 3.All IPv4 packets get processed by the handle_ipv4 function. # handle_ipv4 通过handle_ipv4 来处理。
  18. 4.Inside this function execution flow reaches the point[# 这个point就是lookup_ip4_endpoint#] where another map lookup is triggered. This lookup is needed to identify the local interface that’s supposed to receive this packet and build the correct Ethernet header.
  19. 5.The lookup_ip4_endpoint function is defined inside bpf/lib/eps.h:
  20. # 这个lookup的逻辑是:
  21. static __always_inline __maybe_unused struct endpoint_info *
  22. __lookup_ip4_endpoint(__u32 ip)
  23. {
  24. struct endpoint_key key = {};
  25. key.ip4 = ip;
  26. key.family = ENDPOINT_KEY_IPV4;
  27. return map_lookup_elem(&ENDPOINTS_MAP, &key);
  28. }
  29. # return map_lookup_elem(&ENDPOINTS_MAP 这里返回。ENDPOINTS_MAP。
  30. The ENDPOINTS_MAP is pinned in the file called cilium_lxc which can be found next to the IPCACHE map in /sys/fs/bpf/tc/globals/ directory. The key for the lookup can be built based on the endpoint_key data structure by plugging in values of destination IP (10.0.1.110) and IPv4 address family. The resulting lookup will look similar to this:
  31. key:
  32. 0a 00 01 64 00 00 00 00 00 00 00 00 00 00 00 00
  33. 01 00 00 00
  34. value:
  35. 0c 00 00 00 00 00 ec 02 00 00 00 00 00 00 00 00
  36. 02 a1 1c 41 a5 0c 00 00 ea db e8 e3 75 cb 00 00
  37. 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  38. # 0a 00 01 64 --- 10.0.1.100
  39. # 02 a1 1c 41 a5 0c --- pod's MAC
  40. # ea db e8 e3 75 cb --- lxc's MAC
  41. # 0c --- interface index
  42. # ec 02 --- policy id
  43. At this point the lookup result gets passed to ipv4_local_delivery which does two things:
  44. 1.1:Populates source and destination MAC addresses and decrements TTL.
  45. 1.2:Makes a tail-call to another eBPF program identified by the lxc_id.
  46. 2.The last call is made to the to-container program that passes the packet’s context through ipv4_policy where, finally, it gets redirected the destination veth interface.

Capture The Flow

  1. # env
  2. [root@k8s-1 ~]# kubectl exec -it cni-kq5sq -- ping 10.0.1.100
  3. PING 10.0.1.100 (10.0.1.100): 56 data bytes
  4. 64 bytes from 10.0.1.100: seq=0 ttl=63 time=0.977 ms
  5. ^C
  6. --- 10.0.1.100 ping statistics ---
  7. 1 packets transmitted, 1 packets received, 0% packet loss
  8. round-trip min/avg/max = 0.977/0.977/0.977 ms
  9. [root@k8s-1 ~]#
  10. # k8s-1节点上抓包信息:
  11. # 1.1:Pod的eth0抓包信息:[ICMP Request && ICMP Replay]
  12. [root@k8s-1 ~]# kubectl exec -it cni-kq5sq -- tcpdump -ne -i eth0
  13. tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
  14. listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes
  15. 07:39:20.666747 9e:03:09:77:7d:b0 > b6:3a:d6:e8:f2:49, ethertype IPv4 (0x0800), length 98: 10.0.0.254 > 10.0.1.100: ICMP echo request, id 15616, seq 281, length 64
  16. 07:39:20.667323 b6:3a:d6:e8:f2:49 > 9e:03:09:77:7d:b0, ethertype IPv4 (0x0800), length 98: 10.0.1.100 > 10.0.0.254: ICMP echo reply, id 15616, seq 281, length 64
  17. # 这里我们看到Pod中是有来有回的ICMP包的。
  18. # 1.2:在Pod对应的veth的lxc上抓包:
  19. [root@k8s-1 ~]# tcpdump -ne -i lxc0486be7fa43b
  20. tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
  21. listening on lxc0486be7fa43b, link-type EN10MB (Ethernet), capture size 262144 bytes
  22. 15:41:11.742308 9e:03:09:77:7d:b0 > b6:3a:d6:e8:f2:49, ethertype IPv4 (0x0800), length 98: 10.0.0.254 > 10.0.1.100: ICMP echo request, id 15616, seq 392, length 64
  23. 15:41:12.742793 9e:03:09:77:7d:b0 > b6:3a:d6:e8:f2:49, ethertype IPv4 (0x0800), length 98: 10.0.0.254 > 10.0.1.100: ICMP echo request, id 15616, seq 393, length 64
  24. # 这里我们发现此时只有ICMP的Request的包,却没有ICMP的Replay的包,具体原因,请参考同节点Pod通信的原理介绍。
  25. # 1.3:在cilium_vxlan上抓包:
  26. [root@k8s-1 ~]# tcpdump -ne -i cilium_vxlan
  27. tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
  28. listening on cilium_vxlan, link-type EN10MB (Ethernet), capture size 262144 bytes
  29. 15:43:04.814632 9e:03:09:77:7d:b0 > b6:3a:d6:e8:f2:49, ethertype IPv4 (0x0800), length 98: 10.0.0.254 > 10.0.1.100: ICMP echo request, id 15616, seq 505, length 64
  30. 15:43:04.815137 02:a1:1c:41:a5:0c > ea:db:e8:e3:75:cb, ethertype IPv4 (0x0800), length 98: 10.0.1.100 > 10.0.0.254: ICMP echo reply, id 15616, seq 505, length 64
  31. # 此时我们可以看到这里ICMP的Request 和 Replay都能抓到。
  32. # 1.4:在ens33上抓包:
  33. [root@k8s-1 ~]# tcpdump -nne -vv -i ens33 -f udp
  34. tcpdump: listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
  35. 15:45:37.094936 00:0c:29:bd:fb:4a > 00:0c:29:e2:bf:86, ethertype IPv4 (0x0800), length 148: (tos 0x0, ttl 64, id 58729, offset 0, flags [none], proto UDP (17), length 134)
  36. 192.168.2.21.43558 > 192.168.2.22.8472: [no cksum] OTV, flags [I] (0x08), overlay 0, instance 26951
  37. 9e:03:09:77:7d:b0 > b6:3a:d6:e8:f2:49, ethertype IPv4 (0x0800), length 98: (tos 0x0, ttl 64, id 15400, offset 0, flags [DF], proto ICMP (1), length 84)
  38. 10.0.0.254 > 10.0.1.100: ICMP echo request, id 15616, seq 657, length 64
  39. 15:45:37.095632 00:0c:29:e2:bf:86 > 00:0c:29:bd:fb:4a, ethertype IPv4 (0x0800), length 148: (tos 0x0, ttl 64, id 21023, offset 0, flags [none], proto UDP (17), length 134)
  40. 192.168.2.22.34421 > 192.168.2.21.8472: [no cksum] OTV, flags [I] (0x08), overlay 0, instance 26951
  41. 02:a1:1c:41:a5:0c > ea:db:e8:e3:75:cb, ethertype IPv4 (0x0800), length 98: (tos 0x0, ttl 64, id 50485, offset 0, flags [none], proto ICMP (1), length 84)
  42. 10.0.1.100 > 10.0.0.254: ICMP echo reply, id 15616, seq 657, length 64
  43. # 此时我们在ens33上抓包可以看到,是一个完整的VxLNA的数据包,且有Request和Replay的消息。
  44. # k8s-2上信息:
  45. # ens33上抓包:
  46. [root@k8s-2 ~]# tcpdump -nne -vv -i ens33 -f udp
  47. tcpdump: listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
  48. 15:48:15.227744 00:0c:29:bd:fb:4a > 00:0c:29:e2:bf:86, ethertype IPv4 (0x0800), length 148: (tos 0x0, ttl 64, id 7556, offset 0, flags [none], proto UDP (17), length 134)
  49. 192.168.2.21.43558 > 192.168.2.22.8472: [no cksum] OTV, flags [I] (0x08), overlay 0, instance 26951
  50. 9e:03:09:77:7d:b0 > b6:3a:d6:e8:f2:49, ethertype IPv4 (0x0800), length 98: (tos 0x0, ttl 64, id 28385, offset 0, flags [DF], proto ICMP (1), length 84)
  51. 10.0.0.254 > 10.0.1.100: ICMP echo request, id 15616, seq 815, length 64
  52. 15:48:15.227998 00:0c:29:e2:bf:86 > 00:0c:29:bd:fb:4a, ethertype IPv4 (0x0800), length 148: (tos 0x0, ttl 64, id 31689, offset 0, flags [none], proto UDP (17), length 134)
  53. 192.168.2.22.34421 > 192.168.2.21.8472: [no cksum] OTV, flags [I] (0x08), overlay 0, instance 26951
  54. 02:a1:1c:41:a5:0c > ea:db:e8:e3:75:cb, ethertype IPv4 (0x0800), length 98: (tos 0x0, ttl 64, id 62239, offset 0, flags [none], proto ICMP (1), length 84)
  55. 10.0.1.100 > 10.0.0.254: ICMP echo reply, id 15616, seq 815, length 64
  56. # 在cilium_vxlan上抓包:
  57. [root@k8s-2 ~]# tcpdump -ne -i cilium_vxlan
  58. tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
  59. listening on cilium_vxlan, link-type EN10MB (Ethernet), capture size 262144 bytes
  60. 15:48:39.252415 9e:03:09:77:7d:b0 > b6:3a:d6:e8:f2:49, ethertype IPv4 (0x0800), length 98: 10.0.0.254 > 10.0.1.100: ICMP echo request, id 15616, seq 839, length 64
  61. 15:48:39.252628 02:a1:1c:41:a5:0c > ea:db:e8:e3:75:cb, ethertype IPv4 (0x0800), length 98: 10.0.1.100 > 10.0.0.254: ICMP echo reply, id 15616, seq 839, length 64
  62. # 在Pod的lxc上抓包:
  63. [root@k8s-2 ~]# tcpdump -ne -i lxcaa879877688d
  64. tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
  65. listening on lxcaa879877688d, link-type EN10MB (Ethernet), capture size 262144 bytes
  66. 15:49:27.290525 02:a1:1c:41:a5:0c > ea:db:e8:e3:75:cb, ethertype IPv4 (0x0800), length 98: 10.0.1.100 > 10.0.0.254: ICMP echo reply, id 15616, seq 887, length 64
  67. 15:49:28.290775 02:a1:1c:41:a5:0c > ea:db:e8:e3:75:cb, ethertype IPv4 (0x0800), length 98: 10.0.1.100 > 10.0.0.254: ICMP echo reply, id 15616, seq 888, length 64
  68. # 在Pod中抓包:
  69. [root@k8s-2 ~]# kubectl exec -it cni-4p44n -- tcpdump -ne -i eth0
  70. tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
  71. listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes
  72. 07:50:02.619542 ea:db:e8:e3:75:cb > 02:a1:1c:41:a5:0c, ethertype IPv4 (0x0800), length 98: 10.0.0.254 > 10.0.1.100: ICMP echo request, id 15616, seq 922, length 64
  73. 07:50:02.619811 02:a1:1c:41:a5:0c > ea:db:e8:e3:75:cb, ethertype IPv4 (0x0800), length 98: 10.0.1.100 > 10.0.0.254: ICMP echo reply, id 15616, seq 922, length 64

image.png