Sign In

Curriculum 3: Your First Kubectl Commands

Exploring Namespaces

10 min · 15 XP

Exploring Namespaces

Namespaces are a way to divide a single Kubernetes cluster into multiple virtual clusters. They provide isolation, organization, and access control for resources.

What Are Namespaces?

Think of namespaces like folders on your computer. Just as you organize files into Documents, Downloads, and Projects folders, namespaces organize Kubernetes resources into logical groups. Resources in one namespace are isolated from resources in another.

Common use cases for namespaces include separating environments (dev, staging, production), isolating teams, and managing access control.

Default Namespaces

Every Kubernetes cluster comes with these built-in namespaces:

# List all namespaces
kubectl get namespaces
NAME              STATUS   AGE
default           Active   5d
kube-system       Active   5d
kube-public       Active   5d
kube-node-lease   Active   5d
  • default -- where your resources go if you do not specify a namespace
  • kube-system -- contains cluster components like CoreDNS, kube-proxy, and the metrics server
  • kube-public -- readable by all users, rarely used in practice
  • kube-node-lease -- holds Lease objects for node heartbeats

Working with Namespaces

Specify a namespace with the -n flag:

# List pods in a specific namespace
kubectl get pods -n kube-system

# Describe a service in a namespace
kubectl describe service kube-dns -n kube-system

To see resources across all namespaces, use the -A flag:

kubectl get pods -A

Switching Your Default Namespace

If you are working in a namespace frequently, set it as your default so you do not have to type -n every time:

# Set the default namespace for your current context
kubectl config set-context --current --namespace=development

# Verify your current namespace
kubectl config view --minify --output 'jsonpath={.contexts[0].context.namespace}'

Exploring kube-system

The kube-system namespace is worth exploring because it shows the components that keep your cluster running:

kubectl get pods -n kube-system

You will see system Pods like coredns, etcd, kube-apiserver, kube-scheduler, and others depending on your cluster setup.

Key Takeaways

  • Namespaces divide a cluster into isolated virtual clusters
  • Use -n <namespace> to target a specific namespace
  • Use -A to see resources across all namespaces
  • The kube-system namespace contains critical cluster components
  • Set a default namespace with kubectl config set-context to save typing