kubernete强力删除namespace

问题描述

在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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值