1. # Kubernetes CNI Calico GuideLine
  2. # 时间:20210808-[14:00-17:00]
  3. # 主题:Calico IPIP Mode
  4. # 文档:https://www.yuque.com/docs/share/1b124592-a1f8-4644-9725-38e26507535b?# 《20210808-Calico IPIP Mode》
  5. # 作业:Calico 和 Flannel的区别
  1. # FAQ:
  2. #1.注意VxLAN模式不需要BGP协议参与!!!但是IPIP模式是需要的。
  3. 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
  4. # IPIP Mode 在https://docs.projectcalico.org/manifests/calico.yaml 中修改如下:
  5. # kubectl apply -f https://docs.projectcalico.org/manifests/calico-ipip.yaml
  6. containers:
  7. # Runs calico-node container on each Kubernetes node. This
  8. # container programs network policy and routes on each
  9. # host.
  10. - name: calico-node
  11. image: docker.io/calico/node:v3.18.1
  12. envFrom:
  13. - configMapRef:
  14. # Allow KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT to be overridden for eBPF mode.
  15. name: kubernetes-services-endpoint
  16. optional: true
  17. env:
  18. # Use Kubernetes API as the backing datastore.
  19. - name: DATASTORE_TYPE
  20. value: "kubernetes"
  21. # Wait for the datastore.
  22. - name: WAIT_FOR_DATASTORE
  23. value: "true"
  24. # Set based on the k8s node name.
  25. - name: NODENAME
  26. valueFrom:
  27. fieldRef:
  28. fieldPath: spec.nodeName
  29. # Choose the backend to use.
  30. - name: CALICO_NETWORKING_BACKEND
  31. valueFrom:
  32. configMapKeyRef:
  33. name: calico-config
  34. key: calico_backend
  35. # Cluster type to identify the deployment type
  36. - name: CLUSTER_TYPE
  37. value: "k8s,bgp"
  38. # Auto-detect the BGP IP address.
  39. - name: IP
  40. value: "autodetect"
  41. # Enable IPIP
  42. - name: CALICO_IPV4POOL_IPIP # IPIP模式设置
  43. value: "Alway" # IPIP模式设置的类型:Always,Never,CrossSubnet
  44. # Enable or Disable VXLAN on the default IP pool.
  45. - name: CALICO_IPV4POOL_VXLAN # VxLAN模式设置
  46. value: "Never" # VxLAN模式设置的类型:Always,Never,CrossSubnet
  47. # Set MTU for tunnel device used if ipip is enabled
  48. - name: FELIX_IPINIPMTU
  49. valueFrom:
  50. configMapKeyRef:
  51. name: calico-config
  52. key: veth_mtu
  53. # Set MTU for the VXLAN tunnel device.
  54. - name: FELIX_VXLANMTU
  55. valueFrom:
  56. configMapKeyRef:
  57. name: calico-config
  58. key: veth_mtu
  59. # Set MTU for the Wireguard tunnel device.
  60. - name: FELIX_WIREGUARDMTU
  61. valueFrom:
  62. configMapKeyRef:
  63. name: calico-config
  64. key: veth_mtu
  65. # The default IPv4 pool to create on startup if none exists. Pod IPs will be
  66. # chosen from this range. Changing this value after installation will have
  67. # no effect. This should fall within `--cluster-cidr`.
  68. # - name: CALICO_IPV4POOL_CIDR
  69. # value: "192.168.0.0/16"
  70. # Disable file logging so `kubectl logs` works.
  71. - name: CALICO_DISABLE_FILE_LOGGING
  72. value: "true"
  73. # Set Felix endpoint to host default action to ACCEPT.
  74. - name: FELIX_DEFAULTENDPOINTTOHOSTACTION
  75. value: "ACCEPT"
  76. # Disable IPv6 on Kubernetes.
  77. - name: FELIX_IPV6SUPPORT
  78. value: "false"
  79. # Set Felix logging to "info"
  80. - name: FELIX_LOGSEVERITYSCREEN
  81. value: "info"
  82. - name: FELIX_HEALTHENABLED
  83. value: "true"
  84. securityContext:
  85. 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. cni-mm9pd 1/1 Running 1 123d 10.244.200.194 k8s-2 <none> <none>
  6. cnitest 1/1 Running 0 26s 10.244.231.201 k8s-1 <none> <none> # cni2 测试pod
  7. [root@k8s-1 ~]#
  8. # Calico模式
  9. [root@k8s-1 ~]# calicoctl get ippool -o wide
  10. NAME CIDR NAT IPIPMODE VXLANMODE DISABLED SELECTOR
  11. default-ipv4-ippool 10.244.0.0/16 true Always Never false all()
  12. [root@k8s-1 ~]# calicoctl get ippool default-ipv4-ippool -o yaml
  13. apiVersion: projectcalico.org/v3
  14. kind: IPPool
  15. metadata:
  16. creationTimestamp: "2021-04-04T04:55:21Z"
  17. name: default-ipv4-ippool
  18. resourceVersion: "1323"
  19. uid: ce563f34-8423-4b39-9653-d98cd77bad8a
  20. spec:
  21. blockSize: 26
  22. cidr: 10.244.0.0/16
  23. ipipMode: Always # 使用IPIP Mode,且模式是Always
  24. natOutgoing: true
  25. nodeSelector: all()
  26. vxlanMode: Never
  27. [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. 不同节点通信分析:
  2. env:
  3. [root@k8s-1 ~]# kubectl get pods -o wide
  4. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  5. cni-j7klb 1/1 Running 1 125d 10.244.231.200 k8s-1 <none> <none>
  6. cni-mm9pd 1/1 Running 1 125d 10.244.200.194 k8s-2 <none> <none>
  7. [root@k8s-1 ~]#
  8. 地址信息:
  9. [root@k8s-1 ~]# kubectl exec -it cni-j7klb -- ifconfig
  10. eth0 Link encap:Ethernet HWaddr F2:70:A8:61:D6:FC
  11. inet addr:10.244.231.200 Bcast:10.244.231.200 Mask:255.255.255.255
  12. UP BROADCAST RUNNING MULTICAST MTU:1480 Metric:1
  13. RX packets:10 errors:0 dropped:0 overruns:0 frame:0
  14. TX packets:5 errors:0 dropped:0 overruns:0 carrier:0
  15. collisions:0 txqueuelen:0
  16. RX bytes:941 (941.0 B) TX bytes:364 (364.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. [root@k8s-1 ~]#
  25. [root@k8s-1 ~]# kubectl exec -it cni-mm9pd -- ifconfig
  26. eth0 Link encap:Ethernet HWaddr 42:96:D2:05:82:DA
  27. inet addr:10.244.200.194 Bcast:10.244.200.194 Mask:255.255.255.255
  28. UP BROADCAST RUNNING MULTICAST MTU:1480 Metric:1
  29. RX packets:5 errors:0 dropped:0 overruns:0 frame:0
  30. TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
  31. collisions:0 txqueuelen:0
  32. RX bytes:446 (446.0 B) TX bytes:0 (0.0 B)
  33. lo Link encap:Local Loopback
  34. inet addr:127.0.0.1 Mask:255.0.0.0
  35. UP LOOPBACK RUNNING MTU:65536 Metric:1
  36. RX packets:0 errors:0 dropped:0 overruns:0 frame:0
  37. TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
  38. collisions:0 txqueuelen:1000
  39. RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
  40. [root@k8s-1 ~]#
  41. #1.由上述叙述我们知道了数据包是如何从Pod中到达ROOT NS中,在这里我们就不做赘述。
  42. #2.使用 10.244.231.200 ping 10.244.200.194,数据到达ROOT NS以后,我们查询路由表条目:
  43. [root@k8s-1 ~]# route -n
  44. Kernel IP routing table
  45. Destination Gateway Genmask Flags Metric Ref Use Iface
  46. 0.0.0.0 172.12.1.2 0.0.0.0 UG 100 0 0 ens33
  47. 10.244.200.192 172.12.1.12 255.255.255.192 UG 0 0 0 tunl0 # 通过D_IP,可以匹配到此条路由。非本地路由学习
  48. 10.244.231.192 0.0.0.0 255.255.255.192 U 0 0 0 *
  49. 10.244.231.197 0.0.0.0 255.255.255.255 UH 0 0 0 cali6068269d8a9
  50. 10.244.231.198 0.0.0.0 255.255.255.255 UH 0 0 0 cali2e2bedebfa9
  51. 10.244.231.199 0.0.0.0 255.255.255.255 UH 0 0 0 calid477199c0e4
  52. 10.244.231.200 0.0.0.0 255.255.255.255 UH 0 0 0 cali0949f5d22ad
  53. 10.244.231.201 0.0.0.0 255.255.255.255 UH 0 0 0 cali018c59df19a
  54. 172.12.1.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
  55. 172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0
  56. [root@k8s-1 ~]#
  57. 此时我们需要构造IP in IP 模式的数据包,我们需要知道:
  58. Outer_S_IP Outer_D_IP Outer_S_MAC Outer_D_MAC
  59. Inner_S_IP Inner_D_IP
  60. #3.由路由条目我们知道,去往10.244.231.192/26网段的出接口是本端的tunl0接口:
  61. [root@k8s-1 ~]# ip -d link show tunl0
  62. 4: tunl0@NONE: <NOARP,UP,LOWER_UP> mtu 1480 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
  63. link/ipip 0.0.0.0 brd 0.0.0.0 promiscuity 0
  64. ipip remote any local any ttl inherit nopmtudisc addrgenmode eui64 numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535
  65. [root@k8s-1 ~]#
  66. 我们查看tunl0网卡的信息,发现其实一个ipip设备。
  67. 所以原始数据包会被tunl0设备做一次加工,转换为RAW格式。
  68. 然后再ipip的内核模块下指导IP in IP封装。具体形式为:
  69. Outer_MAC: ———————————————————————— 00:0c:29:bd:fb:4a - 00:0c:29:e2:bf:86
  70. Outer_IP: ———————————————————————— 172.12.1.11 - 172.12.1.12
  71. Inner_IP: ———————————————————————— 10.244.231.200 - 10.244.200.194
  72. # 10.244.231.192/26 via 172.12.1.11 dev tunl0 proto bird onlink
  73. 关于非()本地路由()学习途径问题:
  74. 10.244.200.192 172.12.1.12 255.255.255.192 UG 0 0 0 tunl0 # 通过D_IP,可以匹配到此条路由。非本地路由学习
  75. 此时我们需要弄清楚,我们IP in IP的Outer_D_IP问题,此时由BIRD学习维护。[请注意:非本地路由学习需要解释其来源,因为默认情况下是无法动态学习除本身以外的路由信息@!!!]
  76. # 抓包分析:
  77. [root@k8s-1 ~]# kubectl exec -it cni-j7klb -- ping 10.244.200.194
  78. PING 10.244.200.194 (10.244.200.194): 56 data bytes
  79. 64 bytes from 10.244.200.194: seq=0 ttl=62 time=0.807 ms
  80. ^C
  81. --- 10.244.200.194 ping statistics ---
  82. 1 packets transmitted, 1 packets received, 0% packet loss
  83. round-trip min/avg/max = 0.807/0.807/0.807 ms
  84. [root@k8s-1 ~]#
  85. # 1.tunl0抓包:
  86. [root@k8s-1 ~]# tcpdump -ne -i tunl0
  87. tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
  88. listening on tunl0, link-type RAW (Raw IP), capture size 262144 bytes
  89. 18:45:21.911458 ip: 10.244.231.200 > 10.244.200.194: ICMP echo request, id 11520, seq 0, length 64
  90. 18:45:21.911993 ip: 10.244.200.194 > 10.244.231.200: ICMP echo reply, id 11520, seq 0, length 64
  91. #2.ens33:
  92. No. Time Source Destination Protocol Length Info
  93. 27 2021-08-07 18:46:16.309569 10.244.231.200 10.244.200.194 ICMP 118 Echo (ping) request id=0x3300, seq=0/0, ttl=63 (reply in 29)
  94. Frame 27: 118 bytes on wire (944 bits), 118 bytes captured (944 bits)
  95. Ethernet II, Src: Vmware_bd:fb:4a (00:0c:29:bd:fb:4a), Dst: Vmware_e2:bf:86 (00:0c:29:e2:bf:86)
  96. Internet Protocol Version 4, Src: 172.12.1.11, Dst: 172.12.1.12
  97. Internet Protocol Version 4, Src: 10.244.231.200, Dst: 10.244.200.194
  98. Internet Control Message Protocol
  99. No. Time Source Destination Protocol Length Info
  100. 29 2021-08-07 18:46:16.309969 10.244.200.194 10.244.231.200 ICMP 118 Echo (ping) reply id=0x3300, seq=0/0, ttl=63 (request in 27)
  101. Frame 29: 118 bytes on wire (944 bits), 118 bytes captured (944 bits)
  102. Ethernet II, Src: Vmware_e2:bf:86 (00:0c:29:e2:bf:86), Dst: Vmware_bd:fb:4a (00:0c:29:bd:fb:4a)
  103. Internet Protocol Version 4, Src: 172.12.1.12, Dst: 172.12.1.11
  104. Internet Protocol Version 4, Src: 10.244.200.194, Dst: 10.244.231.200
  105. Internet Control Message Protocol

Cross-Subnet Traffic

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

All inter workload traffic

  1. With ipipMode set to Always, Calico routes traffic using IP in IP for all traffic originating from a Calico enabled-host, to all Calico networked containers and VMs within the IP pool.
  2. apiVersion: projectcalico.org/v3
  3. kind: IPPool
  4. metadata:
  5. name: ippool-ipip-1
  6. spec:
  7. cidr: 192.168.0.0/16
  8. ipipMode: Always
  9. natOutgoing: true