Manage kube multiple contexts

Managing multiple Kubernetes clusters is a daily reality for DevOps and SRE teams. Whether switching between development, staging, and production environments, or working across multiple cloud providers, safe and efficient Kubernetes context management is critical to avoid operational mistakes.

Why Context Switching Matters

Incorrect Kubernetes context usage is one of the most common operational risks in day-to-day cluster management:

  • Executing destructive commands against the wrong cluster
  • Sharing a globally modified kubeconfig across multiple terminals
  • CI/CD pipelines unintentionally mutating local kubeconfig state

The tools discussed below address these risks in different ways, each with its own trade-offs.

Supporting Tools

For an improved command-line experience, it is strongly recommended to install a fuzzy finder such as fzf. When combined with Kubernetes tooling, it enables fast, interactive selection of contexts and namespaces.

kubectl (Built-in Context Management)

kubectl provides native commands for inspecting and managing contexts, clusters, and users within the kubeconfig file. While powerful, these commands are verbose and not optimized for frequent context switching.

Common commands

# View the entire kubeconfig
kubectl config view

# List all configured contexts
kubectl config get-contexts

# List known clusters
kubectl config get-clusters

# Remove an obsolete cluster entry
kubectl config delete-cluster "arn:aws:eks:us-west-2:${AWS_ACCOUNT_ID}:cluster/dev-west2"

This approach is suitable for administrative cleanup, but less convenient for daily interactive use.

kubectx+kubens (The Classic Approach)

kubectx and kubens are lightweight command-line utilities that simplify switching Kubernetes contexts and namespaces by updating the active configuration in the local kubeconfig file. They are widely adopted due to their simplicity and minimal setup requirements.

When combined with fzf, they provide an efficient interactive workflow.

Example usage

# Interactively select a Kubernetes context using fzf
kubectx

# Interactively select a Kubernetes namespace using fzf
kubens

Pros

  • Simple and fast
  • Widely adopted and well-documented
  • Zero runtime dependencies

Cons

  • Modifies kubeconfig globally
  • Unsafe when multiple terminals are open
  • Easy to accidentally target production

Best suited for: Single-shell workflows or environments with a limited number of clusters.

Kubie (Shell-Isolated Contexts)

Kubie addresses one of the major limitations of kubectx: global context mutation. Each shell session maintains its own isolated Kubernetes context and namespace, significantly reducing the risk of accidental cross-cluster operations.

Example usage

# Interactively select a Kubernetes context using fzf
kubie ctx prod-cluster

# Interactively select a Kubernetes namespace using fzf
kubie ns kube-system

Pros

  • Per-shell context isolation
  • Much safer for multi-terminal workflows
  • Supports linting and inspection

Cons

  • Slightly more setup than kubectx
  • Smaller ecosystem

Best suited for: Engineers managing multiple clusters concurrently, especially in production-heavy environments.

K9s (Interactive TUI)

K9s is an interactive terminal-based UI that provides real-time visibility into Kubernetes clusters. In addition to context switching, it offers resource browsing, log streaming, and exec capabilities.

Example usage

k9s
:ctx

Pros

  • Visual overview of clusters
  • Reduces CLI command volume
  • Great for debugging and exploration

Cons

  • Overkill for simple switching
  • Not ideal for scripting

Best suited for: SREs and platform engineers performing operational debugging and cluster inspection.