Skip to content

k8s/namespace删除后状态一直是terminating #38

@kaybinwong

Description

@kaybinwong

昨天使用kubectl delete ns xxx --now=true --force删除了一个namespace之后,其状态一直Terminating,过了好久都没被成功回收。
第一反应是在这namespace下还存在资源,但实际上使用kubectl get all -n xxx却没有发现任何资源,查了调度器也没发现任何有用日志。

记得官网文档明明说会级联删除该空间下的资源的,于是翻看源码namespaced_resources_deleter.go

	// there may still be content for us to remove
	estimate, err := d.deleteAllContent(namespace.Name, *namespace.DeletionTimestamp)
	if err != nil {
		return err
	}
	if estimate > 0 {
		return &ResourcesRemainingError{estimate}
	}

	// we have removed content, so mark it finalized by us
	namespace, err = d.retryOnConflictError(namespace, d.finalizeNamespace)
	if err != nil {
		// in normal practice, this should not be possible, but if a deployment is running
		// two controllers to do namespace deletion that share a common finalizer token it's
		// possible that a not found could occur since the other controller would have finished the delete.
		if errors.IsNotFound(err) {
			return nil
		}
		return err
	}

	// Check if we can delete now.
	if d.deleteNamespaceWhenDone && finalized(namespace) {
		return d.deleteNamespace(namespace)
	}
	return nil

发现是会关联删除资源,但是当依旧有资源没被回收时,就会返回不继续标记终结删除。既然这样是不是命令kubectl get all -n xxx有bug导致查不出资源呢?于是把所有资源都查一遍试试看。

kubectl get certificatesigningrequests,clusterrolebindings,clusterroles,componentstatuses,configmaps,controllerrevisions,cronjobs,customresourcedefinition,daemonsets,deployments,endpoints,events,horizontalpodautoscalers,ingresses,jobs,limitranges,namespaces,networkpolicies,nodes,persistentvolumeclaims,persistentvolumes,poddisruptionbudgets,pods,podsecuritypolicies,podtemplates,replicasets,replicationcontrollers,resourcequotas,rolebindings,roles,secrets,serviceaccounts,services,statefulsets,storageclasses -n xxx

也没发现任何的resources记录。
这就相当奇怪了,没资源,也没日志可查。最后没办法,只能使用下策,通过etcd终端来操作。
前面我们也有简单使用过etcd的工具etcdctl来操作etcd,但只是简单地使用etcd V2(默认) API查看集群的健康状态,但实际上k8s使用的是etcd的V3 API,我们可以通过以下命令来操作。

export ETCDCTL_API=3
etcdctl --endpoints https://192.168.64.75:2379 --cacert /etc/ssl/etcd/ssl/ca.pem --cert /etc/ssl/etcd/ssl/node-passport-dev.pem --key=/etc/ssl/etcd/ssl/node-passport-dev-key.pem --endpoints https://192.168.64.75:2379 --command-timeout=120s --debug=true --insecure-skip-tls-verify=true del /registry/namespaces/xxx

最终namespace被成功删除,可以重新创建使用改namespace。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions