Labels and Selectors

Labels are the mechanism you use to organize Kubernetes objects. A label is a key-value pair with certain restrictions concerning length and allowed values but without any pre-defined meaning.

Let’s create a pod that initially has one label (env=development):

kubectl create -f https://raw.githubusercontent.com/mhausenblas/kbe/master/specs/labels/pod.yaml
pod "labelex" created

kubectl get pods --show-labels
NAME                                   READY     STATUS              RESTARTS   AGE       LABELS
labelex                                0/1       ContainerCreating   0          26s       env=development

You can add a label to the pod as:

kubectl label pods labelex owner=admatic
pod "labelex" labeled

kubectl get pods --show-labels
NAME                                   READY     STATUS    RESTARTS   AGE       LABELS
labelex                                1/1       Running   0          4m        env=development,owner=admatic

To use a label for filtering, for example to list only pods that have an owner that equals michael, use the --selector option.

The --selector option can be abbreviated to -l

kubectl get pods --selector owner=admatic
NAME      READY     STATUS    RESTARTS   AGE
labelex   1/1       Running   0          5m

kubectl get pods -l env=development
NAME      READY     STATUS    RESTARTS   AGE
labelex   1/1       Running   0          6m

Kubernetes objects also support set-based selectors.

kubectl get pods -l 'env in (production, development)'
NAME      READY     STATUS    RESTARTS   AGE
labelex   1/1       Running   0          6m

results matching ""

    No results matching ""