Kubernetes

Kubectl Get Current Context

In this article, we will discuss the kubectl get current context. It is used for viewing the config details of the current context. The beginners might not know what ‘current context’ refers to and what its functions are. This post will elaborate on the concept in detail. So, let’s start with learning the basics first and then going in-depth.

What is Kubectl Context?

A kubectl context is basically a set of access parameters that contain a user, a namespace, and a cluster. A kubectl context is used to combine these sets of access parameters within an appropriate name. By default, the kubectl command line tool makes use of the above-mentioned parameters in order to communicate with the cluster.

What is Kubectl Current-Context?

The kubectl current context is the default cluster for kubectl, and all the commands of kubectl run against that cluster. When a cluster is created using a ‘gcloud container cluster create’ command, an automated entry is created with ‘kubeconfig’ in the user’s environment, and the current context will automatically change to that cluster.

Prerequisite:

Before getting in-depth into kubectl to get the current context, let’s see what the essential prerequisites that need to be met are. Ubuntu 20.04 is used to run the kubectl commands, and the Minikube cluster is installed first before running any command in kubectl. Minikube makes it easier to run the kubernetes commands locally. It runs one node Kubernetes clusters within a VM to make it easy to design and develop for Kubernetes.

To start a minikube, all you need is a VM or a docker environment. Just enter the ‘minikube start’ command on any container or virtual machine manager, and you are good to go. Below is the output of the minikube start command:

Text Description automatically generated

Kubectl Context and Configuration 

Kubernetes uses a YAML file to save the cluster authentication information for kubectl, which is known as kubeconfig. It consists of a list of contexts to which the kubectl refers while executing the commands and saving the file at $HOME/.kube/config, by default.

Kubectl config determines which cluster of Kubernetes will communicate with and modify the configuration details. See the code below in which the kubectl config view’ command is used.

However, to view merged configuration details while using multiple kubeconfig files simultaneously, the ‘KUBECONFIG=~/.kube/config:~/.kube/kubeconfig2’ command is used. Here is the executed command for your understanding:

After executing the above command, when the ‘kubectl config view’ command is executed, the following output is produced:

How to Get the User Password using Kubectl?

To get the password of the ‘e2e’ user, the following command is used:

Now let us see how the above command works, step by step. The jsonpath section of the command jsonpath=’{.users[].name)}’ displays the user password according to the parameter provided in it, i.e., user[] contains nothing, so it will only display the first user password. See the reference code below:

Now, if you want to display the list of users’ passwords, you need to provide a * as a parameter to the ‘users.’ jsonpath=’{.users[*].name)}’ will display the list of user’s passwords because * is provided as a parameter. See the reference command below:

How to Display a List of Kubectl Context?

The get-context command is used to display the list of contexts. The ‘kubectl config get-context’ command will display the list of contexts. See the list of kubectl context below:

How to Find the Current Context of Kubectl?

Kubectl current-context command shows the current context of kubectl. When you enter the ‘kubectl config current-context’ in the virtual machine environment, the following output will be displayed.

The ‘kubectl config use-context cluster-name’ command is used to set the default context to the given cluster name.

For example, the user wants to set the cluster name to minikube; here, the cluster-name is replaced with minikube, i.e., kubectl config use-context minikube. When you run this command, it will switch the current context to minikube. See the code below:

How to Set Passwords with the Set-Credentials Command?

The kubectl config set-credentials command is used to create a new user that supports the basic authentication. The kubectl config set-credential command allows you to choose to define the username and password to set the authentication process. Below you can see how to provide the username and password to the set-credential command.

What is Kubectl Config Set-Context?

The kubectl config set-context is used to save the namespaces permanently for all commands of kubectl in that context. The kubectl config set-context command is used to save the namespace permanently. See the code below:

Moreover, the set-context command is also used to set a context utilizing a certain username and namespace. The command is given below changes the minikube context to gce.

Now, if you run the current-context command, it will return ‘gce’ as the current context is set to ‘gce’ using the set-context command. See the output below:

What is Kubectl Config Unset?

The ‘kubectl config unset command’ unsets or deletes the specified parameter in a kubeconfig file. The following command will delete the user foo, or in other words, unsets the user foo.

What is Alias in Kubectl Config Used for?

The alias can speed up the programing as most of the patterns can be condensed into single-word bash alias. The following alias is defined to set or show context, the next time you need to set or show the context; you just need to use the alias name, which in the below code is ‘kx.’

You may now set or show the namespaces once you’ve established the context in ‘kx.’ Before calling ‘kn’ to set the namespace, the current context must be set. However, these aliases can only work for bash or bash compatible shells.

Conclusion:

The kubectl current context command gives you complete information about the current context; either it is minkube or gce. In this article, the kubectl get current context is discussed in detail, and examples are provided for each command to help you understand the functioning of the current context command better.

About the author

Kalsoom Bibi

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