kubernetes 强制删除 Terminating 的ns

本文档详细介绍了如何强制删除在Kubernetes中处于Terminating状态的命名空间,包括通过kubectl检查状态、保存命名空间描述文件、本地启动kube-proxy、执行删除操作以及在Rancher中处理Terminaling的命名空间。
摘要由CSDN通过智能技术生成

1. 查看kubectl get ns 查看处于Terminating的ns

[root@m01 ~]# kubectl get ns istio-system
NAME           STATUS        AGE
istio-system   Terminating   41h

2. 将处理 Terminating的ns的描述文件保存下来

[root@m01 ~]# kubectl get ns istio-system -o json > istio-system.json
[root@m01 ~]# cat istio-system.json
{
    "apiVersion": "v1",
    "kind": "Namespace",
    "metadata": {
        "creationTimestamp": "2021-03-23T09:14:05Z",
        "deletionTimestamp": "2021-03-25T02:23:32Z",
        "labels": {
            "istio-injection": "disabled"
        },
        "managedFields": [
            {
                "apiVersion": "v1",
                "fieldsType": "FieldsV1",
                "fieldsV1": {
                    "f:metadata": {
                        "f:labels": {
                            ".": {},
                            "f:istio-injection": {}
                        }
                    },
                    "f:status": {
                        "f:phase": {}
                    }
                },
                "manager": "istioctl",
                "operation": "Update",
                "time": "2021-03-23T09:14:05Z"
            },
            {
                "apiVersion": "v1",
                "fieldsType": "FieldsV1",
                "fieldsV1": {
                    "f:status": {
                        "f:conditions": {
                            ".": {},
                            "k:{\"type\":\"NamespaceContentRemaining\"}": {
                                ".": {},
                                "f:lastTransitionTime": {},
                                "f:message": {},
                                "f:reason": {},
                                "f:status": {},
                                "f:type": {}
                            },
                            "k:{\"type\":\"NamespaceDeletionContentFailure\"}": {
                                ".": {},
                                "f:lastTransitionTime": {},
                                "f:message": {},
                                "f:reason": {},
                                "f:status": {},
                                "f:type": {}
                            },
                            "k:{\"type\":\"NamespaceDeletionDiscoveryFailure\"}": {
                                ".": {},
                                "f:lastTransitionTime": {},
                                "f:message": {},
                                "f:reason": {},
                                "f:status": {},
                                "f:type": {}
                            },
                            "k:{\"type\":\"NamespaceDeletionGroupVersionParsingFailure\"}": {
                                ".": {},
                                "f:lastTransitionTime": {},
                                "f:message": {},
                                "f:reason": {},
                                "f:status": {},
                                "f:type": {}
                            },
                            "k:{\"type\":\"NamespaceFinalizersRemaining\"}": {
                                ".": {},
                                "f:lastTransitionTime": {},
                                "f:message": {},
                                "f:reason": {},
                                "f:status": {},
                                "f:type": {}
                            }
                        }
                    }
                },
                "manager": "kube-controller-manager",
                "operation": "Update",
                "time": "2021-03-25T02:23:38Z"
            }
        ],
        "name": "istio-system",
        "resourceVersion": "2372643",
        "selfLink": "/api/v1/namespaces/istio-system",
        "uid": "f33afeaf-bf16-4840-a47d-663d7ba7c880"
    },
    "spec": {
        "finalizers": [
            "kubernetes"
        ]
    },
    "status": {
        "conditions": [
            {
                "lastTransitionTime": "2021-03-25T02:23:37Z",
                "message": "Discovery failed for some groups, 1 failing: unable to retrieve the complete list of server APIs: metrics.k8s.io/v1beta1: the server is currently unable to handle the request",
                "reason": "DiscoveryFailed",
                "status": "True",
                "type": "NamespaceDeletionDiscoveryFailure"
            },
            {
                "lastTransitionTime": "2021-03-25T02:23:38Z",
                "message": "All legacy kube types successfully parsed",
                "reason": "ParsedGroupVersions",
                "status": "False",
                "type": "NamespaceDeletionGroupVersionParsingFailure"
            },
            {
                "lastTransitionTime": "2021-03-25T02:23:38Z",
                "message": "All content successfully deleted, may be waiting on finalization",
                "reason": "ContentDeleted",
                "status": "False",
                "type": "NamespaceDeletionContentFailure"
            },
            {
                "lastTransitionTime": "2021-03-25T02:24:15Z",
                "message": "All content successfully removed",
                "reason": "ContentRemoved",
                "status": "False",
                "type": "NamespaceContentRemaining"
            },
            {
                "lastTransitionTime": "2021-03-25T02:23:38Z",
                "message": "All content-preserving finalizers finished",
                "reason": "ContentHasNoFinalizers",
                "status": "False",
                "type": "NamespaceFinalizersRemaining"
            }
        ],
        "phase": "Terminating"
    }
}

删除此部份

,
    "spec": {
        "finalizers": [
            "kubernetes"
        ]
    },
    "status": {
        "conditions": [
            {
                "lastTransitionTime": "2021-03-25T02:23:37Z",
                "message": "Discovery failed for some groups, 1 failing: unable to retrieve the complete list of server APIs: metrics.k8s.io/v1beta1: the server is currently unable to handle the request",
                "reason": "DiscoveryFailed",
                "status": "True",
                "type": "NamespaceDeletionDiscoveryFailure"
            },
            {
                "lastTransitionTime": "2021-03-25T02:23:38Z",
                "message": "All legacy kube types successfully parsed",
                "reason": "ParsedGroupVersions",
                "status": "False",
                "type": "NamespaceDeletionGroupVersionParsingFailure"
            },
            {
                "lastTransitionTime": "2021-03-25T02:23:38Z",
                "message": "All content successfully deleted, may be waiting on finalization",
                "reason": "ContentDeleted",
                "status": "False",
                "type": "NamespaceDeletionContentFailure"
            },
            {
                "lastTransitionTime": "2021-03-25T02:24:15Z",
                "message": "All content successfully removed",
                "reason": "ContentRemoved",
                "status": "False",
                "type": "NamespaceContentRemaining"
            },
            {
                "lastTransitionTime": "2021-03-25T02:23:38Z",
                "message": "All content-preserving finalizers finished",
                "reason": "ContentHasNoFinalizers",
                "status": "False",
                "type": "NamespaceFinalizersRemaining"
            }
        ],
        "phase": "Terminating"
    }

3.本地启动kube-proxy

kubectl proxy
kubectl proxy --port=8001

4.新开窗口执行删除操作

curl -k -H "Content-Type: application/json" -X PUT --data-binary @istio-system.json http://127.0.0.1:8001/api/v1/namespaces/istio-system/finalize

5.删除namespace

# 正常删除
kubectl delete namespace istio-system
# 强制删除
kubectl delete namespace istio-system --force --grace-period=0

rancher删除Terminaling的命名空间

首先要安装jq:

wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -ivh epel-release-latest-7.noarch.rpm
yum install jq

设置要删除命名空间的名称:

NAMESPACE=test

接着执行如下操作:

# 获取RANCHER_SERVER_URL地址
 RANCHER_SERVER_URL=$( kubectl config view -o json|jq -r .clusters[0].cluster.server )
# 获取config中的token
CLUSTER_TOKEN=$( kubectl config view -o json|jq -r .users[0].user.token )
# kubectl get ns ${NAMESPACE} -ojson| \
jq 'del(.spec.finalizers[])'| \
jq 'del(.metadata.finalizers)' > ${NAMESPACE}.json
curl -k -H "Content-Type: application/json" \
-H "Authorization: Bearer ${CLUSTER_TOKEN}" \
-X PUT --data-binary @${NAMESPACE}.json \
${RANCHER_SERVER_URL}/api/v1/namespaces/${NAMESPACE}/finalize
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值