2

I am trying to create a kubernetes job inside which I will run "dig srv" queries to find out the IP address of all the pods for any specific service running on the same cluster.

Is this achievable ?

I would like to elaborate a little more on the problem statement. There are a few services already running on the cluster. The requirement is to have a tool that can accept a service name and list down the IP addresses of all the pods belonging to that service.

I was able to do this by using kubectl commands along with selector and jq tooling. But for some reasons, I am not allowed to run kubectl commands on this environment.

I want to use dig srv queries to resolve pod IPs for provided service name.

nakul shukla
  • 138
  • 1
  • 8

2 Answers2

1

You can use a headless service (therefore no ClusterIP and no internal loadbalancing). If you provide a selector, you can query for A records of the service.

See: https://kubernetes.io/docs/concepts/services-networking/service/#headless-services

Consider the following example:

Deployment of some pods:

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.16
        ports:
        - containerPort: 80

For this deployment the following headless service is added:

apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  ports:
  - port: 80
    name: web
  clusterIP: None
  selector:
    app: nginx

This can now be queried using DNS (inside the cluster)

$ kubectl run shell  -i --rm --tty --restart=Never --image=busybox
# nslookup -type=A nginx
Server:     10.96.0.10
Address:    10.96.0.10:53

Name:   nginx.default.svc.cluster.local
Address: 10.34.0.2
Name:   nginx.default.svc.cluster.local
Address: 10.42.0.2
Name:   nginx.default.svc.cluster.local
Address: 10.46.0.1

All internal Pod IPs are returned as DNS A records.

Thomas
  • 11,272
  • 2
  • 24
  • 40
  • I went through a lot of Kubernetes documentation but couldn't find the exact solution. Perhaps you could provide an example of reference of such implementation. I have edited the question with a detailed problem statement for better clarity. – nakul shukla Jun 11 '19 at 12:18
  • I could get the IP address of headless service the way you suggested. However, if I use a service that is not a headless service, I only get the IP address of that service. What should I do to list down the IP address of all the pods of a non-headless service? – nakul shukla Jun 12 '19 at 06:44
  • I suggest to decide if you want to access the IPs directly by querying the endpoints of a headless service or if you want to use the internal load balancing with a ClusterIP. If, for whatever reason, you want to have both, you can create two services, one to access the service and one to query the ip addresses. – Thomas Jun 12 '19 at 07:08
  • I did more research and went through documentation. It looks like what I am trying to achieve is not possible. The tool I want to develop accepts a service name and returns the IP addresses of all the pods. This is possible only if the service is headless. Looking up the IPs of all the pods of any other service doesn't seem feasible. – nakul shukla Jun 14 '19 at 06:39
  • If believe for the given problem statement the answer using a (additional) headless service is correct. If that is not what you actually want to achieve, please open a new question and explain your use case more precisely. – Thomas Jun 14 '19 at 09:51
0

This is explained inside DNS for Services and Pods.

Every Service defined in the cluster (including the DNS server itself) is assigned a DNS name. By default, a client Pod’s DNS search list will include the Pod’s own namespace and the cluster’s default domain. This is best illustrated by example:

Assume a Service named foo in the Kubernetes namespace bar. A Pod running in namespace bar can look up this service by simply doing a DNS query for foo. A Pod running in namespace quux can look up this service by doing a DNS query for foo.bar.

Here is a detailed docs for Kubernetes DNS-Based Service Discovery.

As for querying the POD ip address it depends if spec.hostname is specified.

If there exists a headless service in the same namespace as the pod and with the same name as the subdomain, the cluster’s KubeDNS Server also returns an A record for the Pod’s fully qualified hostname. For example, given a Pod with the hostname set to “busybox-1” and the subdomain set to “default-subdomain”, and a headless Service named “default-subdomain” in the same namespace, the pod will see its own FQDN as “busybox-1.default-subdomain.my-namespace.svc.cluster.local”. DNS serves an A record at that name, pointing to the Pod’s IP. Both pods “busybox1” and “busybox2” can have their distinct A records.

The Endpoints object can specify the hostname for any endpoint addresses, along with its IP.

Note: Because A records are not created for Pod names, hostname is required for the Pod’s A record to be created. A Pod with no hostname but with subdomain will only create the A record for the headless service (default-subdomain.my-namespace.svc.cluster.local), pointing to the Pod’s IP address. Also, Pod needs to become ready in order to have a record unless publishNotReadyAddresses=True is set on the Service.

Hope this explains enough.

Crou
  • 10,232
  • 2
  • 26
  • 31