About this Lab
1. Overview

In this lab we will practice using Labels and Annotations
Labels
Labels plays a very important role in Kubernetes.
Labels are key/value pair used to tag and select an object in Kubernetes.
They can help to select one or group of objects with same label or labels.
You can add them, while creating an object and modify them later if required.
All key’s in an object must be unique
metadata:
labels:
key1: value1
key2: value2
You can add multiple labels to your Kubernetes objects.
Example :
release : stable
release : alpha
release : beta
environment : development
environment : stage
environment : production
A label key has two parts, an optional prefix and key name, they are separated by / .
Label key prefix must be a DNS subdomain and could have max 253 character.
If there is no prefix, label is assumed to be private to user.
Control plane component and automation tools must add prefix, when adding a label.
Label key name and value are must and could have max 63 character.
They can only start and end with a letter [a-z,A-Z] or numbers [0-9], you can use dash -, underscore _ , dot . or letters and numbers in between.
Label Selectors
Label selectors are used to identify set of objects
Example :
environment = stage
release != stable
environment in (stage, development)
release notin (alpha, beta)
release
!release
Annotations
Annotation are similar to labels, they are used for information purpose only.
This information could be fetched later.
Some examples are build, release, timestamps, PR number or any information related to an object.
kubernetes.io and k8s.io is the prefix used by Kubernetes core components.
Rules similar to Label apply to Annotation key prefix and name.
Annotation value could be larger and have non human readable data.
Lets Practice
2. Demo
In case you prefer a video, check below our YouTube video for this lab
3. Create Kubernetes cluster
Task: Create Kubernetes cluster with 3 worker nodes.
Master: 1 node
Worker: 2 node
Hint
Solution
Create docker hub account. Docker Hub if you already have one skip this step
Open Play with Kubernetes login with your docker hub account.
Click on start
It will start a 4 hr session
create three instance
click on + ADD NEW INSTANCE three time to add three instances

kubeadm init --apiserver-advertise-address $(hostname -i) --pod-network-cidr 10.5.0.0/16
enter below command on first node
kubectl apply -f https://raw.githubusercontent.com/cloudnativelabs/kube-router/master/daemonset/kubeadm-kuberouter.yaml
capture output of kubeadm join XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

enter captured command in second and third node
kubeadm join XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX



4. Check labels on nodes
Task: Check labels on all worker nodes
Solution
kubectl get nodes --show-labels
5. Apply label to a node
Task: Apply label dc=south on node2
Solution
kubectl label node node2 dc=south
6. Apply label to a node contd.
Task: Apply label dc=east on node3
Solution
kubectl label node node3 dc=east
8. Get node with label contd.
Task: Find all nodes which did not have label dc=south
Solution
kubectl get nodes -l dc!=south
9. Create a pod
Task: Create a pod with below details
name: web
image: nginx
Solution
kubectl run web --image=nginx
10. Check pod labels
Task: Check labels on all pods in default namespace
Solution
kubectl get pods --show-labels
11. Apply label on a pod
Task: Apply label environment=prod on pod web
Solution
kubectl label pods web environment=prod
12. Check pod labels
Task: Confirm the label you applied in previous step
Solution
kubectl get pods --show-labels
13. Apply label on pod
Task: Create a pod using kubectl and following details
name: web1
image: nginx
label:
environment: stage
Solution
kubectl run web1 --image=nginx -l environment=stage
14. Check pod labels
Task: Confirm the label you applied in previous step
Solution
kubectl get pods --show-labels
15. Apply annotation on a pod
Task: Apply annotation release=1.0 on pod web
Solution
kubectl annotate pods web release=1.0
16. Check annotation on a pod
Task: Check annotation on pod web
Solution
kubectl get pods web -o yaml | grep -A 4 annotations
17. Change annotation on a pod
Task: Change annotations on pod web
Solution
kubectl annotate pods web release=1.1 --overwrite
18. Apply labels on a pod
Task: Apply label tier=prod and type=web environment=prod on pod web
Solution
kubectl label pods web tier=prod type=web
19. Apply label and annotation on pod
Task: Create a pod using yaml and following details
name: demo
image: nginx
label:
app: demo
type: web
annotations:
release: v1.0
delivery: Q3
Solution
vi pods.yaml
apiVersion: v1
kind: Pod
metadata:
name: demo
labels:
app: demo
type: web
annotations:
release: v1.0
delivery: Q3
spec:
containers:
- name: demo-nginx
image: nginx
ports:
- containerPort: 80
kubectl apply -f pods.yaml
20. Check annotation on a pod
Task: Check annotation on pod demo
Solution
you will observe something similar to below in annotations, this has been added by kuberentes
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{"delivery":"Q3","release":"v1.0"},"labels":{"app":"demo","type":"web"},"name":"demo","namespace":"default"},"spec":{"containers":[{"image":"nginx","name":"demo-nginx","ports":[{"containerPort":80}]}]}}
21. Cleanup
Task: Delete all open nodes/instances and close session
- Select the node and click on DELETE
- Repeat same for any other open nodes
- click close session
}}