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 等。
1 # load module <module_name> 2 modprobe -- ip_vs 3 modprobe -- ip_vs_rr 4 modprobe -- ip_vs_wrr 5 modprobe -- ip_vs_sh 6 modprobe -- nf_conntrack_ipv4 7 # to check loaded modules, use 8 lsmod | grep -e ip_vs -e nf_conntrack_ipv4 9 # or 10 cut -f1 -d " " /proc/modules | grep -e ip_vs -e nf_conntrack_ipv4
Iptables 示例

1 # Masquerade 2 -A KUBE-MARK-DROP -j MARK --set-xmark 0x8000/0x8000 3 -A KUBE-MARK-MASQ -j MARK --set-xmark 0x4000/0x4000 4 -A KUBE-POSTROUTING -m comment --comment "kubernetes service traffic requiring SNAT" -m mark --mark 0x4000/0x4000 -j MASQUERADE 5 # clusterIP and publicIP 6 -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 7 -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 8 -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 9 # Masq for publicIP 10 -A KUBE-FW-4N57TFCL4MD7ZTDA -m comment --comment "default/nginx: loadbalancer IP" -j KUBE-MARK-MASQ 11 -A KUBE-FW-4N57TFCL4MD7ZTDA -m comment --comment "default/nginx: loadbalancer IP" -j KUBE-SVC-4N57TFCL4MD7ZTDA 12 -A KUBE-FW-4N57TFCL4MD7ZTDA -m comment --comment "default/nginx: loadbalancer IP" -j KUBE-MARK-DROP 13 # Masq for nodePort 14 -A KUBE-NODEPORTS -p tcp -m comment --comment "default/nginx:" -m tcp --dport 30938 -j KUBE-MARK-MASQ 15 -A KUBE-NODEPORTS -p tcp -m comment --comment "default/nginx:" -m tcp --dport 30938 -j KUBE-SVC-4N57TFCL4MD7ZTDA 16 # load balance for each endpoints 17 -A KUBE-SVC-4N57TFCL4MD7ZTDA -m comment --comment "default/nginx:" -m statistic --mode random --probability 0.33332999982 -j KUBE-SEP-UXHBWR5XIMVGXW3H 18 -A KUBE-SVC-4N57TFCL4MD7ZTDA -m comment --comment "default/nginx:" -m statistic --mode random --probability 0.50000000000 -j KUBE-SEP-TOYRWPNILILHH3OR 19 -A KUBE-SVC-4N57TFCL4MD7ZTDA -m comment --comment "default/nginx:" -j KUBE-SEP-6QCC2MHJZP35QQAR 20 # endpoint #1 21 -A KUBE-SEP-6QCC2MHJZP35QQAR -s 10.244.3.4/32 -m comment --comment "default/nginx:" -j KUBE-MARK-MASQ 22 -A KUBE-SEP-6QCC2MHJZP35QQAR -p tcp -m comment --comment "default/nginx:" -m tcp -j DNAT --to-destination 10.244.3.4:80 23 # endpoint #2 24 -A KUBE-SEP-TOYRWPNILILHH3OR -s 10.244.2.4/32 -m comment --comment "default/nginx:" -j KUBE-MARK-MASQ 25 -A KUBE-SEP-TOYRWPNILILHH3OR -p tcp -m comment --comment "default/nginx:" -m tcp -j DNAT --to-destination 10.244.2.4:80 26 # endpoint #3 27 -A KUBE-SEP-UXHBWR5XIMVGXW3H -s 10.244.1.2/32 -m comment --comment "default/nginx:" -j KUBE-MARK-MASQ 28 -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 示例

1 $ ipvsadm -ln 2 IP Virtual Server version 1.2.1 (size=4096) 3 Prot LocalAddress:Port Scheduler Flags 4 -> RemoteAddress:Port Forward Weight ActiveConn InActConn 5 TCP 10.0.0.1:443 rr persistent 10800 6 -> 192.168.0.1:6443 Masq 1 1 0 7 TCP 10.0.0.10:53 rr 8 -> 172.17.0.2:53 Masq 1 0 0 9 UDP 10.0.0.10:53 rr 10 -> 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 示例
1 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 的方法來解決。
