问题描述

在k8s中,有些namespace创建之后,觉得不需要了,想删除掉,但怎么都删不掉。类似于下图,即使用delete --force命令也删不掉,这些ns最终还是现实为terminating状态。
在这里插入图片描述

解决办法

  1. 将该namespace导出为json格式。

    kubectl get ns redis -o json > tmp.json
    
  2. 编辑该json,去除掉spec部分。

vim tmp.json
# 原始数据
{
    "apiVersion": "v1",
    "kind": "Namespace",
    "metadata": {
        "annotations": {
            "cattle.io/status": "{\"Conditions\":[{\"Type\":\"InitialRolesPopulated\",\"Status\":\"True\",\"Message\":\"\",\"LastUpdateTime\":\"2020-05-15T03:14:31Z\"},{\"Type\":\"ResourceQuotaInit\",\"Status\":\"True\",\"Message\":\"\",\"LastUpdateTime\":\"2020-05-15T03:14:30Z\"}]}",
            "field.cattle.io/creatorId": "user-gpdkw",
            "field.cattle.io/projectId": "c-bz2b7:p-w5xc9",
            "lifecycle.cattle.io/create.namespace-auth": "true"
        },
        "creationTimestamp": "2020-05-15T03:05:12Z",
        "deletionTimestamp": "2020-05-15T03:13:31Z",
        "labels": {
            "cattle.io/creator": "norman",
            "field.cattle.io/projectId": "p-w5xc9"
        },
        "name": "redis",
        "resourceVersion": "29284653",
        "selfLink": "/api/v1/namespaces/redis",
        "uid": "e4763eae-9658-11ea-b716-000c291a5adf"
    },
    #删除掉spec包裹的部分
    "spec": {
        "finalizers": [
            "kubernetes"
        ]
    },
    "status": {
        "phase": "Terminating"
    }
}
# 修改后的数据

{
    "apiVersion": "v1",
    "kind": "Namespace",
    "metadata": {
        "annotations": {
            "cattle.io/status": "{\"Conditions\":[{\"Type\":\"InitialRolesPopulated\",\"Status\":\"True\",\"Message\":\"\",\"LastUpdateTime\":\"2020-05-15T03:14:31Z\"},{\"Type\":\"ResourceQuotaInit\",\"Status\":\"True\",\"Message\":\"\",\"LastUpdateTime\":\"2020-05-15T03:14:30Z\"}]}",
            "field.cattle.io/creatorId": "user-gpdkw",
            "field.cattle.io/projectId": "c-bz2b7:p-w5xc9",
            "lifecycle.cattle.io/create.namespace-auth": "true"
        },
        "creationTimestamp": "2020-05-15T03:05:12Z",
        "deletionTimestamp": "2020-05-15T03:13:31Z",
        "labels": {
            "cattle.io/creator": "norman",
            "field.cattle.io/projectId": "p-w5xc9"
        },
        "name": "redis",
        "resourceVersion": "29284653",
        "selfLink": "/api/v1/namespaces/redis",
        "uid": "e4763eae-9658-11ea-b716-000c291a5adf"
    },
    "spec": {
    },
    "status": {
        "phase": "Terminating"
    }
}


  1. 将该修改应用到apiserver中。
# 打开一个终端,开启proxy。不然要经过API server的验证。
kubectl proxy
# 向开放的端口提交修改后的json。
curl -k -H "Content-Type: application/json" -X PUT --data-binary @tmp.json 127.0.0.1:8001/api/v1/namespaces/redis/finalize
# 查看是否应用成功
kubectl get ns 
  1. 脚本
NAMESPACE=rook-ceph
kubectl proxy &
kubectl get namespace $NAMESPACE -o json |jq '.spec = {"finalizers":[]}' >temp.json
curl -k -H "Content-Type: application/json" -X PUT --data-binary @temp.json 127.0.0.1:8001/api/v1/namespaces/$NAMESPACE/finalize

强力删除rancher的Namespace需要下面的办法

kubectl patch namespace cattle-system -p '{"metadata":{"finalizers":[]}}' --type='merge' -n cattle-system
kubectl delete namespace cattle-system --grace-period=0 --force
 
kubectl patch namespace cattle-global-data -p '{"metadata":{"finalizers":[]}}' --type='merge' -n cattle-system
kubectl delete namespace cattle-global-data --grace-period=0 --force
 
kubectl patch namespace local -p '{"metadata":{"finalizers":[]}}' --type='merge' -n cattle-system
 
for resource in `kubectl api-resources --verbs=list --namespaced -o name | xargs -n 1 kubectl get -o name -n local`; do kubectl patch $resource -p '{"metadata": {"finalizers": []}}' --type='merge' -n local; done
 
kubectl delete namespace local --grace-period=0 --force
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐