How to Use Kubectl Config Set-Context

If you are a Kubernetes user, you probably know how to use kubectl, the command-line tool for interacting with your cluster. But did you know that kubectl has a powerful feature that can make your life easier when working with multiple clusters, users, and namespaces?

It’s called kubectl config set-context, and it allows you to create, set, modify, and delete contexts in your kubeconfig file. In this tutorial, you will learn how to use kubectl config set-context to manage your contexts and access your cluster more efficiently.

Key Takeaways

  • Contexts provide a way to manage and organize workloads in a more efficient and scalable manner.
  • You can create and modify contexts in your kubeconfig file by using the command kubectl config set-context with different options.
  • You can switch between contexts by using the command kubectl config use-context with different context names.

Prerequisites

To follow along, make sure you have access to a running Kubernetes cluster. If you don’t have access to one, you can use a tool such as minikube to set up a Kubernetes cluster. Also, you’ll need to have kubectl installed on your local machine to interact with the Kubernetes cluster.

Try the Kubernetes Deployments Lab for free

Kubernetes Deployments Lab
Kubernetes Deployments Lab

What are Contexts in Kubernetes?

Contexts are shortcuts that let you quickly switch between different cluster configurations without typing long and tedious commands. Context details are stored in kubeconfig file - a YAML configuration file used by Kubernetes to authenticate and connect to the Kubernetes API server. The file is typically located in the user's home directory.

You can view contexts and the clusters they belong to using the following command:

kubectl config get-contexts

This will show you a table of all the contexts and their details, along with a star (*) indicating the current context.

The kubeconfig file contains three main sections: clusters, users, and contexts. You view them by opening the file using the command:

kubectl config view

Below is a snippet of the output we get after executing the command:

apiVersion: v1
kind: Config
preferences: {}
clusters:
- name: development
  cluster:
    server: https://1.2.3.4
    certificate-authority: fake-ca-file
- name: test
  cluster:
    server: https://5.6.7.8
    insecure-skip-tls-verify: true
users:
- name: developer
  user:
    client-certificate: fake-cert-file
    client-key: fake-key-file
- name: experimenter
  user:
    username: admin
    password: secret
contexts:
- name: dev-frontend
  context:
    cluster: development
    user: developer
    namespace: frontend
- name: dev-storage
  context:
    cluster: development
    user: developer
    namespace: storage
- name: exp-test
  context:
    cluster: test
    user: experimenter

As you can see, each section has a list of items that have a name and some details. The clusters section lists all the clusters that you have access to, along with their API server URL and certificate authority (or whether to skip TLS verification).

The users section lists all the users that you can authenticate as, along with their credentials (such as certificates, keys, usernames, or passwords). The contexts section lists all the contexts that you can use to access your clusters, along with their cluster, user, and namespace parameters.

A context is essentially a combination of a cluster, a user, and an optional namespace that defines how you want to interact with your cluster.

For example, the context dev-frontend means that you want to use the development cluster, authenticate as the user developer, and work in the namespace frontend.

By using contexts, you don’t have to specify these parameters every time you run a kubectl command. Instead, you can just switch to the context that you want to use and run your commands.

Polish your Kubernetes skills with our course: Kubernetes for the Absolute Beginner.

Kubernetes for the Absolute Beginners – Hands-on Tutorial | KodeKloud
Learn Kubernetes with simple, easy lectures and hands-on labs

How to Create Context

You can create or modify contexts in your kubeconfig file using the command kubectl config set-context. This command has the following syntax:

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

Using the command above, you can create or modify contexts in your kubeconfig file by specifying a name and, optionally, their cluster, user, and namespace parameters. If you omit any of these parameters, they will be inherited from the current context (or left blank if there is no current context). You can also use the --current flag to modify the current context instead of specifying its name.

Let’s see some examples of how to use this command. Suppose you want to create a new context called dev-test that uses the same cluster and user as dev-frontend but with a different namespace called test. You can run this command:

kubectl config set-context dev-test --namespace=test --current

This will create a new context called dev-test with the namespace test and the cluster and user parameters inherited from the current context, which is dev-frontend. You can verify that by running kubectl config view and looking at the contexts section:

contexts:
- name: dev-frontend
  context:
    cluster: development
    user: developer
    namespace: frontend
- name: dev-storage
  context:
    cluster: development
    user: developer
    namespace: storage
- name: dev-test
  context:
    cluster: development
    user: developer
    namespace: test # new context with different namespace
- name: exp-test
  context:
    cluster: test
    user: experimenter

You can also verify the change by running kubectl config get-contexts, which will show you a table of all the contexts and their details, along with a star (*) indicating the current context:

How to Modify Context

Now suppose you want to modify the existing context exp-test to use a different user called tester that you have defined in your users section. You can run this command:

kubectl config set-context exp-test --user=tester

This will modify the context exp-test to use the user tester instead of the user experimenter. You can verify that by running kubectl config view and looking at the contexts section:

contexts:
- name: dev-frontend
  context:
    cluster: development
    user: developer
    namespace: frontend
- name: dev-storage
  context:
    cluster: development
    user: developer
    namespace: storage
- name: dev-test
  context:
    cluster: development
    user: developer
    namespace: test 
- name: exp-test
  context:
    cluster: test
    user: tester # modified context with different user 

You can also verify the change by running the kubectl config get-contexts command. You'll get an output similar to this:

As you can see, using kubectl config set-context is a simple and convenient way to create and modify contexts in your kubeconfig file. You can use this command to define as many contexts as you need for your clusters, users, and namespaces.

How to Switch Between Contexts

Once you have defined your contexts, you can switch between them by using the command kubectl config use-context. This command has the following syntax:

kubectl config use-context <NAME>

You can switch to a different context by specifying its name. This will make the specified context the current context, which means that all subsequent kubectl commands will use its parameters.

Let’s see some examples of how to use this command. If you want to switch to the context dev-storage to work on the storage namespace of the development cluster. You can run this command:

kubectl config use-context dev-storage

This will make dev-storage the current context, which means that you are now using the cluster development, the user developer, and the namespace storage. You can verify that by running kubectl config current-context.

Now suppose you want to switch to the context exp-test to work on the test cluster. You can run this command:

kubectl config use-context exp-test

This will make exp-test the current context, which means that you are now using the cluster test, the user tester, and the default namespace. You can verify that by running kubectl config current-context.

As you can see, using kubectl config use-context is a quick and easy way to switch between contexts. You can use this command to access different clusters, users, and namespaces with just one word.

How to Delete a Context From The Kubeconfig File

Sometimes you may want to delete a context from your kubeconfig file, for example, if you no longer need to access a certain cluster, user, or namespace. To do that, you can use the command:

kubectl config delete-context

Let’s say you want to delete the context dev-storage from your kubeconfig file because you no longer need it. You can run this command:

kubectl config delete-context dev-storage

This will delete the context dev-storage from your kubeconfig file. Run the kubectl config view and look at the context section to verify:

contexts:
- name: dev-frontend
  context:
    cluster: development
    user: developer
    namespace: frontend
- name: exp-test
  context:
    cluster: test
    user: experimenter

As you can see, the context dev-storage and its details are no longer in the file.

As you can see, using kubectl config delete-context is an easy way to delete contexts from your kubeconfig file. You can use this command to remove any contexts that you don’t need anymore.

PRACTICE the concepts discussed here in a real-world live environment for FREE with KodeKloud Engineer. Join thousands of students gaining real, hands-on experience by working on actual project tasks on a live system.

Conclusion

In this tutorial, you learned how to use kubectl config set-context to manage your contexts and access your clusters more efficiently. By using contexts, you can simplify your kubectl commands and avoid errors and confusion when working with multiple clusters, users, and namespaces. You can also save time and effort by switching between different cluster configurations with just one command.

TEST your Kubernetes expertise with our FREE, fun, and challenging set of hands-on Kubernetes Challenges.

Find all our Kubernetes and certification exam preparation courses in our Kubernetes Learning Path.


More on Kubernetes: