Kubernetes Explained Simply: Cleaning Up After Yourself [Part 10]

Written by jameshunt | Published 2021/02/05
Tech Story Tags: k8s | containers | kubectl | open-source | tutorial | kubernetes | devops | kubernetes-article-series

TLDRvia the TL;DR App

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.
Also seen here.

Written by jameshunt | R&D at Stark & Wayne, finding software solutions to customer problems and changing them into executable best practices.
Published by HackerNoon on 2021/02/05