龙空技术网

5分钟6步强制删除kubernetes NameSpace小技巧

黑马程序员 205

前言:

现时大家对“kubectl删除namespace”大概比较注重,小伙伴们都需要了解一些“kubectl删除namespace”的相关内容。那么小编也在网络上网罗了一些对于“kubectl删除namespace””的相关知识,希望看官们能喜欢,咱们快快来学习一下吧!

在使用kubernetes过程中,我们经常会遇到无法删除NameSpace的情况,但是如果一一去删除NameSpace中资源比较麻烦。下面我们给大家介绍强制删除NameSpace的方法。

一、查看已存在的NameSpace

[root@master1 ~]# kubectl get nsNAME STATUS AGEdefault Active 56dingress-nginx Active 49distio-system Terminating 37dkube-node-lease Active 56dkube-public Active 56dkube-system Active 56d
二、获取需要强制删除的NameSpace信息
[root@master1 ~]# kubectl get namespace istio-system -o json > istio-system.json
[root@master1 ~]# cat istio-system.json{"apiVersion": "v1","kind": "Namespace","metadata": {"annotations": {"kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"labels\":{\"istio-injection\":\"disabled\",\"istio-operator-managed\":\"Reconcile\",\"operator.istio.io/component\":\"Base\",\"operator.istio.io/managed\":\"Reconcile\",\"operator.istio.io/version\":\"1.4.3\"},\"name\":\"istio-system\"}}\n"},"creationTimestamp": "2020-01-27T15:26:48Z","deletionTimestamp": "2020-02-15T01:17:05Z","labels": {"istio-injection": "disabled","istio-operator-managed": "Reconcile","operator.istio.io/component": "Base","operator.istio.io/managed": "Reconcile","operator.istio.io/version": "1.4.3"},"name": "istio-system","resourceVersion": "6024170","selfLink": "/api/v1/namespaces/istio-system","uid": "d8bdc915-ee6f-43cd-ac37-5e353218095f"},"spec": {"finalizers": ["kubernetes"]},"status": {"conditions": [{"lastTransitionTime": "2020-02-15T01:17:10Z","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": "2020-02-15T01:17:13Z","message": "All legacy kube types successfully parsed","reason": "ParsedGroupVersions","status": "False","type": "NamespaceDeletionGroupVersionParsingFailure"},{"lastTransitionTime": "2020-02-15T01:17:13Z","message": "All content successfully deleted, may be waiting on finalization","reason": "ContentDeleted","status": "False","type": "NamespaceDeletionContentFailure"},{"lastTransitionTime": "2020-02-15T01:17:40Z","message": "All content successfully removed","reason": "ContentRemoved","status": "False","type": "NamespaceContentRemaining"},{"lastTransitionTime": "2020-02-15T01:17:13Z","message": "All content-preserving finalizers finished","reason": "ContentHasNoFinalizers","status": "False","type": "NamespaceFinalizersRemaining"}],"phase": "Terminating"}}
三、修改已获取的NameSpace信息文件
[root@master1 ~]# cat istio-system.json{"apiVersion": "v1","kind": "Namespace","metadata": {"annotations": {"kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"labels\":{\"istio-injection\":\"disabled\",\"istio-operator-managed\":\"Reconcile\",\"operator.istio.io/component\":\"Base\",\"operator.istio.io/managed\":\"Reconcile\",\"operator.istio.io/version\":\"1.4.3\"},\"name\":\"istio-system\"}}\n"},"creationTimestamp": "2020-01-27T15:26:48Z","deletionTimestamp": "2020-02-15T01:17:05Z","labels": {"istio-injection": "disabled","istio-operator-managed": "Reconcile","operator.istio.io/component": "Base","operator.istio.io/managed": "Reconcile","operator.istio.io/version": "1.4.3"},"name": "istio-system","resourceVersion": "6024170","selfLink": "/api/v1/namespaces/istio-system","uid": "d8bdc915-ee6f-43cd-ac37-5e353218095f"},"spec": {},"status": {"conditions": [{"lastTransitionTime": "2020-02-15T01:17:10Z","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": "2020-02-15T01:17:13Z","message": "All legacy kube types successfully parsed","reason": "ParsedGroupVersions","status": "False","type": "NamespaceDeletionGroupVersionParsingFailure"},{"lastTransitionTime": "2020-02-15T01:17:13Z","message": "All content successfully deleted, may be waiting on finalization","reason": "ContentDeleted","status": "False","type": "NamespaceDeletionContentFailure"},{"lastTransitionTime": "2020-02-15T01:17:40Z","message": "All content successfully removed","reason": "ContentRemoved","status": "False","type": "NamespaceContentRemaining"},{"lastTransitionTime": "2020-02-15T01:17:13Z","message": "All content-preserving finalizers finished","reason": "ContentHasNoFinalizers","status": "False","type": "NamespaceFinalizersRemaining"}],"phase": "Terminating"}}
四、运行kube-proxy
[root@master1 ~]# kubectl proxyStarting to serve on 127.0.0.1:8001
五、通过API执行强制删除操作
[root@master1 ~]# curl -k -H "Content-Type: application/json" -X PUT --data-binary @istio-system.json 
六、强制删除确认
[root@master1 ~]# kubectl get nsNAME STATUS AGEdefault Active 56dingress-nginx Active 49dkube-node-lease Active 56dkube-public Active 56dkube-system Active 56d

标签: #kubectl删除namespace