liveBook · Manning

Chapter 7: Organizing pods and other resources using namespaces and labels

What you will learn in chapter 7 of Kubernetes in Action, Second Edition

The introduction distinguishes two ways to organize objects in a shared Kubernetes cluster: namespaces separate teams’ objects, while labels show what objects belong to and what roles they play.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction distinguishes two ways to organize objects in a shared Kubernetes cluster: namespaces separate teams’ objects, while labels show what objects belong to and what roles they play.

Key terms: object namespaces, object labels

7.1 Organizing objects into namespaces

This section explains how Kubernetes namespaces organize objects in a shared cluster, scope object names and user privileges, and differ from cluster-scoped resources.

Key terms: Kubernetes namespaces, Kubernetes API objects, scope for object names, namespaced, cluster scoped, kubectl api-resources, NAMESPACED column, scope for user privileges, namespace

7.1.1 Listing namespaces and the objects they contain

Shows how to list Kubernetes namespaces and inspect objects in one namespace or across the cluster.

Key terms: Namespace object, default namespace, kubectl get, ns, --namespace, -n, --all-namespaces, -A, NAMESPACE column, kube-system

7.1.2 Creating namespaces

This section shows two ways to create namespaces, explains why operators may include them in manifests, and states the naming rules for namespaces.

Key terms: kubectl create namespace, Namespace object, manifest file, kubectl apply, DNS subdomain names, namespace names

7.1.3 Managing objects in other namespaces

This section explains how kubectl chooses a namespace for objects and how to work in a different namespace using a command option, an object manifest, or the current context.

Key terms: current namespace, default, --namespace, -n, metadata.namespace, object manifest, kubectl apply, current context, kubeconfig, kubectl config set-context --current --namespace

7.1.4 Understanding the (lack of) isolation between namespaces

This section explains that namespaces prevent object-name conflicts but do not, by themselves, isolate workloads at runtime or on the network.

Key terms: namespaces, naming conflicts, virtual cluster, physical cluster, cluster node, OS kernel, container technologies, network isolation, NetworkPolicy

7.1.5 Deleting namespaces

This subsection explains what happens when a namespace is deleted and how to investigate a deletion that remains stuck.

Key terms: Namespace object, Terminating, kubectl delete, --wait=false, kubectl get all, status field, custom object, custom controller, finalizer

7.2 Organizing pods with labels

This section uses a growing, multi-service Kiada deployment to explain why labels are a better way than namespaces to group related pods and objects.

Key terms: Pod object, object labels, namespaces, replicas, canary release, stable version

7.2.1 Introducing labels

Introduces labels as flexible key–value metadata and shows how application and release labels organize pods along two dimensions.

Key terms: label, key–value pair, label key, label value, app label, rel label, stable release, canary release

7.2.2 Adding labels to pods

This section shows how to define, inspect, add, change, and remove labels on pods.

Key terms: metadata.labels, app, rel, kubectl describe, --show-labels, --label-columns, -L, kubectl label, --overwrite, --all

7.2.3 Label syntax rules

This section explains how label keys and values are named, including prefix conventions, syntax limits, and when to use an annotation instead.

Key terms: label key, prefix, kubernetes.io/, k8s.io/, label name, DNS subdomain, label value, annotation

7.2.4 Using standard label keys

This section distinguishes labels Kubernetes uses on system objects from recommended labels users can apply consistently to related application components.

Key terms: well-known labels, system objects, nodes, recommended labels, application instance, label selectors, bulk operations

7.3 Filtering objects with label selectors

This section explains how label selectors filter Kubernetes objects by label values or key presence, and how multiple selectors work together.

Key terms: label selector, equality-based selector, label key, label value, set-based selectors, in, notin, app, !app, combining multiple selectors

7.3.1 Using label selectors for object management with kubectl

This section shows how to use label selectors with kubectl to list matching objects and delete matching pods, with a warning about deletion.

Key terms: label selector, kubectl get, --selector, -l, equality-based selector, set-based selector, label key, presence, absence, !rel

7.3.2 Using label selectors in object manifests

This section shows how labels and selectors in Pod manifests constrain which nodes can run a pod, from simple node selectors to more expressive node affinity.

Key terms: node labels, selectors, worker nodes, node-role: front-end, spec.nodeSelector, equality-based label selector, nodeAffinity, nodeSelectorTerms, matchExpressions, In

7.4 Filtering objects with field selectors

This section introduces field selectors as a way to filter Kubernetes objects by properties other than labels and identifies which fields are available.

Key terms: field selectors, label selectors, object kind, metadata.name, metadata.namespace

7.4.1 Using a field selector in kubectl

This subsection shows how to filter pods with kubectl field selectors, using node assignment and running phase as examples.

Key terms: field selector, spec.nodeName, status.phase!=Running, -A, --all-namespaces, kubectl explain, pod.spec, field selectors

7.4.2 Using field selectors in object manifests

This subsection shows that object manifests can select objects by field values, using a pod’s node affinity as an example.

Key terms: nodeAffinity, matchFields, matchExpressions

7.5 Annotating objects

The section introduces annotations as an alternative when information does not fit the constraints of labels.

Key terms: labels, label value, annotations

7.5.1 Introducing object annotations

Introduces annotations as flexible object metadata, explains why Kubernetes uses them for emerging features, and shows what users can store in them.

Key terms: annotations, labels, annotation keys, annotation values, object annotation, Kubernetes API, schema, deprecated, Git commit hash, build timestamp

7.5.2 Adding annotations to objects

This subsection shows how to add annotations to existing Kubernetes objects or to object manifests, and warns that some YAML annotation values must be quoted.

Key terms: annotations, kubectl annotate, object manifest, annotation value, YAML parser, string

7.5.3 Inspecting an object’s annotations

This section shows how to inspect an object’s annotations and explains why one kubectl-managed annotation may be absent from kubectl describe output.

Key terms: annotations, kubectl get, kubectl describe, jq, YAML, JSON, kubectl.kubernetes.io/last-applied-configuration

7.5.4 Updating and removing annotations

This subsection explains how to change or remove an object annotation with kubectl annotate.

Key terms: kubectl annotate, --overwrite, annotation key

7.6 Summary

This summary explains how namespaces, labels, selectors, and annotations organize and identify Kubernetes objects.

Where this is used later

Concepts explained in this chapter

← Chapter 6: Managing the pod life cycle and container health Chapter 8: Configuring applications with ConfigMaps and Secrets →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.