Switching Between Clusters and Contexts
You'll learn how to navigate between different Kubernetes environments using built-in kubectl commands.
Now that you understand kubeconfig files and contexts, you need to know how to actively switch between them. Think of contexts like browser tabs for different websites—you can have multiple clusters configured (production, staging, development), but you're only actively working with one at a time.
Viewing Available Contexts
Before switching, see what's available:
kubectl config get-contexts
This displays all configured contexts, with an asterisk (*) marking your current active context. You'll see the cluster, user, and namespace for each.
To view just your current context:
kubectl config current-context
Switching Contexts
Change to a different context using:
kubectl config use-context <context-name>
For example, if you have a staging cluster:
kubectl config use-context staging-cluster
After switching, all subsequent kubectl commands target that cluster until you switch again.
Why This Matters
Analogy: It's like switching TV channels—your remote works the same way, but you're viewing completely different content. Same kubectl tool, entirely different infrastructure.
Without proper context switching, you might accidentally deploy to production when you meant staging, or query the wrong cluster's resources. Always verify your current context before running destructive operations.
Viewing Cluster Details
To see the full configuration of your current context:
kubectl config view
Key Takeaway: Use kubectl config use-context to switch between clusters safely, and always verify your current context with kubectl config current-context before making changes—especially in production environments.