Skip to content

6.20 kubectx and kubens

Abstract

kubectx and kubens are lightweight command-line utilities that simplify switching between Kubernetes contexts and namespaces.

In large production Kubernetes environments with multiple clusters and namespaces, these tools significantly reduce operational complexity and human errors.


Why These Tools Are Useful

In real production environments, engineers often work with:

  • Multiple Kubernetes clusters
  • Multiple environments (dev, staging, prod)
  • Dozens or hundreds of namespaces

Using only kubectl config commands becomes slow and error-prone.

Warning

Accidentally executing commands in the wrong cluster or namespace can cause serious production incidents.

These tools simplify context and namespace switching.


kubectx

kubectx is used to switch between Kubernetes contexts (clusters) quickly.

Contexts represent cluster + user + namespace configuration in kubeconfig.

Tip

kubectx replaces lengthy kubectl config use-context commands.


kubectx Installation

sudo git clone https://github.com/ahmetb/kubectx /opt/kubectx
sudo ln -s /opt/kubectx/kubectx /usr/local/bin/kubectx
brew install kubectx

kubectx Usage

List Contexts

kubectx

Displays all contexts configured in kubeconfig.


Switch Context

kubectx <context_name>

Example:

kubectx production-cluster

Switch Back to Previous Context

kubectx -

Show Current Context

kubectx -c

kubens

kubens is used to switch namespaces quickly within the current Kubernetes context.

Tip

Instead of using long commands like:

kubectl config set-context --current --namespace=<namespace>

kubens simplifies this to one command.


kubens Installation

sudo git clone https://github.com/ahmetb/kubectx /opt/kubectx
sudo ln -s /opt/kubectx/kubens /usr/local/bin/kubens
brew install kubectx

kubens Usage

Switch Namespace

kubens <namespace>

Example:

kubens monitoring

Switch Back to Previous Namespace

kubens -

Production Workflow Example

Example production scenario:

  1. Switch cluster
  2. Switch namespace
  3. Execute kubectl commands
kubectx prod-cluster
kubens payments
kubectl get pods

Example

This workflow reduces operational mistakes when managing multiple environments.


Best Practices (Production)

Recommended

  • Always confirm current context before executing commands
  • Use kubectx -c to verify cluster
  • Use kubens to isolate namespace operations
  • Maintain clear naming conventions for clusters
  • Separate dev / staging / prod contexts
  • Use shell prompts that display context and namespace

Observability Tip

Tip

Many engineers customize their shell prompt to display:

  • Kubernetes context
  • Namespace
  • Cluster

This prevents accidental commands in production.

Example tools:

  • kube-ps1
  • starship prompt

Do's

  • Use kubectx for multi-cluster operations
  • Use kubens for namespace isolation
  • Verify cluster before running destructive commands
  • Maintain clear kubeconfig naming conventions
  • Use aliases and shell integrations

Don'ts

  • Don't run commands without verifying context
  • Don't mix production and development contexts
  • Don't use default namespace in production
  • Don't share kubeconfig files insecurely

Security Considerations

Danger

Incorrect context or namespace usage can lead to:

  • deleting production resources
  • modifying critical workloads
  • exposing sensitive services

Mitigation:

  • enforce RBAC policies
  • restrict production access
  • use read-only contexts where possible

kubectx vs kubens

Tool Purpose
kubectx Switch Kubernetes cluster contexts
kubens Switch Kubernetes namespaces

Summary

Quote

  • kubectx simplifies switching between Kubernetes clusters
  • kubens simplifies switching namespaces
  • These tools reduce operational errors in production
  • Essential utilities for engineers managing multi-cluster environments