Kubernetes

Kubectl List and Switch Context

For various projects, we frequently need to switch between Kubernetes clusters. It can be difficult to keep track of each of their kubectl configurations and files. The kubectl command-line client stores the Kubernetes endpoint and credentials in the /.kube/config file by default. You should be able to see the configurations in that file if you use minikube or Docker Desktop’s local Kubernetes. When working with a cloud-based Kubernetes instance, the configuration is provided as a yml file by the cloud console. The file must then be specified as the value of the KUBECONFIG environment variable, which is used by kubectl. This can become quite inconvenient and difficult to manage.

In Kubernetes, a Context is used to aggregate access parameters in a kubeconfig file under easy-to-remember names. Cluster, Namespace, and User are the three parameters that each Context holds. We’ll show you how to use the kubectl command to view and adjust Context in Kubernetes in this article.

To run the instructions in Kubernetes, we have installed Ubuntu 20.04 on our Linux operating system. You can do the same. In order to run Kubernetes on Linux, you must also install the Minikube cluster on your workstation. Minikube creates a smooth experience by allowing you to test commands and programs in a systematic manner. As a result, it offers the best learning experience for Kubernetes beginners. The minikube cluster must be started initially.

Then, in Ubuntu 20.04, go to the command line terminal you just installed. By using the Ctrl+Alt+T shortcut key or putting “Terminal” into the Ubuntu 20.04 system’s search box, you can do so. Either of the aforementioned methods will start the terminal in its entirety. After that, the minikube will be initiated.

$ minikube start

To start the minikube, type “minikube start” into the terminal. A virtual machine capable of running a single node cluster will be constructed, and the Kubernetes cluster will be launched. It also works with the kubectl setup. This will be originally used to communicate with the cluster. Now, let’s get started.

How to Switch Context in Kubernetes?

A context is a configuration that you use to connect to a certain cluster. kubectl config is the traditional solution for switching/reading/manipulating different Kubernetes environments (aka Kubernetes contexts). The commonly used kubectl commands are as follows:

  • Current-context is used to show the current-context
  • Delete-cluster is used for removing the specified cluster from the kubeconfig
  • Get-contexts are used to describe one or many contexts
  • Get-clusters shows clusters that are defined in the kubeconfig
  • Set-context modifies the contextual entry in kubeconfig
  • Set-credentials is a kubeconfig command that creates a user entry.
  • The view is utilized to illustrate the merged settings of kubeconfig

All of your Kubernetes cluster’s resource types are supported via commands. Custom resource definitions have their own RESTful endpoints that kubectl may access because they interface with the Kubernetes API.

Utilize the command “kubectl config set-context my-context —cluster=my-app —namespace=production” to configure per-context parameters. This approach will build a new context named my-context with default Kubernetes cluster and namespace parameters. Any subsequent kubectl calls would use the parameters from the my-context context, linking you to the my-app cluster inside the production namespace.

By default, the kubectl tool communicates with the cluster using parameters from the present Context. The current context will be displayed with the following command.

The following command is used in a kubeconfig file to list all the contexts.

$ kubectl config get-contexts

Create a New Context

Here, we have constructed a context because there is not one that can be used for switching. This command will create a context based on a username.

$ kubectl config set-context gce –user=cluster-admin

The context has now been shifted to the newly formed context.

$ kubectl config use-context gce

Use the following code to revert to a previous place.

$ kubectl config use-context minikube

The effective use of contexts greatly simplifies kubectl interactions. You must manually generate distinct configuration files that are swapped using the KUBECONFIG option or an environment variable if you don’t have them.

Conclusion

This article was about the kubectl list and switch context. Here we have revealed how you can create and use contexts. You could use kubectl config use-context statement to quickly switch among clusters after you have defined your contexts inside one or more configuration files. In addition to that, we also have discussed how you can switch between contexts. You now know that several “contexts” can be defined within configuration files. These allow you to organize frequently used “access parameters” as cluster URLs and user accounts into named references.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content