headless发现独立pod
如果需要让客户端连接到所有pod,需要找出每个pod的ip。可以通过dns查找发现pod ip,通常通过kube-dns查找服务名对应ip时只会显示服务的集群ip。但是如果此时不需要集群ip时,可以设置clusterIP为None,此时kube-dns返回的是每个pod ip而不是服务集群的ip。
headless:
apiVersion: v1 kind: Service metadata: name: kubia-headless spec: clusterIP: None ports: - port: 80 targetPort: 80 selector: app: kubia
deployment:
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment spec: selector: matchLabels: app: kubia replicas: 4 # tells deployment to run 2 pods matching the template template: metadata: labels: app: kubia spec: containers: - name: nginx image: nginx:1.14.2 ports: - containerPort: 80
进入任意pod中进行域名解析:
root@nginx-test:/# nslookup kubia-headless Server: 10.96.0.10 Address: 10.96.0.10#53 Name: kubia-headless.default.svc.cluster.local Address: 10.1.1.186 Name: kubia-headless.default.svc.cluster.local Address: 10.1.1.182 Name: kubia-headless.default.svc.cluster.local Address: 10.1.1.185 Name: kubia-headless.default.svc.cluster.local Address: 10.1.1.183
此时headless服务也是跨pod的负载均衡,但是是通过dns轮询机制而不是通过服务代理。