- Ordered startup and shutdown
- Stable network identities
- Consistent storage provisioning
A StatefulSet is similar to a Deployment in that you define it via a YAML file with a Pod template. The main differences are:
- Change the kind from Deployment to StatefulSet (note the uppercase “S”).
- Include the additional field ‘serviceName’ to specify a headless service.
Converting a Deployment to a StatefulSet
Consider the following example of a MySQL Deployment:serviceName field:
- Deploy Pods one at a time in an ordered, graceful manner.
- Assign a stable, unique DNS record to each Pod, allowing other applications to refer to them reliably.
- Scale Pods sequentially, where each new Pod starts only after the previous one is ready.
Creating and Managing a StatefulSet
You can use standard Kubernetes commands to create and scale your StatefulSet. For example:Customizing Pod Deployment Strategy
By default, StatefulSets follow an ordered approach for both deployment and termination. However, you can override this behavior by setting thepodManagementPolicy to Parallel. This instructs Kubernetes to deploy and terminate all Pods simultaneously while still providing them with stable network identities.
Below is an example StatefulSet configuration that uses the Parallel pod management policy:
While the
Parallel management policy offers faster scaling, it does so at the cost of ordered deployment. Choose the appropriate policy based on your application’s initialization and shutdown needs.