kjh00abc 2019-11-26
一、首先说headless Service和普通Service的区别
headless不分配clusterIP
headless service下的Pod有DNS地址,可以通过Pod的DNS地址解析到Pod的IP地址
普通的service下的Pod没有DNS,只能通过svc的DNS解析到svc的clusterIP
Service的ClusterIP工作原理:一个service可能对应一组endpoints(所有pod的地址+端口),client访问ClusterIP,通过iptables或者ipvs转发到Real Server(Pod),具体操作如下
[ ~]# 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[ ~]# 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> [ ~]# 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决定
下面来看看headless Service的结果
[ ~]# 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 [ ~]# 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> [ ~]# 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 resolve Name: eureka-2.eureka.ms.svc.cluster.local Address 1: 10.244.1.211 eureka-2.eureka.ms.svc.cluster.local
headless Service就是没头的Service,有什么使用场景呢?
[ ~]# kubectl get sts -n ms
NAME READY AGE
eureka 3/3 22h
[ ~]# kubectl get svc -n ms NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE eureka ClusterIP None <none> 8888/TCP 21h [ ~]# 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> [ ~]# 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,他们之间能互相访问,这样对于一些集群类型的应用就可以解决互相身份识别的问题了
三、为什么要用headless service+statefulSet部署有状态应用?
在Linux中不管你是做服务器还是只是平常使用,上网肯定都是最重要和不可缺少的一个因素之一,所以就涉及到它的ip gateway dns等network配置和使用。但是设置Linux网络的方法有两种: