Search code examples
kuberneteskubectlkustomize

Way to stop deploy wrong kubernetes environment


We have a set of kubernetes yamls which is management by kustomize and they will be deployed to different clusters. Each cluster is slightly different which means every environment will have a sub directory (environ/<envname>) including some special kustomization overwrite.

We will manually deploy new version to different environments by command kubeclt apply -k environ/env. But sometimes we do stupid thing like this: kubectl apply -k environ/env1 to the cluster env2 . So is there some method to stop doing a kubectl apply to a wrong environment?


Solution

  • This is a community wiki answer. Feel free to expand it.

    If you are aware that you made a mistake and want to cancel the command right away than there are some options for you:

    • $ kill -9 $! will kill the most recent process executed by the command ($! represents its process ID).

    • Suspend the current process by pressing Ctrl+z and then kill it using kill -9 %% or kill -9 %+. More details regarding this approach can be found here.

    EDIT:

    Including the solution proposed by VASャ from the comments:

    I'd use shell scripts and different configs for each cluster, like that: deploy-cluster1.sh where I'd have kubectl --kubeconfig .kube/cluster1 apply -k environ/cluster1 or even shorter: deploy.sh env1 where deploy.sh contains: kubectl --kubeconfig .kube/$1 apply -k environ/$1

    More details regarding that approach can be found here.