How to Simplify Kubernetes Management With Kubectl Contexts


Graphic showing the Kubernetes logo

Kubectl contexts are a mechanism for shortly switching between completely different clusters, customers, and namespaces inside the CLI. They make it simpler to maneuver between a number of environments with out altering your energetic Kubectl config file.

On this article, we’ll present how you need to use Kubectl to create, handle, and choose completely different contexts. Be sure to’ve acquired Kubectl put in earlier than you proceed.

What’s A Context?

Contexts encapsulate the gathering of settings that let a profitable connection to a Kubernetes cluster. A context can embrace the cluster’s URL, a set of consumer credentials, and the namespace to focus on by default.

Within the absence of contexts, distinctive Kubernetes environments are sometimes dealt with by making a separate config file for every one. You then use the --kubeconfig flag or KUBECONFIG atmosphere variable to load the right file every time you employ Kubectl:

$ export KUBECONFIG=.kube/cluster-1-user-1.yaml
$ kubectl get pods

Contexts allow you to condense particulars of all of your environments into one config file. You should utilize the default .kube/config for every of your clusters, eliminating CLI flags and atmosphere variables. Kubectl contains instructions to modify its energetic context between the choices you’ve created.

Getting ready for Contexts

Contexts are managed utilizing the kubectl config command group. As with the whole lot else in Kubectl, your obtainable context record will probably be loaded from and saved to your energetic config file. That is decided by KUBECONFIG, --kubeconfig, or the default .kube/config.

To start utilizing contexts, you should add just a few clusters and credentials to your config file. You should utilize different kubectl config instructions to set these up:

# Create two cluster connections, qa and prod
$ kubectl set-cluster qa --server= --insecure-skip-tls-verify
$ kubectl set-cluster prod --server= --insecure-skip-tls-verify

# Create two credential pairs
$ kubectl set-credentials qa-user --username=demo --password=demoP@ss_qa
$ kubectl set-credentials prod-user --username=demo password=demoP@ss_prod

Now your config file accommodates connection particulars for 2 separate Kubernetes clusters. It additionally holds two pairs of credentials. Subsequent we’ll create a context to hyperlink the clusters to their respective credentials. You’ll then be capable of use a Kubectl command to leap between the QA and manufacturing environments.

Making a Context

The kubectl config set-context command provides new contexts to your config file. You need to specify a reputation on your context. Use the command’s flags to reference a beforehand added cluster and consumer account.

# Create contexts for the clusters added earlier
$ kubectl config set-context qa-context --cluster=qa --user=qa-user
$ kubectl config set-context prod-context --cluster=prod --user=prod-user

At this level you may run the kubectl config view command to examine all of the adjustments which have been made to your config file:

apiVersion: v1
form: Config
current-context: ""
  - cluster:
    identify: qa
      insecure-skip-tls-verify: true
  - cluster:
    identify: prod
      insecure-skip-tls-verify: true
  - context:
    identify: qa-context
      cluster: qa
      consumer: qa-user
  - context:
    identify: prod-context
      cluster: prod
      consumer: prod-user
  - identify: qa-user
      username: demo
      password: demoP@ss_qa
  - identify: prod-user
      username: demo
      password: demoP@ss_prod

The context definitions level to other forms of object outlined elsewhere within the config file.

Choosing and Utilizing Contexts

Contexts are chosen with the kubectl context use-context command:

$ kubectl context use-context qa-context

The energetic context’s identify is saved as the worth of the current-context subject in your Kubectl config file. All Kubectl instructions will goal the cluster referenced by the chosen context.

# Connects to the cluster as demo:demoP@ss_qa
$ kubectl get pods

# Change the energetic context
$ kubectl config use-context prod-context

# Connects to the cluster as demo:demoP@ss_prod
$ kubectl get pods

The power to shortly change the goal atmosphere inside Kubectl helps you progress between clusters with out being overwhelmed by config flags.

As a result of the chosen context persists till one other one is chosen, you must examine Kubectl is focusing on the atmosphere you count on earlier than working harmful instructions. Use the current-context command to see the chosen context’s identify:

$ kubectl config current-context

You possibly can view all of the contexts in your presently loaded config file with get-contexts:

$ kubectl config get-contexts
          qa-context    qa        qa-user
*         prod-context  prod      prod-user

Together with Namespace Info With Contexts

Thus far we’ve used contexts to pick out a cluster and consumer account. Contexts can embrace namespace info too. When a context has an assigned namespace, Kubectl instructions will robotically embrace the --namespace flag. You possibly can nonetheless use --namespace manually to override the namespace set by the context.

$ kubectl config set-context production-api --cluster=prod --user=prod-user --namespace api

$ kubectl config use-context production-api

# Will get Pods within the "api" namespace inside the "prod" cluster
$ kubectl get pods

There’s no restrict to the variety of contexts you may have. Clusters could seem in a number of contexts, letting you outline separate contexts for every of your necessary namespaces. This avoids repetition of the --namespace flag as you examine completely different sources in your cluster.

Renaming and Deleting Contexts

Rename contexts utilizing the rename-context command:

$ kubectl config rename-context qa-context testing-context

To delete a context, cross its identify to the delete-context command:

$ kubectl config delete-context testing-context

The cluster, consumer, and namespace referenced by a context are modified by repeating the set-context command with the identical context identify. You may as well make modifications by manually modifying your Kubectl config file.

Making Context Switching Even Simpler

Kubectl’s built-in context administration could be enough once you swap clusters on a comparatively rare foundation. Nevertheless, if you happen to’re continuously altering cluster all through your day, the comparatively verbose use-context command can begin to really feel repetitive itself.

Kubectx is a Kubectl plugin that may make context switches even simpler. It shortens use-context and provides just a few further comfort options:

# Equal to "kubectl config use-context prod-context"
$ kubectx prod-context

Relying in your workflow, you might need to conserving utilizing a number of Kubectl config recordsdata too. You could possibly use shell aliases and default atmosphere variables to arrange a personalized workflow that robotically selects a config file and context for every new terminal window.


Kubectl contexts are a strategy to encapsulate a number of logical cluster connections in a single config file. Every context is assigned a cluster URL, consumer account, and namespace. Kubectl instructions will goal the chosen context’s cluster utilizing the referenced credentials.

You possibly can arrange contexts with kubectl config set-context or by manually modifying your .kube/config file. Kubectl additionally contains instructions for managing the named cluster connections and consumer accounts you may reference in your contexts.

Share post:



More like this

Saraya ripped apart on Twitter following her now-deleted post

Saraya's first AEW Dynamite promo did not fairly...

AOC Claims Abortion Bans Are a Means to Ensure Women Are ‘Conscripted’ to Work Against Their Will

Consultant Alexandria Ocasio-Cortez (AOC) believes restrictions on abortion...

West Ham 2 – 0 Wolves