kubenetes如何删除有问题的namespaces

avatar 2021年11月11日18:01:51 评论 395 次浏览

有时候遇到namespaces中的pod都已经删除了,在删除namespaces的时候总是停留在terminating的状态,使用kubectl delete namespaces都是无效的,这里介绍两个删除的方法,第一种需要安装一下jq,如果不能直接使用yum安装的话,需要更新一下EPEL。

[root@www.wulaoer.org ~]# wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
[root@www.wulaoer.org ~]# rpm -ivh epel-release-latest-7.noarch.rpm
[root@www.wulaoer.org ~]# yum install -y jq

第一种方式

安装jq之后在可以连接kubenetes的节点中执行kubectl proxy,然后重新开一个窗口,继续执行下面的命令,把istio-system替换成自己的namespaces即可

[root@www.wulaoer.org ~]# kubectl proxy
Starting to serve on 127.0.0.1:8001

[root@www.wulaoer.org ~]# kubectl get namespace istio-system -o json | jq 'del(.spec.finalizers[] | select("kubernetes"))' | curl -s -k -H "Content-Type: application/json" -X PUT -o /dev/null --data-binary @- http://localhost:8001/api/v1/namespaces/istio-system/finalize

第二种方式

需要先把namespacs的yaml文件导出来,然后,修改一下yaml文件内容,必须把spec中的内容去掉即可

[root@www.wulaoer.org ~]# kubectl get ns istio-operator -o json > delete-ns.json

{
    "apiVersion": "v1",
    "kind": "Namespace",
    "metadata": {
        "annotations": {
            "cattle.io/appIds": "",
            "cattle.io/status": "{\"Conditions\":[{\"Type\":\"InitialRolesPopulated\",\"Status\":\"True\",\"Message\":\"\",\"LastUpdateTime\":\"2021-10-20T07:32:59Z\"},{\"Type\":\"ResourceQuotaInit\",\"Status\":\"True\",\"Message\":\"\",\"LastUpdateTime\":\"2021-10-20T07:32:58Z\"}]}",
            "field.cattle.io/creatorId": "user-zgsz2",
            "field.cattle.io/projectId": "c-chd5q:p-xz229",
            "lifecycle.cattle.io/create.namespace-auth": "true"
        },
        "creationTimestamp": "2021-10-20T07:32:57Z",
        "deletionTimestamp": "2021-10-20T07:47:22Z",
        "labels": {
            "cattle.io/creator": "norman",
            "field.cattle.io/projectId": "p-xz229"
        },
        "managedFields": [
            {
         ..........................
    },
    "spec": {
        "finalizers": [
            "kubernetes"
        ]
    },
    "status": {
        "conditions": [ //里面的都删除即可

        ],
        "phase": "Terminating"
    }
}

[root@www.wulaoer.org ~]# kubectl proxy
Starting to serve on 127.0.0.1:8001
[root@www.wulaoer.org ~]# curl -k -H "Content-Type: application/json" -X PUT --data-binary @delete-ns.json http://127.0.0.1:8081/api/v1/namespaces/istio-operator/finalize

最后一步也要重新开一个新的窗口,因为kubectl proxy就是通过api监听本地的8001端口,然后curl通过本地的8001端口向api发送删除的请求。好了,没有了在看一些其他的吧。。。。

avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: