AI Hints
Home›Kubernetes Tutorial›Namespaces and Deployments
Kubernetes · Lesson 13 of 86

Namespaces

Virtual folders inside a cluster.

⏱ 2 min read▶ Video at 33:54🎓 Free
Watch this lesson in the Kubernetes courseWatch on YouTube ↗Full Kubernetes course

A real cluster is shared by many teams and many apps. Without some organisation, it would become a messy room where everyone's things are mixed together. Namespaces are the solution. In this lesson, you will see how they divide one cluster into separate, tidy spaces.

Namespaces
From the video: Namespaces
  • One cluster, many teams
  • Namespaces keep things apart
  • Like folders on a computer

What a namespace is

A namespace is a virtual folder inside a cluster. Objects in one namespace are separate from those in another, so two teams can both have a pod called web, without any clash. Namespaces are also the place to attach rules, like how much memory a team may use, or who may access what. Kubernetes starts with four, default, where your objects go if you do not choose, kube system, for the cluster's own parts, kube public, and kube node lease.

Virtual folderseparates teams and apps
Same name, no clashweb in two namespaces
Rules attach herequotas and access
default, kube-systemtwo you already know

Working with namespaces

Let's try. We create a namespace called shop. Then we run a pod inside it, using dash n, the namespace flag. Now we list pods. In the default namespace, there is nothing. The pod lives in shop, so we must ask for it with dash n shop. And the capital A flag, shows pods from all namespaces together. If you forget dash n, and see nothing, remember to check your namespace first, it is the most common beginner surprise.

kubectl
kubectl create namespace shop
namespace/shop created
kubectl run web --image=nginx:1.27 -n shop
pod/web created
kubectl get pods
No resources found in default namespace.
kubectl get pods -n shop
NAME   READY   STATUS    RESTARTS   AGE
web    1/1     Running   0          12s
kubectl get pods -A | head -6
NAMESPACE     NAME                          READY   STATUS    RESTARTS      AGE
kube-system   coredns-66bc5c9577-m52m5      1/1     Running   2 (15m ago)   16m
kube-system   etcd-k8s                      1/1     Running   0             16m
kube-system   kindnet-blfp9                 1/1     Running   0             15m
kube-system   kindnet-glr5z                 1/1     Running   0             16m
kube-system   kindnet-mrj8p                 1/1     Running   0             16m

Switching your default

Typing dash n every time is tiring. You can change your default namespace, with the config command. After we set the current context to the shop namespace, a plain kube control get pods now shows the shop pod. That is convenient, but also dangerous, because you may forget where you are, and delete something in the wrong place. Many teams use a helper to show the current namespace in their prompt.

kubectl
kubectl create namespace shop
namespace/shop created
kubectl run web --image=nginx:1.27 -n shop
pod/web created
kubectl config set-context --current --namespace=shop
Context "k8s" modified.
kubectl get pods
NAME   READY   STATUS    RESTARTS   AGE
web    1/1     Running   0          13s
kubectl config set-context --current --namespace=default
Context "k8s" modified.

Summary

Good. Namespaces are virtual folders. They separate teams and apps, and let you attach limits and access rules. Remember dash n, and the capital A flag. Next, we meet the object that keeps a fixed number of pods alive, the replica set.

Key takeaways
  • Namespaces separate teams and apps
  • Use -n, or set a default
  • -A shows every namespace
Next up: ReplicaSets — keeping the right number of pods