Kubernetes 네임스페이스가 비어 있는 경우에만 삭제하시겠습니까?
Kubernetes 네임스페이스가 비어 있는 경우에만 삭제하시겠습니까?
Helm을 사용하여 애플리케이션의 여러 "구성 요소"를 단일 네임스페이스에 배포하고 Jenkins를 사용하여 작업 생성 및 삭제를 트리거하고 있습니다. Helm을 사용하여 네임스페이스를 삭제할 수 있는 것 같지 않아서 Kubernetes 명령만 사용하려고 합니다.
그러나 내가 그것을 사용하면 네임스페이스와 그 모든 자원을 강제로 파괴할 것 같다.
네임스페이스가 비어 있는 경우에만 삭제하려고 합니다. 이것을 할 명령이 있나요?
네임스페이스가 비어 있는 경우에만 삭제하고 싶습니다. 이것을 할 명령이 있나요?
아니요, 그것을 할 명령이 없습니다. 이 동작은 의도된 것입니다.
I would suggest a different approach. You should have all your deployment yamls in version control system for all of the components including namespace. When you want to create use kubectl create -f deployment.yaml
and when you want to delete use kubectl delete -f deployment.yaml
You can run kubectl get all --namespace YOUR_NAMESPACE
and then depends on output call delete namespace
There's not a simple command to check a namespace before delete, it requires some kubectl scripting or a kube API client.
From the github issue discussing get all
s limitations liggit provides an example and adding some jq processing you can get a (slow) command that errors unless it successfully finds all resource types are empty (no items
):
set -o pipefail
kubectl api-resources --verbs=list --namespaced -o name \
| xargs -n 1 kubectl get --ignore-not-found -n YOUR_NAMESPACE -o json \
| jq '.items[] | .kind + "/" + .metadata.name | error'
try this, better iterate over kube-api resources and this will give every resource list inside the namespace.
kubectl api-resources --verbs=list --namespaced -o name \
| xargs -n 1 kubectl get --show-kind --ignore-not-found -l <label>=<value> -n
<namespace>
or another approch
kubectl api-resources --verbs=list --namespaced -o name | `
%{ kubectl get $_ --show-kind --ignore-not-found -l <label>=<value> -n
<namespace> }
See Remove Empty Namespaces Operator, it can do exactly what you want.
Why? Because it's not so easy to iterate over resources in the namespace to decide if it's empty or not. After all, there are "default resources" like default service account and probably other stuff from you tooling/operators.
So these resources should be excluded from iteration. Bash scripting becomes too complicated this way. And one day I decided to implement it with Python.
just use folloing to delete all empty namespaces
kubectl get ns --no-headers -o custom-columns=":metadata.name" | xargs -I{} kubectl get all -n {} 2>&1 | grep "No" | cut -d " " -f 5 | xargs -I{} kubectl delete namespace {}
you can list empty namespaces by this
kubectl get ns --no-headers -o custom-columns=":metadata.name" | xargs -I{} kubectl get all -n {} 2>&1 | grep "No" | cut -d " " -f 5