Sub topic 17
Cleaning up
Local clusters use CPU, memory and disk. They also leave entries in your kubeconfig. This chapter covers removing what you created, in the right order, and checking that nothing is left behind. It assumes you have a running kind or minikube cluster named dev and the web manifest from earlier chapters.
Remove the application first
Deleting a cluster destroys everything inside it, so removing the app is optional. It is still useful when you want to keep the cluster and reset only the workload.
kubectl delete -f web.yamlThis deletes the Deployment and the Service defined in the file. The pods created by the Deployment are removed with it.
Delete the cluster
Each tool has its own delete command.
kind delete cluster --name dev
minikube delete -p devkind delete cluster removes the node containers and the kind-dev entries from your kubeconfig. minikube delete -p dev removes the profile, its virtual machine or container, and its kubeconfig entries.
Check what is left
Confirm that the clusters and contexts are gone.
kind get clusters
minikube profile list
kubectl config get-contextsNormally the contexts disappear with the cluster. If a context remains, for example because the cluster was removed by deleting containers directly, remove the stale entries by hand:
kubectl config delete-context kind-dev
kubectl config delete-cluster kind-dev
kubectl config unset users.kind-devIf the deleted context was your current one, kubectl has no active context. Select another with kubectl config use-context <name> before running further commands.
Reset instead of delete
Sometimes you want a clean cluster, not no cluster. The simplest reset is delete and create again. Both tools start from a fresh state in under a few minutes.
kind delete cluster --name dev
kind create cluster --name dev --config kind-config.yamlFor minikube, minikube stop -p dev keeps the cluster and its data and frees CPU and memory. minikube delete -p dev discards them.
Reclaim disk space
Loaded images and downloaded node images stay on your machine after the cluster is gone. List and remove them through Docker:
docker images
docker image prunedocker image prune removes only dangling images. Remove named images, such as myapp:dev or the kind node image, with docker rmi <image>. To remove every minikube profile and the cache under ~/.minikube, run minikube delete --all --purge.
Common errors
- Deleting the wrong cluster because
--nameor-pwas left out. - Running
kubectlafter deleting the current context and getting a "current-context is not set" error or a connection to a cluster you did not intend. - Stopping the Docker daemon before deleting a kind cluster, which makes
kind delete clusterfail because it cannot reach the node containers.
Exercise
You deleted your kind cluster dev by running docker rm -f on its containers. kubectl config get-contexts still lists kind-dev. Which commands remove the leftovers, and why did they remain?
Show answer
Run kubectl config delete-context kind-dev, kubectl config delete-cluster kind-dev and kubectl config unset users.kind-dev. kind removes the kubeconfig entries only when kind delete cluster --name dev runs. Removing the containers directly skipped that step, so the entries stayed.