Namespaces & Contexts

Beginner

Overview

Namespaces partition a cluster into virtual sub-clusters, so teams or environments can share one physical cluster without colliding. Most commands default to the 'default' namespace.

Use -n <namespace> to target one, or -A / --all-namespaces to see everything. A context bundles a cluster, user, and namespace; kubectl config use-context switches between them.

Setting a default namespace for your current context saves typing -n on every command.

Cheatsheet

kubectl get nsList namespaces
kubectl get pods -n kube-systemTarget a namespace
kubectl create namespace devCreate a namespace
kubectl config get-contextsList available contexts
kubectl config use-context prodSwitch clusters
kubectl config set-context --current --namespace=devSet a default namespace

Try it

A safe, simulated terminal. Run the suggested commands to see typical output.

simulated terminal

Type a command and press Enter, or click a suggestion below to run it.

$

Quick quiz

1. What problem do namespaces solve?

2. Which flag shows resources across every namespace?