Kubernetes

Kubectl Config Set-Context Tutorial — Best Practices & Cheatsheet

Priyansh Khodiyar
7 min readAug 1, 2023
Photo by Paul Hanaoka on Unsplash

In this tutorial on Kubernetes (K8s) / Kubectl config set-context command, we will look at how to use them the associated flags, the defined syntax rules, and much more.

kubectl config set-context is one of many kubectl config commands like set, unset, use-context, view, set-cluster, etc. This tutorial covers the command kubectl config set-context in detail.

Let’s dive right in!

What is kubectl config set context?

kubectl config set-context is a command in the Kubernetes command-line tool kubectl that allows you to modify the context of the current kubectl configuration.

A context in Kubernetes is a collection of access parameters, such as a cluster, namespace, and user. The context is used by kubectl to determine which cluster to connect to and which set of credentials to use.

The kubectl config set-context command allows you to modify the current context or create a new context based on an existing one. You can specify the new values for the cluster, user, namespace, and other parameters that define the context.

For example, you might use kubectl config set-context to change the namespace in which you’re working or switch to a different Kubernetes cluster.

Here is the basic syntax of the kubectl config set-context command:

kubectl config set-context <context-name> [--namespace=<namespace>] [--cluster=<cluster-name>] [--user=<user-name>]

Here are some examples of how you might use the kubectl config set-context command:

# Set the context to use the "dev" namespace
kubectl config set-context --current --namespace=dev
# Switch to a different cluster and namespace
kubectl config set-context --current --namespace=prod --cluster=prod-cluster

How to get started with kubectl config set-context?

To get started with kubectl config set-context, you must have a Kubernetes cluster and the kubectl command-line tool (or CLI ) installed.

Install kubectl

If you haven’t installed kubectl yet, you can follow the official Kubernetes documentation to install it on your system. The installation process varies depending on your operating system.

Create a kubeconfig file

The kubeconfig file is a YAML file that stores the settings for your Kubernetes clusters. You can create a new kubeconfig file by running the following command:

kubectl config create

Add a cluster to your kubeconfig file

Once you have created a kubeconfig file, you can add a cluster to it by running the following command:

kubectl config add-cluster <cluster-name>

Add a user to your kubeconfig file

Once you have added a cluster to your kubeconfig file, you can add a user to it by running the following command:

kubectl config add-user <user-name>

Add a namespace to your kubeconfig file

Once you have added a user to your kubeconfig file, you can add a namespace to it by running the following command:

kubectl config set-context <context-name> --cluster=<cluster-name> --user=<user-name> --namespace=<namespace>

Check the current context

Use the kubectl config current-context command to see which context kubectl is currently using.

kubectl config current-context

Verify the configuration

You can verify that kubectl is configured correctly by running the following command

kubectl config get-contexts

Use kubectl config set-context

Once kubectl is configured, you can start using the kubectl config set-context command to manage your Kubernetes environment.

Here are some examples of how you might use the command:

To switch to a different context, use the following command

kubectl config use-context <context-name>

Use the following command to modify the current context:

kubectl config set-context --current --namespace=<namespace-name>

To create a new context based on an existing one:

kubectl config set-context <new-context-name> --namespace=<namespace-name> --cluster=<cluster-name> --user=<user-name>

With these steps, you should now be able to use the kubectl config set-context command to manage your Kubernetes environment more efficiently.

Basic usage of kubectl config set-context

Before you start modifying your kubeconfig, it’s helpful to know how to display its content. To do this, you can use the following command:

kubectl config view

The above command will display a YAML file like the one shown below.

apiVersion: v1
clusters:
- cluster:
certificate-authority-data: DATA+OMITTED
server: https://kubernetes.docker.internal:6443
name: docker-desktop
contexts:
- context:
cluster: docker-desktop
user: docker-desktop
name: docker-desktop
current-context: docker-desktop
kind: Config
preferences: {}
users:
- name: docker-desktop
user:
client-certificate-data: REDACTED
client-key-data: REDACTED

Kubectl config set-context: Ask for help

If you ever get stuck, just ask for help, like how I did here.

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:
--current=false: Modify the current context
Usage:
kubectl config set-context [NAME | --current] [--cluster=cluster_nickname] [--user=user_nickname]
[--namespace=namespace] [options]

Best Practices for kubectl config set-context

Here are some best practices for using kubectl config set-context:

1. Use descriptive names for your contexts

This will make it easier to identify the context you want to use when you are running kubectl commands.

2. Naming convention

Using a consistent naming convention for your contexts will make it easier to keep track of your contexts.

Keep your kubeconfig file in a secure location:

The kubeconfig file contains sensitive information, such as your Kubernetes cluster’s credentials.

3. Regular backups

Back up your kubeconfig file regularly. This will help you to recover your Kubernetes cluster’s credentials if they are lost or corrupted.

4. Use context switching

One of the biggest benefits of using the kubectl config set-context command is the ability to quickly switch between different Kubernetes contexts.

This can be especially useful if you’re working on multiple projects or need to manage multiple Kubernetes clusters.

However, keep in mind that the exact steps you need to follow may vary depending on your specific Kubernetes environment and the configuration of your kubectl tool.

It’s always a good idea to consult the official Kubernetes documentation or seek help from your team or community if you’re unsure about how to use a particular kubectl command.

kubectl config set-context Cheatsheet

Here are some examples of how to use the kubectl config set-context command.

To set the current context to the “default” context, you can use:

kubectl config use-context default

To create a new context named “staging” that points to the “staging” cluster and the “staging” user in the “default” namespace, write the following kubectl command:

kubectl config set-context staging --cluster=staging --user=staging --namespace=default

Write this kubectl command to delete the “staging” context:

kubectl config delete-context staging

To set or modify a cluster in the kubectl configuration file, use the kubectl command mentioned below. You can specify the cluster name, server address, and other parameters.

kubectl config set-cluster

This command sets/modifies user credentials in the kubectl configuration file. You can specify the user name, authentication type, and other parameters.

kubectl config set-credentials

This is a general-purpose command that can set/modify any field in the kubectl configuration file. You can specify the field name and value as arguments.

kubectl config set

This command sets the namespace for a specific context. You can specify the context name and the namespace name as arguments.

kubectl config set-context --namespace=<namespace> <context-name>

This command sets the cluster for a specific context. You can specify the context name and the cluster name as arguments.

kubectl config set-context --cluster=<cluster-name> <context-name>

This command sets the user for a specific context. You can specify the context name and the user name as arguments.

kubectl config set-context --user=<user-name> <context-name>

This command sets the context in a specific kubectl configuration file. You can specify the context name and the path to the configuration file as arguments.

kubectl config set-context --kubeconfig=<config-file> <context-name>

This command sets the default namespace for the currently selected context. You can specify the namespace name as an argument.

kubectl config set-context --current --namespace=<namespace>

This command sets the default cluster for the currently selected context. You can specify the cluster name as an argument.

kubectl config set-context --current --cluster=<cluster-name>

This command sets the default user for the currently selected context. You can specify the user name as an argument.

kubectl config set-context --current --user=<user-name>

This command sets the namespace for the currently selected context. You can specify the namespace name as an argument.

kubectl config set-context --namespace=<namespace> $(kubectl config current-context)

This command overwrites the default namespace for the currently selected context. You can specify the namespace name as an argument.

kubectl config set-context --current --namespace=<namespace> --overwrite

This command sets the default user and namespace for the currently selected context. You can specify the user name and namespace name as arguments.

kubectl config set-context --current --user=<user-name> --namespace=<namespace>

This command sets the default cluster, user, and namespace for the currently selected context. You can specify the cluster name, user name, and namespace name as arguments.

kubectl config set-context --current --cluster=<cluster-name> --namespace=<namespace> --user=<user-name>

This command sets the default cluster and user for the currently selected context, and sets the default namespace to “default”.

kubectl config set-context --current --cluster=<cluster-name> --user=<user-name> --namespace=default

These advanced commands provide more flexibility in how you manage and configure your kubectl environment.

For example, you can use the $(kubectl config current-context) syntax to set a property for the currently selected context without having to specify the context name explicitly.

Additionally, you can use the — overwrite flag to force kubectl to overwrite an existing property with a new value.

The above-mentioned commands were some permutations of all the kubectl set-context commands you could make use of.

Final thoughts

Overall, the kubectl config set-context command is a useful tool that can help you manage your Kubernetes environment more efficiently.

By taking the time to learn how to use this command effectively, you can streamline your workflow and avoid common configuration errors and then your rocket ship can navigate in the clouds safely.

So folks that’s all from my side, I wrote this guide for Humalect and thought might give it a backlink here. If you enjoyed, say hello to me on LinkedIn

Originally published at https://humalect.com.

--

--

Priyansh Khodiyar

I write highly researched technical articles on things I daily learn, sometimes code, and interview people. khodiyarPriyansh@gmail.com. Check my About section.