Silly Kubectl Trick #10 – Cleaning Up After Yourself

Ahoy, There!

This is just one blog post in an ongoing series about fun things you can do with the Kubernetes CLI, kubectl. We have a whole bunch of these over on our Silly Kubectl Tricks page. Also don’t forget to checkout out the video series on YouTube!

If there’s one thing that Kubernetes makes easy, it’s creating resources – pods, deployments, volumes – before long you’ll have tons of them lying around.

Eventually, you’ll want to tear some of them down, to free up CPU, disk, and RAM for your next wild experiment. This happens to us all the time in our research clusters, and we’ve picked up a few tricks:

kubectl delete -f mad-science.yml

If you’ve been dutifully recording your resource definitions in a YAML file, and just kept kubectl apply-ing it, you’re in luck! The kubectl delete command also understands the -f flag, and will remove every resource defined.

→ kubectl apply -f shield.yml# ... time passes ...
→ kubectl delete -f shield.yml
# clean as a whistle!

This also works if you keep your resources in their own files, inside of a single directory that you apply and delete:

kubectl apply -f experiment-42.d/
kubectl delete -f experiment-42.d/

Another trick we’ve gotten a ton of value out of is using namespaces. If you put each and every experiment, or logical group of resources together under a unique namespace, you can use Kubernetes resource dependency system to your advantage:

kubectl delete ns quantum-crypto-coin

All namespaces resources get deleted when you delete the owning namespace, which is a pretty handy way of tearing down things like pods, services, namespaces, etc. If you’re not clear what is and is not namespaced, you can run the api-resources command to find out:

kubectl api-resources --namespaced
kubectl api-resources --namespaced=false

Namespaces bring more value to the table than just cleanup, but being able to wipe an experiment or work-in-progress with minimal effort is a definite boon.

Spread the word

twitter icon facebook icon linkedin icon