Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switching GCP accounts and contexts with gcloud and kubectl is not working

I have 2 gmail accounts, each account is associated with 2 separate GCP accounts. To switch accounts from the command line, I've been using both of these commands for the past 2 weeks or so:

$ gcloud config configurations activate ACCT_NAME
$ kubectl config set-context CONTEXT

Note: I also have been using 'kubectx' which makes switching contexts a bit easier than using 'kubectl config set-context'; basically its a bit less typing. Both 'kubectl config set-context' and 'kubect' have worked in the past (I think).

It seems like this approach has been working fine until this morning. I tried switching from context A to context B, but now I'm getting errors like this:

$ kubectl get pods -A                                             
Error from server (Forbidden): pods is forbidden: User "<my_email_address>" cannot list resource "pods" in API group "" at the cluster scope: Required "container.pods.list" permission.

The email address shown is for context A, not context B. I checked the following commands to verify the current (or active) context and email accounts are correct:

$ kubectl config current-context
$ gcloud config configurations list

So the current contexts, accounts, and associated email addresses are correct. I'm not clear why kubectl is still trying to access the GKE with the email account from context A (not context B).

Any thoughts?

like image 843
jersey bean Avatar asked Jan 19 '20 23:01

jersey bean


People also ask

How do I switch accounts on Gcloud?

If you want to switch the account used by the gcloud CLI on a per-invocation basis, override the active account using the --account flag.

Does kubectl come with Gcloud?

Cloud Shell comes preinstalled with the Google Cloud CLI and kubectl command-line tool. The gcloud CLI provides the primary command-line interface for Google Cloud, and kubectl provides the primary command-line interface for running commands against Kubernetes clusters.

How do you set context in kubectl?

What this says is that you can create or modify contexts in your kubeconfig file with the command kubectl config set-context. This command also accepts the name of the context to be changed (or --current if you want to change the current context), as well as --user, --cluster, and --namespace options.


1 Answers

So I had posted this question a couple weeks ago:

https://serverfault.com/questions/997220/kubectl-not-showing-new-context-created-in-gcp

For whatever reason, I had to rerun this command to get things working again:

$ gcloud container clusters get-credentials NAME

Note: you can get the cluster name from this command 'gcloud container clusters list'

I have no idea why I had to re-run get-credentials. I don't believe my credentials changed; this took me a while to figure out.

like image 186
jersey bean Avatar answered Nov 15 '22 07:11

jersey bean