How To Use Label Selectors to Identify Resources
  • 24 Jun 2024
  • 2 Minutes to read
  • Contributors
  • Dark
    Light

How To Use Label Selectors to Identify Resources

  • Dark
    Light

Article summary

Background

Labels are a mechanism that allows you to associate meaningful and relevant attributes to resources in your Kubernetes environment. The labels you associate with resources can then be used to organize, categorize, and select those resources. Label selectors are the mechanism you use to select and filter resources based on their labels. Label selectors allow you to precisely identify specific subsets of resources by specifying criteria that match resource labels.

Label Selectors

Label selectors are a search/filter syntax used to identify a set of resources based on the labels associated with the resources. A label selector can consist of multiple requirements; each requirement must be comma-separated. The comma separator acts as a logical AND; when multiple requirements are specified, all must be satisfied. Label selectors are case-sensitive; case mismatches will exclude a resource from the filter result set.

There are two types of label selectors:

  • equality-based

  • set-based

Set-based requirements can be mixed with equality-based requirements in the same label selector to define the appropriate filtering constraints.

Equality-based label selectors

Equality- and inequality-based label selectors filter by directly using label key-value pairs. To be considered a match, resources must have all of the specified labels, but they can have additional labels that are not part of the filtering constraints. Supported operators include =, ==, and !=. The = and == both represent equality, while != represents inequality.

Examples

Label SelectorResults
environment=productionIdentifies all resources with the environment=production label
environment!=betaIdentifies all resources with an environment label that is not environment=beta
app=shopping-cartIdentifies all resources with the app=shopping-cart label
environment=production, app=shopping-cartIdentifies all resources with the environment=production label AND the app=shopping-cart label

Set-based label selectors

Set-based label selectors allow for more complex queries by filtering keys according to a set of values. To be considered a match, resources must have labels that match the specified criteria, but they can have additional labels that are not part of the filtering constraints. Supported operators include in and notin; you can also search on just the key.

Examples

Label SelectorResults
environment in (production, qa)Identifies all resources with an environment=production or an environment=qa label
environment notin (beta)Identifies all resources with an environment label that is not environment=beta
app in (shopping-cart)Identifies all resources with the app=shopping-cart label
environment in (production), app in (shopping-cart)Identifies all resources with the environment=production label AND the app=shopping-cart label
tierIdentifies all resources with a tier label, regardless of the value

More information
Kubernetes documentation: Labels and Selectors


Was this article helpful?