在集群中使用级联删除

本页面向您展示如何在集群进行级联删除时指定要使用的垃圾回收类型。

开始之前 (Before you begin)

您需要拥有一个 Kubernetes 集群,并且 kubectl 命令行工具已被配置为与您的集群通信。建议在至少有两个非控制平面主机的节点组成的集群上运行此教程。如果您还没有集群,可以使用 minikube 创建一个,或者您可以使用以下 Kubernetes 实践环境之一:

您还需要创建一个示例 Deployment 来尝试不同类型的级联删除。每种类型都需要重新创建 Deployment。

检查 Pod 上的 owner references (Check owner references on your pods)

检查 ownerReferences 字段是否存在于您的 Pod 上 (Check that the ownerReferences field is present on your pods)

kubectl get pods -l app=nginx --output=yaml

输出中有一个 ownerReferences 字段,类似这样: (The output has an ownerReferences field similar to this)

apiVersion: v1
    ...
    ownerReferences:
    - apiVersion: apps/v1
      blockOwnerDeletion: true
      controller: true
      kind: ReplicaSet
      name: nginx-deployment-6b474476c4
      uid: 4fdcd81c-bd5d-41f7-97af-3a3b759af9a7
    ...

使用前台级联删除 (Use foreground cascading deletion)

默认情况下,Kubernetes 使用后台级联删除来删除对象的附属资源。您可以使用 kubectl 或 Kubernetes API 切换到前台级联删除,具体取决于您的集群运行的 Kubernetes 版本。

要检查版本,请输入 kubectl version。(To check the version, enter kubectl version.)

您可以使用 kubectl 或 Kubernetes API 使用前台级联删除来删除对象。(You can delete objects using foreground cascading deletion using kubectl or the Kubernetes API.)

使用 kubectl (Using kubectl)

运行以下命令: (Run the following command)

kubectl delete deployment nginx-deployment --cascade=foreground

使用 Kubernetes API (Using the Kubernetes API)

  1. 启动一个本地代理会话 (Start a local proxy session)

    kubectl proxy --port=8080
    
  2. 使用 curl 触发删除 (Use curl to trigger deletion)

    curl -X DELETE localhost:8080/apis/apps/v1/namespaces/default/deployments/nginx-deployment \
        -d '{"kind":"DeleteOptions","apiVersion":"v1","propagationPolicy":"Foreground"}' \
        -H "Content-Type: application/json"
    

    输出包含一个 foregroundDeletion Finalizer,类似这样: (The output contains a foregroundDeletion finalizer like this)

    "kind": "Deployment",
    "apiVersion": "apps/v1",
    "metadata": {
        "name": "nginx-deployment",
        "namespace": "default",
        "uid": "d1ce1b02-cae8-4288-8a53-30e84d8fa505",
        "resourceVersion": "1363097",
        "creationTimestamp": "2021-07-08T20:24:37Z",
        "deletionTimestamp": "2021-07-08T20:27:39Z",
        "finalizers": [
          "foregroundDeletion"
        ]
        ...
    

使用后台级联删除 (Use background cascading deletion)

  1. 创建一个示例 Deployment (Create a sample Deployment).
  2. 使用 kubectl 或 Kubernetes API 删除 Deployment,具体取决于您的集群运行的 Kubernetes 版本。(Use either kubectl or the Kubernetes API to delete the Deployment, depending on the Kubernetes version your cluster runs.)

    要检查版本,请输入 kubectl version。(To check the version, enter kubectl version.)

您可以使用 kubectl 或 Kubernetes API 使用后台级联删除来删除对象。(You can delete objects using background cascading deletion using kubectl or the Kubernetes API.)

Kubernetes 默认使用后台级联删除,即使您运行以下命令时没有指定 --cascade 标志或 propagationPolicy 参数,也会这样做。(Kubernetes uses background cascading deletion by default, and does so even if you run the following commands without the --cascade flag or the propagationPolicy argument.)

使用 kubectl (Using kubectl)

运行以下命令: (Run the following command)

kubectl delete deployment nginx-deployment --cascade=background

使用 Kubernetes API (Using the Kubernetes API)

  1. 启动一个本地代理会话 (Start a local proxy session)

    kubectl proxy --port=8080
    
  2. 使用 curl 触发删除 (Use curl to trigger deletion)

    curl -X DELETE localhost:8080/apis/apps/v1/namespaces/default/deployments/nginx-deployment \
        -d '{"kind":"DeleteOptions","apiVersion":"v1","propagationPolicy":"Background"}' \
        -H "Content-Type: application/json"
    

    输出类似这样: (The output is similar to this)

    "kind": "Status",
    "apiVersion": "v1",
    ...
    "status": "Success",
    "details": {
        "name": "nginx-deployment",
        "group": "apps",
        "kind": "deployments",
        "uid": "cc9eefb9-2d49-4445-b1c1-d261c9396456"
    }
    

删除属主对象并孤立附属资源 (Delete owner objects and orphan dependents)

默认情况下,当您指示 Kubernetes 删除一个对象时,控制器也会删除附属对象。您可以使用 kubectl 或 Kubernetes API 使得 Kubernetes 孤立这些附属资源,具体取决于您的集群运行的 Kubernetes 版本。

要检查版本,请输入 kubectl version。(To check the version, enter kubectl version.)

使用 kubectl (Using kubectl)

运行以下命令: (Run the following command)

kubectl delete deployment nginx-deployment --cascade=orphan

使用 Kubernetes API (Using the Kubernetes API)

  1. 启动一个本地代理会话 (Start a local proxy session)

    kubectl proxy --port=8080
    
  2. 使用 curl 触发删除 (Use curl to trigger deletion)

    curl -X DELETE localhost:8080/apis/apps/v1/namespaces/default/deployments/nginx-deployment \
        -d '{"kind":"DeleteOptions","apiVersion":"v1","propagationPolicy":"Orphan"}' \
        -H "Content-Type: application/json"
    

    输出包含 orphanfinalizers 字段中,类似这样: (The output contains orphan in the finalizers field, similar to this)

    "kind": "Deployment",
    "apiVersion": "apps/v1",
    "namespace": "default",
    "uid": "6f577034-42a0-479d-be21-78018c466f1f",
    "creationTimestamp": "2021-07-09T16:46:37Z",
    "deletionTimestamp": "2021-07-09T16:47:08Z",
    "deletionGracePeriodSeconds": 0,
    "finalizers": [
      "orphan"
    ],
    ...
    

您可以检查由 Deployment 管理的 Pod 是否仍在运行 (You can check that the Pods managed by the Deployment are still running)

kubectl get pods -l app=nginx

接下来 (What's next)

最后修改于 2023 年 1 月 11 日太平洋标准时间 11:12 AM:更新 /tasks/administer-cluster 部分的页面权重 (b1202c78ff) (Last modified January 11, 2023 at 11:12 AM PST: Update page weights in /tasks/administer-cluster section (b1202c78ff))