Get 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
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
Switches to a different Kubernetes context.
Example:
This command switches to the context named "my-production-cluster".
Using kubectx
Shows List of Contexts:
To Change Context:
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
Renames an existing Kubernetes context.
Example:
This command renames "old-context-name" to "new-context-name".
Delete Context
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