Day 1: Tenants – namespaces with limits
Goal: Two teams each get a namespace with resource limits, default values, permissions for their developer group and a network that locks out other teams · Duration: approx. 60 min · Prerequisite: Day 0
Briefing
- A platform shares a cluster between teams. Each team is a tenant. The simplest form, and the one expected in the exam: one namespace per team, hence four fences.
| Fence | Object | Prevents |
|---|---|---|
| Quantity | ResourceQuota | a team eating up the whole cluster |
| Defaults | LimitRange | pods without requests/limits (the quota requires them) |
| Permissions | Role/RoleBinding | a team working in other teams’ namespaces |
| Network | NetworkPolicy | pods of different teams reaching each other |
- Developers are not objects in Kubernetes. The API server believes what the client certificate (or an OIDC token) says: CN = user, O = group. You grant permissions to the group, never to individual people.
- Labels on the namespaces (
team=blue,tier=tenant) are the foundation for later: Kyverno (Days 9, 13) and ApplicationSets (Day 4) select tenants via these labels.
Drill 1: Two team namespaces
# on cp1
mkdir -p ~/cnpe/tag01 && cd ~/cnpe/tag01
# generate the namespace as YAML (without creating it), set labels locally, then create it
# --dry-run=client -o yaml only print the object
# label --local -f - write labels into the YAML from stdin without asking the server
for t in blue green; do
k create namespace team-$t --dry-run=client -o yaml \
| k label --local -f - team=$t tier=tenant -o yaml | k apply -f -
done
# find all tenants via the label
k get ns -l tier=tenant --show-labelsExpected:
namespace/team-blue created
namespace/team-green created
NAME STATUS AGE LABELS
team-blue Active 1s kubernetes.io/metadata.name=team-blue,team=blue,tier=tenant
team-green Active 0s kubernetes.io/metadata.name=team-green,team=green,tier=tenant
Kubernetes sets the label kubernetes.io/metadata.name
itself. You will need it shortly in the NetworkPolicy exercise.
Save your progress? Sign up for free
Drill 2: Quota and defaults
# on cp1
cat > quota-blue.yaml <<'EOF'
apiVersion: v1
kind: ResourceQuota
metadata:
name: team-quota
namespace: team-blue
spec:
hard: # sum across all pods in the namespace
requests.cpu: "2" # promised CPU
requests.memory: 4Gi # promised memory
limits.cpu: "4" # upper limits combined
limits.memory: 8Gi
pods: "20" # number of pods
persistentvolumeclaims: "5" # number of volumes
services.nodeports: "0" # no NodePorts: teams do not open ports on the nodes
---
apiVersion: v1
kind: LimitRange
metadata:
name: team-defaults
namespace: team-blue
spec:
limits:
- type: Container
defaultRequest: # request if the container does not specify one
cpu: 100m
memory: 128Mi
default: # limit if the container does not specify one
cpu: 500m
memory: 256Mi
max: # a single container may never ask for more
cpu: "1"
memory: 1Gi
EOF
k apply -f quota-blue.yaml
# a Deployment without resource settings
k -n team-blue create deployment web --image=nginx:1.29 --replicas=2
k -n team-blue rollout status deploy/web
# which resources did the pod get?
k -n team-blue get pod -l app=web -o jsonpath='{.items[0].spec.containers[0].resources}'; echo
# usage against the quota
k -n team-blue describe quota team-quotaExpected (abridged):
resourcequota/team-quota created
limitrange/team-defaults created
deployment.apps/web created
deployment "web" successfully rolled out
{"limits":{"cpu":"500m","memory":"256Mi"},"requests":{"cpu":"100m","memory":"128Mi"}}
Name: team-quota
Namespace: team-blue
Resource Used Hard
-------- ---- ----
limits.cpu 1 4
limits.memory 512Mi 8Gi
persistentvolumeclaims 0 5
pods 2 20
requests.cpu 200m 2
requests.memory 256Mi 4Gi
services.nodeports 0 0
The Deployment does not specify any resources, yet they are in the
pod: the LimitRange inserted them at creation time. Without a
LimitRange, the quota would reject such pods – a quota on
requests.cpu requires every pod to have a CPU request.
Save your progress? Sign up for free
Drill 3: The limits take effect
Three attempts, each one fails at a different fence:
# on cp1
# 1. too many pods: 9 × 500m limit = 4.5 CPU > 4
k -n team-blue scale deploy web --replicas=9
sleep 8
k -n team-blue get deploy web
k -n team-blue get events --field-selector reason=FailedCreate -o custom-columns=MSG:.message --no-headers | tail -1
k -n team-blue scale deploy web --replicas=2Expected:
deployment.apps/web scaled
NAME READY UP-TO-DATE AVAILABLE AGE
web 8/9 8 8 37s
(combined from similar events): Error creating: pods "web-689b9bdbdd-rgc4d" is forbidden: exceeded quota: team-quota, requested: limits.cpu=500m, used: limits.cpu=4, limited: limits.cpu=4
deployment.apps/web scaled
The Deployment itself is accepted; only the ReplicaSet controller
fails when creating the ninth pod. That is why you see the error only in
the events, not during scale. In the exam
this is a typical “why does the pod not start?”.
# on cp1
# 2. a container above the LimitRange maximum
# --overrides JSON merged into the generated pod object (here: limit 2 CPU)
k -n team-blue run gross --image=nginx:1.29 \
--overrides='{"spec":{"containers":[{"name":"gross","image":"nginx:1.29","resources":{"limits":{"cpu":"2"}}}]}}'
# 3. a NodePort Service
k -n team-blue expose deploy web --type=NodePort --port 80Expected:
Error from server (Forbidden): pods "gross" is forbidden: maximum cpu usage per Container is 1, but limit is 2
Error from server (Forbidden): services "web" is forbidden: exceeded quota: team-quota, requested: services.nodeports=1, used: services.nodeports=0, limited: services.nodeports=0
The API server rejects these two immediately – they are single objects, with no controller in between.
Save your progress? Sign up for free
Drill 4: A developer gets access
Anna belongs to the group team-blue-devs. She generates
her key herself; you only sign her request via the
CertificateSigningRequest API – the private key never
leaves her machine. In the lab you play both roles on cp1.
# on cp1 (as Anna)
# private RSA key, 3072 bits
openssl genrsa -out anna.key 3072
# request: CN = user name, O = group
openssl req -new -key anna.key -subj '/CN=anna/O=team-blue-devs' -out anna.csr# on cp1 (as administrator)
cat > anna-csr.yaml <<EOF
apiVersion: certificates.k8s.io/v1
kind: CertificateSigningRequest
metadata:
name: anna
spec:
request: $(base64 -w0 anna.csr) # request, Base64 without line breaks
signerName: kubernetes.io/kube-apiserver-client # certificate for logging in to the API server
expirationSeconds: 604800 # valid for 7 days
usages: ["client auth"]
EOF
k apply -f anna-csr.yaml
k get csr anna
# approve – only then does the control plane sign
k certificate approve anna
# fetch the certificate and check it
k get csr anna -o jsonpath='{.status.certificate}' | base64 -d > anna.crt
openssl x509 -in anna.crt -noout -subject -enddateExpected:
certificatesigningrequest.certificates.k8s.io/anna created
NAME AGE SIGNERNAME REQUESTOR REQUESTEDDURATION CONDITION
anna 0s kubernetes.io/kube-apiserver-client kubernetes-admin 7d Pending
certificatesigningrequest.certificates.k8s.io/anna approved
subject=O = team-blue-devs, CN = anna
notAfter=Dec 4 22:42:35 2026 GMT
# on cp1
# add Anna as a user and context to the kubeconfig
# --embed-certs=true copy certificate and key into the file instead of pointing to paths
k config set-credentials anna --client-certificate=anna.crt --client-key=anna.key --embed-certs=true
# --cluster=kubernetes name of the cluster in kubeadm's kubeconfig
k config set-context anna --cluster=kubernetes --user=anna --namespace=team-blue
# list pods as Anna
k --context anna get podsExpected:
User "anna" set.
Context "anna" created.
Error from server (Forbidden): pods is forbidden: User "anna" cannot list resource "pods" in API group "" in the namespace "team-blue"
Anna is authenticated (the server knows her name),
but not authorized. Now the permissions – for the
group, via the built-in ClusterRole edit:
# on cp1
# RoleBinding in the namespace: ClusterRole edit, applies only here, for the whole group
k -n team-blue create rolebinding team-blue-devs-edit --clusterrole=edit --group=team-blue-devs
k --context anna get pods
# still nothing in other namespaces
k --context anna -n team-green get pods
# check individual permissions without Anna's certificate: --as/--as-group impersonate her
for v in 'create deployments' 'get secrets' 'delete resourcequota' 'create rolebindings'; do
echo "$v: $(k auth can-i $v -n team-blue --as=anna --as-group=team-blue-devs)"
doneExpected:
rolebinding.rbac.authorization.k8s.io/team-blue-devs-edit created
NAME READY STATUS RESTARTS AGE
web-689b9bdbdd-6wh94 1/1 Running 0 55s
web-689b9bdbdd-8hpcb 1/1 Running 0 55s
Error from server (Forbidden): pods is forbidden: User "anna" cannot list resource "pods" in API group "" in the namespace "team-green"
create deployments: yes
get secrets: yes
delete resourcequota: no
create rolebindings: no
edit allows everything a team needs for its
applications, but no changes to quotas and permissions – otherwise the
team could tear down its own fences. That edit may read
Secrets is intended: applications need Secrets. If you do not want that,
write your own Role.
Save your progress? Sign up for free
Drill 5: Separating the network
Without a NetworkPolicy, every pod reaches every other pod. The pattern for tenants: first deny everything, then allow your own namespace.
# on cp1
k -n team-blue expose deploy web --port 80
cat > netpol-blue.yaml <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny-ingress
namespace: team-blue
spec:
podSelector: {} # applies to all pods in the namespace
policyTypes: ["Ingress"] # incoming; without ingress rules: nothing allowed
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-same-namespace
namespace: team-blue
spec:
podSelector: {}
policyTypes: ["Ingress"]
ingress:
- from:
- podSelector: {} # all pods – without namespaceSelector: only from this namespace
EOF
# test call from both namespaces, before and after
# --rm -i --restart=Never one-off pod, deleted after the command
# curl -m 3 give up after 3 seconds; -w '%{http_code}' print only the status
for ns in team-blue team-green; do
echo "$ns vorher: $(k -n $ns run t-$RANDOM --rm -i --restart=Never --image=curlimages/curl:8.17.0 \
--command -- curl -s -m 3 -o /dev/null -w '%{http_code}' http://web.team-blue 2>/dev/null | head -c 3)"
done
k apply -f netpol-blue.yaml
for ns in team-blue team-green; do
echo "$ns nachher: $(k -n $ns run t-$RANDOM --rm -i --restart=Never --image=curlimages/curl:8.17.0 \
--command -- curl -s -m 3 -o /dev/null -w '%{http_code}' http://web.team-blue 2>/dev/null | head -c 3)"
doneExpected:
service/web exposed
team-blue vorher: 200
team-green vorher: 200
networkpolicy.networking.k8s.io/default-deny-ingress created
networkpolicy.networking.k8s.io/allow-same-namespace created
team-blue nachher: 200
team-green nachher: 000
000 means: no answer within three seconds – Cilium drops
the packets.
Save your progress? Sign up for free
Exercise: Team Green in five minutes
Time limit: 5 minutes. Team Green gets the same fences as Team Blue:
quota, LimitRange, NetworkPolicies and edit for the group
team-green-devs. Use Team Blue’s files.
Solution
# on cp1
# rewrite and apply each file individually
for f in quota-blue.yaml netpol-blue.yaml; do
sed 's/team-blue/team-green/g' $f | k apply -f -
done
k -n team-green create rolebinding team-green-devs-edit --clusterrole=edit --group=team-green-devs
k get limitrange -AExpected:
resourcequota/team-quota created
limitrange/team-defaults created
networkpolicy.networking.k8s.io/default-deny-ingress created
networkpolicy.networking.k8s.io/allow-same-namespace created
rolebinding.rbac.authorization.k8s.io/team-green-devs-edit created
NAMESPACE NAME CREATED AT
team-blue team-defaults 2026-11-27T22:46:46Z
team-green team-defaults 2026-11-27T22:48:24Z
The trap you fall into under time pressure:
sed … quota-blue.yaml netpol-blue.yaml | k apply -f - in
one call. The first file ends without ---,
the second begins without one – the last object of the first and the
first object of the second merge into one YAML document, and one of them
(here the LimitRange) disappears without an error message. Hence the
loop.
Save your progress? Sign up for free
Challenge
Time limit: 10 minutes. The platform’s monitoring (from Day 10 in the
namespace monitoring) must reach Team Blue’s pods on port
80, while all other namespaces still cannot. Write the NetworkPolicy and
test it with a test pod in a namespace monitoring.
Solution
# on cp1
cat > allow-monitoring.yaml <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-monitoring
namespace: team-blue
spec:
podSelector: {}
policyTypes: ["Ingress"]
ingress:
- from:
- namespaceSelector: # select the namespace via its automatic name label
matchLabels:
kubernetes.io/metadata.name: monitoring
ports:
- port: 80
protocol: TCP
EOF
k apply -f allow-monitoring.yaml
k create namespace monitoring
k -n monitoring run t --rm -i --restart=Never --image=curlimages/curl:8.17.0 \
--command -- curl -s -m 3 -o /dev/null -w '%{http_code}\n' http://web.team-blue
k -n team-green run t --rm -i --restart=Never --image=curlimages/curl:8.17.0 \
--command -- curl -s -m 3 -o /dev/null -w '%{http_code}\n' http://web.team-blue
k delete namespace monitoringExpected (abridged):
networkpolicy.networking.k8s.io/allow-monitoring created
namespace/monitoring created
200
000
pod team-green/t terminated (Error)
The line terminated (Error) is the desired result here:
curl returns an error code after the timeout, and kubectl reports
it.
NetworkPolicies are additive: every rule that matches allows. There is no “deny” except the default, as soon as any policy selects a pod.
Save your progress? Sign up for free
Checkpoint
# on cp1
# two tenants
k get ns -l tier=tenant -o name
# one quota and one LimitRange each, three NetworkPolicies in team-blue, two in team-green
k get quota,limitrange,netpol -n team-blue -o name
k get quota,limitrange,netpol -n team-green -o name
# Anna is allowed in team-blue, not in team-green
k auth can-i create deployments -n team-blue --as=anna --as-group=team-blue-devs
k auth can-i create deployments -n team-green --as=anna --as-group=team-blue-devsExpected:
namespace/team-blue
namespace/team-green
resourcequota/team-quota
limitrange/team-defaults
networkpolicy.networking.k8s.io/allow-monitoring
networkpolicy.networking.k8s.io/allow-same-namespace
networkpolicy.networking.k8s.io/default-deny-ingress
resourcequota/team-quota
limitrange/team-defaults
networkpolicy.networking.k8s.io/allow-same-namespace
networkpolicy.networking.k8s.io/default-deny-ingress
yes
no
Interview Note
- Namespace per team or cluster per team? Namespaces share the control plane and nodes – cheap, but only “soft” separation. A cluster per team separates hard, but costs operational effort. In between are virtual clusters (vCluster).
- Why does a quota need a LimitRange? A quota on requests/limits rejects pods without these values; the LimitRange inserts defaults.
- How does Kubernetes know users and groups? Not as objects at all – from the certificate (CN/O) or the OIDC token. RBAC binds permissions to these names.
Enterprise Note
- Log people in via OIDC (Keycloak, Dex, Entra ID) and take groups from the directory; client certificates cannot be revoked and are only suitable for emergency access.
- Do not roll out tenant fences by hand but as a template: Kyverno
generate(Day 9) or Crossplane (Day 8) create quota, LimitRange, policies and bindings with every new namespace. - Restrict egress too (DNS, internal services, internet only via proxy) – here only ingress.
- Align quotas with real usage (Day 12, OpenCost) instead of round numbers.