Cyberithub

How to delete all kubernetes objects deployed by kustomize tool?

Advertisements

In this article, we will see how to delete all kubernetes objects deployed by kustomize tool. As you already know, kustomize is an open source kubernetes native configuration management tool designed to streamline the process of complex deployment across multiple environments such as in dev, sit, uat, pre and prod. It makes life simpler by allowing us to keep all non changing configuration in bases and the rest in overlays to allow deployment of only the changes required to be deployed. Since kubectl is natively integrated with kustomize, you don't need any extra tools for managing your kubernetes objects.

But one question which often comes is that if we are using kustomize to deploy kubernetes objects then can we use the same tool to delete the deployed objects in case it is not needed anymore. This question is especially important when you are accustomed of deleting objects such as deployment, statefulset, service, cronjob etc, manually by using kubectl delete command. This also leaves a space for an error when you forgot to delete some of the deployed objects which are not needed anymore. So to deal with this situation, we will see how to delete all kubernetes objects deployed by kustomize tool.

 

How to delete all kubernetes objects deployed by kustomize tool?

How to delete all kubernetes objects deployed by kustomize tool?

Also Read: 30 Most Frequently Asked Questions about Kubernetes Pods

Before going further, let's take an example and understand how kustomize can be used for deploying kubernetes objects in a cluster. In our example, we have kustomization.yaml file available under kustomize/overlays/example/app/hello-app directory which will be scanned by kustomize tool to understand the objects that needs to be deployed in our cluster. Here we are deploying below kubernetes objects:-

  • configmap
  • service account
  • secret
  • deployment

To deploy these objects, we are using kustomize build --load-restrictor LoadRestrictionsNone --enable-alpha-plugins kustomize/overlays/example/app/hello-app | kubectl apply -f - command as shown below.

cyberithub@macos1066 % kustomize build --load-restrictor LoadRestrictionsNone --enable-alpha-plugins kustomize/overlays/example/app/hello-app | kubectl apply -f - 
configmap/example-config-hello created
serviceaccount/example-svc-acct created 
secret/example-secret created
deployment.apps/example-hello-app created

Let's break above command and understand what it exactly doing:-

  • kustomize build:  this command build the configuration by checking the files in base and overlays directories as mentioned in kustomization.yaml and create the final YAML configuration
  • --load-restrictor LoadRestrictionsNone: this option is used to disable restriction on loading files from outside the directory, allowing access to external resources.
  • --enable-alpha-plugins: this option is used to extend kustomize functionality by enabling alpha-stage plugins.
  • kustomize/overlays/example/app/hello-app: this is the overlay path which contains kustomization.yaml file for the hello-app.
  • kubectl apply -f - : this command applies the generated YAML configuration to kubernetes cluster

To check above deployed objects, we can use below kubectl commands.

kubectl get configmap
kubectl get svc
kubectl get secrets
kubectl get deployment

Now the question comes when you don't need above deployed objects anymore and now you need to remove it from your kubernetes cluster. You can use kubectl delete command to delete all of the deployed objects individually or to make life much easier, you can use generated YAML configuration file only to delete all the deployed resources. This way you can reduce the complexity of finding and deleting objects individually and this can also save lot of your time and effort. So in our case to delete all kubernetes objects, we can use kustomize build kustomize/overlays/example/app/hello-app | kubectl delete -f- command.

cyberithub@macos1066 % kustomize build kustomize/overlays/example/app/hello-app | kubectl delete -f-
configmap "example-config-hello" deleted
serviceaccount "example-svc-acct" deleted
secret "example-secret" deleted
deployment "example-hello-app" deleted

You may notice that in above command we are using the same kustomize build to generate the YAML configuration but instead of piping the output to kubectl apply, this time we are piping it to kubectl delete command. So that this command will remove all kubernetes objects defined in kustomization.yaml file as evident from output as well. That's it. All the deployed objects would be deleted automatically. It is that simple. Hope this helps you.

Leave a Comment