headless不分配clusterIPmysql
headless service能夠經過解析service的DNS,返回全部Pod的地址和DNS(statefulSet部署的Pod纔有DNS)sql
普通的service,只能經過解析service的DNS返回service的ClusterIPapi
statefulSet下的Pod有DNS地址,經過解析Pod的DNS能夠返回Pod的IP
deployment下的Pod沒有DNSapp
Service的ClusterIP工做原理:一個service可能對應一組endpoints(全部pod的地址+端口),client訪問ClusterIP,經過iptables或者ipvs轉發到Real Server(Pod),具體操做以下less
[root@master01 ~]# kubectl get svc -n ms #獲取全部svc,看到gateway這個service的clusterIP是10.0.0.14 NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE eureka ClusterIP None <none> 8888/TCP 21h gateway ClusterIP 10.0.0.14 <none> 9999/TCP 20h mysql-production ClusterIP 10.0.0.251 <none> 3306/TCP 23h portal ClusterIP 10.0.0.124 <none> 8080/TCP 17h
[root@master01 ~]# kubectl describe svc gateway -n ms #看到gateway這個service的具體信息 Name: gateway Namespace: ms Labels: <none> Annotations: kubectl.kubernetes.io/last-applied-configuration: {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"gateway","namespace":"ms"},"spec":{"ports":[{"name":"gateway","po... Selector: app=gateway,project=ms Type: ClusterIP IP: 10.0.0.14 Port: gateway 9999/TCP TargetPort: 9999/TCP Endpoints: 10.244.1.212:9999 #該service下的Pod地址 Session Affinity: None Events: <none>
[root@master01 ~]# kubectl exec -it gateway-6cd76c98fb-8w92t -n ms sh #進入一個容器測試解析 / # nslookup gateway.ms.svc.cluster.local #測試解析gateway這個service的DNS nslookup: can't resolve '(null)': Name does not resolve Name: gateway.ms.svc.cluster.local Address 1: 10.0.0.14 gateway.ms.svc.cluster.local #這個結果就是gateway這個service的ClusterIP
從上面的結果能看到,雖然Service有1個endpoint,可是DNS查詢時只會返回Service的ClusterIP地址,具體Client訪問的是哪一個real server,由iptables或者ipvs決定測試
[root@master01 ~]# kubectl get svc -n ms #查看全部SVC,看到eureka的這個headless service NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE eureka ClusterIP None <none> 8888/TCP 21h gateway ClusterIP 10.0.0.14 <none> 9999/TCP 20h mysql-production ClusterIP 10.0.0.251 <none> 3306/TCP 23h portal ClusterIP 10.0.0.124 <none> 8080/TCP 18h [root@master01 ~]# kubectl describe svc eureka -n ms #看到eureka的這個headless service下面的endpoints Name: eureka Namespace: ms Labels: <none> Annotations: kubectl.kubernetes.io/last-applied-configuration: {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"eureka","namespace":"ms"},"spec":{"clusterIP":"None","ports":[{"n... Selector: app=eureka,project=ms Type: ClusterIP IP: None Port: eureka 8888/TCP TargetPort: 8888/TCP Endpoints: 10.244.1.210:8888,10.244.1.211:8888,10.244.2.45:8888 Session Affinity: None Events: <none> [root@master01 ~]# kubectl exec -it gateway-6cd76c98fb-8w92t -n ms sh #進入容器測試解析 / # nslookup eureka.ms.svc.cluster.local #經過解析eureka這個headless service的DNS地址,能夠看到關聯的具體endpoints信息 nslookup: can't resolve '(null)': Name does not resolve Name: eureka.ms.svc.cluster.local Address 1: 10.244.1.211 eureka-2.eureka.ms.svc.cluster.local Address 2: 10.244.1.210 eureka-0.eureka.ms.svc.cluster.local Address 3: 10.244.2.45 eureka-1.eureka.ms.svc.cluster.local
/ # nslookup eureka-2.eureka.ms.svc.cluster.local #解析pod的DNS記錄,也能返回Pod的IP
nslookup: can't resolve '(null)': Name does not resolvespa
Name: eureka-2.eureka.ms.svc.cluster.local
Address 1: 10.244.1.211 eureka-2.eureka.ms.svc.cluster.localcode
[root@master01 ~]# kubectl get sts -n ms
NAME READY AGE
eureka 3/3 22hblog
[root@master01 ~]# kubectl get svc -n ms NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE eureka ClusterIP None <none> 8888/TCP 21h [root@master01 ~]# kubectl describe svc eureka -n ms Name: eureka Namespace: ms Labels: <none> Annotations: kubectl.kubernetes.io/last-applied-configuration: {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"eureka","namespace":"ms"},"spec":{"clusterIP":"None","ports":[{"n... Selector: app=eureka,project=ms Type: ClusterIP IP: None Port: eureka 8888/TCP TargetPort: 8888/TCP Endpoints: 10.244.1.210:8888,10.244.1.211:8888,10.244.2.45:8888 Session Affinity: None Events: <none> [root@master01 ~]# kubectl exec -it gateway-6cd76c98fb-8w92t -n ms sh / # nslookup eureka.ms.svc.cluster.local nslookup: can't resolve '(null)': Name does not resolve Name: eureka.ms.svc.cluster.local Address 1: 10.244.1.211 eureka-2.eureka.ms.svc.cluster.local Address 2: 10.244.1.210 eureka-0.eureka.ms.svc.cluster.local Address 3: 10.244.2.45 eureka-1.eureka.ms.svc.cluster.local / # nslookup eureka-2.eureka.ms.svc.cluster.local nslookup: can't resolve '(null)': Name does not resolve Name: eureka-2.eureka.ms.svc.cluster.local Address 1: 10.244.1.211 eureka-2.eureka.ms.svc.cluster.local / # nslookup eureka-1.eureka.ms.svc.cluster.local nslookup: can't resolve '(null)': Name does not resolve Name: eureka-1.eureka.ms.svc.cluster.local Address 1: 10.244.2.45 eureka-1.eureka.ms.svc.cluster.local / # nslookup eureka-0.eureka.ms.svc.cluster.local nslookup: can't resolve '(null)': Name does not resolve Name: eureka-0.eureka.ms.svc.cluster.local Address 1: 10.244.1.210 eureka-0.eureka.ms.svc.cluster.local
如上,eureka就是咱們場景的StatefulSet,對應的pod就是eureka-0,eureka-1,eureka-2,他們之間能互相訪問,這樣對於一些集羣類型的應用就能夠解決互相身份識別的問題了dns
1.headless service會爲關聯的Pod分配一個域
<service name>.$<namespace name>.svc.cluster.local
2.StatefulSet會爲關聯的Pod保持一個不變的Pod Name
statefulset中Pod的hostname格式爲$(StatefulSet name)-$(pod序號)
3.StatefulSet會爲關聯的Pod分配一個dnsName$<Pod Name>.$<service name>.$<namespace name>.svc.cluster.local