Hello All,
I wanted to customize the hostname of a container in a pod.
Could you please let me know if it’s possible in Kubernetes.
In Docker, I can do it by adding -h example : docker run -i -t -d -h “myhostname.com”
I tried init containers in the deployment.yaml. The Init container does it job but my main-container doesn’t get hostname which I set in init container.
initContainers:
- name: set-hostname
image: busybox
command: [“sh”, “/scripts/set-hostname.sh”]
volumeMounts:
- name: scripts
mountPath: /scripts
securityContext:
privileged: true # Required to change the hostname
containers:
- name: main-container
image: main-image
ports:
- containerPort: 8989
volumeMounts:
- name: scripts
mountPath: /scripts
==> set-hostname.sh is part of the confimap.
set-hostname.sh:
apiVersion: v1
kind: ConfigMap
metadata:
name: hostname-script-config
data:
set-hostname.sh: |
#!/bin/sh
HOSTNAME=“my-hostname.com”
echo “Setting hostname to ${HOSTNAME}”
hostname ${HOSTNAME}
echo ${HOSTNAME} > /etc/hostname
echo “127.0.0.1 ${HOSTNAME}” >> /etc/hosts
Thanks,
Ramesh.