How to label Kubernetes node?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Kubernetes node labels are key-value pairs attached to nodes that enable workload scheduling, organization, and selection. Labels are assigned with kubectl label nodes <node> key=value and used in Pod specifications via nodeSelector or nodeAffinity to control which nodes a Pod runs on. Common use cases include marking nodes by hardware (GPU, SSD), environment (production, staging), region, or team ownership. Labels do not affect node behavior — they only provide metadata for scheduling and querying.
Adding Labels to Nodes
Viewing Node Labels
Updating and Removing Labels
Using Labels with nodeSelector
The simplest way to schedule Pods on labeled nodes:
The Pod only runs on nodes that have both gpu=true AND environment=production labels.
Using Node Affinity (Advanced)
Node affinity provides more expressive scheduling rules:
required: Pod must run on matching nodes (hard constraint)preferred: Scheduler tries matching nodes but falls back if unavailable (soft constraint)
Labeling Nodes in a Manifest
Common Label Conventions
| Label | Purpose | Example Values |
environment | Deployment stage | production, staging, development |
tier | Application tier | frontend, backend, database |
region | Geographic region | us-east-1, eu-west-1 |
disk-type | Storage type | ssd, hdd |
gpu | GPU availability | true, nvidia-a100 |
team | Owning team | platform, ml, web |
kubernetes.io/os | Operating system (built-in) | linux, windows |
node.kubernetes.io/instance-type | Cloud instance type (built-in) | m5.xlarge |
Automation with Scripts
Common Pitfalls
- Forgetting
--overwritewhen updating a label:kubectl label nodes worker-1 env=stagingfails if theenvlabel already exists. Add--overwriteto update existing labels. - Using invalid label key/value characters: Label keys must match
[a-zA-Z0-9._-]and be at most 63 characters. Values follow the same rules. Keys can optionally include a DNS prefix likecompany.com/role. Invalid characters cause a validation error. - Relying on labels for security isolation: Labels are metadata only — they do not enforce security boundaries. A misconfigured
nodeSelectoror a missing label does not prevent a Pod from being scheduled on the wrong node if the scheduler falls back. Use taints and tolerations for hard isolation. - Not labeling nodes consistently across the cluster: If some nodes have
env=productionand others haveenvironment=production, selectors match different sets. Establish naming conventions and enforce them with automation or admission controllers. - Removing a label that active Pods depend on: Removing a label from a node does not evict Pods already running on it (
IgnoredDuringExecution). However, new Pods withnodeSelectorfor that label will not be scheduled on the node. Plan label changes carefully.
Summary
- Add labels with
kubectl label nodes <node> key=value— use--overwriteto update - Remove labels by appending
-to the key:kubectl label nodes <node> key- - Use
nodeSelectorfor simple scheduling constraints (exact match) - Use
nodeAffinityfor complex rules (In, NotIn, Exists, required vs preferred) - Follow consistent naming conventions for labels across the cluster
- Labels are metadata only — use taints/tolerations for hard scheduling constraints

