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:
| |
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
| |
- Add the label tempLabel=True to the Pod named name
| |
- Change the Pod named name to the label tempLabel=False
| |
- Add labels to all Pods
| |
- Delete the label whose key is tempLabel from the Pod named name
To delete, you only need to append - after the key.
| |
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:
- equality-based
Equality-based label conditions support three operators: =, ==, and !=. Here, = and == are synonymous, and multiple conditions can be joined with commas. For example:
| |
- set-based
Set-based label conditions support three operators: in, notin, and exists. For example:
| |
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:
| |
Set-based:
| |
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:
| |
