This page looks best with JavaScript enabled

Labels and Selectors in Kubernetes

 ·  ☕ 3 min read

1. Labels

1.1 What Labels Are

Labels are key-value pairs attached to an object. You can add labels directly when the object is created, or modify them dynamically afterwards.

Label format:

1
2
3
4
"labels": {
  "key1" : "value1",
  "key2" : "value2"
}

Format requirements:

  • Key: must be unique
  • Value: must start with a letter or a digit, may contain letters, digits, hyphens, dots, and underscores, and is at most 63 characters long

1.2 What Labels Are For

Users employ labels to identify, organize, and select resource objects.

Labels have no direct meaning for how the system runs, and they are not used to store structured or complex data. That is because labels are indexed and reverse-indexed, for querying and monitoring.

For complex structures attached to a resource object, you should use annotations.

Annotations can attach arbitrary non-identifying metadata to an object. Using clients such as tools and libraries, we can retrieve this data. The metadata in an annotation may be structured or unstructured, and there is no restriction on the character range.

1.3 Label Operations

  • View the details of the Pod named name
1
kubectl describe pods name
  • Add the label tempLabel=True to the Pod named name
1
2
kubectl label pods name tempLabel=True
pod/name labeled
  • Change the Pod named name to the label tempLabel=False
1
2
kubectl label --overwrite pods name tempLabel=False
pod/name labeled
  • Add labels to all Pods
1
2
3
kubectl label pods --all tempLabel2=True
pod/kube-nginx-7c765ffd95-2pxfk labeled
...
  • Delete the label whose key is tempLabel from the Pod named name

To delete, you only need to append - after the key.

1
2
kubectl label pods name tempLabel-
pod/name labeled

2. Labels Selectors

2.1 What Labels Selectors Are

Labels selectors are label selectors.

Label selectors are a core part of Kubernetes. In practice, multiple objects usually carry the same labels. You filter out a set of resource objects by label and operate on them in bulk.

2.2 Types of Labels Selectors

The Kubernetes API currently supports two types of selectors:

  1. equality-based

Equality-based label conditions support three operators: =, ==, and !=. Here, = and == are synonymous, and multiple conditions can be joined with commas. For example:

1
frontend:environment=production, tier!=frontend
  1. set-based

Set-based label conditions support three operators: in, notin, and exists. For example:

1
2
3
4
environment in (production, qa)
tier notin (frontend, backend)
partition
!partition

2.3 How to Use Labels Selectors

  • When querying and filtering through the API, just add GET filter parameters

Equality-based:

?labelSelector=environment%3Dproduction,tier%3Dfrontend

Set-based:

?labelSelector=environment+in+%28production%2Cqa%29%2Ctier+in+%28frontend%29

  • Use kubectl in the console

Equality-based:

1
kubectl get pods -l environment=production,tier=frontend

Set-based:

1
kubectl get pods -l 'environment in (production),tier in (frontend)'

3. Field Selectors

Field selectors let you filter resource objects by field values.

Different resource objects support different filter fields. All resource types support filtering on the metadata.name and metadata.namespace fields.

Here is a usage example, selecting all Pods whose phase field is Running:

1
2
3
4
5
kubectl get pods --field-selector status.phase=Running
NAME                                              READY   STATUS    RESTARTS   AGE
kube-nginx-7c765ffd95-2pxfk                       1/1     Running   3          17d
my-harbor-harbor-chartmuseum-567fb69cb6-q88ss     1/1     Running   0          3d20h
...

WeChat Official Account
WRITTEN BY
WeChat Official Account