liveBook · Manning

Ordinal index

As explained in Kubernetes for Developers

An ordinal is an incrementing integer that identifies a Pod's stable position within a StatefulSet.

An ordinal index is an incrementing integer that identifies a Pod’s stable position in a StatefulSet: 0, 1, 2, and so on. Unlike Deployment Pods, which receive new random names after replacement, a recreated StatefulSet Pod retains its ordinal and attached identity. section 9.2.1

The ordinal makes individual Pods addressable. A Pod can be reached through the pattern STATEFULSET_NAME-POD_ORDINAL.SERVICE_NAME, such as mariadb-0.mariadb-service. This depends on the stable network identities provided by a headless Service. section 9.2.1

Stateful applications use the ordinal to:

  • Preserve a Pod’s position when disruption causes it to be recreated. section 9.2.1
  • Associate a Pod with its stable network name and persistent disk. section 9.2.2
  • Assign different roles to Pods according to their position in the set. section 9.2.2

Assigning Redis roles

A three-Pod Redis StatefulSet uses ordinal 0 for the primary Pod and assigns the replica role to other ordinals. An init container extracts the ordinal from the hostname, then copies primary.conf for ordinal 0 or replica.conf for every other Pod. Redis replicas therefore reference redis-0.redis-service as their primary. section 9.2.2

How Kubernetes for Developers covers it

How deep it goes
Explains numbering, recreation, DNS naming, persistent disks, and init-container role selection, but not the underlying Kubernetes controller implementation.
Examples
Demonstrates mariadb-0.mariadb-service and a three-Pod Redis deployment that makes ordinal 0 primary and other Pods replicas.
What you can do after
Apply it via StatefulSet naming and initialization logic to address Pods individually and configure roles from their positions.
How it connects
Connects ordinal indexes with StatefulSets, Pods, Deployments, headless Services, persistent disks, init containers, ConfigMaps, and Redis replication.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kubernetes for Developers. The section links point to the parts of the book it draws on.