π Kubernetes Pod hostname and subdomain¶
In Kubernetes, every Pod gets a hostname and can optionally be assigned a subdomain. These fields are part of the Pod spec and directly influence Pod DNS resolution.
π 1. Default Behavior¶
- By default, a Podβs hostname = the Podβs
metadata.name. - The Pod is reachable only via its IP address (not stable, changes on restart).
- The Service (if created) provides a stable DNS name.
Example:
apiVersion: v1
kind: Pod
metadata:
name: web-1
spec:
containers:
- name: nginx
image: nginx:1-alpine
Inside the Pod:
hostname # output: web-1
DNS available:
- Service FQDN (if exposed):
<service>.<namespace>.svc.cluster.local
π 2. Setting spec.hostname¶
You can override the default Pod hostname with spec.hostname. When set, this value takes precedence over the Pod's metadata.name.
spec:
hostname: custom-host
Effect:
- Inside the Pod β
hostname = custom-host. - No DNS entry created (still only Service DNS is usable).
Use case:
- When you need a specific hostname inside the container (for legacy apps, logging, monitoring).
π 3. Adding spec.subdomain¶
When spec.subdomain is set along with hostname, Kubernetes creates a Pod-specific DNS entry under the Service domain.
apiVersion: v1
kind: Pod
metadata:
name: section100
spec:
hostname: section100
subdomain: section
containers:
- name: nginx
image: nginx:1-alpine
And you have a Service:
apiVersion: v1
kind: Service
metadata:
name: section
spec:
clusterIP: None # Headless Service
selector:
app: your-app
ports:
- port: 80
Now, the Pod is resolvable at:
<section-hostname>.<subdomain>.<namespace>.svc.cluster.local
Example:
section100.section.lima-workload.svc.cluster.local
This DNS name follows the Pod even if its IP changes.
π 4. Service DNS¶
Every Service always gets its own stable DNS entry, independent of hostname/subdomain:
<service>.<namespace>.svc.cluster.local
Example:
section.lima-workload.svc.cluster.local
This resolves to the Service ClusterIP (for normal Services) or to Pod IPs directly (for Headless Services).
π Scenarios Summary¶
| Case | hostname | subdomain | Service Type | Pod FQDN | Service FQDN |
|---|---|---|---|---|---|
| Default | Pod name | none | ClusterIP | β Not available | β
<svc>.<ns>.svc.cluster.local |
| Custom hostname only | custom | none | ClusterIP | β Not available | β |
| Hostname + Subdomain (ClusterIP Service) | section100 | section | ClusterIP | β
section100.section.<ns>.svc.cluster.local | β |
| Hostname + Subdomain (Headless Service) | section100 | section | Headless | β Pod FQDN resolves directly to Pod IP | β Service FQDN resolves to all Pod IPs |
π 5. When to Use¶
-
hostnameonly: Use when the app inside the Pod expects a particular hostname but doesnβt need DNS resolution. -
hostname+subdomain: Use with a Service (usually Headless) when you need stable per-Pod DNS. Example use cases: -
StatefulSets (e.g., databases like Cassandra, Kafka, MongoDB).
- When Pods must talk to each other by stable names instead of changing IPs.
β Key Formula for Pod FQDN¶
When both hostname and subdomain are set:
<hostname>.<subdomain>.<namespace>.svc.cluster.local
When only Service exists:
<service>.<namespace>.svc.cluster.local
π Quick DNS Test¶
Deploy a temporary Pod:
kubectl run dns-test --image=busybox:1.28 -it --restart=Never -- nslookup <fqdn>
Example:
nslookup section100.section.lima-workload.svc.cluster.local
β¨ In summary:
hostname= containerβs hostname.subdomain+ Service = stable Pod-level DNS.- Service DNS always exists, Pod DNS only exists if you set both.
- This is critical in StatefulSets and Headless Services where Pods must be uniquely addressable.