22

I need to set a static hostname in a Kubernetes replication controller. Docker supports it with some runtime flags, however, Kubernetes replication controllers don't appear to support it. The environment: OS - CentOS 6.6 Approach to use sysctl to change the variable kernel.hostname does not work for a K8s replication controller. The host name is not changed. Use: sysctl kernel.hostname to read the current hostname, and sysctl kernel.hostname=NEW_HOSTNAME

Is it possible to set a hostname in a Kubernetes replication controller?

3 Answers 3

38

In 1.7 you can set the hostname directly in the Deployment spec

spec:
  replicas: 1
  template:
    spec:
      hostname: myhostname
      containers:
        ...

Old Answer

Now that 1.2 has landed, you can set a static hostname in a Replication Controller or Deployment spec using the pod.beta.kubernetes.io/hostname annotation.

spec:
  replicas: 1
  template:
    metadata:
      annotations:
        pod.beta.kubernetes.io/hostname: myhostname
      labels:
        ...
5
  • 1
    Is it possible, that this is only working at deploymentsets and not at statefulsets? Commented May 14, 2018 at 11:03
  • 1
    Correct. Statefulsets control names in a different way
    – morloch
    Commented May 15, 2018 at 6:59
  • What will happen if I specify replicas to more than 1? Will all of them have the same hostname? Which is of course not OK Commented Aug 23, 2022 at 18:00
  • not sure actually. At a guess, I would assume that it would either: * overwrite the DNS entry * add another IP to the pool of IP addresses * start a "round robin" dns response (that is, a different IP each lookup)
    – morloch
    Commented Aug 24, 2022 at 1:03
  • It's worth noting that this doesn't allows multiple-part hostnames (so no dots . allowed in the hostname). Also, this affects the system hostname but not kubernetes name resolution, which continues to use the usual DNS naming scheme for pods and services.
    – jjmontes
    Commented Oct 23, 2023 at 10:32
6

Kubernetes (currently) treats pods as cattle, not pets, so it's undesirable to specify hostnames of the individual pods. There is a lengthy discussion in the github issue on the needs of (re-)using hostnames and how to solve that. It seems that the nominal services (a.k.a. PetSets), which is yet to be implemented, may help resolve this issue.

1

This feature (hostnames) is coming in kubernetes v1.2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.