kube-proxy

每臺機器上都運行一個 kube-proxy 服務,它監聽 API server 中 service 和 endpoint 的變化情況,並通過 iptables 等來爲服務配置負載均衡(僅支持 TCP 和 UDP)。

kube-proxy 可以直接運行在物理機上,也可以以 static pod 或者 daemonset 的方式運行。

kube-proxy 當前支持以下幾種實現

  • userspace:最早的負載均衡方案,它在用戶空間監聽一個端口,所有服務通過 iptables 轉發到這個端口,然後在其內部負載均衡到實際的 Pod。該方式最主要的問題是效率低,有明顯的性能瓶頸。

  • iptables:目前推薦的方案,完全以 iptables 規則的方式來實現 service 負載均衡。該方式最主要的問題是在服務多的時候產生太多的 iptables 規則,非增量式更新會引入一定的時延,大規模情況下有明顯的性能問題

  • ipvs:爲解決 iptables 模式的性能問題,v1.11 新增了 ipvs 模式(v1.8 開始支持測試版,並在 v1.11 GA),採用增量式更新,並可以保證 service 更新期間連接保持不斷開

  • winuserspace:同 userspace,但僅工作在 windows 節點上

注意:使用 ipvs 模式時,需要預先在每臺 Node 上加載內核模塊 nf_conntrack_ipv4, ip_vs, ip_vs_rr, ip_vs_wrr, ip_vs_sh 等。

# load module <module_name>
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack_ipv4

# to check loaded modules, use
lsmod | grep -e ip_vs -e nf_conntrack_ipv4
# or
cut -f1 -d " "  /proc/modules | grep -e ip_vs -e nf_conntrack_ipv4

Iptables 示例

Kube-proxy iptables 示意圖

(圖片來自cilium/k8s-iptables-diagram)

Kube-proxy NAT 示意圖

(圖片來自kube-proxy iptables "nat" control flow

Iptables 示例

# Masquerade
-A KUBE-MARK-DROP -j MARK --set-xmark 0x8000/0x8000
-A KUBE-MARK-MASQ -j MARK --set-xmark 0x4000/0x4000
-A KUBE-POSTROUTING -m comment --comment "kubernetes service traffic requiring SNAT" -m mark --mark 0x4000/0x4000 -j MASQUERADE

# clusterIP and publicIP
-A KUBE-SERVICES ! -s 10.244.0.0/16 -d 10.98.154.163/32 -p tcp -m comment --comment "default/nginx: cluster IP" -m tcp --dport 80 -j KUBE-MARK-MASQ
-A KUBE-SERVICES -d 10.98.154.163/32 -p tcp -m comment --comment "default/nginx: cluster IP" -m tcp --dport 80 -j KUBE-SVC-4N57TFCL4MD7ZTDA
-A KUBE-SERVICES -d 12.12.12.12/32 -p tcp -m comment --comment "default/nginx: loadbalancer IP" -m tcp --dport 80 -j KUBE-FW-4N57TFCL4MD7ZTDA

# Masq for publicIP
-A KUBE-FW-4N57TFCL4MD7ZTDA -m comment --comment "default/nginx: loadbalancer IP" -j KUBE-MARK-MASQ
-A KUBE-FW-4N57TFCL4MD7ZTDA -m comment --comment "default/nginx: loadbalancer IP" -j KUBE-SVC-4N57TFCL4MD7ZTDA
-A KUBE-FW-4N57TFCL4MD7ZTDA -m comment --comment "default/nginx: loadbalancer IP" -j KUBE-MARK-DROP

# Masq for nodePort
-A KUBE-NODEPORTS -p tcp -m comment --comment "default/nginx:" -m tcp --dport 30938 -j KUBE-MARK-MASQ
-A KUBE-NODEPORTS -p tcp -m comment --comment "default/nginx:" -m tcp --dport 30938 -j KUBE-SVC-4N57TFCL4MD7ZTDA

# load balance for each endpoints
-A KUBE-SVC-4N57TFCL4MD7ZTDA -m comment --comment "default/nginx:" -m statistic --mode random --probability 0.33332999982 -j KUBE-SEP-UXHBWR5XIMVGXW3H
-A KUBE-SVC-4N57TFCL4MD7ZTDA -m comment --comment "default/nginx:" -m statistic --mode random --probability 0.50000000000 -j KUBE-SEP-TOYRWPNILILHH3OR
-A KUBE-SVC-4N57TFCL4MD7ZTDA -m comment --comment "default/nginx:" -j KUBE-SEP-6QCC2MHJZP35QQAR

# endpoint #1
-A KUBE-SEP-6QCC2MHJZP35QQAR -s 10.244.3.4/32 -m comment --comment "default/nginx:" -j KUBE-MARK-MASQ
-A KUBE-SEP-6QCC2MHJZP35QQAR -p tcp -m comment --comment "default/nginx:" -m tcp -j DNAT --to-destination 10.244.3.4:80

# endpoint #2
-A KUBE-SEP-TOYRWPNILILHH3OR -s 10.244.2.4/32 -m comment --comment "default/nginx:" -j KUBE-MARK-MASQ
-A KUBE-SEP-TOYRWPNILILHH3OR -p tcp -m comment --comment "default/nginx:" -m tcp -j DNAT --to-destination 10.244.2.4:80

# endpoint #3
-A KUBE-SEP-UXHBWR5XIMVGXW3H -s 10.244.1.2/32 -m comment --comment "default/nginx:" -j KUBE-MARK-MASQ
-A KUBE-SEP-UXHBWR5XIMVGXW3H -p tcp -m comment --comment "default/nginx:" -m tcp -j DNAT --to-destination 10.244.1.2:80

如果服務設置了 externalTrafficPolicy: Local 並且當前 Node 上面沒有任何屬於該服務的 Pod,那麼在 KUBE-XLB-4N57TFCL4MD7ZTDA 中會直接丟掉從公網 IP 請求的包:

-A KUBE-XLB-4N57TFCL4MD7ZTDA -m comment --comment "default/nginx: has no local endpoints" -j KUBE-MARK-DROP

ipvs 示例

Kube-proxy IPVS mode 列出了各種服務在 IPVS 模式下的工作原理。

$ ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.0.0.1:443 rr persistent 10800
  -> 192.168.0.1:6443             Masq    1      1          0
TCP  10.0.0.10:53 rr
  -> 172.17.0.2:53                Masq    1      0          0
UDP  10.0.0.10:53 rr
  -> 172.17.0.2:53                Masq    1      0          0

注意,IPVS 模式也會使用 iptables 來執行 SNAT 和 IP 僞裝(MASQUERADE),並使用 ipset 來簡化 iptables 規則的管理:

ipset 名
成員
用途

KUBE-CLUSTER-IP

All service IP + port

Mark-Masq for cases that masquerade-all=true or clusterCIDR specified

KUBE-LOOP-BACK

All service IP + port + IP

masquerade for solving hairpin purpose

KUBE-EXTERNAL-IP

service external IP + port

masquerade for packages to external IPs

KUBE-LOAD-BALANCER

load balancer ingress IP + port

masquerade for packages to load balancer type service

KUBE-LOAD-BALANCER-LOCAL

LB ingress IP + port with externalTrafficPolicy=local

accept packages to load balancer with externalTrafficPolicy=local

KUBE-LOAD-BALANCER-FW

load balancer ingress IP + port with loadBalancerSourceRanges

package filter for load balancer with loadBalancerSourceRanges specified

KUBE-LOAD-BALANCER-SOURCE-CIDR

load balancer ingress IP + port + source CIDR

package filter for load balancer with loadBalancerSourceRanges specified

KUBE-NODE-PORT-TCP

nodeport type service TCP port

masquerade for packets to nodePort(TCP)

KUBE-NODE-PORT-LOCAL-TCP

nodeport type service TCP port with externalTrafficPolicy=local

accept packages to nodeport service with externalTrafficPolicy=local

KUBE-NODE-PORT-UDP

nodeport type service UDP port

masquerade for packets to nodePort(UDP)

KUBE-NODE-PORT-LOCAL-UDP

nodeport type service UDP port withexternalTrafficPolicy=local

accept packages to nodeport service withexternalTrafficPolicy=local

啓動 kube-proxy 示例

kube-proxy --kubeconfig=/var/lib/kubelet/kubeconfig --cluster-cidr=10.240.0.0/12 --feature-gates=ExperimentalCriticalPodAnnotation=true --proxy-mode=iptables

kube-proxy 工作原理

kube-proxy 監聽 API server 中 service 和 endpoint 的變化情況,並通過 userspace、iptables、ipvs 或 winuserspace 等 proxier 來爲服務配置負載均衡(僅支持 TCP 和 UDP)。

kube-proxy 不足

kube-proxy 目前僅支持 TCP 和 UDP,不支持 HTTP 路由,並且也沒有健康檢查機制。這些可以通過自定義 Ingress Controller 的方法來解決。

Last updated