Kubectl Get Current Context – Linux Hint

In this article, we will discuss the current context of kubectl. Used to view the configuration details of the current context. Beginners may not know what the “current context” refers to and what its functions are. This publication will elaborate on the concept in detail. So, let’s start by learning the basics first and then digging deeper.

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 access parameter sets into an appropriate name. By default, the kubectl command-line tool uses the parameters mentioned above to communicate with the cluster.

What is Kubectl Current-Context?

The current kubectl context

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

Prerequisite:

Before we delve into kubectl to get the current context, let’s see what are the essential prerequisites that must be met. Ubuntu 20.04 is used to run kubectl commands, and the Minikube cluster is installed first before running any kubectl commands. Minikube makes it easy to run kubernetes commands locally. Run single-node Kubernetes clusters within a virtual machine to facilitate Kubernetes design and development.

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

Auto-generated text description

Kubectl context and configuration Kubernetes

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

The Kubectl configuration determines which Kubernetes cluster will communicate and modifies the configuration details. See the following code that uses the kubectl config view’ command.

<img src

=”https://linuxhint.com/wp-content/uploads/2022/04/word-image-413.png” alt=”” />

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

: After running the above command,

when the command ‘kubectl config view’ is executed, the

following output occurs:

How to get user password

using Kubectl? To obtain the password

of the user ‘e2e’, the following command is used: Now let’s

see how the above command works, step by step. The jsonpath section of the jsonpath='{.users[].name)}’ command 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 user passwords

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

How to display

a Kubectl

context list?

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

the kubectl context list below: How to find the current kubectl context? The current context command of kubectl displays

the current context of kubectl

. When you enter the ‘kubectl config current-context’ into the virtual machine environment, the following output will be displayed.

The command ‘kubectl config use-context cluster-name’ 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, that is, kubectl config use-context minikube. When you run this command, it changes the current context to minikube. See the following code:

How to set

passwords with the Set-Credentials command?

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

What is Kubectl Config Set-Context?

The kubectl

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

In addition, the set-context command is also used to establish a context using a certain user name and namespace. The command given below changes the context minikube to gce.

Now, if you run the current-context command

, it will return ‘gce’ since the current context is set to ‘gce’ using the set-context command. See the result below:

What is Kubectl Config Unset?

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

What is Alias used for in Kubectl Config?

The alias can

speed up programming, as most patterns can be condensed into single-word bash aliases. The following alias is defined to set or display the context, the next time you need to set or display the context; You just need to use the alias name, which in the following code is ‘KX’.

You can now set or display the namespaces once you have set the context to ‘kx’. Before calling ‘kn’ to set the namespace, the current context must be set. However, these aliases can only work for shells that support bash or bash.

Conclusion:

The kubectl current context command provides complete information about the current context; either 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 better understand how the current context command works.