简体   繁体   中英

Find endpoint IP address in k8s

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.

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM