Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete orphaned replicasets with Kubernetes?

Tags:

kubernetes

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?

like image 865
Chris Stryczynski Avatar asked Jan 22 '20 14:01

Chris Stryczynski


People also ask

How do I delete ReplicaSets?

To delete a ReplicaSet and all of its Pods, use kubectl delete . The Garbage collector automatically deletes all of the dependent Pods by default.

How do I permanently delete deployments in Kubernetes?

First, confirm the name of the node you want to remove using kubectl get nodes , and make sure that all of the pods on the node can be safely terminated without any special procedures. Next, use the kubectl drain command to evict all user pods from the node.

How do I remove artifacts from Kubernetes?

These artifacts include your ledger data in persistent storage and the keys that are stored in Kubernetes secrets. Log in to your console and go to the node overview page. Click Delete under the node name and enter the node name on the slider to permanently remove the node from the cluster.


2 Answers

you can edit your deployment and limit the history to 2 or 0

kubectl -n master edit deploy you_deployment

spec:
   replicas: 2
   revisionHistoryLimit: 2
like image 40
Efrain Garay Avatar answered Sep 16 '22 15:09

Efrain Garay


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
like image 151
DT. Avatar answered Sep 16 '22 15:09

DT.