Ora

How Do I Delete a StatefulSet Pod?

Published in Kubernetes StatefulSet Management 3 mins read

To permanently delete pods managed by a StatefulSet, the primary and most effective method is to delete the StatefulSet resource itself. While you can delete individual pods directly, the StatefulSet controller will typically recreate them immediately to maintain its desired state.

Understanding StatefulSet Pod Deletion

StatefulSets are designed to manage stable, unique pods. This means that direct deletion of an individual pod is usually not a permanent solution, as the StatefulSet controller will strive to restore the desired number of replicas. For lasting removal, you must interact with the StatefulSet resource itself.

Deleting the StatefulSet Resource (Permanent Deletion)

The most common way to permanently remove all pods associated with a StatefulSet is to delete the StatefulSet resource. This process is consistent with deleting other Kubernetes resources.

Steps to Delete a StatefulSet:

  1. Identify the StatefulSet: Ensure you know the name of the StatefulSet you wish to delete.

  2. Execute the Deletion Command: Use the kubectl delete command, specifying the StatefulSet either by its name or by its original configuration file.

    • By Name:

      kubectl delete statefulset <statefulset-name>

      Example: kubectl delete statefulset my-database

    • By File:

      kubectl delete -f <statefulset-file.yaml>

      Example: kubectl delete -f my-database-statefulset.yaml

  3. Delete Associated Headless Service (If Applicable): After deleting the StatefulSet, you may need to separately delete any associated headless service. This service is often used by StatefulSets for stable network identities and is not automatically removed with the StatefulSet itself.

    kubectl delete service <headless-service-name>

    Example: kubectl delete service my-database-headless

Deletion Strategies

When deleting a StatefulSet, consider the --cascade option for different behaviors regarding its dependent objects (like pods).

Option Description
Default (Foreground) This is the default behavior. The Kubernetes API server sets the deletionTimestamp for the StatefulSet, and the StatefulSet controller then deletes its pods in reverse ordinal order. Once all pods are gone, the StatefulSet resource itself is removed.
--cascade=false Deletes only the StatefulSet resource, leaving its managed pods running. The pods will no longer be managed by any controller. This is also known as "orphan" deletion.

Scaling Down a StatefulSet (Controlled Removal)

If you only need to reduce the number of pods managed by a StatefulSet rather than deleting all of them, you can scale down the StatefulSet. This is a controlled way to remove pods, starting from the highest ordinal index.

kubectl scale statefulset <statefulset-name> --replicas=<new-number>

Example: To reduce a StatefulSet named my-database from 3 pods to 1:

kubectl scale statefulset my-database --replicas=1

The StatefulSet controller will terminate pods with the largest ordinal numbers first (e.g., my-database-2, then my-database-1).

Deleting Individual StatefulSet Pods (Temporary Action)

You can delete an individual pod belonging to a StatefulSet using kubectl delete pod. However, it's crucial to understand the implications:

kubectl delete pod <pod-name>

Example: kubectl delete pod my-database-0

When you delete an individual StatefulSet pod:

  • The StatefulSet controller immediately detects that the desired number of replicas is not met.
  • It will attempt to recreate the deleted pod to restore the desired state.
  • This action is primarily useful for restarting a problematic pod or forcing a re-scheduling, not for permanent removal from the StatefulSet's managed set.

Considerations for Persistent Storage

StatefulSets are often used with persistent storage. When you delete a StatefulSet, its associated Persistent Volume Claims (PVCs) are not automatically deleted by default. This design prevents accidental data loss.

  • To retain data: Do nothing; the PVCs will remain.

  • To delete data: You must explicitly delete the PVCs after the StatefulSet and its pods are gone.

    kubectl delete pvc <pvc-name-1> <pvc-name-2> ...

In summary, for permanent removal of StatefulSet pods, focus on deleting or scaling down the StatefulSet resource itself.