0
votes

I have a StatefulSet with 2 pods. It has a headless service and each pod has a LoadBalancer service that makes it available to the world.

Let's say pod names are pod-0 and pod-1.

If I want to delete pod-0 but keep pod-1 active, I am not able to do that.

I tried

kubectl delete pod pod-0

This deletes it but then restarts it because StatefulSet replica is set to 2.

So I tried

kubectl delete pod pod-0
kubectl scale statefulset some-name --replicas=1

This deletes pod-0, deletes pod-1 and then restarts pod-0. I guess because when replica is set to 1, StatefulSet wants to keep pod-0 active but not pod-1.

But how do I keep pod-1 active and delete pod-0?

3
Why are you trying to do this? Why does "pod-0" not existing really matter?Ben
Each pod holds specific data created by a user. Hence the stateful set. I want to store all the data created by a user and serve it back the next time the user needs it. When one user has created many pods or many users have created many pods, I want to be able to shut down any arbitrary pod based on user request.crossvalidator
One way out of this would be to create a StatefulSet with replica count 1 for each pod. This would ensure any pod can be removed based on user request and still preserve the persistent volume behind it. But that would mean a lot of StatefulSets to manage ..a bit inefficient I thought.crossvalidator
one way is set it with persistant volume, the other way is to set Redis or other cache solution. So you never mind to lost any pods.BMW

3 Answers

1
votes

This is not supported by the StatefulSet controller. Probably the best you could do is try to create that pod yourself with a sleep shim and maybe you could be faster. But then the sts controller will just be unhappy forever.

1
votes

You can try using a custom controller like: https://github.com/openkruise/kruise/

You can have more fine grain control with selective pod removal, if you use a CloneSet custom resource.

apiVersion: apps.kruise.io/v1alpha1
kind: CloneSet
spec:
  # ...
  replicas: 4
  scaleStrategy:
    podsToDelete:
    - sample-9m4hp # you select which pod to remove

https://openkruise.io/en-us/docs/cloneset.html

The issue of removing specific pods of a deployment or StatefulSet has been opened for years with no resolution: https://github.com/kubernetes/kubernetes/issues/45509

0
votes
  • Statefulset always creates the pods with indices 0..(replica-1).
  • If you really want to have a finer control over individual pods, i guess you would need to create separate STS objects (with replica = 1)