unable to create new content in namespace kubernetes-dashboard because it is being terminated K8s面板工具刪除報錯問題解決(K8s删除命名空间脚本)

報錯

image

解決辦法

┌──[root@vms81.liruilongs.github.io]-[~/ansible]
└─$kubectl get ns kubernetes-dashboard  -o json > kubernetes-dashboard.json
┌──[root@vms81.liruilongs.github.io]-[~/ansible]
└─$vim kubernetes-dashboard.json
┌──[root@vms81.liruilongs.github.io]-[~/ansible]
└─$

image


修改為:
image


┌──[root@vms81.liruilongs.github.io]-[~/ansible]
└─$kubectl proxy --port=8081 &
[1] 16126
┌──[root@vms81.liruilongs.github.io]-[~/ansible]
└─$Starting to serve on 127.0.0.1:8081

┌──[root@vms81.liruilongs.github.io]-[~/ansible]
└─$curl -k -H "Content-Type:application/json" -X PUT --data-binary @kubernetes-dashboard.json http://127.0.0.1:8081/api/v1/namespaces/kubernetes-dashboard/finalize
{
  "kind": "Namespace",
  "apiVersion": "v1",
  "metadata": {
    "name": "kubernetes-dashboard",
    "uid": "e4405048-05a1-4031-b725-da2539a076d7",
    "resourceVersion": "1981658",
    "creationTimestamp": "2022-02-17T17:24:00Z",
    "deletionTimestamp": "2022-02-17T17:44:49Z",
    "labels": {
      "kubernetes.io/metadata.name": "kubernetes-dashboard"
    },
    "annotations": {
      "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"name\":\"kubernetes-dashboard\"}}\n"
    },
    "managedFields": [
      {
        "manager": "kubectl-client-side-apply",
        "operation": "Update",
        "apiVersion": "v1",
        "time": "2022-02-17T17:24:00Z",
        "fieldsType": "FieldsV1",
        "fieldsV1": {"f:metadata":{"f:annotations":{".":{},"f:kubectl.kubernetes.io/last-applied-configuration":{}},"f:labels":{".":{},"f:kubernetes.io/metadata.name":{}}}}
      },
      {
        "manager": "kube-controller-manager",
        "operation": "Update",
        "apiVersion": "v1",
        "time": "2022-02-17T17:44:54Z",
        "fieldsType": "FieldsV1",
        "fieldsV1": {"f:status":{"f:conditions":{".":{},"k:{\"type\":\"NamespaceContentRemaining\"}":{".":{},"f:lastTransitionTime":{},"f:message":{},"f:reason":{},"f:status":{},"f:type":{}},"k:{\"type\":\"NamespaceDeletionContentFailure\"}":{".":{},"f:lastTransitionTime":{},"f:message":{},"f:reason":{},"f:status":{},"f:type":{}},"k:{\"type\":\"NamespaceDeletionDiscoveryFailure\"}":{".":{},"f:lastTransitionTime":{},"f:message":{},"f:reason":{},"f:status":{},"f:type":{}},"k:{\"type\":\"NamespaceDeletionGroupVersionParsingFailure\"}":{".":{},"f:lastTransitionTime":{},"f:message":{},"f:reason":{},"f:status":{},"f:type":{}},"k:{\"type\":\"NamespaceFinalizersRemaining\"}":{".":{},"f:lastTransitionTime":{},"f:message":{},"f:reason":{},"f:status":{},"f:type":{}}}}},
        "subresource": "status"
      }
    ]
  },
  "spec": {

  },
  "status": {
    "phase": "Terminating",
    "conditions": [
      {
        "type": "NamespaceDeletionDiscoveryFailure",
        "status": "True",
        "lastTransitionTime": "2022-02-17T17:44:54Z",
        "reason": "DiscoveryFailed",
        "message": "Discovery failed for some groups, 1 failing: unable to retrieve the complete list of server APIs: metrics.k8s.io/v1beta1: an error on the server (\"Internal Server Error: \\\"/apis/metrics.k8s.io/v1beta1?timeout=32s\\\": the server could not find the requested resource\") has prevented the request from succeeding"
      },
      {
        "type": "NamespaceDeletionGroupVersionParsingFailure",
        "status": "False",
        "lastTransitionTime": "2022-02-17T17:44:54Z",
        "reason": "ParsedGroupVersions",
        "message": "All legacy kube types successfully parsed"
      },
      {
        "type": "NamespaceDeletionContentFailure",
        "status": "False",
        "lastTransitionTime": "2022-02-17T17:44:54Z",
        "reason": "ContentDeleted",
        "message": "All content successfully deleted, may be waiting on finalization"
      },
      {
        "type": "NamespaceContentRemaining",
        "status": "False",
        "lastTransitionTime": "2022-02-17T17:44:54Z",
        "reason": "ContentRemoved",
        "message": "All content successfully removed"
      },
      {
        "type": "NamespaceFinalizersRemaining",
        "status": "False",
        "lastTransitionTime": "2022-02-17T17:44:54Z",
        "reason": "ContentHasNoFinalizers",
        "message": "All content-preserving finalizers finished"
      }
    ]
  }
}┌──[root@vms81.liruilongs.github.io]-[~/ansible]
└─$
┌──[root@vms81.liruilongs.github.io]-[~/ansible]
└─$kubectl get ns
NAME                         STATUS   AGE
default                      Active   67d
ingress-nginx                Active   57d
kube-node-lease              Active   67d
kube-public                  Active   67d
kube-system                  Active   67d
liruiling-job-create         Active   62d
liruilong-dameonset-create   Active   62d
liruilong-deploy-create      Active   67d
liruilong-heml-create        Active   39d
liruilong-network-create     Active   46d
liruilong-rbac-create        Active   32d
liruilong-svc-create         Active   62d
liveness-probe               Active   62d
metallb-system               Active   58d
monitoring                   Active   33d
┌──[root@vms81.liruilongs.github.io]-[~/ansible]
└─$

完整的脚本

#!/bin/bash
read -p "执行kubectl proxy了没 ?(y/n) " ans
case $ans in
	y) 
		;;
	*)
	echo "先在其他终端执行kubectl proxy"
	exit 1 ;;
esac
if [ $# -eq 0 ] ; then
	echo "后面加上你所要删除的ns."
	exit 1
fi

kubectl get namespace $1 -o json > logging.json
sed  -i '/"finalizers"/{n;d}' logging.json
curl -k -H "Content-Type: application/json" -X PUT --data-binary @logging.json http://127.0.0.1:8001/api/v1/namespaces/${1}/finalize
posted @ 2022-02-18 19:03  山河已无恙  阅读(695)  评论(0编辑  收藏  举报