Sign In

Curriculum 10: Working with Namespaces

Creating Namespaces

10 min · 15 XP

Creating Namespaces

Namespaces let you partition a single Kubernetes cluster into isolated virtual clusters. They are the primary tool for organizing resources, separating teams, and enforcing access control.

Creating a Namespace Imperatively

The fastest way to create a namespace is with kubectl create:

# Create a namespace called "development"
kubectl create namespace development

# Verify it exists
kubectl get namespaces

Creating a Namespace Declaratively

For version-controlled infrastructure, define the namespace in a YAML file:

apiVersion: v1
kind: Namespace
metadata:
  name: staging
  labels:
    team: platform
    environment: staging

Apply the manifest:

kubectl apply -f namespace.yaml

Generating YAML from the CLI

You can combine imperative and declarative approaches using dry-run:

kubectl create namespace production \
  --dry-run=client -o yaml > namespace.yaml

This generates the YAML without creating the resource, letting you review and customize before applying.

Naming Best Practices

Follow these conventions when naming namespaces:

  • Use lowercase letters, numbers, and hyphens only
  • Keep names short and descriptive -- payments-api, not the-payments-api-service-team
  • Align names with teams or environments -- team-frontend, staging, prod
  • Avoid prefixing with kube- since Kubernetes reserves that prefix for system namespaces

Default Namespaces

Every cluster ships with these namespaces:

NamespacePurpose
defaultWhere resources land when no namespace is specified
kube-systemKubernetes control plane components
kube-publicPublicly readable resources, mostly unused
kube-node-leaseNode heartbeat tracking
# List all namespaces
kubectl get ns

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

Key Takeaways

  • Use kubectl create namespace for quick creation
  • Use YAML manifests for production and version control
  • Stick to lowercase, hyphen-separated names
  • Avoid the kube- prefix -- it is reserved for system use