Labels & Annotations
In this session , we will discuss the role of Labels and Annotations , also its role in fundamental k8s design.
In this session , we will discuss the role of Labels and Annotations , also its role in fundamental k8s design.
We 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. The metadata in an annotation can be small or large, structured or unstructured, and can include characters not permitted by labels.
Its just a place to store more metadata which is not used for any selection , grouping or operations.
Lets say , if you want to add a download URL to pod.
$ kubectl annotate pod coffee-app url=https://hub.docker.com/r/ansilh/demo-webapp
pod/coffee-app annotated
k8s@k8s-master-01:~$ kubectl describe pod coffee-app
Name: coffee-app
Namespace: default
Priority: 0
PriorityClassName: <none>
Node: k8s-worker-01/192.168.56.202
Start Time: Fri, 04 Jan 2019 00:47:10 +0530
Labels: app=frontend
run=coffee-app
Annotations: cni.projectcalico.org/podIP: 10.10.1.11/32
url: https://hub.docker.com/r/ansilh/demo-webapp
Status: Running
IP: 10.10.1.11
...
Annotations
filed containe two entries
cni.projectcalico.org/podIP: 10.10.1.11/32
url: https://hub.docker.com/r/ansilh/demo-webapp
Use same annotate command and mention only key with a dash (-) at the end of the key .
Below command will remove the annotation url: https://hub.docker.com/r/ansilh/demo-webapp
from Pod.
k8s@k8s-master-01:~$ kubectl annotate pod coffee-app url-
pod/coffee-app annotated
k8s@k8s-master-01:~$
Annotation
after removal.
k8s@k8s-master-01:~$ kubectl describe pod coffee-app
Name: coffee-app
Namespace: default
Priority: 0
PriorityClassName: <none>
Node: k8s-worker-01/192.168.56.202
Start Time: Fri, 04 Jan 2019 00:47:10 +0530
Labels: app=frontend
run=coffee-app
Annotations: cni.projectcalico.org/podIP: 10.10.1.11/32
Status: Running
IP: 10.10.1.11
If you have a bucket of white dominos and you want to group it based on the number of dots.
Lets say we want all dominos with 10 dots; we will take domino one by one and if its having 10 dots ,we will put it aside and continue the same operation until all dominos were checked.
Likewise , suppose if you have 100 pods and few of them are nginx and few of them are centos , how we can see only nginx pods ?
We need a label on each pod so that we can tell kubectl
command to show the pods with that label.
In kubernetes , label is a key value pair and it provides ‘identifying metadata’ for objects. These are fundamental qualities of objects that will be used for grouping , viewing and operating.
For now we will se how we can view them (Will discuss about grouping and operation on pod groups later)
Lets run a Coffee app Pod
k8s@k8s-master-01:~$ kubectl run coffee-app --image=ansilh/demo-coffee --restart=Never
pod/coffee-app created
k8s@k8s-master-01:~$ kubectl get pods
NAME READY STATUS RESTARTS AGE
coffee-app 1/1 Running 0 4s
k8s@k8s-master-01:~$
k8s@k8s-master-01:~$ kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
coffee-app 1/1 Running 0 37s run=coffee-app
k8s@k8s-master-01:~$
As you can see above , the lables is run=coffee-app
which is a key value pair - key
is run
value
is coffee-app
.
When we run Pod imperatively , kubectl
ass this label to Pod.
We can add label to Pod using kubectl label
command.
k8s@k8s-master-01:~$ kubectl label pod coffee-app app=frontend
pod/coffee-app labeled
k8s@k8s-master-01:~$
Here we have add a label app=frontend
to pod coffee-app
.
Lets start another coffee application pod with name coffee-app02.
k8s@k8s-master-01:~$ kubectl run coffee-app02 --image=ansilh/demo-coffee --restart=Never
pod/coffee-app02 created
k8s@k8s-master-01:~$
Now we have two Pods.
k8s@k8s-master-01:~$ kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
coffee-app 1/1 Running 0 5m5s app=frontend,run=coffee-app
coffee-app02 1/1 Running 0 20s run=coffee-app02
k8s@k8s-master-01:~$
Lets see how can I select the Pods with label app=frontend.
k8s@k8s-master-01:~$ kubectl get pods --selector=app=frontend
NAME READY STATUS RESTARTS AGE
coffee-app 1/1 Running 0 6m52s
k8s@k8s-master-01:~$
You can add as many as label you want.
We can add a prefix like app
( eg: app/dev=true
) which is also a valid label.
Limitations | |
---|---|
Prefix | DNS subdomain with 256 characters |
Key | 63 characters |
Value | 63 characters |
See the labels of coffee-app
k8s@k8s-master-01:~$ kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
coffee-app 1/1 Running 0 28m app=frontend,run=coffee-app
coffee-app02 1/1 Running 0 24m run=coffee-app02
Remove the app
label
k8s@k8s-master-01:~$ kubectl label pod coffee-app app-
pod/coffee-app labeled
Resulting output
k8s@k8s-master-01:~$ kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
coffee-app 1/1 Running 0 29m run=coffee-app
coffee-app02 1/1 Running 0 24m run=coffee-app02
k8s@k8s-master-01:~$