Kubectl Get Context: Its Uses and How to Get Started – Loft Labs

Kubernetes, the open-source container orchestration system, has gained a lot of traction in recent years. It is now one of the most popular ways to deploy and manage applications at scale. While Kubernetes is a powerful tool, it can be daunting for beginners to get started. In this post, you’ll learn how the kubectl command can be used to manage your Kubernetes contexts, making things easier for you.

The kubectl utility is a command-line interface (CLI) for interacting with Kubernetes. You can use it to manage Kubernetes resources such as pods, services, and deployments. To install kubectl, you simply need to run the commands according to the installation guide.

If you’re new to Kubernetes, the following resources can help you learn about the platform and how to get started. The Kubernetes documentation is a comprehensive resource covering all aspects of Kubernetes. Includes concept guides, installation instructions, and usage examples.

The

kubectl configuration file is a configuration file that stores all the information needed to interact with a Kubernetes cluster. It contains

the following information: The name of the Kubernetes cluster The location of the Kubernetes API server The

  • credentials (username and password) to authenticate with
  • the Kubernetes API server

  • The names of all contexts defined
  • in the cluster In the following sections, we will focus on a particular use case of the kubectl command: manage

  • Kubernetes
  • contexts

.

To view the configuration file, you can use the following

command: The output of this command

is shown below

.

codeblock7

Figure: The output of

the kubectl config view command

As you can see, this Kubernetes configuration file contains information about the clusters, contexts, and users that kubectl can access

. apiVersion defines the version of the configuration file. The clusters section contains information about Kubernetes clusters that kubectl can access. The contexts section contains information about the contexts that kubectl can access. The

  • current-context field specifies the context that kubectl is currently using. The
  • users

  • section contains information about the users that kubectl can access
  • .

A Kubernetes context is a group of access parameters that define which cluster you are interacting with, which user you are using, and which namespace you are working in. It is useful if you need to access different clusters for different purposes or if you want to limit your access to certain parts of a cluster. For example, if you have multiple Kubernetes clusters, each with its own set of users and permissions, you can use kubectl config use-context to switch between them.

alt_text

kubectl config get-contexts is a command that retrieves information about the current Kubernetes context. This command is part of the kubectl utility, which interacts with Kubernetes clusters. When you first install Kubernetes, a default context is created. This context points to a local Kubernetes cluster running on your development machine. If you want to work with multiple Kubernetes clusters, you can create additional contexts.

#Finding the current context in Kubectl To find the current context

, you can simply run the following command:

It will print the name of the

current context in kubectl

. The following screenshot shows the output.

alt_text

Figure: The output of

the kubectl config current-context command

You can switch between these contexts by using the kubectl config use-context command. You can create additional contexts if you want to work with multiple Kubernetes clusters. For example, you can have a context for each of the following:

Your on-premises Kubernetes cluster A tentative Kubernetes cluster A production Kubernetes cluster

Let’s look at some of the most common uses of this command.

alt_text

#Listing Contexts

Kubernetes As I mentioned earlier, the kubectl config get-contexts command can be used to enumerate all available Kubernetes contexts. To do this, simply run the following command in the terminal window:

It will print a list of all the contexts available in your Kubernetes configuration, as well as the currently active context. By default, the active context is marked with an asterisk.

codeblock5

Figure: The output of the kubectl config get-contexts command

By default, Kubernetes uses the default context. However, you can create and use multiple contexts if you have multiple Kubernetes clusters.

#Viewing

Kubernetes

context information

In addition to listing the available contexts, the kubectl get context command can also be used to view information about a specific context. To do this, simply specify

the name of the context you want to view:

Replace <context_name> with the name of the context you want to change to. For example, if you are switching to the staging

context, type the following:

It prints information such as the cluster name, the user name of the author or creator, and the namespace for the specified context.

alt_text

Figure: The output of the kubectl config get-contexts command

#Creating a new context

The kubectl config set-context command is used to create a new context in a kubeconfig file.

Here <context-name> is the new context you want to create, <namespace-name> is the namespace this context should point to, <user-name> is the

user you want to authenticate with, and <cluster-name> is the name of the Kubernetes cluster.

For example, the following command will create a new context named staging that points to a staging Kubernetes cluster

: The output of the above command

is shown below

:

codeblock

Figure: The output of the

kubectl config set-context command

You can verify the creation of this new context by listing the contexts using the kubectl config get-contexts command described in the previous section. The kubectl set-context can also be used to modify an existing context.

#Switching Kubernetes contexts If you want to switch to a different

Kubernetes

context

, you can use the kubectl config use-context command below

.

Here <context-name> is the name of the context you want to change to. It will change your active Kubernetes context to the one you specified. You can verify that the context has been changed by running the kubectl get current-context command without any arguments.

alt_text

Figure: The output of the kubectl config use-context

command

As you can see, the previous command changed the current context to a context called staging. You can verify this by running kubectl config current-context as in the screenshot above.

#Best practices when using ‘kubectl config set-context’

Switching between contexts is common when working with Kubernetes. You can set aliases for frequently used contexts to make it easier. For example, you can define an alias in the ~/.bashrc file or in the shell configuration file as shown below.

This is an easy way to shorten the command you would use to switch to the production context. After that, instead of typing the entire command, you can type the k8s-prod command to switch to the production context.

alt_text

#Deleting a

Kubernetes

context

The kubectl delete context command is used to delete an existing context from a kubeconfig file. This command has the following syntax

:

This is the name of the context that you want to delete

.

For example:

The above command will remove the context named demo from the current Kubernetes configuration. The following screenshot shows the output.

codeblock3

Figure: The output of the kubectl config delete-context

command

In this article, we cover the kubectl command-line tool. We start by showing you the command kubectl config get-contexts. This command is used to enumerate and view Kubernetes contexts. Next, we look at the kubectl config set-context command. This command allows you to switch between Kubernetes contexts. Next, we look at the kubectl config create-context command. This command allows you to create custom contexts in your kubeconfig file. Finally, we saw how to delete a context using the kubectl config delete-context command.

As you can see, the kubectl command is a versatile tool that can be used in many ways. Whether you’re looking to list, view, or switch between Kubernetes contexts, this command has you covered. For the full list of options for this command, take a look at the official reference of this tool and start using it today.

This post was written by Tarun Telang. Tarun is a software engineering leader with over 16 years of experience in the software industry with some of the world’s most recognized software development companies such as Microsoft, Oracle, BlackBerry and SAP. His areas of expertise include Java, web, mobile and cloud. He also has experience in managing software projects using agile and test-driven development methodologies.

Contact US