Cleaning Up Kubernetes: A Guide to Finding Unused Resources with Kor

Farshad Nickfetrat - Sep 10 - - Dev Community

Image description
If you’ve been running Kubernetes for a while, you probably know how messy things can get. When applications come and go, they often leave behind unused or forgotten resources. These “orphans” don’t serve any purpose but still sit there, wasting space and potentially even costing you money. That’s where Kor - Kubernetes Orphaned Resources Finder comes in!

What Exactly Is Kor?

Image description

Kor is a tool designed to help you find and clean up those orphaned resources in your Kubernetes cluster. Think of it like a janitor that keeps your Kubernetes environment tidy by identifying and removing stuff you no longer need — like old PVCs (Persistent Volume Claims), ConfigMaps, Secrets, or even abandoned Services.

If you're running a busy Kubernetes cluster with tons of deployments, chances are you've got a bunch of orphaned resources sitting around. And here’s the kicker: they can actually slow down your cluster or eat up valuable resources. Kor solves that problem by tracking down those leftovers and helping you decide what to keep or toss.
Why You Need Kor

Managing Kubernetes resources can get tricky, especially when things aren’t cleaned up properly. Here’s why Kor can make your life easier:

Efficient Resource Management: Orphaned resources can clog up your cluster. Kor helps you find and get rid of them, freeing up space and resources for the stuff that actually matters.

Cost Savings: Depending on your cloud provider, orphaned resources can cost you. Things like unused volumes or IP addresses can add up in cost over time. Kor can help you avoid these hidden charges by cleaning up the mess.

Cluster Hygiene: Keeping a cluster clean and organized is key to maintaining performance. With Kor, you reduce the chances of having an overly cluttered cluster that’s harder to manage and debug.
Enter fullscreen mode Exit fullscreen mode

How Kor Works

Kor operates by scanning your Kubernetes cluster and identifying any resources that no longer have any dependent objects. For example, if a PVC is sitting there without a Pod or Deployment using it, Kor will flag it as orphaned. Similarly, ConfigMaps or Secrets that no longer have references can be identified.

Kor provides a report of these resources and gives you options to remove them. It’s simple, lightweight, and efficient!
What Resources Can Kor Find?

Kor is designed to find a variety of orphaned resources, including:

Persistent Volume Claims (PVCs): Old storage volumes that are no longer being used.

ConfigMaps: Configuration files that are no longer tied to active services.

Secrets: Credentials and keys left behind after services or pods are deleted.

Services: Networking endpoints that aren’t in use anymore.
Enter fullscreen mode Exit fullscreen mode

How to Get Started with Kor

Kor is easy to set up and integrate with your Kubernetes environment. Once you’ve installed it, you can run it as a CronJob or a one-time scan, depending on how frequently you want to clean up.

You’ll get a detailed list of orphaned resources, and you can either manually review them or set up automated cleanups to make sure your cluster stays neat and tidy.

*Installation : *

For macOS users :

brew install kor

Install the binary to your $GOBIN or $GOPATH/bin:

go install github.com/yonahd/kor@latest

Kubectl plugin

kubectl krew install kor

Kor provides various subcommands to identify and list unused resources. The available commands are:

all - Gets all unused resources for the specified namespace or all namespaces.

configmap - Gets unused ConfigMaps for the specified namespace or all namespaces.

secret - Gets unused Secrets for the specified namespace or all namespaces.

service - Gets unused Services for the specified namespace or all namespaces.

serviceaccount - Gets unused ServiceAccounts for the specified namespace or all namespaces.

deployment - Gets unused Deployments for the specified namespace or all namespaces.

statefulset - Gets unused StatefulSets for the specified namespace or all namespaces.

role - Gets unused Roles for the specified namespace or all namespaces.

clusterrole - Gets unused ClusterRoles for the specified namespace or all namespaces (namespace refers to RoleBinding).

hpa - Gets unused HPAs for the specified namespace or all namespaces.

pod - Gets unused Pods for the specified namespace or all namespaces.

pvc - Gets unused PVCs for the specified namespace or all namespaces.

pv - Gets unused PVs in the cluster (non namespaced resource).

storageclass - Gets unused StorageClasses in the cluster (non namespaced resource).

ingress - Gets unused Ingresses for the specified namespace or all namespaces.

pdb - Gets unused PDBs for the specified namespace or all namespaces.

crd - Gets unused CRDs in the cluster (non namespaced resource).

job - Gets unused jobs for the specified namespace or all namespaces.

replicaset - Gets unused replicaSets for the specified namespace or all namespaces.

daemonset- Gets unused DaemonSets for the specified namespace or all namespaces.

finalizer - Gets unused pending deletion resources for the specified namespace or all namespaces.

networkpolicy - Gets unused NetworkPolicies for the specified namespace or all namespaces.

exporter - Export Prometheus metrics.

version - Print kor version information.
Enter fullscreen mode Exit fullscreen mode

in this scenario i want to list all unused resources in default namespace

kor all -n default --show-reason

And here is the result

Image description

About Author :
Hi 👋, I’m Farshad Nick (Farshad nickfetrat)

📝 I regularly write articles on packops.dev and packops.ir

💬 Ask me about Devops , Cloud , Kubernetes , Linux

📫 How to reach me on my linkedin

Here is my Github repo
Enter fullscreen mode Exit fullscreen mode
. . .
Terabox Video Player