✦ For everyone, free.

Practical knowledge for real and everyday life

Home

Kubernetes StatefulSet Network Identity Management

Kubernetes StatefulSet Network Identity Management ensures consistent network identity for stateful applications through persistent IP allocation and stable pod networking.

Kubernetes StatefulSet Network Identity Management is the operational practice of building client connection logic and DNS-consuming automation that correctly handles the fact that a stable Pod DNS name has an underlying IP address that changes every time that Pod is recreated, requiring resolution to happen at connection time rather than being cached indefinitely.


Stable Name, Unstable IP

What Actually Stays Constant Across Replacement

It is the DNS name, web-0.headless-service.namespace.svc.cluster.local, that remains stable across a Pod's replacement, not the underlying IP address, which is reassigned fresh each time the Pod is recreated on whatever node it lands on. Network identity management practice ensures every client and configuration correctly relies on the name, never a cached or hardcoded IP.

kubectl exec network-identity-management-example-0 -- hostname -i

Running this command before and after a Pod recreation reliably shows different IP addresses for the same stable name.


Avoiding IP Caching in Client Applications

Resolving at Connection Time, Not Application Startup

A common mistake is resolving a peer's DNS name once at application startup and caching the resulting IP for the life of the process; when that peer is later recreated with a new IP, the cached value becomes stale and connections silently begin failing. Network identity management practice requires re-resolution on every new connection attempt, or at minimum on connection failure, rather than a single startup-time lookup.

# Anti-pattern: resolve once, cache forever
PEER_IP=$(getent hosts web-0.headless-service | awk '{print $1}')
# Correct pattern: resolve fresh on each connection attempt
connect_to() {
  local ip=$(getent hosts "$1" | awk '{print $1}')
  connect "$ip"
}

DNS TTL and CoreDNS Caching Considerations

Balancing Freshness Against Query Load

CoreDNS applies a configurable TTL to the records it serves for headless Service Pod entries; network identity management practice tunes this TTL to balance how quickly clients observe an IP change after a Pod recreation against the additional DNS query load a very short TTL generates across a large cluster.

# CoreDNS Corefile excerpt
kubernetes cluster.local {
  ttl 5
}

Connection Retry and Reconnect Logic

Building Resilience Into the Client, Not the Network

Because a brief window always exists between a Pod's disappearance and its DNS-stable replacement becoming ready, network identity management practice pushes retry and exponential backoff logic into client connection code itself, treating a failed connection attempt to a StatefulSet peer as an expected, recoverable event rather than a fatal error.

for attempt in range(5):
    try:
        connect(resolve("web-0.headless-service"))
        break
    except ConnectionError:
        time.sleep(2 ** attempt)

External Access Considerations

Headless Services Are Cluster-Internal by Design

Because a headless Service has no cluster IP and no external load balancer integration, network identity management for external clients requires a separate mechanism entirely, an ingress or gateway pointing at a normal, non-headless Service, or a dedicated per-Pod exposure strategy, since the stable per-Pod DNS names themselves are only resolvable from within the cluster's own DNS.

kubectl exec -it debug-pod -- nslookup network-identity-management-example-0.headless-service.default.svc.cluster.local

Network Identity Management Diagram

web-0.headless-service IP 10.0.1.5 (before) IP 10.0.2.9 (after)

Building this re-resolution discipline into every consumer of stateful workload DNS names, rather than assuming a name-to-IP mapping stays fixed once observed, is what prevents the class of connection failures that otherwise recur silently every time a StatefulSet Pod is naturally replaced.