Stable identity
- Docker & Kubernetes course
- Advanced
- Lesson 15/70
- 4 min
Learn the stable name and ordinal of Pods.
Stable identity
A StatefulSet pod's identity consists of three parts and doesn't change even when the pod is recreated: the ordinal (db-0), the network name (db-0.db) and the storage (the data-db-0 PVC). Applications use this identity to determine roles; e.g. "the one with ordinal 0 is the primary":
# start-up script
SIRA=${HOSTNAME##*-} # db-2 -> 2
if [ "$SIRA" = "0" ]; then
echo "starting as primary"
else
echo "replicating from db-0.db"
fiVerifying the identity
A pod's host name is the same as the pod name. You can query the DNS names with a temporary pod from inside the cluster:
$ kubectl exec db-1 -- hostname
db-1
$ kubectl run -it --rm dns --image=busybox:1.36 --restart=Never -- nslookup db-0.db
Name: db-0.db.default.svc.cluster.local
Address: 10.244.1.12Practice Query the identity
Write two commands: run hostname in db-1; in an interactive pod dns with busybox:1.36 that is deleted on exit and doesn't restart, run nslookup db-0.db.
Starter code
# buraya yazSample solution
kubectl exec db-1 -- hostname
kubectl run -it --rm dns --image=busybox:1.36 --restart=Never -- nslookup db-0.dbExpected output
db-1 Server: 10.96.0.10 Name: db-0.db.default.svc.cluster.local Address: 10.244.1.12 pod "dns" deleted
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 If the db-2 pod moves to another node, which changes?
- The pod IP address Correct answer
- The pod name
- The DNS name (db-2.db)
- The attached PVC
Explanation: The IP changes, but the DNS name is updated to point at the new IP.
2 Print the pod's host name.
kubectl exec db-1 -- ____
kubectl exec db-1 -- hostname
Explanation: A StatefulSet pod's hostname is the pod name.
3 Match.
| Term | Meaning |
|---|---|
ordinal |
db-0, db-1 |
network name |
db-0.db |
storage |
data-db-0 |
pod IP |
Can change |