The issue opens pre-filled with this page title and URL.
A namespace splits a Kubernetes cluster into distinct name spaces: a name only makes sense inside its namespace, so two resources with the same name can coexist in two namespaces, and access rights as well as quotas are attached to a namespace. In this lesson, you create two namespaces, you place the same objects in them, you check what stays separate (names, Secrets) and what does not (the network, the Nodes), then you place the protections that fill these gaps.
Create a namespace with a command and with a manifest
Name the initial namespaces of a cluster and say what each one holds
Work in a namespace with -n, then change the default namespace
Prove what a namespace isolates and what it does not
Place the protections of a team namespace: quotas, limits, NetworkPolicy, RBAC
How do you create a namespace?
Two gestures create a namespace. The kubectl create namespace command is enough for a test. A manifest lets you add labels and can be versioned. Create team-dev with the command and team-qa with a manifest that carries the label equipe: qa:
Terminal window
kubectlcreatenamespaceteam-dev
kubectlapply-f-<<'EOF'
apiVersion: v1
kind: Namespace
metadata:
name: team-qa
labels:
equipe: qa
EOF
kubectlgetnamespaces--show-labels
namespace/team-dev created
namespace/team-qa created
NAME STATUS AGE LABELS
default Active 25s kubernetes.io/metadata.name=default
kube-node-lease Active 25s kubernetes.io/metadata.name=kube-node-lease
kube-public Active 25s kubernetes.io/metadata.name=kube-public
kube-system Active 25s kubernetes.io/metadata.name=kube-system
local-path-storage Active 21s kubernetes.io/metadata.name=local-path-storage
team-dev Active 0s kubernetes.io/metadata.name=team-dev
team-qa Active 0s equipe=qa,kubernetes.io/metadata.name=team-qa
The cluster already contains namespaces that you did not create, visible in the list above. The first four exist in every Kubernetes cluster and each has a specific role; the fifth, local-path-storage, is added by kind to provide local storage:
Namespace
What it holds
default
Resources created without specifying a namespace
kube-system
The cluster components (DNS, network proxy, network plugin)
kube-public
Data readable without authentication, such as the cluster-info ConfigMap
kube-node-lease
One small object per node, which signals that the node is alive
Swipe to see more
Check what kube-system and kube-node-lease contain:
The two commands list the cluster components (CoreDNS, etcd, the API server, the kindnet network plugin, kube-proxy) and one Lease object per node, the small object described in the table. Do not create your applications in kube-system: it belongs to the cluster and you risk disturbing its components.
What does a namespace separate?
A namespace first separates names: two objects of the same type and the same name can exist if they are in two different namespaces. Create an api Deployment in each of the two namespaces, then list them together with the -A option (all namespaces):
Waiting for deployment "api" rollout to finish: 0 of 1 updated replicas are available...
deployment "api" successfully rolled out
Waiting for deployment "api" rollout to finish: 0 of 1 updated replicas are available...
deployment "api" successfully rolled out
NAMESPACE NAME READY UP-TO-DATE AVAILABLE AGE
team-dev api 1/1 1 1 12s
team-qa api 1/1 1 1 12s
The two api coexist without conflict, and the NAMESPACE column tells them apart (team-dev and team-qa, each at 1/1). Without the -n option, kubectl works in default: the two Deployments are therefore not there. To avoid repeating -n team-dev in every command, change the default namespace of the context, then restore it:
A Secret created in one namespace does not exist in the other. The cle Secret is created in team-dev; reading it from team-qa fails with NotFound, and the failure is intended:
Error from server (NotFound): secrets "cle" not found
ConfigMaps, ServiceAccounts, Roles and quotas behave the same way: they belong to a namespace. Access rights (RBAC) and quotas are therefore attached to it, and that is what makes a namespace useful for a team.
What does a namespace not separate?
A namespace is not a network security boundary. Expose the api Deployment of team-qa through a Service, then call it from a Pod of team-dev using its DNS name completed with the namespace (api.team-qa, as in the lesson Services):
The call succeeds and returns the beginning of the Nginx welcome page: a Pod in one namespace reaches a Service in another without any obstacle. The cluster network does not distinguish namespaces as long as no NetworkPolicy restricts it.
A reminder about the name used: the short name api only resolves in the namespace of the client, so it does not designate the team-qa Service from team-dev. The name <service>.<namespace>, or <service>.<namespace>.svc.cluster.local, works from any namespace. The gesture is demonstrated in the section How do you reach a Service by its DNS name? of the Services lesson.
Conversely, some resources belong to no namespace, because they describe the whole cluster:
The diagram sums up the two findings: inside each namespace, the objects are separated; between the two, the network stays open, and the Nodes and volumes are shared.
Which protections should you add to a team namespace?
A newly created namespace has no limit: describe confirms it.
Terminal window
kubectldescribenamespaceteam-dev
Name: team-dev
Labels: kubernetes.io/metadata.name=team-dev
Annotations: <none>
Status: Active
No resource quota.
No LimitRange resource.
The lines No resource quota. and No LimitRange resource. say that the Pods of this namespace can, together, consume all the resources of the cluster. Four protections complete the namespace, and each has its own lesson:
Protection
What it limits
Lesson
ResourceQuota
The total resources and objects that the namespace can consume
This lesson does not teach any of these four protections: it places them, so that you know what a namespace alone does not do.
How do you navigate between several namespaces?
To see the Pods of all namespaces with the name of their namespace, the -o custom-columns option chooses the columns. Each column is written TITLE:path, columns are separated by a comma without a space, and the quotes keep the list in one piece:
The everyday trap is creating a resource in the wrong namespace because -n was forgotten. The command kubectl config view --minify --output 'jsonpath={..namespace}' tells you which namespace you are working in. It returns an empty line as long as the context sets no namespace, and default after a set-context --current --namespace=default.
Deleting a namespace deletes all the resources it contains. By default, the command waits for the deletion to finish: it only gives control back once the namespace is empty and deleted. With --wait=false, it gives control back immediately and the namespace stays visible in the Terminating state while its content disappears. A namespace that stays stuck in Terminating signals a resource that refuses to be deleted.
Delete team-qa and check what remains:
Terminal window
kubectldeletenamespaceteam-qa
kubectlgetnamespaceteam-qa
kubectlgetdeployments-A-lapp=api
namespace "team-qa" deleted
Error from server (NotFound): namespaces "team-qa" not found
NAMESPACE NAME READY UP-TO-DATE AVAILABLE AGE
team-dev api 1/1 1 1 26s
The api Deployment of team-qa disappeared with its namespace, and get namespace answers NotFound: the error is intended, it proves the deletion. System namespaces, such as kube-system, are not deleted lightly: their components keep the cluster running. Now clean up the rest:
Terminal window
kubectldeletenamespaceteam-dev
namespace "team-dev" deleted
Put it into practice
The guided lab has you create two team namespaces, equipe-api and equipe-front, on a kind cluster. You deploy the api application (nginx, as in the lesson) and its Service in the first one, remembering -n so that nothing lands in default. Then, from a client Pod in the second, you reach api by its qualified nameapi.equipe-api and you observe that the short name fails: run this request with a short timeout, for example wget -T 3, so as not to wait. You record both results in ~/namespaces/releve.txt, under the keys reponse-nom-qualifie and nom-court. The validation replays both requests, and it rejects an application also left in default or a Service that would make the short name answer.
Key takeaways
A namespace separates names and attaches rights and quotas; it separates neither the network nor the Nodes.
Four namespaces exist in every cluster: default, kube-system, kube-public, kube-node-lease (kind adds one, local-path-storage).
Without -n, kubectl works in the namespace of the context, default by default; kubectl config set-context --current --namespace=<name> changes it.
A Secret, a ConfigMap or a Role of one namespace does not exist in another.
A Pod reaches a Service in another namespace through <service>.<namespace>, as long as no NetworkPolicy forbids it.
A new namespace has no limit: ResourceQuota, LimitRange, NetworkPolicy and RBAC are set up separately.
Deleting a namespace deletes all of its content.
Test your knowledge
Seven questions to check that the essentials are in place: what a namespace isolates, what it does not isolate, and the protection that answers each gap.
Knowledge check
Check what you have learned with this interactive quiz
7 questions
5 min
80% to pass
How it works
The timer starts when you click Start
Multiple choice, true/false and short answer questions
You can move between questions
Detailed results are shown at the end
Starts the quiz and the timer
⏱️--:--
0 / 7
Review
(0/0)
--:--
0%
0Correct
0To review
0:00Time
-Level
Score by domain
A domain groups several guides and gets several questions, so its score is more reliable than a per-topic score, which often rests on a single question.
Summary
Strengths
No topic passed at 80% on at least two questions yet.