Sub topic 11
Peer discovery and bootstrapping by ordinal
This chapter assumes you can read the StatefulSet and headless Service manifests from earlier chapters. It also assumes you know that Pod names follow the pattern <statefulset>-<ordinal>. Clustered software such as databases, brokers, and coordination services must find its peers and decide who plays which role at startup. A StatefulSet gives you two things to build on: predictable DNS names and predictable ordinals.
How peers find each other
Each Pod in a StatefulSet gets a DNS name through its governing headless Service:
<pod-name>.<service-name>.<namespace>.svc.cluster.localFor Pod demo-0 in the default namespace, governed by Service demo, the name is demo-0.demo.default.svc.cluster.local. Within the same namespace, demo-0.demo is enough. Because the name is tied to the ordinal and not to an IP address, it keeps working after the Pod is rescheduled and gets a new IP.
A config file can therefore list peers statically. A three-member cluster can always be written as demo-0.demo, demo-1.demo, demo-2.demo. The alternative is to query the headless Service name itself (demo). That returns one A record for each ready Pod, which is useful when the member count changes.
Choosing a role from the ordinal
A Pod's hostname equals its Pod name, so a startup script can extract the ordinal from the hostname. The common convention is that ordinal 0 acts as the seed or initial primary, and every other ordinal joins it.
The manifest below demonstrates the pattern. It uses a plain busybox image so you can see the logic without database software.
apiVersion: v1
kind: Service
metadata:
name: demo
spec:
clusterIP: None
publishNotReadyAddresses: true
selector:
app: demo
ports:
- name: peer
port: 7000
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: demo
spec:
serviceName: demo
replicas: 3
selector:
matchLabels:
app: demo
template:
metadata:
labels:
app: demo
spec:
containers:
- name: node
image: busybox:1.36
command: ["/bin/sh", "-c"]
args:
- |
ordinal="${HOSTNAME##*-}"
if [ "$ordinal" = "0" ]; then
echo "$(hostname): starting as seed"
else
echo "$(hostname): joining demo-0.demo"
until nslookup demo-0.demo >/dev/null 2>&1; do
echo "waiting for demo-0.demo"
sleep 2
done
echo "$(hostname): found seed, joining"
fi
while true; do sleep 30; doneAfter applying it, kubectl logs demo-0 shows the seed message. kubectl logs demo-1 and kubectl logs demo-2 show the join messages.
Why publishNotReadyAddresses matters
Normally a headless Service publishes DNS records only for Pods that pass their readiness probe. A real cluster member often cannot become ready until it has contacted its peers. If peers cannot resolve each other until all are ready, the cluster deadlocks. Setting publishNotReadyAddresses: true on the Service publishes records for Pods before they are ready, so members can find each other during bootstrap.
Interaction with ordered startup
With the default OrderedReady policy, demo-1 is not created until demo-0 is Ready. This helps bootstrapping, because the seed is up before any joiner starts. It also means that the readiness probe of demo-0 must not require the other members to exist. Otherwise demo-0 never becomes Ready, demo-1 is never created, and the rollout stalls.
If members do not need to start in sequence, podManagementPolicy: Parallel starts all Pods at once. Joiners must then retry until the seed is reachable, as the loop in the example does.
Common mistakes
- Treating ordinal 0 as the permanent primary. After a failover in a database, another member may be primary. The ordinal only decides initial roles. Keep the current role in the application's own state, usually on the persistent volume.
- Running the bootstrap logic on every restart. Check whether the data directory already holds cluster state, and skip the initial setup if it does.
- Hard-coding the namespace or cluster domain. Use short names within a namespace, or read the namespace from the downward API.
- Forgetting that scaling down removes the highest ordinals. Make sure the application can deregister those members cleanly.
Exercise
A three-member StatefulSet uses the default OrderedReady policy. The readiness probe of demo-0 checks that all three members are connected to each other. What happens when you create the StatefulSet, and how do you fix it?
Show answer
demo-0 never becomes Ready because demo-1 and demo-2 do not exist yet. Since OrderedReady waits for demo-0 to be Ready before creating demo-1, the rollout stalls. Fix it by making the readiness probe check only that demo-0 itself is serving, or by setting podManagementPolicy to Parallel. Also set publishNotReadyAddresses: true on the headless Service so peers can resolve each other before they are ready.