Sub topic 2
Node labels, including well-known labels
This chapter assumes you can run kubectl, know what a Node and a Pod are, and have used labels and label selectors on other objects. Node labels are the data that nodeSelector and node affinity match against, so every later chapter in this tutorial depends on them.
What a node label is
A node label is a key/value pair stored in the metadata.labels of a Node object. The scheduler does not know what your hardware is. It only reads these labels. If a node has no label that describes its SSD, no scheduling rule can ask for one.
Labels come from three sources:
- The kubelet adds a set of labels when it registers the node, such as hostname, OS, and architecture.
- Cloud providers or cluster tooling add labels such as zone, region, and instance type.
- You add custom labels with
kubectl label.
Adding and inspecting labels
Add a label with kubectl label nodes. The -L flag adds a column for a label key, and --show-labels prints every label.
kubectl label nodes worker-1 disktype=ssd
kubectl get nodes -L disktype
kubectl get nodes --show-labelsThe first command sets disktype=ssd on worker-1. The second lists all nodes with a DISKTYPE column, which is empty for nodes that lack the label. The third prints all labels for every node.
To change an existing label, add --overwrite. To remove one, append a minus sign to the key.
kubectl label nodes worker-1 disktype=nvme --overwrite
kubectl label nodes worker-1 disktype-You can also filter nodes with a selector: kubectl get nodes -l disktype=ssd.
Well-known labels
Kubernetes defines standard label keys so that tools and manifests behave the same across clusters. You can use them in selectors without adding them yourself.
| Label key | Meaning | Example value |
|---|---|---|
| `kubernetes.io/hostname` | Name of the node, as the kubelet reports it | `worker-1` |
| `kubernetes.io/os` | Operating system | `linux` |
| `kubernetes.io/arch` | CPU architecture | `amd64`, `arm64` |
| `topology.kubernetes.io/region` | Region the node runs in | `us-east-1` |
| `topology.kubernetes.io/zone` | Failure zone the node runs in | `us-east-1a` |
| `node.kubernetes.io/instance-type` | Machine size, set by some cloud providers | `m5.large` |
The OS, architecture, and hostname labels are set by the kubelet. Region, zone, and instance type usually come from the cloud provider integration. On a bare-metal or local cluster, the topology labels may not exist unless someone adds them.
Check what your cluster provides before you write a rule that depends on a label:
kubectl get nodes -L kubernetes.io/os,kubernetes.io/arch,topology.kubernetes.io/zoneChoosing your own label keys
Custom labels are plain strings, so a consistent convention keeps them manageable.
- Describe a property of the node, such as
disktype=ssd, not an intended workload, such asuse-for-database=true. Properties stay true when workloads change. - Use a prefix you control for shared clusters, for example
example.com/gpu-count. Unprefixed keys are reserved for end users, and thekubernetes.io/andk8s.io/prefixes are reserved for Kubernetes components. - Keep values within label limits: at most 63 characters, alphanumeric at the ends, with
-,_, and.allowed inside.
Common errors
- Typo in the key or value.
disktype=ssdanddiskType=ssdare different labels. A rule that matches no node leaves its pod Pending. - Label set on the wrong node, or lost on replacement. Labels added with
kubectllive on the Node object. If a node is deleted and recreated, for example by an autoscaler or a node group update, your manual labels are gone unless the node group configuration applies them. - Assuming a label exists. Well-known cloud labels may be missing on local clusters.
Exercise
You run kubectl label nodes worker-2 disktype=ssd and then kubectl get nodes -L disktype. What does the DISKTYPE column show for a node you never labeled, and how do you remove the label from worker-2 afterwards?
Show answer
The column is empty for nodes without the label. Remove the label from worker-2 with kubectl label nodes worker-2 disktype-.