1. # Kubernetes CNI Calico GuideLine
  2. # 时间:20210808-[14:00-17:00]
  3. # 主题:Calico VxLAN Mode
  4. # 文档:https://www.yuque.com/docs/share/ef274d9f-c139-4187-85ed-45ae7a93246c?# 《20210808-Calico VxLAN Mode》
  5. # 作业:Calico 和 Flannel的区别
  1. # FAQ:
  2. # Switching from IP-in-IP to VXLAN
  3. By default, the Calico manifests enable IP-in-IP encapsulation. If you are on a network that blocks IP-in-IP, such as Azure, you may wish to switch to Calico’s VXLAN encapsulation mode. To do this at install time (so that Calico creates the default IP pool with VXLAN and no IP-in-IP configuration has to be undone):
  4. 1.Start with one of the Calico for policy and networking manifests.
  5. 2.Replace environment variable name CALICO_IPV4POOL_IPIP withCALICO_IPV4POOL_VXLAN. Leave the value of the new variable as “Always”.
  6. 3.Optionally, (to save some resources if you’re running a VXLAN-only cluster) completely disable Calico’s BGP-based networking:
  7. Replace calico_backend: "bird" with calico_backend: "vxlan". This disables BIRD.
  8. Comment out the line - -bird-ready and - -bird-live from the calico/node readiness/liveness check (otherwise disabling BIRD will cause the readiness/liveness check to fail on every node):
  9. livenessProbe:
  10. exec:
  11. command:
  12. - /bin/calico-node
  13. - -felix-live
  14. # - -bird-live
  15. readinessProbe:
  16. exec:
  17. command:
  18. - /bin/calico-node
  19. # - -bird-ready
  20. - -felix-ready
  21. #1.注意VxLAN模式不需要BGP协议参与!!!但是IPIP模式是需要的。
  22. Calico supports two types of encapsulation: VXLAN and IP in IP. VXLAN is supported in some environments where IP in IP is not (for example, Azure). VXLAN has a slightly higher per-packet overhead because the header is larger, but unless you are running very network intensive workloads the difference is not something you would typically notice. The other small difference between the two types of encapsulation is that Calico’s VXLAN implementation does not use BGP, whereas Calico’s IP in IP implementation uses BGP between Calico nodes. # From Calico Docs
  23. # https://calicousers.slack.com/archives/CPTH1KS00/p1619330778149100 # Slack Channel
  24. # VxLAN Mode 在https://docs.projectcalico.org/manifests/calico.yaml 中修改如下:
  25. # kubectl apply -f https://docs.projectcalico.org/manifests/calico-vxlan.yaml
  26. containers:
  27. # Runs calico-node container on each Kubernetes node. This
  28. # container programs network policy and routes on each
  29. # host.
  30. - name: calico-node
  31. image: docker.io/calico/node:v3.18.1
  32. envFrom:
  33. - configMapRef:
  34. # Allow KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT to be overridden for eBPF mode.
  35. name: kubernetes-services-endpoint
  36. optional: true
  37. env:
  38. # Use Kubernetes API as the backing datastore.
  39. - name: DATASTORE_TYPE
  40. value: "kubernetes"
  41. # Wait for the datastore.
  42. - name: WAIT_FOR_DATASTORE
  43. value: "true"
  44. # Set based on the k8s node name.
  45. - name: NODENAME
  46. valueFrom:
  47. fieldRef:
  48. fieldPath: spec.nodeName
  49. # Choose the backend to use.
  50. - name: CALICO_NETWORKING_BACKEND
  51. valueFrom:
  52. configMapKeyRef:
  53. name: calico-config
  54. key: calico_backend
  55. # Cluster type to identify the deployment type
  56. - name: CLUSTER_TYPE
  57. value: "k8s,bgp"
  58. # Auto-detect the BGP IP address.
  59. - name: IP
  60. value: "autodetect"
  61. # Enable IPIP
  62. - name: CALICO_IPV4POOL_IPIP # IPIP模式设置
  63. value: "Never" # IPIP模式设置的类型:Always,Never,CrossSubnet
  64. # Enable or Disable VXLAN on the default IP pool.
  65. - name: CALICO_IPV4POOL_VXLAN # VxLAN模式设置
  66. value: "Always" # VxLAN模式设置的类型:Always,Never,CrossSubnet
  67. # Set MTU for tunnel device used if ipip is enabled
  68. - name: FELIX_IPINIPMTU
  69. valueFrom:
  70. configMapKeyRef:
  71. name: calico-config
  72. key: veth_mtu
  73. # Set MTU for the VXLAN tunnel device.
  74. - name: FELIX_VXLANMTU
  75. valueFrom:
  76. configMapKeyRef:
  77. name: calico-config
  78. key: veth_mtu
  79. # Set MTU for the Wireguard tunnel device.
  80. - name: FELIX_WIREGUARDMTU
  81. valueFrom:
  82. configMapKeyRef:
  83. name: calico-config
  84. key: veth_mtu
  85. # The default IPv4 pool to create on startup if none exists. Pod IPs will be
  86. # chosen from this range. Changing this value after installation will have
  87. # no effect. This should fall within `--cluster-cidr`.
  88. # - name: CALICO_IPV4POOL_CIDR
  89. # value: "192.168.0.0/16"
  90. # Disable file logging so `kubectl logs` works.
  91. - name: CALICO_DISABLE_FILE_LOGGING
  92. value: "true"
  93. # Set Felix endpoint to host default action to ACCEPT.
  94. - name: FELIX_DEFAULTENDPOINTTOHOSTACTION
  95. value: "ACCEPT"
  96. # Disable IPv6 on Kubernetes.
  97. - name: FELIX_IPV6SUPPORT
  98. value: "false"
  99. # Set Felix logging to "info"
  100. - name: FELIX_LOGSEVERITYSCREEN
  101. value: "info"
  102. - name: FELIX_HEALTHENABLED
  103. value: "true"
  104. securityContext:
  105. privileged: true

Install Calcio With Kubernetes API Datastore

  • [x] Install Calico

    1. #1. download resources template:
    2. # curl https://docs.projectcalico.org/manifests/calico.yaml -O
    3. #2. install Calico with Kubernetes API
    4. kubectl apply -f calico.yaml
    5. #3. 检查状态:
    6. [root@k8s-1 ~]# kubectl get pods -o wide -A | grep calico
    7. kube-system calico-kube-controllers-69496d8b75-tvgzw 1/1 Running 1 123d 10.244.231.199 k8s-1 <none> <none>
    8. kube-system calico-node-ktrhr 1/1 Running 1 123d 172.12.1.11 k8s-1 <none> <none>
    9. kube-system calico-node-m8n5d 1/1 Running 1 123d 172.12.1.12 k8s-2 <none> <none>
    10. [root@k8s-1 ~]#
    11. # 查看默认模式(Calico默认模式为IPIP Mode):
    12. [root@k8s-1 ~]# calicoctl get ippool -o wide
    13. NAME CIDR NAT IPIPMODE VXLANMODE DISABLED SELECTOR
    14. default-ipv4-ippool 10.244.0.0/16 true Always Never false all()
    15. [root@k8s-1 ~]#
    16. # 其实Calico提供了一个接口来配置其不同的Mode:
    17. [root@k8s-1 ~]# calicoctl get ippool default-ipv4-ippool -o yaml
    18. apiVersion: projectcalico.org/v3
    19. kind: IPPool
    20. metadata:
    21. creationTimestamp: "2021-04-04T04:55:21Z"
    22. name: default-ipv4-ippool
    23. resourceVersion: "1323"
    24. uid: ce563f34-8423-4b39-9653-d98cd77bad8a
    25. spec:
    26. blockSize: 26 # IP地址块的大小,,默认是26位,支持IPV4和IPV6
    27. cidr: 10.244.0.0/16 # IP地址的范围
    28. ipipMode: Always # ipipMode模式,支持的由:1.Always(default mode) 2.Never 3.CrossSubnet
    29. natOutgoing: true # SNAT(源地址映射)
    30. nodeSelector: all() # 选中相应的节点,接受IPAM地址分配管理
    31. vxlanMode: Never # VxLAN 模式开关(IPIP和VxLAN模式只能同时存在一种模式)
    32. [root@k8s-1 ~]# 具体的介绍见下表格:
    33. # From this URL:https://docs.projectcalico.org/archive/v3.20/reference/resources/ippool
  • [x] IP POOL 配置说明:

image.png

同节点同Pod通信

  1. # 采用container 模式
  2. 这个模式指定新创建的容器和已经存在的一个容器共享一个 Network Namespace,而不是和宿主机共享。新创建的容器不会创建自己的网卡,配置自己的 IP,而是和一个指定的容器共享 IP、端口范围等。同样,两个容器除了网络方面,其他的如文件系统、进程列表等还是隔离的。# [两个容器的进程可以通过 lo 网卡设备通信。]

同节点不同Pod通信

  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-j7klb 1/1 Running 1 123d 10.244.231.200 k8s-1 <none> <none> # cni1 测试pod
  5. cnitest 1/1 Running 0 26s 10.244.231.201 k8s-1 <none> <none> # cni2 测试pod
  6. [root@k8s-1 ~]#
  7. # Calico模式
  8. [root@k8s-1 ~]# calicoctl get ippool -o wide
  9. NAME CIDR NAT IPIPMODE VXLANMODE DISABLED SELECTOR
  10. default-ipv4-ippool 10.244.0.0/16 true Never Always false all()
  11. [root@k8s-1 ~]# calicoctl get ippool default-ipv4-ippool -o yaml
  12. apiVersion: projectcalico.org/v3
  13. kind: IPPool
  14. metadata:
  15. creationTimestamp: "2021-04-04T04:55:21Z"
  16. name: default-ipv4-ippool
  17. resourceVersion: "1323"
  18. uid: ce563f34-8423-4b39-9653-d98cd77bad8a
  19. spec:
  20. blockSize: 26
  21. cidr: 10.244.0.0/16
  22. ipipMode: Never
  23. natOutgoing: true
  24. nodeSelector: all()
  25. vxlanMode: Never # 使用VxLAN Mode,且模式是Always
  26. [root@k8s-1 ~]#

Pod NS到ROOT NS

  1. # 此时对象为:cni1 ping cni2 对应的pod:10.244.231.200 ping 10.244.231.201
  2. [root@k8s-1 ~]# kubectl get pods -o wide
  3. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  4. cni-j7klb 1/1 Running 1 123d 10.244.231.200 k8s-1 <none> <none> # cni1 测试pod
  5. cnitest 1/1 Running 0 26s 10.244.231.201 k8s-1 <none> <none> # cni2 测试pod
  6. #1.进入cni-j7klb pod:
  7. [root@k8s-1 ~]# kubectl exec -it cni-j7klb bash
  8. kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
  9. bash-5.1# ifconfig
  10. eth0 Link encap:Ethernet HWaddr F2:70:A8:61:D6:FC # MAC地址:F2:70:A8:61:D6:FC
  11. inet addr:10.244.231.200 Bcast:10.244.231.200 Mask:255.255.255.255 # 从这里看,为32位掩码的一个主机地址,这点对于理解此Case非常重要
  12. UP BROADCAST RUNNING MULTICAST MTU:1480 Metric:1
  13. RX packets:5 errors:0 dropped:0 overruns:0 frame:0
  14. TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
  15. collisions:0 txqueuelen:0
  16. RX bytes:446 (446.0 B) TX bytes:0 (0.0 B)
  17. lo Link encap:Local Loopback
  18. inet addr:127.0.0.1 Mask:255.0.0.0
  19. UP LOOPBACK RUNNING MTU:65536 Metric:1
  20. RX packets:0 errors:0 dropped:0 overruns:0 frame:0
  21. TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
  22. collisions:0 txqueuelen:1000
  23. RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
  24. bash-5.1#
  25. rbash-5.1# route -n
  26. Kernel IP routing table
  27. Destination Gateway Genmask Flags Metric Ref Use Iface
  28. 0.0.0.0 169.254.1.1 0.0.0.0 UG 0 0 0 eth0 # 此时我们知道所有的数据报文从该Pod中出去,那么需要发送到169.254.1.1对应的下一跳
  29. 169.254.1.1 0.0.0.0 255.255.255.255 UH 0 0 0 eth0
  30. bash-5.1#
  31. 因为从路由表中我们可以看到:我们需要构造一个完整的数据报文,需要一些必要的元素:
  32. S_IP :10.244.231.200 D_IP: $(cni2)
  33. S_MAC:F2:70:A8:61:D6:FC D_AMC:$(169.254.1.1)
  34. # 在这里我们看到169.254.1.1,但是我们却在整个集群中找不到此地址,那我们如何才能解析到其对应的MAC地址呢?
  35. 那我们又如何获取到获取相应的MAC地址呢?
  36. # 1. Why does my container have a route to 169.254.1.1?
  37. In a Calico network, each host acts as a gateway router for the workloads that it hosts. In container deployments, Calico uses 169.254.1.1 as the address for the Calico router. By using a link-local address, Calico saves precious IP addresses and avoids burdening the user with configuring a suitable address.
  38. While the routing table may look a little odd to someone who is used to configuring LAN networking, using explicit routes rather than subnet-local gateways is fairly common in WAN networking.
  39. ## 解释:在calico启动的时候,会设置169.254.1.1作为一个默认的gateway给容器。所以我们在容器中show路右边可以查看到:
  40. [root@k8s-1 ~]# kubectl exec -it cnitest bash
  41. kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
  42. bash-5.1# route -n
  43. Kernel IP routing table
  44. Destination Gateway Genmask Flags Metric Ref Use Iface
  45. 0.0.0.0 169.254.1.1 0.0.0.0 UG 0 0 0 eth0
  46. 169.254.1.1 0.0.0.0 255.255.255.255 UH 0 0 0 eth0
  47. bash-5.1#
  48. ##########################################################################################################################################################
  49. # 2. Why can’t I see the 169.254.1.1 address mentioned above on my host?
  50. Calico tries hard to avoid interfering with any other configuration on the host. Rather than adding the gateway address to the host side of each workload interface, Calico sets the proxy_arp flag on the interface. This makes the host behave like a gateway, responding to ARPs for 169.254.1.1 without having to actually allocate the IP address to the interface.
  51. # 解释:
  52. Calico避免设置一些ip地址在HOST主机上,而是为每一个workload设置一个网关地址,但是这个地址并不配置在具体的主机上,而是回复相应的ARP消息。此时涉及到Linux中的一个Proxy_ARP相关的概念,通过Proxy_ARP的配置,此时我们主机就可以扮演成一个网关,来回复169.254.1.1对应的MAC地址。
  53. 可通过查询:
  54. [root@k8s-1 ~]# cat /proc/sys/net/ipv4/conf/calid477199c0e4/proxy_arp # 注意此配置是接口关联型
  55. 1
  56. ##########################################################################################################################################################
  57. # 3. I’ve heard Calico uses proxy ARP, doesn’t proxy ARP cause a lot of problems?
  58. It can, but not in the way that Calico uses it.
  59. In container deployments, Calico only uses proxy ARP for resolving the 169.254.1.1 address. The routing table inside the container ensures that all traffic goes via the 169.254.1.1 gateway so that is the only IP that will be ARPed by the container.
  60. # 解释:
  61. calico 仅仅使用proxy_arp来解决mac地址解析问题。可保证所有的流量均需要走三层的路由来做解析。
  62. ##########################################################################################################################################################
  63. # 4. Why do all cali* interfaces have the MAC address ee:ee:ee:ee:ee:ee?
  64. In some setups the kernel is unable to generate a persistent MAC address and so Calico assigns a MAC address itself. Since Calico uses point-to-point routed interfaces, traffic does not reach the data link layer so the MAC Address is never used and can therefore be the same for all the cali* interfaces.
  65. # 解释:
  66. 由于Linux内核无法提供一个稳定MAC地址,而Calico网络中使用ponint-to-point 去路由数据包,数据包并不涉及链路层,所以自然也是用不到相应的MAC地址,该MAC地址仅仅为了完成标准的TCP/IP协议栈封装数据报文。
  67. ##########################################################################################################################################################
  68. 所以这里涉及到一个Linux Proxy_ARP:我们需要一探究竟.
  69. # 科普:
  70. # https://docs.projectcalico.org/reference/faq

Linux Proxy ARP

image.png

  1. # topo:
  2. ns0 ns1
  3. | |
  4. veth0 veth0
  5. (169.254.1.1) (169.254.1.1)
  6. | |
  7. ens33 ens33
  8. |_______________________|
  9. 172.12.1.11 (Proxy_ARP) 172.12.1.12
  10. # k8s-1 [echo 1 > /proc/sys/net/ipv4/conf/veth0/proxy_arp]
  11. ip link add veth0 type veth peer name eth0 # 配置veth pair
  12. ip netns add ns0 # 添加NS
  13. ip link set eth0 netns ns0 # 把eth0接口添加到ns0中
  14. ip netns exec ns0 ip a add 10.1.1.10/24 dev eth0 # 为ns0中的eth0配置相应的IP地址
  15. ip netns exec ns0 ip link set eth0 up # 设置eth0接口up
  16. ip netns exec ns0 ip route add 169.254.1.1 dev eth0 scope link # 添加路由。eth0 scope link 表明:只在这个设备合法。实际上添加交换"路由"
  17. ip netns exec ns0 ip route add default via 169.254.1.1 dev eth0 # 添加默认路由
  18. ip link set veth0 up # 设置veth pair的另外一端veth0 up
  19. ip route add 10.1.1.10 dev veth0 scope link # 添加10.1.1.10的交换"路由"
  20. ip route add 10.1.1.20 via 172.12.1.12 dev ens33 # 添加到10.1.1.20的路由
  21. echo 1 > /proc/sys/net/ipv4/conf/veth0/proxy_arp # 针对接口开启Proxy_ARP
  22. # k8s-2 [echo 1 > /proc/sys/net/ipv4/conf/veth0/proxy_arp]
  23. ip link add veth0 type veth peer name eth0
  24. ip netns add ns1
  25. ip link set eth0 netns ns1
  26. ip netns exec ns1 ip a add 10.1.1.20/24 dev eth0
  27. ip netns exec ns1 ip link set eth0 up
  28. ip netns exec ns1 ip route add 169.254.1.1 dev eth0 scope link
  29. ip netns exec ns1 ip route add default via 169.254.1.1 dev eth0
  30. ip link set veth0 up
  31. ip route add 10.1.1.20 dev veth0 scope link
  32. ip route add 10.1.1.10 via 172.12.1.11 dev ens33
  33. echo 1 > /proc/sys/net/ipv4/conf/veth0/proxy_arp
  34. # 逻辑分析:
  35. ns0中的10.1.1.10 ping 10.1.1.20:
  36. #1.进入ns0中查看:
  37. [root@k8s-1 ~]# ip netns exec ns0 ifconfig
  38. eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
  39. inet 10.1.1.10 netmask 255.255.255.0 broadcast 0.0.0.0
  40. inet6 fe80::e4f8:f3ff:fe12:2fc2 prefixlen 64 scopeid 0x20<link>
  41. ether e6:f8:f3:12:2f:c2 txqueuelen 1000 (Ethernet)
  42. RX packets 16 bytes 1216 (1.1 KiB)
  43. RX errors 0 dropped 0 overruns 0 frame 0
  44. TX packets 16 bytes 1216 (1.1 KiB)
  45. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  46. [root@k8s-1 ~]#
  47. #2.由于ping的地址是10.1.1.20/32位的一个主机地址,和10.1.1.10 并不在同一个网关,这点需要注意:
  48. 此时查看路由表:
  49. [root@k8s-1 ~]# ip netns exec ns0 route -n
  50. Kernel IP routing table
  51. Destination Gateway Genmask Flags Metric Ref Use Iface
  52. 0.0.0.0 169.254.1.1 0.0.0.0 UG 0 0 0 eth0 # 由于目的地址10.1.1.20/32的一个主机地址,和10.1.1.10/32不在一个网段,会匹配到这条
  53. 10.1.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 # 这个是对于10.1.1.0/24 网络的路由条目
  54. 169.254.1.1 0.0.0.0 255.255.255.255 UH 0 0 0 eth0 # 169.254.1.1的路由配置条目
  55. [root@k8s-1 ~]#
  56. 所以此时需要构造一个数据包:
  57. S_IP: 10.1.1.10 S_MAC: e6:f8:f3:12:2f:c2
  58. D_IP: 10.1.1.20 D_AMC: $(169.254.1.1)'s MAC
  59. # 3.此时查看veth0对应的MAC地址:
  60. veth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
  61. inet6 fe80::68b3:42ff:fe22:f462 prefixlen 64 scopeid 0x20<link>
  62. ether 6a:b3:42:22:f4:62 txqueuelen 1000 (Ethernet) # 6a:b3:42:22:f4:62 MAC地址
  63. RX packets 17 bytes 1286 (1.2 KiB)
  64. RX errors 0 dropped 0 overruns 0 frame 0
  65. TX packets 17 bytes 1286 (1.2 KiB)
  66. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  67. 这里的eth0 和 veth0 为一对veth pair。所以数据报文出去到ROOT NS中用的接口应该为veth0.
  68. # [root@k8s-1 ~]# ip a
  69. 13: veth0@if12: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
  70. link/ether 6a:b3:42:22:f4:62 brd ff:ff:ff:ff:ff:ff link-netnsid 5
  71. inet6 fe80::68b3:42ff:fe22:f462/64 scope link
  72. valid_lft forever preferred_lft forever
  73. [root@k8s-1 ~]# ip netns exec ns0 ethtool -S eth0
  74. NIC statistics:
  75. peer_ifindex: 13 # ethtool -S eth0去查看ROOT NS中对应的接口
  76. rx_queue_0_xdp_packets: 0
  77. rx_queue_0_xdp_bytes: 0
  78. rx_queue_0_drops: 0
  79. rx_queue_0_xdp_redirect: 0
  80. rx_queue_0_xdp_drops: 0
  81. rx_queue_0_xdp_tx: 0
  82. rx_queue_0_xdp_tx_errors: 0
  83. tx_queue_0_xdp_xmit: 0
  84. tx_queue_0_xdp_xmit_errors: 0
  85. [root@k8s-1 ~]#
  86. #4. 由于Linux 内核中基于veth0开启了Proxy_ARP以后,所以虽然veth0的IP地址不是169.254.1.1,但是由于开启了Proxy_ARP,所以会针对P2P的恢复该MAC地址:6a:b3:42:22:f4:62
  87. [root@k8s-1 ~]# ip netns exec ns0 arp -n
  88. Address HWtype HWaddress Flags Mask Iface
  89. 10.1.1.20 ether 6a:b3:42:22:f4:62 C eth0 # $(169.254.1.1)'s MAC
  90. 172.12.1.11 ether 6a:b3:42:22:f4:62 C eth0 # $(169.254.1.1)'s MAC
  91. [root@k8s-1 ~]#
  92. S_IP: 10.1.1.10 S_MAC: e6:f8:f3:12:2f:c2
  93. D_IP: 10.1.1.20 D_AMC: 6a:b3:42:22:f4:62
  94. #5. 抓包显示为:
  95. [root@k8s-1 ~]# ip netns exec ns0 tcpdump -ne -i eth0
  96. # Proxy_ARP
  97. tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
  98. listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
  99. 16:18:14.221245 e6:f8:f3:12:2f:c2 > Broadcast, ethertype ARP (0x0806), length 42: Request who-has 10.1.1.20 tell 10.1.1.10, length 28
  100. 16:18:14.502035 6a:b3:42:22:f4:62 > e6:f8:f3:12:2f:c2, ethertype ARP (0x0806), length 42: Reply 10.1.1.20 is-at 6a:b3:42:22:f4:62, length 28
  101. # ICMP Request And Reply
  102. 16:18:14.502066 e6:f8:f3:12:2f:c2 > 6a:b3:42:22:f4:62, ethertype IPv4(0x0800), length 98: 10.1.1.10 > 10.1.1.20: ICMP echo request, id41080, seq 1, length 64
  103. 16:18:14.807454 6a:b3:42:22:f4:62 > e6:f8:f3:12:2f:c2, ethertype IPv4 (0x0800), length 98: 10.1.1.20 > 10.1.1.10: ICMP echo reply, id 41080, seq 1, length 64
  104. # 至此我们弄明白了,针对于Calico环境的Pod数据包是如何送到Linux的 ROOT NS中。

同节点的Pod to Pod

  1. # 从以上的分析我们知道,数据包是如何从Pod中送到ROOT NS中。但是我们还需要解决的是从cni1 pod到cni2的pod的过程。
  2. #1.我们先查看两Pod的地址信息:
  3. [root@k8s-1 ~]# kubectl get pods -o wide | grep k8s-1
  4. cni-j7klb 1/1 Running 1 125d 10.244.231.200 k8s-1 <none> <none>
  5. cnitest 1/1 Running 0 42h 10.244.231.201 k8s-1 <none> <none>
  6. [root@k8s-1 ~]#
  7. # 地址信息:
  8. [root@k8s-1 ~]# kubectl exec -it cni-j7klb -- ifconfig
  9. eth0 Link encap:Ethernet HWaddr F2:70:A8:61:D6:FC
  10. inet addr:10.244.231.200 Bcast:10.244.231.200 Mask:255.255.255.255 # IP:10.244.231.200/32 MAC:F2:70:A8:61:D6:FC
  11. UP BROADCAST RUNNING MULTICAST MTU:1480 Metric:1
  12. RX packets:10 errors:0 dropped:0 overruns:0 frame:0
  13. TX packets:5 errors:0 dropped:0 overruns:0 carrier:0
  14. collisions:0 txqueuelen:0
  15. RX bytes:941 (941.0 B) TX bytes:364 (364.0 B)
  16. lo Link encap:Local Loopback
  17. inet addr:127.0.0.1 Mask:255.0.0.0
  18. UP LOOPBACK RUNNING MTU:65536 Metric:1
  19. RX packets:0 errors:0 dropped:0 overruns:0 frame:0
  20. TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
  21. collisions:0 txqueuelen:1000
  22. RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
  23. [root@k8s-1 ~]# kubectl exec -it cnitest -- ifconfig
  24. eth0 Link encap:Ethernet HWaddr FE:92:69:10:B1:04
  25. inet addr:10.244.231.201 Bcast:10.244.231.201 Mask:255.255.255.255 # IP:10.244.231.201/32 MAC:FE:92:69:10:B1:04
  26. UP BROADCAST RUNNING MULTICAST MTU:1480 Metric:1
  27. RX packets:5 errors:0 dropped:0 overruns:0 frame:0
  28. TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
  29. collisions:0 txqueuelen:0
  30. RX bytes:446 (446.0 B) TX bytes:0 (0.0 B)
  31. lo Link encap:Local Loopback
  32. inet addr:127.0.0.1 Mask:255.0.0.0
  33. UP LOOPBACK RUNNING MTU:65536 Metric:1
  34. RX packets:0 errors:0 dropped:0 overruns:0 frame:0
  35. TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
  36. collisions:0 txqueuelen:1000
  37. RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
  38. # 我们观察主机上路由条目:
  39. [root@k8s-1 ~]# route -n
  40. Kernel IP routing table
  41. Destination Gateway Genmask Flags Metric Ref Use Iface
  42. 0.0.0.0 172.12.1.2 0.0.0.0 UG 100 0 0 ens33
  43. 10.244.200.192 172.12.1.12 255.255.255.192 UG 0 0 0 tunl0
  44. 10.244.231.192 0.0.0.0 255.255.255.192 U 0 0 0 * # 黑洞路由
  45. 10.244.231.197 0.0.0.0 255.255.255.255 UH 0 0 0 cali6068269d8a9
  46. 10.244.231.198 0.0.0.0 255.255.255.255 UH 0 0 0 cali2e2bedebfa9
  47. 10.244.231.199 0.0.0.0 255.255.255.255 UH 0 0 0 calid477199c0e4
  48. 10.244.231.200 0.0.0.0 255.255.255.255 UH 0 0 0 cali0949f5d22ad # 目标cni-j7klb pod
  49. 10.244.231.201 0.0.0.0 255.255.255.255 UH 0 0 0 cali018c59df19a # 目标cnitest pod
  50. 172.12.1.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
  51. 172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0
  52. [root@k8s-1 ~]#
  53. [root@k8s-1 ~]#
  54. # 注意我们的地址都是32位的主机地址,也就意味着他们并不是属于同一个网段。这点很重要。
  55. #2. 此时我们简化一下我们的网络拓扑:
  56. Pod(cni-j7klb) Pod(cnitest)
  57. 10.244.231.200/32 MAC:F2:70:A8:61:D6:FC IP:10.244.231.201/32 MAC:FE:92:69:10:B1:04
  58. | |
  59. \ /
  60. \__________________ Router __________________/
  61. | |
  62. 169.254.1.1 169.254.1.1
  63. Proxy_ARP
  64. # 从上边的topo:
  65. 此时Linux Host就扮演者一个Router的角色。那么问题就变成了路由上的两台主机的通信问题。
  66. # 此时这里涉及到一个黑洞路由问题:以192.168.100.0/26为例说明:
  67. CalicoIP地址的分配策略相关,具体表现为一个/26的IP Block192.168.100.0/26分配给了A机器之后,在另外一台B机器上又出现了该IP Block内的一个IP 192.168.100.10,同时因为A机器上有该IP Block的blackhole路由blackhole 192.168.100.0/26 proto bird,所以导致A机器上所有的Pod访问192.168.100.10时因为黑洞路由原因直接失败。
  68. 一种辅助防止地址冲突的手段算是。

不同节点Pod通信

  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-cqhpz 1/1 Running 1 125d 10.244.200.200 k8s-2 <none> <none>
  5. cni-ks2dx 1/1 Running 1 125d 10.244.231.194 k8s-1 <none> <none>
  6. [root@k8s-1 ~]#
  7. # 地址信息:
  8. [root@k8s-1 ~]# kubectl exec -it cni-cqhpz -- ifconfig
  9. eth0 Link encap:Ethernet HWaddr 7E:15:D3:38:8F:65
  10. inet addr:10.244.200.200 Bcast:10.244.200.200 Mask:255.255.255.255
  11. UP BROADCAST RUNNING MULTICAST MTU:1450 Metric:1
  12. RX packets:5 errors:0 dropped:0 overruns:0 frame:0
  13. TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
  14. collisions:0 txqueuelen:0
  15. RX bytes:446 (446.0 B) TX bytes:0 (0.0 B)
  16. lo Link encap:Local Loopback
  17. inet addr:127.0.0.1 Mask:255.0.0.0
  18. UP LOOPBACK RUNNING MTU:65536 Metric:1
  19. RX packets:0 errors:0 dropped:0 overruns:0 frame:0
  20. TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
  21. collisions:0 txqueuelen:1000
  22. RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
  23. [root@k8s-1 ~]# kubectl exec -it cni-ks2dx -- ifconfig
  24. eth0 Link encap:Ethernet HWaddr 96:6B:B3:F8:02:6B
  25. inet addr:10.244.231.194 Bcast:10.244.231.194 Mask:255.255.255.255
  26. UP BROADCAST RUNNING MULTICAST MTU:1450 Metric:1
  27. RX packets:5 errors:0 dropped:0 overruns:0 frame:0
  28. TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
  29. collisions:0 txqueuelen:0
  30. RX bytes:446 (446.0 B) TX bytes:0 (0.0 B)
  31. lo Link encap:Local Loopback
  32. inet addr:127.0.0.1 Mask:255.0.0.0
  33. UP LOOPBACK RUNNING MTU:65536 Metric:1
  34. RX packets:0 errors:0 dropped:0 overruns:0 frame:0
  35. TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
  36. collisions:0 txqueuelen:1000
  37. RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
  38. [root@k8s-1 ~]#
  39. [root@k8s-1 ~]# kubectl get pods -o wide
  40. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  41. cni-cqhpz 1/1 Running 1 125d 10.244.200.200 k8s-2 <none> <none>
  42. cni-ks2dx 1/1 Running 1 125d 10.244.231.194 k8s-1 <none> <none>
  43. # 使用 10.244.231.194 ping 10.244.200.200 过程分析:
  44. #1.由Pod到ROOT NS过程,在此不再赘述。
  45. #2.到达ROOT NS后实现跨节点通信过程分析:
  46. 在k8s-1节点上查看路由表信息:
  47. [root@k8s-1 ~]# route -n
  48. Kernel IP routing table
  49. Destination Gateway Genmask Flags Metric Ref Use Iface
  50. 0.0.0.0 172.12.1.2 0.0.0.0 UG 100 0 0 ens33
  51. 10.244.200.192 10.244.200.192 255.255.255.192 UG 0 0 0 vxlan.calico # 此时发现10.244.200.200 匹配到该条路由,且出接口是vxlan.calico
  52. 10.244.231.194 0.0.0.0 255.255.255.255 UH 0 0 0 cali341351b3f6f
  53. 172.12.1.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
  54. 172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0
  55. [root@k8s-1 ~]#
  56. 而 接口vxlan.calico为vxlan设备:
  57. [root@k8s-1 ~]# ip -d link show vxlan.calico
  58. 6: vxlan.calico: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UNKNOWN mode DEFAULT group default
  59. link/ether 66:bd:dc:d1:59:40 brd ff:ff:ff:ff:ff:ff promiscuity 0
  60. vxlan id 4096 local 172.12.1.11 dev ens33 srcport 0 0 dstport 4789 nolearning ageing 300 udpcsum noudp6zerocsumtx noudp6zerocsumrx addrgenmode eui64 numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535
  61. [root@k8s-1 ~]#
  62. 抓包可得:
  63. #1.在k8s-1-cni-ks2dx 上的eth0抓包:
  64. No. Time Source Destination Protocol Length Info
  65. 1 2021-08-07 21:49:17.638613 6a:9c:78:a1:88:60 Broadcast ARP 42 Who has 169.254.1.1? Tell 10.244.231.194
  66. Frame 1: 42 bytes on wire (336 bits), 42 bytes captured (336 bits)
  67. Ethernet II, Src: 6a:9c:78:a1:88:60 (6a:9c:78:a1:88:60), Dst: Broadcast (ff:ff:ff:ff:ff:ff)
  68. Address Resolution Protocol (request)
  69. No. Time Source Destination Protocol Length Info
  70. 2 2021-08-07 21:49:17.638631 ee:ee:ee:ee:ee:ee 6a:9c:78:a1:88:60 ARP 42 169.254.1.1 is at ee:ee:ee:ee:ee:ee
  71. Frame 2: 42 bytes on wire (336 bits), 42 bytes captured (336 bits)
  72. Ethernet II, Src: ee:ee:ee:ee:ee:ee (ee:ee:ee:ee:ee:ee), Dst: 6a:9c:78:a1:88:60 (6a:9c:78:a1:88:60)
  73. Address Resolution Protocol (reply)
  74. No. Time Source Destination Protocol Length Info
  75. 3 2021-08-07 21:49:17.638633 10.244.231.194 10.244.200.199 ICMP 98 Echo (ping) request id=0x2100, seq=0/0, ttl=64 (reply in 4)
  76. Frame 3: 98 bytes on wire (784 bits), 98 bytes captured (784 bits)
  77. Ethernet II, Src: 6a:9c:78:a1:88:60 (6a:9c:78:a1:88:60), Dst: ee:ee:ee:ee:ee:ee (ee:ee:ee:ee:ee:ee)
  78. Internet Protocol Version 4, Src: 10.244.231.194, Dst: 10.244.200.199
  79. Internet Control Message Protocol
  80. No. Time Source Destination Protocol Length Info
  81. 4 2021-08-07 21:49:17.639096 10.244.200.199 10.244.231.194 ICMP 98 Echo (ping) reply id=0x2100, seq=0/0, ttl=62 (request in 3)
  82. Frame 4: 98 bytes on wire (784 bits), 98 bytes captured (784 bits)
  83. Ethernet II, Src: ee:ee:ee:ee:ee:ee (ee:ee:ee:ee:ee:ee), Dst: 6a:9c:78:a1:88:60 (6a:9c:78:a1:88:60)
  84. Internet Protocol Version 4, Src: 10.244.200.199, Dst: 10.244.231.194
  85. Internet Control Message Protocol
  86. #2.在vxlan.calico上抓包:
  87. No. Time Source Destination Protocol Length Info
  88. 1 2021-08-07 21:49:17.638663 10.244.231.194 10.244.200.199 ICMP 98 Echo (ping) request id=0x2100, seq=0/0, ttl=63 (reply in 2)
  89. Frame 1: 98 bytes on wire (784 bits), 98 bytes captured (784 bits)
  90. Ethernet II, Src: 66:bd:dc:d1:59:40 (66:bd:dc:d1:59:40), Dst: 66:c8:b2:49:0d:d9 (66:c8:b2:49:0d:d9)
  91. Internet Protocol Version 4, Src: 10.244.231.194, Dst: 10.244.200.199
  92. Internet Control Message Protocol
  93. No. Time Source Destination Protocol Length Info
  94. 2 2021-08-07 21:49:17.639053 10.244.200.199 10.244.231.194 ICMP 98 Echo (ping) reply id=0x2100, seq=0/0, ttl=63 (request in 1)
  95. Frame 2: 98 bytes on wire (784 bits), 98 bytes captured (784 bits)
  96. Ethernet II, Src: 66:c8:b2:49:0d:d9 (66:c8:b2:49:0d:d9), Dst: 66:bd:dc:d1:59:40 (66:bd:dc:d1:59:40)
  97. Internet Protocol Version 4, Src: 10.244.200.199, Dst: 10.244.231.194
  98. Internet Control Message Protocol
  99. # 3.在ens33上抓包:
  100. No. Time Source Destination Protocol Length Info
  101. 21 2021-08-07 21:49:17.638678 10.244.231.194 10.244.200.199 ICMP 148 Echo (ping) request id=0x2100, seq=0/0, ttl=63 (reply in 22)
  102. Frame 21: 148 bytes on wire (1184 bits), 148 bytes captured (1184 bits)
  103. Ethernet II, Src: Vmware_bd:fb:4a (00:0c:29:bd:fb:4a), Dst: Vmware_e2:bf:86 (00:0c:29:e2:bf:86)
  104. Internet Protocol Version 4, Src: 172.12.1.11, Dst: 172.12.1.12
  105. User Datagram Protocol, Src Port: 54162, Dst Port: 4789
  106. Virtual eXtensible Local Area Network
  107. Ethernet II, Src: 66:bd:dc:d1:59:40 (66:bd:dc:d1:59:40), Dst: 66:c8:b2:49:0d:d9 (66:c8:b2:49:0d:d9)
  108. Internet Protocol Version 4, Src: 10.244.231.194, Dst: 10.244.200.199
  109. Internet Control Message Protocol
  110. No. Time Source Destination Protocol Length Info
  111. 22 2021-08-07 21:49:17.639053 10.244.200.199 10.244.231.194 ICMP 148 Echo (ping) reply id=0x2100, seq=0/0, ttl=63 (request in 21)
  112. Frame 22: 148 bytes on wire (1184 bits), 148 bytes captured (1184 bits)
  113. Ethernet II, Src: Vmware_e2:bf:86 (00:0c:29:e2:bf:86), Dst: Vmware_bd:fb:4a (00:0c:29:bd:fb:4a)
  114. Internet Protocol Version 4, Src: 172.12.1.12, Dst: 172.12.1.11
  115. User Datagram Protocol, Src Port: 38071, Dst Port: 4789
  116. Virtual eXtensible Local Area Network
  117. Ethernet II, Src: 66:c8:b2:49:0d:d9 (66:c8:b2:49:0d:d9), Dst: 66:bd:dc:d1:59:40 (66:bd:dc:d1:59:40)
  118. Internet Protocol Version 4, Src: 10.244.200.199, Dst: 10.244.231.194
  119. Internet Control Message Protocol
  120. # 关于对端Pod在哪一个node上,是采用fdb表的查询得到。和Flannel的VXLAN一样。
  121. # 从Calico的官网上说明运行的时候不用BGP,和尽量保持和Flannel一样的原因是:为了能使从Flannel上移植到Calico的环境上。
  122. # 具体答复见下边记录:

抓包如下:

VxLAN CrossSubnet Traffic

  1. Configure VXLAN encapsulation for only cross subnet traffic
  2. VXLAN encapsulation can be performed selectively, and only for traffic crossing subnet boundaries.
  3. To enable this feature, set vxlanMode to CrossSubnet.
  4. apiVersion: projectcalico.org/v3
  5. kind: IPPool
  6. metadata:
  7. name: ippool-vxlan-cross-subnet-1
  8. spec:
  9. cidr: 192.168.0.0/16
  10. vxlanMode: CrossSubnet
  11. natOutgoing: true

VxLAN All Inner Worklaod Traffic

  1. Configure VXLAN encapsulation for all inter workload traffic
  2. With vxlanMode set to Always, Calico routes traffic using VXLAN for all traffic originating from a Calico enabled host, to all Calico networked containers and VMs within the IP pool.
  3. apiVersion: projectcalico.org/v3
  4. kind: IPPool
  5. metadata:
  6. name: ippool-vxlan-1
  7. spec:
  8. cidr: 192.168.0.0/16
  9. vxlanMode: Always
  10. natOutgoing: true
  11. If you use only VXLAN pools, BGP networking is not required. You can disable BGP to reduce the moving parts in your cluster by Customizing the manifests. Set the calico_backend setting to vxlan, and disable the BGP readiness check.