Working With Kubernetes Contexts

kubectl is the defacto command line tool for administering Kubernetes clusters. Connecting to a cluster via kubectl requires a Kubernetes config file, this in turn contains one or more contexts. A context is simply a label that associates a specific cluster with a specific user. As with most things in the world of Kubernetes, sooner or later you will run into YAML, and config files are no exception to this. Unless the KUBECONFIG environment variable specifies otherwise, the path to the kube config file is /user/home/.kube

Some useful kubectl command lines for using contexts are as follows:

The contexts you have available can be listed via:

kubectl config get-contexts

The current context can be obtained via:

kubectl config current-context

To set the current context use:

kubectl config set-context <cluster-name>

The two main ways of managing access to multiple clusters are to use multiple contexts within the same config file, or to to use multiple config files. If you elect to go down the route of using just one config file, a nice way of merging contexts into your kube config file is to use the konfig plugin, this allows you to do things such as the following:

kubectl konfig import -s context_to_import.yaml

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s