这种模式是生产中使用最多的,由集群内部访问时通过service域名解析得到cluster IP,客户端访问cluster IP时,系统会通过iptable或者ipvs将流量负载均衡到后端Pod上。
# svc-no-vip.yaml
apiVersion: v1
kind: Service
metadata:
name: slb-s1
namespace: default
spec:
selector:
app: nginx
release: stable
partition: website
tier: slb
ports:
- name: http
port: 80
targetPort: 80
[root@maxiaoke nginx-01]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
slb-s1 ClusterIP 10.100.138.11 <none> 80/TCP 51m
# 在node节点查看 IPVS 规则
[root@master_01_vm_160_10 ~]# ipvsadm -ln
TCP 10.100.138.11:80 rr
-> 10.200.3.10:80 Masq 1 0 0
-> 10.200.4.8:80 Masq 1 0 0
-> 10.200.4.9:80 Masq 1 0 0
-> 10.200.5.10:80 Masq 1 0 0
-> 10.200.5.11:80 Masq 1 0 0
[root@maxiaoke yaml]# kubectl describe svc slb-s1
Name: slb-s1
Namespace: default
Labels: <none>
Annotations: <none>
Selector: app=nginx,partition=website,release=stable,tier=slb
Type: ClusterIP
IP Families: <none>
IP: 10.100.138.11
IPs: 10.100.138.11
Port: http 80/TCP
TargetPort: 80/TCP
Endpoints: 10.200.3.10:80,10.200.4.8:80,10.200.4.9:80 + 2 more...
Session Affinity: None
Events: <none>
# 在node节点使用VIP访问测试
[root@master_01_vm_160_10 ~]# curl 10.100.138.11/info
2021-10-13T07:24:43+00:00|v1.0.3|nginx-deploy-699fdd685d-8nb5w|ok
[root@master_01_vm_160_10 ~]# curl 10.100.138.11/info
2021-10-13T07:24:45+00:00|v1.0.3|nginx-deploy-699fdd685d-ffswz|ok
# 在pod中测试域名解析
[root@nginx-deploy-699fdd685d-4zgtx /]# curl -s http://slb-s1.default.svc.cluster.local/info # 全域名
2021-10-13T07:28:19+00:00|v1.0.3|nginx-deploy-699fdd685d-lsv9q|ok
[root@nginx-deploy-699fdd685d-4zgtx /]# curl -s http://slb-s1/info # 短域名
2021-10-13T07:27:57+00:00|v1.0.3|nginx-deploy-699fdd685d-ffswz|ok