Kubernetes Context Management

Essential commands for managing Kubernetes contexts

Get Current Context

kubectl config current-context

Displays the currently active Kubernetes context.

Usage:

Use this command to check which Kubernetes cluster and namespace you're currently working with.

List All Contexts

kubectl config get-contexts

Shows a list of all available Kubernetes contexts along with their details.

Output Explanation:

  • CURRENT → Asterisk (*) indicates the active context
  • NAME → Name of the context
  • CLUSTER → Cluster associated with the context
  • AUTHINFO → Authentication information
  • NAMESPACE → Default namespace for the context

Change Context

kubectl config use-context [contextName]

Switches to a different Kubernetes context.

Example:

kubectl config use-context my-production-cluster

This command switches to the context named "my-production-cluster".

Using kubectx

Shows List of Contexts:

kubectx

To Change Context:

kubectx <contextName>

A community tool that provides an easier way to list and change contexts.

Installation:

  • macOS (Brew): brew install kubectx
  • Ubuntu: sudo apt install kubectx
  • Windows (Chocolatey): choco install kubectx-ps

To list contexts: kubectx

Rename Context

kubectl config rename-context [old-name] [new-name]

Renames an existing Kubernetes context.

Example:

kubectl config rename-context old-context-name new-context-name

This command renames "old-context-name" to "new-context-name".

Delete Context

kubectl config delete-context [contextName]

Removes a Kubernetes context from your configuration.

Important:

This command only removes the context reference from your kubeconfig file. It does not delete the actual cluster resources.

About Kubernetes Contexts

What is a Context?

A context in Kubernetes is a set of access parameters that contains a Kubernetes cluster, a user, and a namespace. The current context is the cluster that is currently the default for kubectl commands.

Contexts are stored in the kubeconfig file, typically located at ~/.kube/config.

Why Use Multiple Contexts?

  • Switch between different environments (development, staging, production)
  • Work with multiple clusters from different cloud providers
  • Manage access to different namespaces within the same cluster
  • Isolate work for different projects or teams