Selectors

Selectors provide a means of matching a set of matching resources (commonly Pods) based on their labels. Labels can be used both in manifests to define a set of replicas matching a constraint and in the API (and clients) when searching for objects.

Selectors are always ANDed together, so objects must match all selectors. There is no OR.

Types

The types can be combined, and will be ANDed together.

Equality-based

Equality-based selectors are based on equality (key = value, key == value) and inequality (key != value; only via the API, and not in manifests).

In a manifest:

# Inside a selector
matchLabels:
    app: my-app

In the API and clients, selectors are comma-delimited lists of key-value pairs, e.g.:

environment=prod,tier=frontend

Set-based

Set-based selectors support In and NotIn operations against a key and a set of values, and Exists (key) and DoesNotExist (!key) operations against a key.

In a manifest:

# Inside a selector
matchExpressions:
  - key: app
      operator: In
      values:
        - my-app
        - my-other-app
  - key: some-feature
      operator: Exists

In the API and clients, selectors are comma-delimited lists of key-operation-values tuples:

environment in (prod, uat),tier in (frontend)

kubectl

In the CLI, use the -l switch.


Backlinks