liveBook · Manning

Label

As explained in Google Anthos in Action

A Kubernetes label is a key-value metadata pair attached to a resource for identification, organization, and selection.

A Kubernetes label is a key-value pair attached to a resource, such as `app: frontend-webserver`. Labels identify attributes and organize resources without fixing group membership: a selector dynamically matches resources carrying the requested labels. A Service, for example, uses a selector to link to application Pods, whose IP addresses can change when they are replaced. section 3.2.6

Node labels also guide Pod placement. Label nodes that provide a capability, then set a matching `nodeSelector` in the workload. For a fast-disk node, run `kubectl label node <node-name> disk=fast` and specify `nodeSelector: {disk: fast}` in the Pod specification. Check node labels with `kubectl get nodes --show-labels` and Pod placement with `kubectl get pods -o wide`. section 3.2.7 section 3.4.1

A label attracts matching workloads; it does not prevent other Pods from using the node. To reserve GPU nodes for workloads that tolerate the restriction, use a taint as well as a label. A GPU Pod then needs both a matching node selector and a matching toleration. Node affinity offers label-based placement conditions beyond a simple node selector. section 3.2.7 section 3.4.1

Select a fast-disk node

Label a node `disk=fast`, then deploy a Pod with `nodeSelector: {disk: fast}`. In the placement example, the NGINX Pod reached `Running` on the labeled node. This demonstrates placement by capability, not exclusive use of that node. section 3.4.1

Labels can also select entire clusters for configuration delivery. In ACM, Cluster definitions carry labels such as `country: us` or `is-rollout-tester: true`; ClusterSelectors use those labels to target regional applications or test versions. Keep selectors for different versions nonoverlapping: if a general deployment also matches rollout testers, both deployments can apply to those clusters. section 11.3.3

apiVersion: v1
kind: Pod
metadata:
labels:
 run: nginx-gpu
name: nginx-gpu
spec:
containers:
- image: nginx
 name: nginx-gpu
restartPolicy: Always
nodeSelector:
 workload: gpu
tolerations:
- key: "workload"
 operator: "Equal"
 value: "gpu"
 effect: "NoSchedule"

The GPU Pod manifest shows the two distinct placement requirements together: `nodeSelector` matches the node label, while `tolerations` permits scheduling despite its taint. section 3.4.1

---
kind: ClusterSelector
apiVersion: configmanagement.gke.io/v1
metadata:
 name: rollout-testers
spec:
 selector:
   matchLabels:
     is-rollout-tester: true
---
kind: ClusterSelector
apiVersion: configmanagement.gke.io/v1
metadata:
 name: non-testers
spec:
 selector:
   matchExpressions:
   - key: is-rollout-tester
     operator: DoesNotExist
---
kind: ClusterSelector
apiVersion: configmanagement.gke.io/v1
metadata:
 name: country-us
spec:
 selector:
   matchLabels:
     country: us

These ClusterSelectors show exact label matching for US clusters and separate expression-based selection for rollout testers and non-testers. section 11.3.3

How Google Anthos in Action covers it

How deep it goes
Defines key-value labels and works through selectors, scheduling commands, placement checks, and cluster targeting; does not detail label API internals.
Examples
Uses fast-disk and GPU node placement, NGINX Pod manifests, and regional and rollout-test cluster selectors.
What it assumes
Assumes familiarity with Pods, nodes, deployments, manifests, and kubectl while defining labels and selectors before applying them.
What you can do after
Operate/configure it by labeling nodes or clusters, matching workloads with selectors, and checking placement without mistaking labels for restrictions.
How it connects
Connects labels to selectors, Services, Pods, node selectors, node affinity, taints, tolerations, and ClusterSelectors.

Also explained in

Compare how 7 books explain label →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Google Anthos in Action. The section links point to the parts of the book it draws on.