首页 > 解决方案 > 在 Kubernetes 中什么是 deletecollection?

问题描述

列出 K8s 中的所有 API 资源时,您会得到:

$ kubectl api-resources -owide
NAME                              SHORTNAMES   APIGROUP                       NAMESPACED   KIND                             VERBS
bindings                                                                      true         Binding                          [create]
componentstatuses                 cs                                          false        ComponentStatus                  [get list]
configmaps                        cm                                          true         ConfigMap                        [create delete deletecollection get list patch update watch]
endpoints                         ep                                          true         Endpoints                        [create delete deletecollection get list patch update watch]
events                            ev                                          true         Event                            [create delete deletecollection get list patch update watch]
limitranges                       limits                                      true         LimitRange                       [create delete deletecollection get list patch update watch]
namespaces                        ns                                          false        Namespace                        [create delete get list patch update watch]
nodes                             no                                          false        Node                             [create delete deletecollection get list patch update watch]
persistentvolumeclaims            pvc                                         true         PersistentVolumeClaim            [create delete deletecollection get list patch update watch]
persistentvolumes                 pv                                          false        PersistentVolume                 [create delete deletecollection get list patch update watch]
pods                              po                                          true         Pod                              [create delete deletecollection get list patch update watch]
podtemplates                                                                  true         PodTemplate                      [create delete deletecollection get list patch update watch]
replicationcontrollers            rc                                          true         ReplicationController            [create delete deletecollection get list patch update watch]
resourcequotas                    quota                                       true         ResourceQuota                    [create delete deletecollection get list patch update watch]
secrets                                                                       true         Secret                           [create delete deletecollection get list patch update watch]
serviceaccounts                   sa                                          true         ServiceAccount                   [create delete deletecollection get list patch update watch]
services                          svc                                         true         Service                          [create delete get list patch update watch]
mutatingwebhookconfigurations                  admissionregistration.k8s.io   false        MutatingWebhookConfiguration     [create delete deletecollection get list patch update watch]
... etc ...

许多列出deletecollection听起来有用的动词,但我无法运行它,例如

$ kubectl deletecollection
Error: unknown command "deletecollection" for "kubectl"
Run 'kubectl --help' for usage.
unknown command "deletecollection" for "kubectl"

我也无法在文档中找到它,除非它出现在上面的 api-resources 输出中或作为动词提及。

有没有办法删除收藏?

听起来它会比我通常最终做的 grep/awk/xargs 序列更好,如果它确实做了我认为它应该做的事情。即删除某种类型的所有pod。

标签: kuberneteskubectl

解决方案


动词是delete指删除单个资源,例如单个 Pod。动词是指同时deletecollection删除多个资源,例如使用标签或字段选择器的多个 Pod 或命名空间中的所有 Pod。

从 API 文档中举一些例子:

  1. 删除单个 PodDELETE /api/v1/namespaces/{namespace}/pods/{name}
  2. 删除多个 Pod(或,deletecollection):
    1. 命名空间中的所有 podDELETE /api/v1/namespaces/{namespace}/pods
    2. 命名空间中与给定标签选择器匹配的所有 pod:DELETE /api/v1/namespaces/{namespace}/pods?labelSelector=someLabel%3dsomeValue

关于 kubectl:您不能deletecollection使用kubectl.

相反,kubectl将自行推断是否使用deletedeletecollection取决于您如何调用kubectl delete. 当删除单个源(kubectl delete pod $POD_NAME)时,kubectl 将使用delete调用,当使用标签选择器或简单地删除所有 Pod(kubectl delete pods -l $LABEL=$VALUEkubectl delete pods --all)时,它将使用deletecollection动词。


推荐阅读