I have multiple kubernetes clusters and want to ensure that when I kubectl apply a deployment, I'm targeting the correct cluster.
I have all my clusters configured in contexts in the root /.kube/config file but I don't want to rely on statefully switching my current context to the correct one before running each apply command.
i.e. This is not satisfactory
kubectl config use-context cluster-1-context
kubectl apply ./deploy-to-cluster-1.yml
kubectl config use-context cluster-2-context
kubectl apply ./deploy-to-cluster-2.yml
I read the docs on config for multiple clusters and the only way I can find to do this is by copy/pasting the config for a particular cluster into a custom config file and specifying that with the --kubeconfig option on the apply command.
kubectl apply ./deploy-to-cluster-1.yml --kubeconfig ./config-cluster-1
kubectl apply ./deploy-to-cluster-2.yml --kubeconfig ./config-cluster-2
This works, but it seems really cumbersome.
For such a common requirement I'd expect there to just be a simple option on apply, or perhaps even a field in the deployment yml, that lets you specify (or restrict) the deployment to a particular context/cluster name, but I've read through a lot of the relevant documentation and can't find any such option.
Is there a better way to do this?