How to delete all resources from Kubernetes one time?

后端 未结 3 1905
暗喜
暗喜 2020-12-23 16:10

Include:

  • Daemon Sets
  • Deployments
  • Jobs
  • Pods
  • Replica Sets
  • Replication Controllers
  • Stateful Sets
  • Se
相关标签:
3条回答
  • 2020-12-23 16:40

    You can also delete Kubernetes resources with the help of labels attached to it. For example, suppose below label is attached to all resource

    metadata:
      name: label-demo
      labels:
        env: dev
        app: nginx
    

    now just execute the below commands

    deleting resources using app label
    $ kubectl delete pods,rs,deploy,svc,cm,ing -l app=nginx
    
    deleting resources using envirnoment label
    $ kubectl delete pods,rs,deploy,svc,cm,ing -l env=dev
    
    0 讨论(0)
  • 2020-12-23 16:58

    Method 1: To delete everything from the current namespace (which is normally the default namespace) using kubectl delete:

    kubectl delete all --all
    

    all refers to all resource types such as pods, deployments, services, etc. --all is used to delete every object of that resource type instead of specifying it using its name or label.

    To delete everything from a certain namespace you use the -n flag:

    kubectl delete all --all -n {namespace}
    

    Method 2: You can also delete a namespace and re-create it. This will delete everything that belongs to it:

    kubectl delete namespace {namespace}
    kubectl create namespace {namespace}
    

    Note (thanks @Marcus): all in kubernetes does not refers to every kubernetes object, such as admin level resources (limits, quota, policy, authorization rules). If you really want to make sure to delete eveything, it's better to delete the namespace and re-create it. Another way to do that is to use kubectl api-resources to get all resource types, as seen here:

    kubectl delete "$(kubectl api-resources --namespaced=true --verbs=delete -o name | tr "\n" "," | sed -e 's/,$//')" --all
    
    0 讨论(0)
  • 2020-12-23 17:00

    Kubernetes Namespace would be the perfect options for you. You can easily create namespace resource.

    kubectl create -f custom-namespace.yaml

    $  apiVersion: v1
        kind: Namespace
        metadata:
          name:custom-namespace
    

    Now you can deploy all of the other resources(Deployment,ReplicaSet,Services etc) in that custom namespaces.

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

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

    For example:

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

    you can list all the pods in custom-namespace.

    kubectl get pods --namespace=custom-namespace

    0 讨论(0)
提交回复
热议问题