How to delete orphaned replicasets with Kubernetes?

1/22/2020

Not sure how but I've got numerous pods running that seem to be due to multiple repliacasets for each deployment.

This occurred after I did some heavy editing of multiple deployments.

Is there some easy way of deleting orphaned replica sets? As opposed to manually inspecting each, and determining if it matches with a deployment, and then delete it?

-- Chris Stryczynski
kubernetes

1 Answer

1/22/2020

revisionHistoryLimit is an optional field that specifies the number of old ReplicaSets to retain to allow rollback

By default, 10 old ReplicaSets will be kept, change it to one so you dont have more than one old replicaset.

Offical Link

Tested the field as below

Created NGINX deployment updated multiple times and generate few replicaset as listed below

$ kubectl get all
NAME                         READY   STATUS    RESTARTS   AGE
pod/nginx-854998f596-6jtth   1/1     Running   0          18s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   9d

NAME                    READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx   1/1     1            1           6m20s

NAME                               DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-59d5958c9f   0         0         0       4m5s
replicaset.apps/nginx-669cf47c4f   0         0         0       94s
replicaset.apps/nginx-6ff549666b   0         0         0       2m21s
replicaset.apps/nginx-854998f596   1         1         1       2m7s
replicaset.apps/nginx-966c7f84     0         0         0       108s

Edit the running deployment and update revisionHistoryLimit field and set to zero as revisionHistoryLimit: 0

$ kubectl edit deployments.apps nginx
deployment.apps/nginx edited

Old Replica set are removed.

$ kubectl get all
NAME                         READY   STATUS    RESTARTS   AGE
pod/nginx-854998f596-6jtth   1/1     Running   0          52s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   9d

NAME                    READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx   1/1     1            1           6m54s

NAME                               DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-854998f596   1         1         1       2m41s
-- DT.
Source: StackOverflow