service类型

clusterip实现

iptables

新增servcie,ip为:10.0.1.175(无法被ping),后端pod为3个
kube-proxy会在所有node上写入iptables规则

1
-A KUBE-SERVICES -d 10.0.1.175/32 -p tcp -m comment --comment "default/hostnames: cluster IP" -m tcp --dport 80 -j KUBE-SVC-NWV5X2332I4OT4T3

意思是目标为10.0.1.175端口为80的走KUBE-SVC-NWV5X2332I4OT4T3,查看一下

1
2
3
-A KUBE-SVC-NWV5X2332I4OT4T3 -m comment --comment "default/hostnames:" -m statistic --mode random --probability 0.33332999982 -j KUBE-SEP-WNBA2IHDGP2BOBGZ
-A KUBE-SVC-NWV5X2332I4OT4T3 -m comment --comment "default/hostnames:" -m statistic --mode random --probability 0.50000000000 -j KUBE-SEP-X3P2623AGDH6CDF3
-A KUBE-SVC-NWV5X2332I4OT4T3 -m comment --comment "default/hostnames:" -j KUBE-SEP-57KPRZ3JQVENLNBR

有三条对应的规则
第一条规则可能性是30%,第二条时只剩两条规则了,所以为50%,第三条之后就没了,所以不是random了,打开某一条对应规则

1
2
3
4
5
6
7
8
-A KUBE-SEP-57KPRZ3JQVENLNBR -s 10.244.3.6/32 -m comment --comment "default/hostnames:" -j MARK --set-xmark 0x00004000/0x00004000
-A KUBE-SEP-57KPRZ3JQVENLNBR -p tcp -m comment --comment "default/hostnames:" -m tcp -j DNAT --to-destination 10.244.3.6:9376

-A KUBE-SEP-WNBA2IHDGP2BOBGZ -s 10.244.1.7/32 -m comment --comment "default/hostnames:" -j MARK --set-xmark 0x00004000/0x00004000
-A KUBE-SEP-WNBA2IHDGP2BOBGZ -p tcp -m comment --comment "default/hostnames:" -m tcp -j DNAT --to-destination 10.244.1.7:9376

-A KUBE-SEP-X3P2623AGDH6CDF3 -s 10.244.2.3/32 -m comment --comment "default/hostnames:" -j MARK --set-xmark 0x00004000/0x00004000
-A KUBE-SEP-X3P2623AGDH6CDF3 -p tcp -m comment --comment "default/hostnames:" -m tcp -j DNAT --to-destination 10.244.2.3:9376

就是对应到相应的pod ip,后面就可以走路由规则进行同node或者跨node访问了

ipvs

iptables规则为每个pod生成规则,且iptables无法增量更新,每更新一个pod就要把全集群规则刷新一遍,集群规模较大的时候效率较低
引入ipvs

工作流程
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  [@node-2 ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.96.0.1:443 rr
-> 10.176.32.20:6443 Masq 1 1 0
TCP 10.97.43.84:80 rr
-> 10.244.84.143:80 Masq 1 0 0
-> 10.244.84.144:80 Masq 1 0 0
-> 10.244.139.79:80 Masq 1 0 0
-> 10.244.139.80:80 Masq 1 0 0
-> 10.244.139.81:80 Masq 1 0 0
-> 10.244.247.14:80 Masq 1 0 0
-> 10.244.247.15:80 Masq 1 0 0
-> 10.244.247.16:80 Masq 1 0 0
-> 10.244.247.17:80 Masq 1 0 0