Kubernetes keeps creating objects

7/9/2019

Being new to the K8s, I am trying to clean up the whole namespace after running some tests on a Windows 10 machine. In short, I thought it would be as easy as running kubectl.exe delete deployment but the deployments are created back after a second and I don't know how to get rid of them. See the followings for the details of what I did:

1.kubectl get deployments,rs (to see what we already have)

NAME                               DESIRED   CURRENT   UP-TO-DATE   AVAILABLE AGE
deployment.extensions/postgresql   1         1         1            1           18m
deployment.extensions/redis        1         1         1            1           16m
NAME                                         DESIRED   CURRENT   READY     AGE  
replicaset.extensions/postgresql-c8cb9fff6   1         1         1         18m
replicaset.extensions/redis-5678477b7c       1         1         1         16m

2. kubectl scale deployment redis --replicas=0 (Scale down the deployment)

deployment.extensions "redis" scaled

3. kubectl get deployments,rs (Check again how it looks)

NAME                               DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
deployment.extensions/postgresql   1         1         1            1           21m
deployment.extensions/redis        0         0         0            0           19m
NAME                                         DESIRED   CURRENT   READY     AGE
replicaset.extensions/postgresql-c8cb9fff6   1         1         1         21m
replicaset.extensions/redis-5678477b7c       0         0         0         19m

4. kubectl delete deployment.extensions/redis (Delete the deployment)

deployment.extensions "redis" deleted

5. kubectl get deployments,rs (Check again and see that it is back!)

NAME                               DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
deployment.extensions/postgresql   1         1         1            1           23m
deployment.extensions/redis        1         1         1            1           27s  
NAME                                         DESIRED   CURRENT   READY     AGE
replicaset.extensions/postgresql-c8cb9fff6   1         1         1         23m
replicaset.extensions/redis-5678477b7c       1         1         1         27s

6. kubectl.exe get events (Looking into the events):
Among other things I can see "Scaled down replica set redis-5678477b7c to 0" and then "Scaled up replica set redis-5678477b7c to 1" which looks like it was never actually deleted but immediately scaled up again after the delete command was executed.

Not sure what I am missing but have already checked a couple of other posts like Kubernetes pod gets recreated when deleted and How to delete all resources from Kubernetes one time? but neither one worked for me.

Forgot to say that the K8s cluster is managed by the Docker Desktop.

-- Ramin Toussi
kubectl
kubernetes

1 Answer

7/9/2019
  1. Use kubectl delete deployment <the name of deployment >

  2. If you need to clean up whole namespace , use kubectl delete namespace <namespace-name>

  3. Then re-create the same namespace by kubectl create ns command , if you need the same namespace.

  4. You can also clean up the namespace by using --all options with objects:

e.g

   kubecetl delete deployment --all
   kubecetl delete statefulset --all
   kubectl delete pvc --all
   kubectl delete secrets --all
   kubectl delete service --all

and so on.

As pointed out by @David Maze, you're deleting the ReplicaSet instead of the Deployment that's managing it.

From the documentation:

You can define Deployments to create new ReplicaSets

The Deployment will automatically create and manage a ReplicaSet to control the pods. You need to delete the Deployment to erase it's managed resources.

-- Ijaz Ahmad Khan
Source: StackOverflow