I have a K3s cluster with system pods (i.e. kube-system
namespace) and my application pods:
kube-system pod/calico-node-xxxx
kube-system pod/calico-kube-controllers-xxxxxx
kube-system pod/metrics-server-xxxxx
kube-system pod/local-path-provisioner-xxxxx
kube-system pod/coredns-xxxxx
app-system pod/my-app-xxxx
db-system pod/my-db-xxxxx
I am looking for a shell/kubectl command (for automation script) that can delete my application namespace (want to modify kubectl delete namespace app-system db-system
) without mentioning the app namespaces name in the command (as in future if more app namespace would come in cluster, I have to edit this script every time).
That means I want to delete all namespace in cluster except kube-system
something like - kubectl delete namespace -v kube-system
(I know -v
is not a valid parameter here, just showing how in grep -v
is used to except the following words. Similar thing looking for kubectl delete ns...
)
I've created the following code , so you can use it as a wrapper. You can name the script whatever you want. For example
exclude_ns_removal
Make the script executable:
Run it as follows:
The result will be something like:
If the output looks good, you should modify this line
to
May be simpler than the previous answers -- writing scripts or loops here is overkill, kubernetes does it all for you:
That's a lot for something that could be solved with