k8s刪除Terminating狀態的命名空間

背景: 咱們都知道在k8s中namespace有兩種常見的狀態,即Active和Terminating狀態,其中後者通常會比較少見,只有當對應的命名空間下還存在運行的資源,可是該命名空間被刪除時纔會出現所謂的terminating狀態,這種狀況下只要等待k8s自己將命名空間下的資源回收後,該命名空間將會被系統自動刪除。可是今天遇到命名空間下已沒相關資源,但依然沒法刪除terminating狀態的命名空間的狀況,特此記錄一下.json

查看命名空間詳情api

$ kubectl  get ns  | grep rdb
rdbms                  Terminating   6d21h

$ kubectl  get ns rdbms -o yaml
apiVersion: v1
kind: Namespace
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","kind":"Namespace","metadata":{"annotations":{},"name":"rdbms"}}
  creationTimestamp: "2019-10-14T12:17:44Z"
  deletionTimestamp: "2019-10-14T12:30:27Z"
  name: rdbms
  resourceVersion: "8844754"
  selfLink: /api/v1/namespaces/rdbms
  uid: 29067ddf-56d7-4cce-afa3-1fbdbb221ab1
spec:
  finalizers:
  - kubernetes
status:
  phase: Terminating

查看該命名空間下的資源app

# 查看k8s集羣中能夠使用命名空間隔離的資源
$ kubectl api-resources -o name --verbs=list --namespaced | xargs -n 1 kubectl get --show-kind --ignore-not-found -n rdbms

# 發現rdbms命名空間下並沒有資源佔用

嘗試對命名空間進行刪除curl

# 直接刪除命名空間rdbms
## 提示刪除操做未能完成,說系統會在肯定沒用沒用資源後將會被自動刪除
$ kubectl  delete ns rdbms
Error from server (Conflict): Operation cannot be fulfilled on namespaces "rdbms": The system is ensuring all content is removed from this namespace.  Upon completion, this namespace will automatically be purged by the system.


# 使用強制刪除(依然沒法刪除該命名空間)
$ kubectl  delete ns rdbms --force --grace-period=0
warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.
Error from server (Conflict): Operation cannot be fulfilled on namespaces "rdbms": The system is ensuring all content is removed from this namespace.  Upon completion, this namespace will automatically be purged by the system.

使用原生接口刪除ui

# 獲取namespace的詳情信息
$ kubectl  get ns rdbms  -o json > rdbms.json

# 查看napespace定義的json配置
## 刪除掉spec部分便可
$ cat rdbms.json
{
    "apiVersion": "v1",
    "kind": "Namespace",
    "metadata": {
        "annotations": {
            "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"name\":\"rdbms\"}}\n"
        },
        "creationTimestamp": "2019-10-14T12:17:44Z",
        "deletionTimestamp": "2019-10-14T12:30:27Z",
        "name": "rdbms",
        "resourceVersion": "8844754",
        "selfLink": "/api/v1/namespaces/rdbms",
        "uid": "29067ddf-56d7-4cce-afa3-1fbdbb221ab1"
    },
    "spec": {
        "finalizers": [
            "kubernetes"
        ]
    },
    "status": {
        "phase": "Terminating"
    }
}

# 使用http接口進行刪除
$ curl -k -H "Content-Type:application/json" -X PUT --data-binary @rdbms.json https://x.x.x.x:6443/api/v1/namespaces/rdbms/finalize
{
  "kind": "Namespace",
  "apiVersion": "v1",
  "metadata": {
    "name": "rdbms",
    "selfLink": "/api/v1/namespaces/rdbms/finalize",
    "uid": "29067ddf-56d7-4cce-afa3-1fbdbb221ab1",
    "resourceVersion": "8844754",
    "creationTimestamp": "2019-10-14T12:17:44Z",
    "deletionTimestamp": "2019-10-14T12:30:27Z",
    "annotations": {
      "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"name\":\"rdbms\"}}\n"
    }
  },
  "spec": {

  },
  "status": {
    "phase": "Terminating"
  }

# 再次查看namespace發現已經被刪除了
$ kubectl  get ns  | grep rdb

歡迎關注個人公衆號: BGBiao,一塊兒進步~ this

相關文章
相關標籤/搜索