| 
 | 1 | +---  | 
 | 2 | +title: Pod Hostname  | 
 | 3 | +content_type: concept  | 
 | 4 | +weight: 85  | 
 | 5 | +---  | 
 | 6 | + | 
 | 7 | +<!-- overview -->  | 
 | 8 | + | 
 | 9 | +This page explains how to set a Pod's hostname,   | 
 | 10 | +potential side effects after configuration, and the underlying mechanics.  | 
 | 11 | + | 
 | 12 | +<!-- body -->  | 
 | 13 | + | 
 | 14 | +## Default Pod Hostname  | 
 | 15 | + | 
 | 16 | +When a Pod is created, its hostname (as observed from within the Pod)   | 
 | 17 | +is derived from the Pod's metadata.name value.   | 
 | 18 | +Both the hostname and its corresponding fully qualified domain name (FQDN)   | 
 | 19 | +are set to the metadata.name value (from the Pod's perspective)  | 
 | 20 | + | 
 | 21 | +```yaml  | 
 | 22 | +apiVersion: v1  | 
 | 23 | +kind: Pod  | 
 | 24 | +metadata:  | 
 | 25 | +  name: busybox-1  | 
 | 26 | +spec:  | 
 | 27 | +  containers:  | 
 | 28 | +  - image: busybox:1.28  | 
 | 29 | +    command:  | 
 | 30 | +      - sleep  | 
 | 31 | +      - "3600"  | 
 | 32 | +    name: busybox  | 
 | 33 | +```  | 
 | 34 | +The Pod created by this manifest will have its hostname and fully qualified domain name (FQDN) set to `busybox-1`.  | 
 | 35 | + | 
 | 36 | +## Hostname with pod's hostname and subdomain fields  | 
 | 37 | +The Pod spec includes an optional `hostname` field.   | 
 | 38 | +When set, this value takes precedence over the Pod's `metadata.name` as the   | 
 | 39 | +hostname (observed from within the Pod).  | 
 | 40 | +For example, a Pod with spec.hostname set to `my-host` will have its hostname set to `my-host`.  | 
 | 41 | + | 
 | 42 | +The Pod spec also includes an optional `subdomain` field,   | 
 | 43 | +indicating the Pod belongs to a subdomain within its namespace.   | 
 | 44 | +If a Pod has `spec.hostname` set to "foo" and spec.subdomain set   | 
 | 45 | +to "bar" in the namespace `my-namespace`, its hostname becomes `foo` and its   | 
 | 46 | +fully qualified domain name (FQDN) becomes   | 
 | 47 | +`foo.bar.my-namespace.svc.cluster-domain.example` (observed from within the Pod).  | 
 | 48 | + | 
 | 49 | +When both hostname and subdomain are set, the cluster's DNS server will   | 
 | 50 | +create A and/or AAAA records based on these fields.   | 
 | 51 | +Refer to: [Pod's hostname and subdomain fields](/docs/concepts/services-networking/dns-pod-service/#pod-hostname-and-subdomain-field).  | 
 | 52 | + | 
 | 53 | +## Hostname with pod's setHostnameAsFQDN fields  | 
 | 54 | + | 
 | 55 | +{{< feature-state for_k8s_version="v1.22" state="stable" >}}  | 
 | 56 | + | 
 | 57 | +When a Pod is configured to have fully qualified domain name (FQDN), its  | 
 | 58 | +hostname is the short hostname. For example, if you have a Pod with the fully  | 
 | 59 | +qualified domain name `busybox-1.busybox-subdomain.my-namespace.svc.cluster-domain.example`,  | 
 | 60 | +then by default the `hostname` command inside that Pod returns `busybox-1` and the  | 
 | 61 | +`hostname --fqdn` command returns the FQDN.  | 
 | 62 | + | 
 | 63 | +When both `setHostnameAsFQDN: true` and the subdomain field is set in the Pod spec,  | 
 | 64 | +the kubelet writes the Pod's FQDN  | 
 | 65 | +into the hostname for that Pod's namespace. In this case, both `hostname` and `hostname --fqdn`  | 
 | 66 | +return the Pod's FQDN.  | 
 | 67 | + | 
 | 68 | +The Pod's FQDN is constructed in the same manner as previously defined.  | 
 | 69 | +It is composed of the Pod's `spec.hostname` (if specified) or `metadata.name` field,  | 
 | 70 | +the `spec.subdomain`, the `namespace` name, and the cluster domain suffix.  | 
 | 71 | + | 
 | 72 | +{{< note >}}  | 
 | 73 | +In Linux, the hostname field of the kernel (the `nodename` field of `struct utsname`) is limited to 64 characters.  | 
 | 74 | + | 
 | 75 | +If a Pod enables this feature and its FQDN is longer than 64 character, it will fail to start.  | 
 | 76 | +The Pod will remain in `Pending` status (`ContainerCreating` as seen by `kubectl`) generating  | 
 | 77 | +error events, such as "Failed to construct FQDN from Pod hostname and cluster domain".  | 
 | 78 | + | 
 | 79 | +This means that when using this field,   | 
 | 80 | +you must ensure the combined length of the Pod's `metadata.name` (or `spec.hostname`)   | 
 | 81 | +and `spec.subdomain` fields results in an FQDN that does not exceed 64 characters.  | 
 | 82 | +{{< /note >}}  | 
 | 83 | + | 
 | 84 | +## Hostname with pod's hostnameOverride  | 
 | 85 | +{{< feature-state feature_gate_name="HostnameOverride" >}}  | 
 | 86 | + | 
 | 87 | +Setting a value for `hostnameOverride` in the Pod spec causes the kubelet   | 
 | 88 | +to unconditionally set both the Pod's hostname and fully qualified domain name (FQDN)  | 
 | 89 | +to the `hostnameOverride` value.   | 
 | 90 | + | 
 | 91 | +The hostnameOverride field has a length limitation of 64 characters   | 
 | 92 | +and must adhere to the DNS subdomain names standard defined in [RFC 1123](https://datatracker.ietf.org/doc/html/rfc1123).  | 
 | 93 | + | 
 | 94 | +Example:  | 
 | 95 | +```yaml  | 
 | 96 | +apiVersion: v1  | 
 | 97 | +kind: Pod  | 
 | 98 | +metadata:  | 
 | 99 | +  name: busybox-2-busybox-example-domain  | 
 | 100 | +spec:  | 
 | 101 | +  hostnameOverride: busybox-2.busybox.example.domain  | 
 | 102 | +  containers:  | 
 | 103 | +  - image: busybox:1.28  | 
 | 104 | +    command:  | 
 | 105 | +      - sleep  | 
 | 106 | +      - "3600"  | 
 | 107 | +    name: busybox  | 
 | 108 | +```  | 
 | 109 | +{{< note >}}  | 
 | 110 | +This only affects the hostname within the Pod; it does not affect the Pod's A or AAAA records in the cluster DNS server.  | 
 | 111 | +{{< /note >}}  | 
 | 112 | + | 
 | 113 | +If hostnameOverride is set alongside hostname and subdomain fields:  | 
 | 114 | +* The hostname inside the Pod is overridden to the hostnameOverride value.  | 
 | 115 | +* The Pod's A and/or AAAA records in the cluster DNS server are still generated based on the hostname and subdomain fields.  | 
 | 116 | + | 
 | 117 | +Note: If `hostnameOverride` is set, you cannot simultaneously set the `hostNetwork` and `setHostnameAsFQDN` fields.  | 
 | 118 | +The API server will explicitly reject any create request attempting this combination.  | 
 | 119 | + | 
 | 120 | +For details on behavior when `hostnameOverride` is set in combination with   | 
 | 121 | +other fields (hostname, subdomain, setHostnameAsFQDN, hostNetwork),   | 
 | 122 | +see the table in the [KEP-4762 design details](https://github.com/kubernetes/enhancements/blob/master/keps/sig-network/4762-allow-arbitrary-fqdn-as-pod-hostname/README.md#design-details ).  | 
0 commit comments