kubernetes

K8S: How do I delete deployments with 0 replicas


How do I clear old deployments? I'm able shrink a deployment to 0 replicas via kubectl scale deployment.v1.apps/hello-kubernetes3 --replicas=0, but as shown below they're still present in some form.

$ kubectl get rs -o wide
NAME                           DESIRED   CURRENT   READY   AGE   CONTAINERS         IMAGES                            SELECTOR
hello-kubernetes-5cb547b7d     1         1         1       27m   hello-kubernetes   paulbouwer/hello-kubernetes:1.8   app=hello-kubernetes,pod-template-hash=5cb547b7d
hello-kubernetes-6d9fd679cd    0         0         0       32m   hello-kubernetes   paulbouwer/hello-kubernetes:1.8   app=hello-kubernetes,pod-template-hash=6d9fd679cd
hello-kubernetes3-6d9fd679cd   0         0         0       25m   hello-kubernetes   paulbouwer/hello-kubernetes:1.8   app=hello-kubernetes,pod-template-hash=6d9fd679cd

Solution

  • This is a community wiki answer as a part of it would be based on @meaningqo comment but I would like to share some more light on this topic with a help of the official documentations.

    What you were doing in the first place is not deleting a deployment but actually scaling it to 0. In order to delete a deployment or any other resource you should use the kubectl delete command:

    Delete resources by filenames, stdin, resources and names, or by resources and label selector.

    JSON and YAML formats are accepted. Only one type of the arguments may be specified: filenames, resources and names, or resources and label selector.

    Note that:

    Some resources, such as pods, support graceful deletion. These resources define a default period before they are forcibly terminated (the grace period) (...) Because these resources often represent entities in the cluster, deletion may not be acknowledged immediately.

    So you may want to wait a bit before seeing the results.

    Referring to your second question. There are also other options aimed to work with ReplicaSets specifically:

    I also recommend familiarizing yourself with the whole ReplicaSet guide for better understanding of this particular topic.