k8s的namespace一直处于terminating的解法

先试了强制替换,无法替换掉,强制删除,也删除不掉namespace

[root@master k8s-study]# vi ns-demo.yaml
[root@master k8s-study]# kubectl create -f ns-demo.yaml
namespace/demo created
[root@master k8s-study]# kubectl get -f ns-demo.yaml
NAME   STATUS   AGE
demo   Active   17s
[root@master k8s-study]# cp ns-demo.yaml ns-demo-v2.yaml
[root@master k8s-study]# vi ns-demo-v2.yaml
[root@master k8s-study]# kubectl replace -f ns-demo-v2.yaml
namespace/demo replaced
[root@master k8s-study]# kubectl get -f ns-demo.yaml
NAME   STATUS   AGE
demo   Active   102s
[root@master k8s-study]# kubectl get -f ns-demo.yaml -o yaml
apiVersion: v1
kind: Namespace
metadata:
  creationTimestamp: "2023-07-26T13:22:49Z"
  labels:
    kubernetes.io/metadata.name: demo
  name: demo
  resourceVersion: "42052444"
  uid: 5ff91497-0bb6-448a-8fb2-0aacb017bacc
spec:
  finalizers:
  - kubernetes
status:
  phase: Active
[root@master k8s-study]# kubectl get -f ns-demo-v2.yaml -o yaml
apiVersion: v1
kind: Namespace
metadata:
  creationTimestamp: "2023-07-26T13:22:49Z"
  labels:
    kubernetes.io/metadata.name: demo
  name: demo
  resourceVersion: "42052444"
  uid: 5ff91497-0bb6-448a-8fb2-0aacb017bacc
spec:
  finalizers:
  - kubernetes
status:
  phase: Active
[root@master k8s-study]# kubectl replace --force -f ns-demo-v2.yaml
namespace "demo" deleted

^C
[root@master k8s-study]# kubectl replace --force=true -f ns-demo-v2.yaml
namespace "demo" deleted
^C
[root@master k8s-study]# kubectl replace --force=true --grace-period=0 -f ns-demo-v2.yaml
namespace "demo" deleted
^C
[root@master k8s-study]# kubectl get ns/demo
NAME   STATUS        AGE
demo   Terminating   10m
[root@master k8s-study]# kubectl get ns/demo -o json
{
    "apiVersion": "v1",
    "kind": "Namespace",
    "metadata": {
        "creationTimestamp": "2023-07-26T13:22:49Z",
        "deletionTimestamp": "2023-07-26T13:25:24Z",
        "labels": {
            "kubernetes.io/metadata.name": "demo"
        },
        "name": "demo",
        "resourceVersion": "42052458",
        "uid": "5ff91497-0bb6-448a-8fb2-0aacb017bacc"
    },
    "spec": {
        "finalizers": [
            "kubernetes"
        ]
    },
    "status": {
        "phase": "Terminating"
    }
}
[root@master k8s-study]# kubectl edit ns/demo -o yaml
apiVersion: v1
kind: Namespace
metadata:
  creationTimestamp: "2023-07-26T13:22:49Z"
  deletionTimestamp: "2023-07-26T13:25:24Z"
  labels:
    kubernetes.io/metadata.name: demo
  name: demo
  resourceVersion: "42052458"
  uid: 5ff91497-0bb6-448a-8fb2-0aacb017bacc
spec:
  finalizers:
  - kubernetes
status:
  phase: Terminating
[root@master k8s-study]# kubectl get ns/demo
NAME   STATUS        AGE
demo   Terminating   11m
[root@master k8s-study]# kubectl delete ns/demo
namespace "demo" deleted
^C
[root@master k8s-study]# kubectl delete --force ns/demo
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.
namespace "demo" force deleted
^C
[root@master k8s-study]# kubectl delete --force --grace-period=0 ns/demo
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.
namespace "demo" force deleted
^C



[root@master k8s-study]# curl -H "Content-Type: application/json" -XPUT -d '{"apiVersion":"v1","kind":"Namespace","metadata":{"name":"demo"},"spec":{"finalizers":[]}}' http://localhost:10249/api/v1/namespaces/delete-me/finalize
404 page not found
[root@master k8s-study]# curl -H "Content-Type: application/json" -XPUT -d '{"apiVersion":"v1","kind":"Namespace","metadata":{"name":"demo"},"spec":{"finalizers":[]}}' http://localhost:31811/api/v1/namespaces/delete-me/finalize
curl: (52) Empty reply from server
[root@master k8s-study]# kubectl get ns/demo -o yaml
apiVersion: v1
kind: Namespace
metadata:
  creationTimestamp: "2023-07-26T13:22:49Z"
  deletionTimestamp: "2023-07-26T13:25:24Z"
  labels:
    kubernetes.io/metadata.name: demo
  name: demo
  resourceVersion: "42052458"
  uid: 5ff91497-0bb6-448a-8fb2-0aacb017bacc
spec:
  finalizers:
  - kubernetes
status:
  phase: Terminating
[root@master k8s-study]# kubectl get ns/demo -o json
{
    "apiVersion": "v1",
    "kind": "Namespace",
    "metadata": {
        "creationTimestamp": "2023-07-26T13:22:49Z",
        "deletionTimestamp": "2023-07-26T13:25:24Z",
        "labels": {
            "kubernetes.io/metadata.name": "demo"
        },
        "name": "demo",
        "resourceVersion": "42052458",
        "uid": "5ff91497-0bb6-448a-8fb2-0aacb017bacc"
    },
    "spec": {
        "finalizers": [
            "kubernetes"
        ]
    },
    "status": {
        "phase": "Terminating"
    }
}
[root@master k8s-study]# curl -H "Content-Type: application/json" -XPUT -d '{"apiVersion":"v1","kind":"Namespace","metadata":{"name":"demo"},"spec":{"finalizers":[]}}' http://localhost:31811/api/v1/namespaces/demo/finalize
curl: (52) Empty reply from server
[root@master k8s-study]# curl -H "Content-Type: application/json" -H “Expect:” -XPUT -d '{"apiVersion":"v1","kind":"Namespace","metadata":{"name":"demo"},"spec":{"finalizers":[]}}' http://localhost:31811/api/v1/namespaces/demo/finalize
curl: (52) Empty reply from server
[root@master k8s-study]# curl -H "Content-Type: application/json" -XPUT -d '{"apiVersion":"v1","kind":"Namespace","metadata":{"name":"demo"},"spec":{"finalizers":[]}}' http://localhost:10249/api/v1/namespaces/demo/finalize
404 page not found

NAMESPACE=demo
kubectl get ns $NAMESPACE -o json | jq '.spec.finalizers=[]' > ns.json
kubectl proxy --port=8899 &
PID=$!
curl -X PUT http://localhost:8899/api/v1/namespaces/$NAMESPACE/finalize -H "Content-Type: application/json" --data-binary @ns.json
kill $PID

这段代码的含义是在 Kubernetes 环境中删除一个命名空间(Namespace)及其关联的资源,包括在命名空间中运行的所有容器、服务、持久卷等。让我们逐行解释代码的作用:

  1. NAMESPACE=demo 这一行指定了要删除的命名空间的名称,代码中将其设置为 "demo"。你可以根据需要将 "demo" 替换为你要删除的特定命名空间的名称。

  2. kubectl get ns $NAMESPACE -o json | jq '.spec.finalizers=[]' > ns.json 这一行代码使用 kubectl 命令获取指定命名空间的详细信息,并将其以 JSON 格式输出。然后通过 jq 工具(一个命令行 JSON 处理工具)将 .spec.finalizers 的值设置为空数组,这样做的目的是将命名空间中的 finalizer 清空。finalizer 是用于在删除命名空间时执行清理操作的机制。

  3. kubectl proxy --port=8899 & 这一行代码启动了一个 kubectl 代理服务器,并监听在本地的 8899 端口上。代理服务器的目的是允许后续的 curl 请求与 Kubernetes API 进行交互,从而绕过直接与集群通信的需求。

  4. PID=$! 这一行代码将 kubectl 代理服务器的进程 ID 存储在变量 PID 中。这样做是为了在后续操作中能够终止该进程,以便清理代理服务器。

  5. curl -X PUT http://localhost:8899/api/v1/namespaces/$NAMESPACE/finalize -H "Content-Type: application/json" --data-binary @ns.json 这一行代码使用 curl 命令向 Kubernetes API 发出 PUT 请求,调用命名空间的 finalize 操作。在这个请求中,通过将之前生成的 ns.json 文件的内容作为请求体,来执行对命名空间的最终清理。由于之前已经将 finalizer 清空,这个操作将立即删除命名空间和其所有资源。

  6. kill $PID 最后,这一行代码终止之前启动的 kubectl 代理服务器,释放占用的端口和资源。

需要注意的是,这段代码执行时需要确保当前的环境配置有足够的权限来删除命名空间及其资源,否则可能会失败。同时,谨慎使用这样的代码,因为删除命名空间将导致其中的所有资源永久丢失,无法恢复。在生产环境中,务必谨慎执行类似的操作,并确认执行之前的后果。