kubectl set context & use context Tutorial (Practical Examples)

kubectl set context tutorial

In this guide, we will look at how to use kubectl set context for managing multiple Kubernetes cluster contexts

Kubectl Context

When you work on Kubernetes projects, you will have to work with multiple kubernetes clusters at a time in your workstation. It could be clusters that are part of the same environments or different environments. Even while learning, you might have to deal with more than one cluster.

Here is where kubectl config set context command comes in handy.

A context is a set of parameters (cluster, namespace, user) present in the kubeconfig file that are required to connect to a kubernetes cluster. A single kubeconfig file can contain more than one context.

Here is an example context block from kubeconfig file with three cluster contexts.

contexts:
- context:
    cluster: dev-cluster
    user: dev-user
  name: dev

- context:
    cluster: stage-cluster
    user: stage-user
  name: stage

- context:
    cluster: prod-cluster
    user: prod-user
  name: prod

current-context: dev

You can view all the contexts using the following kubectl command

kubectl config view

Now that we have understood what a context is, let’s look at different practical examples to understand kubectl set context in detail.

List Cluster Contexts

To list all the availble contexts in your workstation, you can use the following kubectl command. the -o=name flag lists only the context names.

kubectl config get-contexts -o=name

You will get the list of contexts as shown below.

arn:aws:eks:us-west-2:814200988517:cluster/custom-cluster
do-sfo3-k8s-1-28-2-do-0-sfo3-1699501871578
kubernetes-admin@kubernetes
kubernetes-the-hard-way

What is kubectl config set context ?

Kubectl set context is used to modify the existing context or create new cluster context if not present. Also, you cannot switch to a different context using the set context command.

You can learn about other set context options using the help flag as shown below.

kubectl config set-context --help

Here is the output. The options sections has all the supported options.

$ kubectl config set-context --help

Set a context entry in kubeconfig.

 Specifying a name that already exists will merge new fields on top of existing values for those fields.

Examples:
  # Set the user field on the gce context entry without touching other values
  kubectl config set-context gce --user=cluster-admin

Options:
    --cluster='':
        cluster for the context entry in kubeconfig

    --current=false:
        Modify the current context

    --namespace='':
        namespace for the context entry in kubeconfig

    --user='':
        user for the context entry in kubeconfig

Usage:
  kubectl config set-context [NAME | --current] [--cluster=cluster_nickname] [--user=user_nickname] [--namespace=namespace] [options]

Use "kubectl options" for a list of global command-line options (applies to all commands).

Setting default namespace using set context

By default, all the commands get executed in the default namespace if you don’t specify a namespace with the -n flag. You can change that behavior by setting the current namespace using the set-context command.

For example, if you want to set kube-system as your default namespace, you can use the following command systax.

kubectl config set-context --current --namespace [namespace-name]

For example,

kubectl config set-context --current --namespace kube-system

Once you set the current namespace, you don’t have to use the -n flag with the kubectl command. All the kubectl command gets executed in the current namespace set by the set-context command.

Creating a new context

Lets say I work with a cluster with two different namespaces. Example, app and monitoring namespace.

I dont want to specify namespace overtime i work with the specific namespace because I can only set one namespace as default.

So what I can do is, create a separate contexts for the same cluster setting app and monitoring namespaces as default.

The context will use the same configurations for cluster authentication.

What is kubectl config use context?

kubectl config use context command is primarily used to switch to a different cluster context.

For example, here is the list of contexts available in my Kubeconfig file.

$ kubectl config get-contexts -o=name

arn:aws:eks:us-west-2:814200988517:cluster/custom-cluster
do-sfo3-k8s-1-28-2-do-0-sfo3-1699501871578
kubernetes-admin@kubernetes
kubernetes-the-hard-way

Here is the syntax to set the current context.

kubectl config use-context [context-name]

For example,

kubectl config use-context kubernetes-admin@kubernetes

When you use a new context, the value is also set in the Kubeconfig file. You can find a parameter called current-context: in the Kubeconfig file.

You can verify the if the context is set to current using the following command.

kubectl config current-context

kubectl delete context

You can delete the context created by kubectl using the delete-context command.

Here is the command syntax.

kubectl config delete-context [context-name]

For example,

kubectl config delete-context stage-k8s-cluster

Kubectl Context Alias

If you regularly switch between contexts to work on different clusters, you can create an alias to list and set the context.

Here is the alias to list the contexts.

alias klist='kubectl config get-contexts -o=name'

You can add the following line to your .bashrc or .zshrc file, depending on the shell you use:

Create the following alias to set the context with a custom namespace.

kset() {
    local context="$1"
    local namespace="$2"
    kubectl config use-context "$context" --namespace "$namespace"
}

Here is how the aliases look in your bashrc or zshrc file.

alias k=kubectl

alias klist='kubectl config get-contexts -o=name'

kset() {
    local context="$1"
    local namespace="$2"
    kubectl config use-context "$context" --namespace "$namespace"
}

kdel() {
    kubectl config delete-context "$1"
}

Here is how you can use these aliases

  1. List the contexts with klist command.
  2. Set the context and namespace using the kset command

Here is the example.

Kubectl Set Context Aliases

Set Context FAQ’s

How to set current context in kubectl?

You can use the kubectl config use-context command with the context name to set the current context.

For example, kubectl config use-context dev-cluster

Where dev-cluster is the context name.

How do I set context in Kubernetes for specific namespace?

You can use the --namespace flag with the kubectl config use-context command to set a current namespace in context.

What is the difference between kubectl set and kubectl use context?

The set-context parameters modifies the cluster context or create a new one if not present. The use context parameter is used to switch to a different context.

kubectl Alternatives to Set Contexts

If you dont want to use the native commands to set kubectl contexs, you can make use the following alternative open source utilites.

  1. Kubie
  2. K9s
  3. Kubectx
  4. KubeContext (GUI)
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like