Skip to main content
kubens is a utility to switch between Kubernetes namespaces quickly. It provides a faster way to change the active namespace than using kubectl config set-context --current --namespace.

Basic Usage

List All Namespaces

Display all namespaces in the current context:
kubens
Example output:
default
kube-system
kube-public
kube-node-lease
production
staging
development
The current active namespace is highlighted with a different color.

Switch to a Namespace

Switch to a specific namespace by name:
kubens <namespace-name>
1

Switch namespace

kubens production
Output:
Active namespace is "production".
2

Verify the switch

kubectl config view --minify --output 'jsonpath={..namespace}'
Output:
production
All subsequent kubectl commands will operate in the selected namespace by default.

Switch to Previous Namespace

Quickly switch back to the previous namespace using the - shorthand:
kubens -
Example:
# Currently in 'production'
kubens staging
# Active namespace is "staging".

kubens -
# Active namespace is "production".
The - shorthand works like cd - in bash, allowing you to quickly toggle between two namespaces in the current context.

Namespace Operations

Show Current Namespace

Display the currently active namespace:
kubens -c
# or
kubens --current
Output:
production
If no namespace is set:
(namespace not set)

Force Switch to Namespace

Switch to a namespace even if it doesn’t exist (useful for creating it later):
kubens <namespace-name> --force
# or
kubens <namespace-name> -f
Example:
kubens new-feature --force
# Active namespace is "new-feature".

# The namespace doesn't exist yet
kubectl get pods
# Error from server (NotFound): namespaces "new-feature" not found

# Create the namespace
kubectl create namespace new-feature
# namespace/new-feature created

# Now kubectl commands work
kubectl get pods
# No resources found in new-feature namespace.
By default, kubens validates that the namespace exists before switching. Use --force only when you’re sure you want to set a namespace that doesn’t exist yet.

Command Reference

All Commands

CommandDescription
kubensList all namespaces
kubens <NAME>Switch to namespace NAME
kubens <NAME> -fForce switch to NAME (even if it doesn’t exist)
kubens -Switch to previous namespace
kubens -c, --currentShow current namespace
kubens -h, --helpShow help message
kubens -V, --versionShow version

Using as kubectl Plugin

kubens can also be used as a kubectl plugin:
kubectl ns
kubectl ns production
kubectl ns -
When installed as a kubectl plugin (as kubectl-ns), all the same commands work with kubectl ns instead of kubens.

Practical Examples

Working Across Namespaces

# Check what namespaces are available
kubens

# Switch to production to check status
kubens production
# Active namespace is "production".

kubectl get pods
# Lists pods in production

# Quick check in staging
kubens staging
# Active namespace is "staging".

kubectl get pods
# Lists pods in staging

# Jump back to production
kubens -
# Active namespace is "production".

Development Workflow

# Start in your feature namespace
kubens feature-auth-refactor
# Active namespace is "feature-auth-refactor".

# Deploy your changes
kubectl apply -f deployment.yaml

# Test against staging data
kubens staging
kubectl port-forward svc/database 5432:5432

# Back to your feature namespace
kubens -

# Run integration tests
kubectl logs -f deployment/api

Multi-Cluster Namespace Management

kubens works with the current context. Combine with kubectx for multi-cluster workflows:
# Switch to dev cluster
kubectx dev-cluster
# Switched to context "dev-cluster".

# Switch to your namespace in dev
kubens my-feature
# Active namespace is "my-feature".

# Switch to staging cluster
kubectx staging-cluster
# Switched to context "staging-cluster".

# Switch to same namespace in staging
kubens my-feature
# Active namespace is "my-feature".
kubens stores the previous namespace per context. Each context maintains its own history, so switching contexts doesn’t affect your namespace history.

Understanding Namespace Validation

By default, kubens validates that the namespace exists by querying the Kubernetes API:
# This will fail if namespace doesn't exist
kubens nonexistent
# Error: no namespace exists with name "nonexistent"

# Override validation with --force
kubens nonexistent --force
# Active namespace is "nonexistent".
Namespace validation requires cluster connectivity. If you’re offline or the cluster is unreachable, use --force to set the namespace anyway.

Troubleshooting

No Current Context

If you see an error about no current context:
kubens
# Error: current-context is not set
Set a context first using kubectx:
kubectx minikube
kubens

Cluster Not Accessible

If the cluster is not accessible, namespace validation fails:
kubens production
# Error: failed to query if namespace exists (is cluster accessible?)
Use --force to set the namespace anyway:
kubens production --force
# Active namespace is "production".

Next Steps

Interactive Mode

Use fzf for interactive namespace selection

kubectx Commands

Learn about managing contexts with kubectx

Build docs developers (and LLMs) love