3

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?

crossvalidator
  • 437
  • 6
  • 12
  • Why are you trying to do this? Why does "pod-0" not existing really matter? – Ben Jan 23 '20 at 00:12
  • 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 Jan 23 '20 at 01:55
  • 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 Jan 23 '20 at 01:58
  • 1
    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 Jan 23 '20 at 03:03

4 Answers4

2

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.

coderanger
  • 52,400
  • 4
  • 52
  • 75
  • What's a sleep shim? What about creating a new StatefulSet with replica count 1 to host each pod? That way any pod can be deleted independently. But it doesnt scale well. Too many statefulsets – crossvalidator Jan 23 '20 at 03:53
  • It sounds like your use case does not match that of stateful sets. There isn't much difference between a Deployment and Sts for 1 replica. – coderanger Jan 23 '20 at 23:59
1

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

A. Kanso
  • 11
  • 2
0
  • 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)
pr-pal
  • 3,248
  • 26
  • 18
0

ReplicaSet in K8s 1.21+ will have PodDeletionCost POD annotation to solve that issue for Deployments but so far nothting for STS.

taitelman
  • 612
  • 5
  • 9