Kubernetes Labels
Labels in Kubernetes are intended to be used to specify identifying attributes of objects that are meaningful and relevant to users but are not used by the Kubernetes itself. Labels are fundamental qualities of the object that will be used for grouping, viewing, and operating. Each object can have a set of key/value labels defined. Each Key must be unique for a given object.
Labels have a simple syntax, where both the key and value are represented by strings.
Label keys can be broken down into two parts: an optional prefix and a name, separated by a slash(/). The prefix can be a DNS sub-domain with a 253-character limit.
The key name is required and must be shorter than 63 characters, beginning and ending with an alphanumeric character (a-z0-9A-Z) with dashes (--), underscores (_), dots (.), and alphanumerics between.
**Label values **are strings with a maximum length of 63 characters. The contents of the labels follow the same rules as for label keys. Here are some examples of labels in Kubernetes
key | value |
---|---|
kubernetesio/cluster-service | true |
appVersion | 1.0.0 |
UseCase
Labels enable users to map their own organizational structures onto system objects in a loosely coupled fashion, without requiring clients to store these mappings.
Service deployments and batch processing pipelines are often multi-dimensional entities (e.g., multiple partitions or deployments, multiple release tracks, multiple tiers, multiple micro-services per tier).
Management often requires cross-cutting operations, which breaks encapsulation of strictly hierarchical representations, especially rigid hierarchies determined by the infrastructure rather than by users.
To label, a resource use the below command
Syntax
kuectl label resource-type resource-name env=prod(label-name)
Rename the label by using the below command
Syntax
kuectl label --overwrite resource-type resource-name env=testing(label-name)
Delete the label by using the below command
Syntax
kuectl label resource-type resource-name env-
label all the pods in a namespace with the same label use the following command
Syntax
kuectl label resource-type --all env=prod(label-name)