<p>I am creating two pods with a custom docker image(ubuntu is the base image). I am trying to ping the pods from their terminal. I am able to reach it using the IP address but not the hostname. How to achieve without manually adding <code>/etc/hosts</code> in the pods?</p>
<p>Note: I am not running any services in the node. I am basically trying to setup slurm using this.</p>
<p>Pod Manifest File:</p>
<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: slurmctld
  labels:
    app: slurm
spec:
  nodeName: docker-desktop
  hostname: slurmctld
  containers:
    - name: slurmctld
      image: slurmcontroller
      imagePullPolicy: Always
      ports:
        - containerPort: 6817
      resources:
        requests:
          memory: &quot;1000Mi&quot;
          cpu: &quot;1000m&quot;
        limits:
          memory: &quot;1500Mi&quot;
          cpu: &quot;1500m&quot;  
      command: [ &quot;/bin/bash&quot;, &quot;-c&quot;, &quot;--&quot; ]
      args: [ &quot;while true; do sleep 30; done;&quot; ]
---
apiVersion: v1
kind: Pod
metadata:
  name: worker1
  labels:
    app: slurm
spec:
  nodeName: docker-desktop
  hostname: worker1        
  containers:
    - name: worker1
      image: slurmworker
      imagePullPolicy: Always
      ports:
        - containerPort: 6818
      resources:
        requests:
          memory: &quot;1000Mi&quot;
          cpu: &quot;1000m&quot;
        limits:
          memory: &quot;1500Mi&quot;
          cpu: &quot;1500m&quot;    
      command: [ &quot;/bin/bash&quot;, &quot;-c&quot;, &quot;--&quot; ]
      args: [ &quot;while true; do sleep 30; done;&quot; ]
</code></pre>

Akhil
<p>From the docs <a href="https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/#pods" rel="nofollow noreferrer">here</a></p>
<blockquote>
<p>In general a pod has the following DNS resolution:</p>
<p><code>pod-ip-address.my-namespace.pod.cluster-domain.example</code>.</p>
<p>For example, if a pod in the <code>default</code> namespace has the IP address
<code>172.17.0.3</code>, and the domain name for your cluster is <code>cluster.local</code>, then the Pod has a DNS name:</p>
<p><code>172-17-0-3.default.pod.cluster.local</code>.</p>
<p>Any pods created by a Deployment or DaemonSet exposed by a Service
have the following DNS resolution available:</p>
<p><code>pod-ip-address.deployment-name.my-namespace.svc.cluster-domain.example</code></p>
</blockquote>
<p>If you don't like to deal with ever changing IP of a pod then you need to create <a href="https://kubernetes.io/docs/concepts/services-networking/service/" rel="nofollow noreferrer">service</a> to expose the pods using DNS hostnames. Below is an example of service to expose the <code>slurmctld</code> pod.</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: slurmctld-service
spec:
  selector:
    app: slurm
  ports:
    - protocol: TCP
      port: 80
      targetPort: 6817
</code></pre>
<p>Assuming you are doing these on <code>default</code> namespace You should now be able to access it via <code>slurmctld-service.default.svc.cluster.local</code></p>
