欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

K8s删除terminating的namespace

程序员文章站 2024-03-11 16:58:13
...

K3s/k8s 删除terminating状态的Namespace

说明: 删除已经停止的 namespace 和 删除 普通的 pod 、deployment、svc等不同。不能使用 --force。具体步骤如下:

1、导出描述的json文件

kubectl get ns longhorn-system -o json > longhorn-system.json

a)打开导出的json文件 longhorn-system.json

{
    "apiVersion": "v1",
    "kind": "Namespace",
    "metadata": {
        "annotations": {
            "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"name\":\"longhorn-system\"}}\n"
        },
        "creationTimestamp": "2020-06-15T03:38:43Z",
        "deletionTimestamp": "2020-06-15T13:50:41Z",
        "managedFields": [
            ...........此处省略..........
        ],
        "name": "longhorn-system",
        "resourceVersion": "78488",
        "selfLink": "/api/v1/namespaces/longhorn-system",
        "uid": "219da01a-019b-48df-96bc-2ab227db0b40"
    },
    "spec": {
        "finalizers": [
			"kubernetes"  // 删除 
        ]
    },
    "status": {
        "conditions": [
		......此处省略.....
        ],
        "phase": "Terminating"
    }
}

b) 删除 spec 字段中的 “kubernetes”,改为

{
    "apiVersion": "v1",
    "kind": "Namespace",
    "metadata": {
        "annotations": {
            "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"name\":\"longhorn-system\"}}\n"
        },
        "creationTimestamp": "2020-06-15T03:38:43Z",
        "deletionTimestamp": "2020-06-15T13:50:41Z",
        "managedFields": [
            ...........此处省略..........
        ],
        "name": "longhorn-system",
        "resourceVersion": "78488",
        "selfLink": "/api/v1/namespaces/longhorn-system",
        "uid": "219da01a-019b-48df-96bc-2ab227db0b40"
    },
    "spec": {
        "finalizers": [

        ]
    },
    "status": {
        "conditions": [
		......此处省略.....
        ],
        "phase": "Terminating"
    }
}

2、执行清除命令

kubectl replace --raw "/api/v1/namespaces/$[ longhorn-system ]/finalize" -f ./longhorn-system.json

其中 $[ longhorn-system ] 替换为 对应的 名称空间。比如 “/api/v1/namespaces/test-namespace/finalize”