k8s 删除异常对象(pod、namespace、pv、pvc)

1. 删除异常pod

kubectl delete pod xxxx --force --grace-period=0

2. 删除异常namespace

说明:删除Terminatin状态的namespace

- 现象

  • 如下,iot-01 和iot-02两个namespace删除后,一直处于Terminating状态
[root@devops-master ~]# kubectl get namespaces
NAME                        STATUS        AGE
cattle-prometheus-p-5pn7p   Active        97m
cattle-system               Active        252d
default                     Active        252d
iot-01                      Terminating   249d
iot-02                      Terminating   228d
iot-test-01                 Active        6d19h
iot-test-03                 Active        20h
  • 查看namespace下没有任何对象了
[root@devops-master ~]# kubectl get all -n iot-01
No resources found.

- 解决方法一(edit对象)

  • 进入编译
[root@devops-master ~]# kubectl edit namespaces iot-01
  • 将finalizers注释掉,并保存修改
  creationTimestamp: "2019-08-15T05:38:52Z"
  deletionGracePeriodSeconds: 0
  deletionTimestamp: "2020-04-20T05:52:38Z"
#  finalizers:
#  - finalizers.kubesphere.io/namespaces
  labels:
    cattle.io/creator: norman
    field.cattle.io/projectId: p-cmbs8
  name: iot-01
  resourceVersion: "53801851"
  selfLink: /api/v1/namespaces/iot-01
  uid: 3061673c-dcd9-4442-80ae-0cd438d7df86
spec: {}
status:
  phase: Terminating
  • 验证
    iot-01 被删除了
[root@devops-master ~]# kubectl get namespaces
NAME                        STATUS        AGE
cattle-prometheus-p-5pn7p   Active        97m
cattle-system               Active        252d
default                     Active        252d
iot-02                      Terminating   228d
iot-test-01                 Active        6d19h
iot-test-03                 Active        20h

- 解决方法二 (通过API接口)

如果在方法一中,我们没有找到 finalizers:这两行,我们使用一下方法解决:

本示例,我要删除的是park-stores这个namespace,但是状态一直是Terminating,切用方法一没有 finalizers:这两行

  • 导出 namespace 配置
kubectl get namespace park-stores -o json > park-stores.json
  • 修改导出的配置
{
    "apiVersion": "v1",
    "kind": "Namespace",
    "metadata": {
        "annotations": {
            "cattle.io/status": "{\"Conditions\":[{\"Type\":\"ResourceQuotaInit\",\"Status\":\"True\",\"Message\":\"\",\"LastUpdateTime\":\"2021-04-14T09:49:08Z\"},{\"Type\":\"InitialRolesPopulated\",\"Status\":\"True\",\"Message\":\"\",\"LastUpdateTime\":\"2021-04-14T09:49:08Z\"}]}",
            "field.cattle.io/projectId": "c-mx7mw:p-bgsbc",
            "lifecycle.cattle.io/create.namespace-auth": "true"
        },
        "creationTimestamp": "2021-04-14T09:49:28Z",
        "deletionTimestamp": "2022-11-14T07:13:27Z",
        "labels": {
            "field.cattle.io/projectId": "p-bgsbc"
        },
        "name": "park-stores",
        "resourceVersion": "330626203",
        "selfLink": "/api/v1/namespaces/park-stores",
        "uid": "deeba1a0-772f-4aee-a793-bcd1b00610fa"
    },
    "spec": {
        "finalizers": [
            "kubernetes"
        ]
    },
    "status": {
        "phase": "Terminating"
    }
}

删掉以下一段

    "spec": {
        "finalizers": [
            "kubernetes"
        ]
    },
  • 开一个api的临时端口

我们新打开一个终端,输入如下命令:

[root@DoM01 ~]#  kubectl proxy
Starting to serve on 127.0.0.1:8001

这个接口只允许本地访问,但好处是不用认证。因为这是前台执行的程序,一会儿删除之后Ctrl +c 结束即可。

  • 通过这个api接口删除
curl -k -H "Content-Type: application/json" -X PUT --data-binary @park-stores.json http://127.0.0.1:8001/api/v1/namespaces/park-stores/finalize

3. 删除异常PV PVC

- 删除异常 pv

kubectl patch pv PV_NAME -p '{"metadata":{"finalizers":null}}'

- 删除pvc

kubectl patch pvc PVC_NAME -p '{"metadata":{"finalizers":null}}'

在这里插入图片描述

posted on 2021-12-14 20:04  运维开发玄德公  阅读(53)  评论(0编辑  收藏  举报  来源

导航