Skip to main content

Command Palette

Search for a command to run...

Handling Multiple Environments With kubectl

Published
2 min read
Handling Multiple Environments With kubectl

Introduction

To find what environment your kubectl is pointing to, you can check its current context. A context is a set of access parameters, like the cluster and user, defined in your kubeconfig file.

Here are the most common kubectl commands to inspect your current environment:

View your current context

To see only the name of the currently active context, use:

kubectl config current-context

This is a quick and simple way to verify your target environment before running commands that modify resources.


List all contexts

To see a table of all available contexts defined in your kubeconfig file, run:

kubectl config get-contexts

The output will list each context, its associated cluster and user, and its default namespace. The currently active context is marked with an asterisk (*).

Example output:

CURRENT   NAME         CLUSTER      AUTHINFO       NAMESPACE
*         dev          minikube     minikube       default
          prod         prod-cluster user-prod      prod-ns
          qa           qa-cluster   user-qa        qa-ns

In this example, the dev context is the active one.


View the full kubeconfig file

To view the complete details of your kubeconfig file, including cluster URLs, authentication information, and all defined contexts, run:

kubectl config view

This command provides a comprehensive overview of your entire configuration.


Switch contexts

If you need to change your environment, you can switch to a different context using the use-context command:

kubectl config use-context <context-name>

Replace with the name of the context you want to use, such as prod or qa. For example, to switch to the production environment:

kubectl config use-context prod

Conclusion

To summarize, the environment your kubectl is pointing to is determined by its active context, which is configured in your kubeconfig file. This context includes the cluster, user, and namespace for your kubectl commands.

The essential commands for managing your environments are:

  • kubectl config current-context: Shows the name of the environment you are currently connected to.
  • kubectl config get-contexts: Lists all available environments defined in your kubeconfig file. The active context is marked with an asterisk (*).
  • kubectl config use-context <context-name>: Switches your active environment to the specified context.

More from this blog

Cloud Tuned

623 posts

Your starting point for anything cloud: AWS, Azure, GCP, Serverless, Architecture, Hybrid Cloud, Systems Design and other Information Technology topics.