StaffCoder
Kubernetes
15 modules · 60 challengesSign up
Module 4 · Namespaces & Resource Organization

Context & Scoping Discipline

easyNamespaceskubectl Contexts—No data yet

Problem statement

Half the postmortems end the same way: the command was right, the namespace was not. Someone deleted the ops copy while looking at the edge copy, someone restarted production while reading a staging dashboard. Two namespaces are seeded here - platform-ops and platform-edge - and the job is to make every command say, one way or another, exactly which one it is talking to.

About this exercise

Every namespaced command resolves its namespace from one of three places, in this order:

1. An explicit namespace flag on the command. Unambiguous, survives being pasted into someone else's shell, and is the only form safe to put in a runbook.

2. The current context's default namespace. A kubeconfig context is a saved (cluster, user, namespace) triple, and its namespace is what an unflagged command uses. Setting it is how you stop typing the flag a hundred times; forgetting you set it is how the wrong namespace gets hit.

3. default - the namespace named default, which is where everything lands when nobody has decided anything.

Knowing which kinds even have a namespace is the other half. The API publishes its own list of resource kinds, and each entry says whether that kind is namespaced. Pods, ConfigMaps and Services are; nodes, namespaces and PersistentVolumes are not - a namespace flag against a cluster-scoped kind is meaningless, and the API ignores it rather than protecting you.

Namespaces are also a hard wall for references. A pod's ConfigMap and Secret references are resolved inside the pod's own namespace only - there is no syntax for "the ConfigMap over there". The reference is accepted when the pod is created, and then the kubelet cannot find the object, so the container never starts and the pod sits in a waiting state with the reason recorded on it.

kubectl config --help and kubectl get --help are the reference for the flags. The seeded platform-ops holds a ConfigMap region-config with the key region; platform-edge holds nothing.

How this project is graded

The grader inspects the live cluster and your kubeconfig: it checks which namespace each object you created actually landed in, reads the namespace recorded on your current context, and waits for the cross-namespace pod to settle before reading the reason off it. Re-running the tests does not reset your work.

What you will build

Three pods, one context setting and two answer files:

pod  probe-explicit  in platform-edge  image busybox:1.36, any long-running command
                     created with the namespace named on the command itself

pod probe-default in platform-ops image busybox:1.36, any long-running command created with NO namespace on the command - it must land there because the current context says so

current context default namespace set to platform-ops

pod cross-ref in platform-edge image busybox:1.36, any long-running command with an environment variable REGION taken from the key region of the ConfigMap region-config - which lives in platform-ops, not in platform-edge. Do not copy the ConfigMap across.

answers/scopes.txt NAMESPACED <one resource name per line> CLUSTER <one resource name per line>

Classify exactly these six, written as the API names them: configmaps, namespaces, nodes, persistentvolumes, pods, services

answers/cross-namespace.txt the state of the cross-ref pod as the cluster reports it, including the reason recorded for the container that will not start and the name of the object it could not find

Step 1 - name the namespace on the command

Create probe-explicit so that it lands in platform-edge and in no other namespace.

explicit_namespace_flag_targets_only_that_namespace should pass

Step 2 - make the default deliberate

Point your current context's default namespace at platform-ops, then create probe-default without naming a namespace anywhere on the command.

context_default_namespace_is_configured and unflagged_command_lands_in_the_context_namespace should pass

Step 3 - which kinds even have a namespace

Classify the six resource names into the two headings, from what the API says about each kind rather than from memory.

resource_listing_partitions_namespaced_kinds and resource_listing_partitions_cluster_scoped_kinds should pass

Step 4 - the wall between namespaces

Create cross-ref in platform-edge pointing at a ConfigMap that only exists in platform-ops, and capture what the cluster records about it. Leave it failing - that is the evidence.

cross_namespace_configmap_reference_never_starts should pass

When every test passes

You have:

  • Commands that carry their namespace with them, safe to paste into a runbook
  • A context whose default namespace you chose on purpose instead of inheriting
  • The difference between a kind that has a namespace and one that cannot
  • Proof that a reference does not cross a namespace, and the reason the cluster gives when one tries

You do not have yet:

  • A whole application standing in more than one namespace at once. Running the same stack per environment is the next exercise.

Verify

Click Test after every step.

More in Namespaces & Resource Organization

See the module
Namespace BasicsGive three environments a namespace each, put the same object names in all of them, list within and across namespaces, and delete a namespace to see the cascade.—No data yeteasyPer-Environment StacksRun one application three times over - dev, staging and prod in their own namespaces under identical names, each labelled for its environment, and tear one down without touching the rest.—No data yeteasyTenant Estate SetupMove three tenants onto one cluster: a labelled namespace and an identically named stack each, tooling that scopes every operation, an inventory that attributes objects to tenants, and an eviction that disturbs nobody else.—No data yeteasy

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.