<p>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.</p>

<p>Is this achievable ? </p>

<p><em>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.</em></p>

<p>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. </p>

<p>I want to use <code>dig srv</code> queries to resolve pod IPs for provided service name.</p>

<p>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.</p>

<p>See: <a href="https://kubernetes.io/docs/concepts/services-networking/service/#headless-services" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/services-networking/service/#headless-services</a></p>

<p>Consider the following example:</p>

<p>Deployment of some pods:</p>

<pre><code>---
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
</code></pre>

<p>For this deployment the following  headless service is added:</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  ports:
  - port: 80
    name: web
  clusterIP: None
  selector:
    app: nginx
</code></pre>

<p>This can now be queried using DNS (inside the cluster)</p>

<pre><code>$ 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
</code></pre>

<p>All internal Pod IPs are returned as DNS A records.</p>
