Headless Service 和Service

定于spec:clusterIP: None

还记得Service的Cluster IP是做什么的吗?对,一个Service可能对应多个EndPoint(Pod),client访问的是Cluster IP,通过iptables规则转到Real Server,从而达到负载均衡的效果(实现原理请见这里)。如下:

有头service

kubectl get service
NAME                      CLUSTER-IP       EXTERNAL-IP       PORT(S)           AGE
nginx-service             10.107.124.218   192.168.128.158   80/TCP,443/TCP    1d

kubectl describe  service nginx-service    
Name:                   nginx-service
Namespace:              default
Labels:                 
Selector:               component=nginx
Type:                   ClusterIP
IP:                     10.107.124.218
External IPs:           192.168.128.158
Port:                   nginx-http      80/TCP
Endpoints:              10.244.2.9:80
Port:                   nginx-https     443/TCP
Endpoints:              10.244.2.9:443

nslookup nginx-service.default.svc.cluster.local  10.96.0.10  #这个ip为k8s dns的ip
Server:         10.96.0.10
Address:        10.96.0.10#53

Name:   nginx-service.default.svc.cluster.local
Address: 10.107.124.218

虽然service有2个endpoint,但是dns查询时只会返回service的地址。具体client访问的是哪个Real Server,是由iptables来决定的。

无头service

kubectl get service
NAME                      CLUSTER-IP       EXTERNAL-IP       PORT(S)    AGE
nginx                     None                         80/TCP     1h

kubectl describe  service nginx
Name:                   nginx
Namespace:              default
Labels:                 app=nginx
Selector:               app=nginx
Type:                   ClusterIP
IP:                     None
Port:                   web     80/TCP                 
Endpoints:              10.244.2.17:80,10.244.2.18:80 #通过web获取到pod的ip地址

nslookup nginx.default.svc.cluster.local 10.96.0.10
Server:         10.96.0.10
Address:        10.96.0.10#53

Name:   nginx.default.svc.cluster.local
Address: 10.244.2.17
Name:   nginx.default.svc.cluster.local
Address: 10.244.2.18

dns查询会如实的返回2个真实的endpoint

猜你喜欢

转载自www.cnblogs.com/kevincaptain/p/10600733.html