Kubernetes: Headless Service and Endpoints

Say you have Kubernetes node on 192.168.1.10 and a database node on 192.168.1.11.

First, we create a dummy service:

kubectl create service clusterip my-db --clusterip None
kubectl get service my-db
NAME    TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
my-db   ClusterIP   None         <none>        <none>    83s

When you create a service you will also get an endpoint:

kubectl get endpoint my-db
NAME    ENDPOINTS   AGE
my-db   <none>      88s

To get my-db to point to 192.168.1.11.

First, we need to edit the endpoint to point to 192.168.1.11.

kubectl edit endpoint my-db

Add these lines to the end of the file:

...
subsets:
- addresses:
  - ip: 192.168.1.11

Then we need to remove spec.selector from service.

kubectl edit service my-db

Remove these lines:

spec:
...
  selector:
    app: my-db
...

Go inside any container in the same namespace and run the command:

nslookup my-db

You should get:

Name:   my-db.default.svc.cluster.local
Address: 192.168.1.11

References:


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *