K8S中删除Terminating状态的命名空间

发布于:2024-05-10 ⋅ 阅读:(31) ⋅ 点赞:(0)

1.查看当前命名空间

[root@k8smaster1 home]# kubectl get ns
NAME              STATUS        AGE
app               Active        16d
default           Active        17d
hd                Active        16d
ingress-nginx     Terminating   5m8s
kube-node-lease   Active        17d
kube-public       Active        17d
kube-system       Active        17d
kuboard           Active        16d
zhangq            Active        16d

由上面可以看出,咱们删除ingress-nginx服务没删除成功,ingress-nginx命名空间的状态是Terminating 的状态导致删除命令卡主

2.使用强制删除的命名试验能否删除成功

[root@k8smaster1 home]# kubectl delete ns ingress-nginx --force --grace-period=0
Warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.
namespace "ingress-nginx" force deleted
#删除命名一直卡住,执行无效

3.Terminating 状态的命名空间一般使用强制删除的命名是不可以的,咱们使用以下方式删除Terminating状态的命名空间

获取namespace的配置文件,格式为json

[root@k8smaster1 home]# [root@k8smaster1 home]# kubectl get namespace ingress-nginx -o json > tmp.json

修改tmp.yaml中的配置

[root@k8smaster1 home]# vi tmp.json
"spec": {
        "finalizers": [
            "kubernetes"
        ]
    },
#修改为
"spec": {
    },

开启apiserver的代理

[root@k8smaster1 ingress]#  kubectl proxy --port=8088
Starting to serve on 127.0.0.1:8088

调用api开始删除,命名如下

[root@k8smaster1 home]# curl -k -H "Content-Type:application/json" -X PUT --data-binary @tmp.json http://127.0.0.1:8088/api/v1/namespaces/ingress-nginx/finalize

#http://127.0.0.1:8088/api/v1/namespaces/《要删除的命名空间》/finalize    #这里的namespaces后面要写你要删除的命名空间

检查是否删除成功

[root@k8smaster1 home]# kubectl get ns
NAME              STATUS   AGE
app               Active   16d
default           Active   17d
hd                Active   16d
kube-node-lease   Active   17d
kube-public       Active   17d
kube-system       Active   17d
kuboard           Active   16d
zhangq            Active   16d

以上可以看出删除成功