<p>As you know, in k8s, we can set additional entry in /etc/hosts with hostAliases in deployment.yaml like:</p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: hostaliases-pod
spec:
  restartPolicy: Never
  hostAliases:
  - ip: "127.0.0.1"
    hostnames:
    - "foo.local"
</code></pre>

<p>But I want the ip is the POD own IP, then I assign a hostname to the POD. e.g.</p>

<pre><code>  hostAliases:
  - ip: "$POD_IP"
    hostnames:
    - "myname"
</code></pre>

<p>Is is possible? and how to?</p>

<p>I don't think it's possible that way. Kubectl has a condition that hostAliases[].ip must be a valid IP. There is no way to insert there anything but an IP.</p>

<p>That said, there are other solutions:</p>

<ul>
<li><p>By default kubernetes add to /etc/hosts an entry for POD_IP and POD_NAME, so maybe you can use that.</p></li>
<li><p>You can always modify the entrypoint of the container to write that entry in /etc/hosts. Here is an example using the downward API:</p></li>
</ul>

<p><code>
apiVersion: v1
kind: Pod
metadata:
  name: test
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox:1.24
      command: [ "sh", "-c"]
      args:
      - echo $MY_POD_IP myname &gt;&gt; /etc/hosts;
          &lt;INSERT YOU ENTRYPOINT HERE&gt;
      env:
        - name: MY_POD_IP
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
  restartPolicy: Never
</code></p>
