As you know, in k8s, we can set additional entry in /etc/hosts with hostAliases in deployment.yaml like:
apiVersion: v1
kind: Pod
metadata:
name: hostaliases-pod
spec:
restartPolicy: Never
hostAliases:
- ip: "127.0.0.1"
hostnames:
- "foo.local"
But I want the ip is the POD own IP, then I assign a hostname to the POD. e.g.
hostAliases:
- ip: "$POD_IP"
hostnames:
- "myname"
Is is possible? and how to?
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.
That said, there are other solutions:
By default kubernetes add to /etc/hosts an entry for POD_IP and POD_NAME, so maybe you can use that.
You can always modify the entrypoint of the container to write that entry in /etc/hosts. Here is an example using the downward API:
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 >> /etc/hosts; <INSERT YOU ENTRYPOINT HERE> env: - name: MY_POD_IP valueFrom: fieldRef: fieldPath: status.podIP restartPolicy: Never