kubernetes學習Service之headless和statefulSet結合

1、首先說headless Service和普通Service的區別

headless不分配clusterIPmysql

headless service能夠經過解析serviceDNS,返回全部Pod的地址和DNS(statefulSet部署的Pod纔有DNS)sql

普通的service,只能經過解析serviceDNS返回serviceClusterIPapi

2、statefulSet和Deployment控制器的區別

statefulSet下的PodDNS地址,經過解析PodDNS能夠返回PodIP
deployment下的Pod沒有DNSapp

3、普通Service接續service的DNS結果

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查詢時只會返回ServiceClusterIP地址,具體Client訪問的是哪一個real server,由iptables或者ipvs決定測試

4、headless Service的解析service的DNS結果

[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

 
  • 根據結果看到,dns查詢會返回3個endpoint,也就是3個pod地址和DNS,經過解析podDNS也能返回Pod的IP

5、headless Service就是沒頭的Service,有什麼使用場景呢?

  • 第一種:自主選擇權,有時候client想本身決定使用哪一個Real Server,能夠經過查詢DNS來獲取Real Server的信息
  • 第二種:headless  service關聯的每一個endpoint(也就是Pod),都會有對應的DNS域名;這樣Pod之間就能夠互相訪問

  [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

 

6、爲何要用headless service+statefulSet部署有狀態應用?

1.headless service會爲關聯的Pod分配一個域
<service name>.$<namespace name>.svc.cluster.local
2.StatefulSet會爲關聯的Pod保持一個不變的Pod Name
statefulsetPodhostname格式爲$(StatefulSet name)-$(pod序號)
3.StatefulSet會爲關聯的Pod分配一個dnsName$<Pod Name>.$<service name>.$<namespace name>.svc.cluster.local

相關文章
相關標籤/搜索