KodeCloud CKAD Labels, Selectors and Annotations
Links: 111 KodeCloud Index
Labels and Selectors¶
-
Labels and Selectors are a standard method to group things together and filter them according to your needs.
- Labels are properties attached to each item.
- Selectors help us filter these items.
- Labels are properties attached to each item.
-
Over a period of time we may have a ton of objects in k8s. We will need a way to group them together.
-
Sample pod definition file specifying labels
-
We can have as many labels we want.
- Selecting pods with specific labels:
k get pods --selector app=App1
k get all -o wide --selector env=Prod
-
Specifying multiple selectors:
k get all --selector env=prod,bu=finance,tier=frontend
-
Instead of
--selector
we can also use-l
:k get all -l env=prod
Labels are limited to 63 characters.
Annotations¶
- While labels and selectors are used to group and select objects whereas annotations are used to attach arbitrary non-identifying metadata (k8s doesn't care about this data) to objects.
- Clients such as tools and libraries can retrieve this metadata and can retrieve it for configuration. For example ingress controllers.
- Annotations are present in the metadata and they are defined on the same level as labels.
You can use either labels or annotations to attach metadata to Kubernetes objects. Labels can be used to select objects and to find collections of objects that satisfy certain conditions. In contrast, annotations are not used to identify and select objects.
- Annotations are also key-value pairs (similar to labels) that can be used to store the unstructured information pertaining to the Kubernetes objects.
- Annotations can be used to add timestamps, commit SHA, issue tracker links, or names/information about users who are responsible for specific objects in an organization.
- Annotations can also be used to add information about client libraries or tools.
Labels are for k8s while annotations are for humans.
- Labels have some constraints (like they must be limited to 63 characters), annotations have no constraints.
While annotations do not inherently imply semantics to the Kubernetes core, it is still possible for them to affect operation in some cases.
- A good example of this is with the NGINX Ingress controller (along with other ones).
- The NGINX Ingress controller allows you to add Kubernetes annotations onto your ingress objects to affect their behaviour.
- Most of these map cleanly to the configuration options available in NGINX, and as such, it is a nice way to allow mapping NGINX specific concepts onto your Kubernetes resources.
- The NGINX Ingress controller is then able to read these annotations and apply them as needed.
Reference¶
Last updated: 2023-05-04