I have several gcloud accounts and thus different kubernetes' contexts.
I find myself frequently switching between these using:
$ gcloud config configurations activate ACCOUNT
$ kubectl config set-context CONTEXT
or alternatively
$ gcloud config configurations activate ACCOUNT
$ kubectx CONTEXT
(for kubectx see https://github.com/ahmetb/kubectx)
This feels like a lot of typing. Also it also seems like if you switch gcloud accounts, that it could intuitively switch the contexts (each k8s context maps to a gcloud account).
I'm curious to see other people's workflows. Or is there a more efficient or conventional workflow?
The reason I need to switch both account and context is because each gcloud account has a separate email address. So I'll get this error if I don't switch both:
$ kubectl get pods
Error from server (Forbidden): pods is forbidden: User
"myemail@email.com" cannot list resource "pods" in API group "" in
the namespace "default": Required "container.pods.list" permission.
Maybe there's a way to setup gcloud to allow both users in each account? Otherwise I'd have to switch both account and contexts.
I suppose the quickest way to switch is creating aliases in your ~/.bashrc or ~/.zshrc file:
alias k8s.acct1="gcloud config configurations activate acct1; kubectx context1"
alias k8s.acct2="gcloud config configurations activate acct2; kubectx context2"
Another really great alternative is to set the KUBECONFIG environment variable within each console window (and you could even build alias shortcuts to quickly set this up for each session).
So in one console window you run:
export KUBECONFIG=~/acct1-kubeconfig
and in another window you run:
export KUBECONFIG=~/acct2-kubeconfig
And then suppose you have a OpenShift cluster, you do the same:
export KUBECONFIG=~/os-kubeconfig
Each console session then has kubectl access to each respective account and cluster.