Tearing down a GKE cluster to "brand new" state without deleting it?

9/2/2021

I am playing with Kubernetes really for the first time, and have a brand new (empty) GKE cluster up on GCP.

I am going to play around with YAML Kustomize files and try to get a few services deployed there, but what I'm really looking for is a command (or set of kubectl/gcloud commands) to restore the GKE cluster to a totally "new" slate. This is because it's probably going to take several dozen (or more!) attempts at configuring and tweaking my YAML files until I get the configs and behavior down just right, and each time I mess up I want to start over with a completely "clean"/new GKE cluster. For reasons outside the scope of this question, deleting and recreating the GKE cluster really isn't a viable option.

My Kustomize files and deploment scripts will create Kubernetes operators, namespaces, persistent volumes (and claims), various services and all sorts of other resources. But I need to be able to drop/delete them all and bring the cluster back to the brand new state.

Is this possible, and if so, whats the process/command(s) involved? FWIW I have cluster admin permissions.

-- hotmeatballsoup
google-kubernetes-engine
kubernetes

1 Answer

9/3/2021

As mentioned by @dany L, Kubernetes Namespace will be the perfect option for deleting the resources. Create a custom namespace by using the command

Kubectl create namespace custom-name 

and deploy all the resources(Deployment,ReplicaSet,Services etc.,) into the namespace.

To work with Namespace, you need to add --namespace flag to k8s commands.

For example:

kubectl create -f deployment.yaml --namespace=custom-namespace

If you want to delete all of these resources, you just need to delete the custom namespace. By deleting the custom namespace, all the other resources would be deleted. Without it, ReplicaSet might create new pods when existing pods are deleted. Run the following command for deleting the namespace.

kubectl delete namespace custom-name

To list down all the resources associated to a specific namespace, you can run the following command

kubectl api-resources --verbs=list --namespaced -o name  | xargs -n 1 kubectl get --show-kind --ignore-not-found -n <namespace>

The kubectl api-resources enumerates the resource types available in your cluster. So we can use it by combining it with kubectl get to list every instance of every resource type in a Kubernetes namespace.

Refer to this link to list all the resources in a namespace.

-- Srividya
Source: StackOverflow