天天看點

Kubernetes Headless Service

1. Headless Service

headless service 需要将 spec.clusterIP 設定成 None。

因為沒有ClusterIP,kube-proxy 并不處理此類服務,因為沒有load balancing或 proxy 代理設定,在通路服務的時候回傳回後端的全部的Pods IP位址,主要用于開發者自己根據pods進行負載均衡器的開發(設定了selector) 或 StatefulSet.

2. Test Headless Service With selectors

$cat deployment.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 2 # tells deployment to run 2 pods matching the template
  template: # create pods using pod definition in this template
    metadata:
      # unlike pod-nginx.yaml, the name is not included in the meta data as a unique name is
      # generated from the deployment name
      labels:
        app: nginx_test
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80      
$ cat headless_service.yaml
kind: Service
apiVersion: v1
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx_test
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  clusterIP: None      
nslookup nginx-service
nslookup: can't resolve '(null)': Name does not resolve

Name:      nginx-service
Address 1: 10.10.23.36
Address 2: 10.10.23.39      

繼續閱讀