
I recently had a task of identifying the high number of unused ReplicatSets in our Kubernetes cluster.
Unused ReplicatSets typically exist if you have multiple revisions of your deployments, and the unused ReplicatSets belong to previous revisions. They are still there in order to support any potential rollback.
The number of unused ReplicatSets were found by running:
kubectl get replicaset -A | awk ‘{if ($2 + $3 + $4 == 0) print $1}’ | wc -l
The $2, $3 and $4 columns are Desired, Current and Ready state respectivly for the ReplicatSets.
If you are doing deployments today, you would be using deployment api version apps/v1, and unless you have explicitly set revisionHistoryLimit to a number, it will default to 10 revisions.
Prior to Kubernetes 1.16 it was also possible to use deployment api version extensions/v1beta1 or apps/v1, and these had a default revisionHistoryLimit of 2147483647.
Find deployments that revisionHistoryLimit greater than 10:
kubectl get deployments -A -o custom-columns=NAMESPACE:.metadata.namespace,NAME:.metadata.name,REV_HISTORY:.spec.revisionHistoryLimit | awk ‘{ if ($3 > 10) print }’
So the third column in the output from kubectl is the revisionHistoryLimit, and if it is larger than 10, then we print the namespace, the deployment name and the value of revisionHistoryLimit.
This way I was able to identify all deployments in the cluster that were potentially having a high number of ReplicatSets.
In order to change the revisionHistoryLimit, I could then use the kubectl patch deployment command to fix these deployments.
The syntax would be:
kubectl patch deployment -n <namespace> <deployment> --type=json -p=’[{“op”: “replace”, “path”: “/spec/revisionHistoryLimit”, “value”: 10}]’
Using xargs you could take the output from the kubectl get deployments command shown earlier, and pipe it over to this kubectl patch deployment command. Only do that is you understand how xargs works, so you don’t unintentionally patch the wrong deployments.
The ReplicatSets older than the 10 newest revisions would now be picked up by the Kubernetes garbage collector.