image

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

In case you prefer a video, check below our YouTube video for this lab

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

image

on first instance enter below command, this node will be master node

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

image

enter captured command in second and third node

kubeadm join  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

image

image

Check node status, all 3 nodes should be in ready state

image

Task: Check labels on all worker nodes

Solution
kubectl get nodes  --show-labels

Task: Apply label dc=south on node2

Solution
kubectl label node node2 dc=south

Task: Apply label dc=east on node3

Solution
kubectl label node node3 dc=east

Task: Find nodes with label dc=south

Solution
kubectl get nodes -l dc=south

Task: Find all nodes which did not have label dc=south

Solution
kubectl get nodes -l dc!=south

Task: Create a pod with below details

name: web
image: nginx
Solution
kubectl run web --image=nginx

Task: Check labels on all pods in default namespace

Solution
kubectl get pods --show-labels

Task: Apply label environment=prod on pod web

Solution
kubectl label pods web environment=prod

Task: Confirm the label you applied in previous step

Solution
kubectl get pods --show-labels

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

Task: Confirm the label you applied in previous step

Solution
kubectl get pods --show-labels

Task: Apply annotation release=1.0 on pod web

Solution
kubectl annotate pods web release=1.0

Task: Check annotation on pod web

Solution
kubectl get pods web -o yaml | grep -A 4 annotations

Task: Change annotations on pod web

Solution
 kubectl annotate pods web release=1.1 --overwrite

Task: Apply label tier=prod and type=web environment=prod on pod web

Solution
kubectl label pods web tier=prod type=web

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

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}]}]}}

Task: Delete all open nodes/instances and close session

  1. Select the node and click on DELETE
  2. Repeat same for any other open nodes
  3. click close session

cleanup


Click on ‘Submit Feedback’ on the bottom left of the page to submit any questions/feedback.