Before Cleaning Up Your K8s Image Registry: How to Identify and Preserve Used Images

 Hey DevOps folks, Gonchik here! Today, I want to share some insights and practical tips on managing your Kubernetes image registry more effectively. If you’re dealing with GitLab’s Container Registry, you know how quickly it can fill up with outdated and unused images. Storage costs can skyrocket, and managing all those images can become a nightmare. Let’s talk about how to avoid accidentally deleting the images your clusters still need.

Easy to clean, hard to restore. Dalle-3

Why Cleaning Up is Crucial

We’ve all been there — our CI/CD pipelines are constantly pushing new images with every commit, and over time, old deployments and misconfigured scripts leave behind a cluttered mess. This bloat doesn’t just affect costs; it can complicate day-to-day operations and make it harder to find the images you actually need.

The Importance of Identifying Used Images

Before you hit that “delete” button, it’s absolutely critical to know which images are still in use by your running pods. This is especially important in production environments where a mistake can lead to downtime and impact your end users. We need a foolproof way to identify these images so we can exclude them from cleanup routines.

Here’s my go-to command for extracting a list of all images used by running pods in a Kubernetes cluster. This list is invaluable for reference when cleaning up your GitLab registry:

kubectl get pods --all-namespaces -o jsonpath="{.items[*].spec['initContainers', 'containers'][*].image}" | tr -s '[[:space:]]' '\n' | sort > images.txt

Breaking Down the Command

Let me explain what’s happening here:

1. Fetch Pods from All Namespaces: kubectl get pods — all-namespaces

This command grabs all pods across all namespaces in your cluster, ensuring you don’t miss anything.

2. Extract Image Fields with JSONPath: -o jsonpath=”{.items[*].spec.{initContainers[*].image,containers[*].image}}”

The JSONPath syntax extracts the image fields from both initContainers and containers in each pod spec.

3. Translate and Squeeze Spaces: tr -s ‘[[:space:]]’ ‘\n’

The tr command is used to translate and squeeze spaces into newlines, putting each image on its own line.

4. Sort the Image List: sort

The sorted list makes it easier to reference and cross-check.

5. Redirect to File: > images.txt

Finally, we redirect the sorted list to a file named images.txt for easy access.

Validating and Making Exceptions

Now that you have images.txt, it’s time to roll up your sleeves and start cross-referencing this list with the images in your GitLab container registry. This step ensures that nothing critical gets wiped out. Once you have this list, update your cleaner script or automation logic to exclude these images from deletion.

Implementing the Cleanup

Armed with your used images list, you can move forward with cleaning up your Kubernetes image registry. GitLab offers several built-in features for cleanup, like expiration policies, and you can also create custom scripts using GitLab’s API. This will help you manage storage costs and keep your registry in tip-top shape.

Wrapping Up

Cleaning up your Kubernetes image registry is an ongoing process, but the key is to do it without risking any downtime or service disruption. By taking the time to identify and preserve the images that are still in use, especially in your production clusters, you can ensure a smoother and safer cleanup process.

So, go ahead and declutter your registry — your future self will thank you!

Happy cleaning, Gonchik

Feel free to tweak it further to add more personal anecdotes or insights!




Comments

Popular posts from this blog

How only 2 parameters of PostgreSQL reduced anomaly of Jira Data Center nodes

Stories about detecting Atlassian Confluence bottlenecks with APM tool [part 1]

Unveiling the VMProtect-devirtualization Project: A Review that project