✦ For everyone, free.

Practical knowledge for real and everyday life

Home

7.4 Kubernetes Label Structure

Kubernetes Label Structure defines how metadata is organized and applied to resources, enabling efficient selection and management within a Kubernetes cluster.

Kubernetes Label Structure is the precise syntactic definition governing how a label key and its corresponding value must be formed, specifying an optional prefix segment, a required name segment, and length and character constraints on each, forming the exact rules the API server enforces whenever a label is submitted as part of any object's metadata.


The Two-Part Key Structure

Prefix and Name Segments

A label key consists of an optional prefix and a required name, separated by a single forward slash, such as app.kubernetes.io/name, where app.kubernetes.io is the prefix and name is the name segment; a key with no slash, such as simply tier, is valid and treated as having no prefix at all.

Purpose of the Prefix

The prefix segment exists to namespace a label key's meaning, preventing collisions between, for example, a project's own internal version label and an unrelated tool's own differently-intended version label, by requiring each to qualify their key under a distinct, owned prefix.


Prefix Constraints

DNS Subdomain Format

When present, a label key's prefix must be a valid DNS subdomain, meaning it follows the same format as a DNS subdomain name, lowercase alphanumeric characters, hyphens, and dots, with a maximum length of 253 characters, and must not begin or end with a dot.

Reserved Prefixes

Certain prefixes, notably kubernetes.io and k8s.io, and their subdomains, are reserved for use by Kubernetes core components and cannot be used by arbitrary third-party tools or applications, protecting against a non-core actor claiming a label namespace that could be confused for an official, system-defined one.


Name Segment Constraints

Character and Length Limits

The name segment, whether or not a prefix is present, must be 63 characters or fewer and must consist of alphanumeric characters, hyphens, underscores, and dots, beginning and ending with an alphanumeric character; unlike the prefix, the name segment permits underscores in addition to hyphens and dots.

Case Sensitivity

Label keys are case-sensitive, meaning Environment and environment are treated as two entirely distinct keys by the API server, a detail that matters for consistency when establishing team-wide labeling conventions, since inconsistent casing effectively fragments what was intended to be a single, unified label.


Label Value Constraints

Shorter and Simpler Than Keys

Label values follow a similar but distinct constraint set: up to 63 characters, and, if non-empty, must begin and end with an alphanumeric character while permitting alphanumeric characters, hyphens, underscores, and dots in between, the same character rules as a key's name segment but without any prefix concept.

Empty Values Are Permitted

A label value can be empty, which is a valid and sometimes deliberately used pattern, for instance to indicate the mere presence or absence of a characteristic through the key alone, such as a special-workload label with an empty value, functioning essentially as a boolean flag on the object.


Practical Examples of Valid and Invalid Labels

Valid Label Examples

Keys like app, tier, app.kubernetes.io/name, and example.com/team-owner are all valid, as are values like frontend, v1.2.3, and an empty string, each satisfying the respective character and length constraints described above.

Common Invalid Patterns

A key exceeding 63 characters in its name segment, a value containing a space or an underscore-adjacent leading character like _env, or a prefix using uppercase letters, such as Example.com/team, would all be rejected by the API server's validation, since none satisfy the exact structural rules governing labels.


Why Strict Syntax Matters

Predictable Parsing and Tooling Support

Because label syntax is strictly and uniformly enforced, every tool in the ecosystem, from kubectl to third-party selectors and dashboards, can reliably parse and reason about labels without needing to handle a wide variety of unexpected or ambiguous formats, a predictability that a looser, more permissive syntax would undermine.