kubernetes强制删除资源

653 阅读1分钟

之所以namespace、pod、pv、pvc会处于"Terminating"状态无法删除,那是因为kubelet有阻塞,有其他的资源在使用该namespace,比如CRD等,尝试重启kubelet,再删除该namespace也不好使。

正确的删除方法:删除pod--> 删除pvc ---> 删除pv --> 删除namespace

强制删除pod

kubectl delete pod <your-pod-name> -n <name-space> --force --grace-period=0

参数 --force --grace-period=0,grace-period表示过渡存活期,默认30s,在删除POD之前允许POD慢慢终止其上的容器进程,从而优雅退出,0表示立即终止POD

强制删除pv pvc

kubectl patch pv xxx -p '{"metadata":{"finalizers":null}}'
kubectl patch pvc xxx -p '{"metadata":{"finalizers":null}}'

强制删除namespace

kubectl delete ns <terminating-namespace> --force --grace-period=0

如果这样删除仍不好使的话,那么需要采用如下方法:

$ kubectl get namespace <terminating-namespace> -o yaml

输出如下:

apiVersion: v1
kind: Namespace
...
spec:
  finalizers:
  - kubernetes
status:
...

$ kubectl get namespace <terminating-namespace> -o json >tmp.json

编辑tmp.josn,删除finalizers 字段的值

{
  "apiVersion": "v1",
  "kind": "Namespace",
  ...
  "spec": {    #从此行开始删除
    "finalizers": []
  },           # 删到此行
  "status": {
    "phase": "Terminating"
  }
}

删除namespace

$ kubectl proxy
$ curl -k -H "Content-Type: application/json" -X PUT --data-binary @tmp.json http://127.0.0.1:8001/api/v1/namespaces/<terminating-namespace>/finalize
$ kubectl get namespaces