Skip to main content

kubectx

kubectx is a tool to switch between Kubernetes contexts (clusters) quickly. It also includes kubens for switching between namespaces.

Prerequisites

Installation

Official installation guide: github.com/ahmetb/kubectx

Interactive Mode

Install fzf alongside kubectx for interactive fuzzy-search selection of contexts and namespaces. See the Interactive Mode documentation.

# Using Chocolatey
choco install kubectx

# Using scoop
scoop install kubectx

Or download from GitHub Releases.

Verify Installation

kubectx --help
kubens --help

kubectx Usage

Switch between Kubernetes contexts (clusters):

# List all contexts (current is highlighted)
kubectx

# Switch to a specific context
kubectx my-cluster

# Switch to previous context
kubectx -

kubens Usage

kubens changes the default namespace for kubectl commands, so you don't have to specify -n <namespace> every time.

Example

# Without kubens - must specify namespace each time
kubectl get pods -n demo-app
kubectl get services -n demo-app
kubectl logs my-pod -n demo-app

# With kubens - set namespace once
kubens demo-app
kubectl get pods # returns pods in demo-app namespace
kubectl get services # returns services in demo-app namespace
kubectl logs my-pod # gets logs from demo-app namespace

Common Commands

# List all namespaces (current is highlighted)
kubens

# Switch to a specific namespace
kubens demo-app

# Switch to previous namespace
kubens -