当前位置:  首页>> 技术小册>> Kubernets合辑6-服务发现

这种模式是生产中使用最多的,由集群内部访问时通过service域名解析得到cluster IP,客户端访问cluster IP时,系统会通过iptable或者ipvs将流量负载均衡到后端Pod上。

  1. # svc-no-vip.yaml
  2. apiVersion: v1
  3. kind: Service
  4. metadata:
  5. name: slb-s1
  6. namespace: default
  7. spec:
  8. selector:
  9. app: nginx
  10. release: stable
  11. partition: website
  12. tier: slb
  13. ports:
  14. - name: http
  15. port: 80
  16. targetPort: 80
  1. [root@maxiaoke nginx-01]# kubectl get svc
  2. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  3. slb-s1 ClusterIP 10.100.138.11 <none> 80/TCP 51m
  4. # 在node节点查看 IPVS 规则
  5. [root@master_01_vm_160_10 ~]# ipvsadm -ln
  6. TCP 10.100.138.11:80 rr
  7. -> 10.200.3.10:80 Masq 1 0 0
  8. -> 10.200.4.8:80 Masq 1 0 0
  9. -> 10.200.4.9:80 Masq 1 0 0
  10. -> 10.200.5.10:80 Masq 1 0 0
  11. -> 10.200.5.11:80 Masq 1 0 0
  12. [root@maxiaoke yaml]# kubectl describe svc slb-s1
  13. Name: slb-s1
  14. Namespace: default
  15. Labels: <none>
  16. Annotations: <none>
  17. Selector: app=nginx,partition=website,release=stable,tier=slb
  18. Type: ClusterIP
  19. IP Families: <none>
  20. IP: 10.100.138.11
  21. IPs: 10.100.138.11
  22. Port: http 80/TCP
  23. TargetPort: 80/TCP
  24. Endpoints: 10.200.3.10:80,10.200.4.8:80,10.200.4.9:80 + 2 more...
  25. Session Affinity: None
  26. Events: <none>
  1. # 在node节点使用VIP访问测试
  2. [root@master_01_vm_160_10 ~]# curl 10.100.138.11/info
  3. 2021-10-13T07:24:43+00:00|v1.0.3|nginx-deploy-699fdd685d-8nb5w|ok
  4. [root@master_01_vm_160_10 ~]# curl 10.100.138.11/info
  5. 2021-10-13T07:24:45+00:00|v1.0.3|nginx-deploy-699fdd685d-ffswz|ok
  6. # 在pod中测试域名解析
  7. [root@nginx-deploy-699fdd685d-4zgtx /]# curl -s http://slb-s1.default.svc.cluster.local/info # 全域名
  8. 2021-10-13T07:28:19+00:00|v1.0.3|nginx-deploy-699fdd685d-lsv9q|ok
  9. [root@nginx-deploy-699fdd685d-4zgtx /]# curl -s http://slb-s1/info # 短域名
  10. 2021-10-13T07:27:57+00:00|v1.0.3|nginx-deploy-699fdd685d-ffswz|ok

该分类下的相关小册推荐: