2

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?

aisensiy
  • 1,460
  • 3
  • 26
  • 42
  • If you were `kubectl`, how would you know it's the wrong environment? – David Maze Jan 19 '21 at 12:38
  • So I think it may be some bash alias to prevent the wrong deploy? Bash can get the current kubectl content. – aisensiy Jan 20 '21 at 02:30
  • 2
    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` https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/ – VAS Jan 26 '21 at 19:19
  • @VASャ Yeah, that is a good method. Thanks. – aisensiy Feb 01 '21 at 05:41

2 Answers2

0

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.

Wytrzymały Wiktor
  • 11,492
  • 5
  • 29
  • 37
0

Recently I got a new solution here.

direnv which can change env after switch into different directories this force me to switch the KUBECONFIG env. Follow Mastering the KUBECONFIG file to get more details.

aisensiy
  • 1,460
  • 3
  • 26
  • 42