In this blog, we will explore about Labels and Annotation. We will explain their use cases so that we can decide whether to use labels or annotation for a particular use case.
In my previous blog we have already seen different kinds of pods and their life cycle.
In general, we always want to organize our pods based on some properties so that we can query against specific pods. To do so we will be add labels to the pods.
- Functional Kubernetes Cluster configured
- Kubernetes’ command-line tool, kubectl, which is the command-line interface for running commands against Kubernetes clusters.
- Clone this git repo for all the yaml files which we will be using in this blog
- Pod Create with Labels
- Adding Labels to a Running Pod
- Select Kubernetes objects using Label selector
- Modify/Delete the Label
- Pod create with Annotation
- Modify/Delete the annotation
- They are are the metadata which contain unique information of the Kubernetes objects
- key-value pair attached to the objects like pod
- We can attach the label at the time of creation or we can update it at runtime
- Kubernetes internally maintains the mappings of labels to corresponding objects using optimized data structures to make these queries faster.
- With labels, you can also run your specific pods on selected nodes.
Organize Pods by team/project/organization
- You can organize pods so that each team will see only their pods
- Specific to certain projects
Run a Pod on Specific Nodes
This is a very basic requirement when you want to run your pod on a specific node because of its hardware/specific configuration etc.
We can do that using
Create a Pod with Labels
- name: first-container
Create a pod
kubectl create -f pod-with-labels.yamlkubectl describe pod pod-with-labels
Adding Labels to a Running Pod
You can update the existing label or add the new one using below command
kubectl label pod pod-with-labels app=nginx1 --overwrite
kubectl label pod pod-with-labels env=dev
kubectl label pod pod-with-labels team=dev org=abc
How to remove a label
kubectl label pod pod-with-labels env-
Selecting Kubernetes Objects Using Label Selectors
kubectl get pods -l app=nginx1
kubectl get pods -l app=nginx1,foo=bar
Annotations are also key-value pairs 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.
commit-message: testign for nginx
- name: first-container
Create a Pod
kubectl create -f pod-with-annotation.yamlkubectl describe pod pod-with-annotation
How to add annotation using command line
kubectl annotate pod pod-with-annotation jiralink=DED-232
kubectl annotate --overwrite pod <pod_name> <annotation_key>=<annotation_label>
Delete the annotation
kubectl annotate pod pod-with-annotation jiralink-
You can find more articles on Kubernetes on my website.