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

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

Check labels on all worker nodes

Solution
kubectl get nodes  --show-labels

Apply label dc=south on node2

Solution
kubectl label node node2 dc=south

Apply label dc=east on node3

Solution
kubectl label node node3 dc=east

Find nodes with label dc=south

Solution
kubectl get nodes -l dc=south

Find all nodes which did not have label dc=south

Solution
kubectl get nodes -l dc!=south

Create a pod with below details

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

Check labels on all pods in default namespace

Solution
kubectl get pods --show-labels

Apply label enviornment=prod on pod web

Solution
kubectl label pods web enviornment=prod

Confirm the label you applied in previous step

Solution
kubectl get pods --show-labels

Create a pod using kubectl and following details

name: web1
image: nginx
label:
  environment: stage

Solution
kubectl run web1 --image=nginx -l environment=stage

Confirm the label you applied in previous step

Solution
kubectl get pods --show-labels

Apply annotation release=1.0 on pod web

Solution
kubectl annotate pods web release=1.0

Check annotation on pod web

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

Change annotation on pod web

Solution
 kubectl annotate pods web release=1.1 --overwrite

Apply label tier=prod and type=web enviornment=prod on pod web

Solution
kubectl label pods web tier=prod type=web

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

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

Delete all three nodes and close session

  1. click on first node and then DELETE
  2. Repeat same for second and third node
  3. click close session

image

Congratulations you have completed this lab

Let us know if you still have any question
Youtube video for this lab