Changing Kubernetes kubectl Environments

Changing Kubernetes kubectl Environments

Recently, I setup a new Kubernetes environment and ran into the scenario of needing to switch between multiple kubernetes (k8s) environments. To manage a k8s environment you must use the kubectl command and when you have multiple k8s environments you must point kubectl to the environment you which to manage. After digging through some documentation I found the following commands helpful:

Use the current-context command to display the context that kubectl is currently pointing to. (docs)

kubectl config current-context

To see all of the contexts available to kubectl so I use the view command. (docs)

kubectl config view

Use one of the context's names in the output to change which context kubectl should use. (docs)

kubectl config use-context gke_project-name-123_us-central1-a_my-gke-cluster-1

One thing to watch out for is the set-context command. This is different from the use-context command in that the set-context lets you make changes to the configuration of a context instead of instructing kubectl to use it.

set-context sets a context entry in kubeconfig. Specifying a name that already exists will merge new fields on top of existing values for those fields.

# Set the user field on the gce context entry without touching other values
kubectl config set-context gke_project-name-123_us-central1-a_my-gke-cluster-1 --user=cluster-admin