Skip to content

Kubernetes: Working with multiple contexts

Published:

A context in kubernetes defines the current scope under which all the kubectl commands will run. In simple words, it defines the cluster against which all the kubectl commands will execute.

The contexts are easy to work with as long as you have a fixed number of contexts and context files. However as the number of contexts keep on growing it becomes rather difficult to manage them. In this post I will try to demystify the way of working and managing multiple contexts.

A list of all available contexts can be fetched via:

kubectl config get-contexts

Also the current context in use can be checked via:

kubectl config current-context

Similarly the current context can be switched to another one via:

kubectl config use-context <context-name>

All the above mentioned commands work well as long as all the contexts are present in the same configuration file, which is usually located at $HOME/.kube/config. This holds true depending upon the way the context was downloaded to your console. Sometimes the contexts are downloaded to different locations and in order to use those contexts, the following ways are preferred: