Resolve ip addresses of a headless service

11/2/2018

I created a service, which has 3 pods assigned.

enter image description here

I would like to access the service through its hostname by an other service in the same project. How can I do that?

Tried:

alxtbk@dns-test:~$ ping elassandra-0.elassandra
ping: elassandra-0.elassandra: Name or service not known

alxtbk@dns-test:~$ ping elassandra-0.default.svc.cluster.local
ping: elassandra-0.default.svc.cluster.local: Name or service not known

alxtbk@dns-test:~$ ping elassandra.default.svc.cluster.local
ping: elassandra.default.svc.cluster.local: Name or service not known

What is the correct way to resolve the ip adresses of the headless service?

-- Alex Tbk
google-cloud-platform
kubernetes

1 Answer

11/2/2018

For such Services, a cluster IP is not allocated, kube-proxy does not handle these services, and there is no load balancing or proxying done by the platform for them. How DNS is automatically configured depends on whether the service has selectors defined.

With selectors

For headless services that define selectors, the endpoints controller creates Endpoints records in the API, and modifies the DNS configuration to return A records (addresses) that point directly to the Pods backing the Service.

Without selectors

For headless services that do not define selectors, the endpoints controller does not create Endpoints records. However, the DNS system looks for and configures either:

CNAME records for ExternalName-type services.

A records for any Endpoints that share a name with the service, for all other types.

so you maybe be able to do:

kubectl get ep

to get the endpoints and then use them inside another kubernetes service.

-- Ijaz Ahmad Khan
Source: StackOverflow