Find endpoint IP address in k8s

6/7/2021

I need to create a deployment descriptor "A" yaml in which I can find the endpoint IP address of a pod (that belongs to a deployment "B") . There is an option to use Downward API but I don't know if I can use it in that case.

-- Angel
kubernetes

2 Answers

6/7/2021

What you are looking for is an Headless service (see documentation).

With an headless service, the service will not have an own IP address. If you specify a selector for the service, the DNS service will return the pods' IP when you query the service's name.

Quoting the documentation:

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

In order to create an headless service, simply set the .spec.clusterIP to None and specify the selector as you would normally do with a traditional service.

-- whites11
Source: StackOverflow

6/8/2021

If I understand correctly, you want to map the test.api.com hostname to the IP address of a specific Pod.
As @whites11 rightly pointed out, you can use Headless Services 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 (IP addresses) that point directly to the Pods backing the Service.

In this case, it may be difficult to properly configure the /etc/hosts file inside a Pod, but it is possible to configure the Kubernetes cluster DNS to achieve this goal.

If you are using CoreDNS as a DNS server, you can configure CoreDNS to map one domain (test.api.com) to another domain (headless service DNS name) by adding a rewrite rule.

I will provide an example to illustrate how it works.


First, I prepared a sample web Pod with an associated web Headless Service:

# kubectl get pod,svc -o wide
NAME           READY   STATUS    RESTARTS   AGE   IP          NODE      NOMINATED NODE   READINESS GATES
pod/web        1/1     Running   0          66m   10.32.0.2   kworker   <none>           <none>

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE   SELECTOR
service/web          ClusterIP   None         <none>        80/TCP    65m   run=web

We can check if the web headless Service returns A record (IP address) that points directly to the web Pod:

# kubectl exec -i -t dnsutils -- nslookup web.default.svc
Server:         10.96.0.10
Address:        10.96.0.10#53

Name:   web.default.svc.cluster.local
Address: 10.32.0.2

Next, we need to configure CoreDNS to map test.api.com -> web.default.svc.cluster.local.

Configuration of CoreDNS is stored in the coredns ConfigMap in the kube-system namespace. You can edit it using:

# kubectl edit cm coredns -n kube-system

Just add one rewrite rule, like in the example below:

apiVersion: v1
data:
  Corefile: |
    .:53 {
        errors
        health {
           lameduck 5s
        }
        rewrite name test.api.com web.default.svc.cluster.local # mapping test.api.com to web.default.svc.cluster.local
...

To reload CoreDNS, we may delete coredns Pods (coredns is deployed as Deployment, so new Pods will be created)

Finally, we can check how it works:

# kubectl exec -i -t dnsutils -- nslookup test.api.com
Server:         10.96.0.10
Address:        10.96.0.10#53

Name:   test.api.com
Address: 10.32.0.2

As you can see, the test.api.com domain also returns the IP address of the web Pod.

For more information on the rewrite plugin, see the Coredns rewrite documentation.

-- matt_j
Source: StackOverflow