获取有关 Kubernetes 中要删除的资源的警告
Obtain warning about resources to be deleted in Kubernetes
与其使用 Kubernetes 的 default
命名空间,不如为每个应用程序或基础架构的每个逻辑组件创建单独的命名空间。
OReilly 的“Cloud Native DevOps with Kubernetes”强调了一个相关的潜在风险:
You could use a namespace as a kind of temporary virtual cluster, and delete the namespace when you’re finished with it. But be careful! Deleting a namespace deletes all the resources within it. You really don’t want to run that command against the wrong namespace.
(...)
So don’t delete namespaces unless they really are temporary, and you’re sure they don’t contain any production resources.
在 Kubernetes' documentation 关于命名空间的文章中,我看到了类似的警告:
Warning: This deletes everything under the namespace!
当然我们需要小心,但是资源可以这么轻易地被删除,这真是一个可怕的想法。
有什么方法可以通过以下方式获得有关将删除哪些资源的警告:
kubectl delete namespaces <ns-name>
?
本机没有获取警告的方法。您可以使用以下命令检查命名空间中的所有资源。当命名空间被删除时,该命名空间中的所有资源都将被删除。
kubectl get all -n namespacename
另外一个最佳实践是将 kubernetes yamls 保存在版本控制系统中,例如 git 以便您可以在错误删除的情况下再次应用它们。
删除命名空间时,该命名空间下的所有命名空间资源都将被删除。没有直接的方法来列出命名空间中的所有资源(kubectl get all
仅列出一组选定的资源)。
但是,您可以枚举所有命名空间 资源类型:
kubectl get api-resources --namespaced=true
然后您可以遍历这些资源类型并检查您的命名空间中是否有它们的任何实例 kubectl get
。
例如,以下命令列出 ns-name
命名空间中的所有资源:
for r in $(kubectl api-resources --namespaced=true --no-headers 2>/dev/null | cut -d ' ' -f 1); do
kubectl get "$r" -n ns-name --no-headers -o custom-columns=:.metadata.name | sed "s/^/$r /"
done
这是您删除 ns-name
命名空间时将删除的所有资源。
与其使用 Kubernetes 的 default
命名空间,不如为每个应用程序或基础架构的每个逻辑组件创建单独的命名空间。
OReilly 的“Cloud Native DevOps with Kubernetes”强调了一个相关的潜在风险:
You could use a namespace as a kind of temporary virtual cluster, and delete the namespace when you’re finished with it. But be careful! Deleting a namespace deletes all the resources within it. You really don’t want to run that command against the wrong namespace. (...) So don’t delete namespaces unless they really are temporary, and you’re sure they don’t contain any production resources.
在 Kubernetes' documentation 关于命名空间的文章中,我看到了类似的警告:
Warning: This deletes everything under the namespace!
当然我们需要小心,但是资源可以这么轻易地被删除,这真是一个可怕的想法。
有什么方法可以通过以下方式获得有关将删除哪些资源的警告:
kubectl delete namespaces <ns-name>
?
本机没有获取警告的方法。您可以使用以下命令检查命名空间中的所有资源。当命名空间被删除时,该命名空间中的所有资源都将被删除。
kubectl get all -n namespacename
另外一个最佳实践是将 kubernetes yamls 保存在版本控制系统中,例如 git 以便您可以在错误删除的情况下再次应用它们。
删除命名空间时,该命名空间下的所有命名空间资源都将被删除。没有直接的方法来列出命名空间中的所有资源(kubectl get all
仅列出一组选定的资源)。
但是,您可以枚举所有命名空间 资源类型:
kubectl get api-resources --namespaced=true
然后您可以遍历这些资源类型并检查您的命名空间中是否有它们的任何实例 kubectl get
。
例如,以下命令列出 ns-name
命名空间中的所有资源:
for r in $(kubectl api-resources --namespaced=true --no-headers 2>/dev/null | cut -d ' ' -f 1); do
kubectl get "$r" -n ns-name --no-headers -o custom-columns=:.metadata.name | sed "s/^/$r /"
done
这是您删除 ns-name
命名空间时将删除的所有资源。