Silly Kubectl Trick #10: Cleaning Up After Yourself
Two of the most effective ways to clean up your resources are the kubectl delete command and using namespaces.
Join the DZone community and get the full member experience.Join For Free
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:
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.
This also works if you keep your resources in their own files, inside of a single directory that you apply and delete:
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:
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:
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.
Published at DZone with permission of . See the original article here.
Opinions expressed by DZone contributors are their own.