DEV Community

Cover image for K8s Exercise : Labels and Annotations
Akshay Rao
Akshay Rao

Posted on

K8s Exercise : Labels and Annotations

Introduction
Hi, I am Akshay Rao, will be starting a exercise series on k8s.
In this blog there will not explanation only problems and solutions.if you want explanation have a look at this series:-
https://dev.to/aksrao1998/series/24887

Pre-requisite
have minikube or kind running in the local machine.

Note:- k is alias for kubectl.

Let's Start
Problem

  1. Create 3 pods with names nginx1,nginx2,nginx3. All of them should have the label app=v1.
  2. Show all labels of the pods.
  3. Change the labels of pod 'nginx2' to be app=v2.
  4. Get the label 'app' for the pods (show a column with APP labels).
  5. Get only the 'app=v2' pods
  6. Add a new label tier=web to all pods having 'app=v2' or 'app=v1' labels.
  7. Add an annotation 'owner: marketing' to all pods having 'app=v2' label.
  8. Remove the 'app' label from the pods we created before.
  9. Annotate pods nginx1, nginx2, nginx3 with "description='mydescription'" value.
  10. Check the annotations for pod nginx1.
  11. Remove the annotations for these three pods.

Solution

#1 Soln
[ ~ (⎈|minikube:mynamespace)]$ k run nginx1 --image=nginx -l app=v1
pod/nginx1 created
[~ (⎈|minikube:mynamespace)]$ k run nginx2 --image=nginx -l app=v1
pod/nginx2 created
[~ (⎈|minikube:mynamespace)]$ k run nginx3 --image=nginx -l app=v1
pod/nginx3 created

#2 Soln
[~ (⎈|minikube:mynamespace)]$ k get po -l app=v1
NAME     READY   STATUS    RESTARTS   AGE
nginx1   1/1     Running   0          2m11s
nginx2   1/1     Running   0          2m4s
nginx3   1/1     Running   0          115s

#3 Soln
[~ (⎈|minikube:mynamespace)]$ k label pod nginx2 app=v2 --overwrite
pod/nginx2 labeled

#4 Soln
[ ~ (⎈|minikube:mynamespace)]$ k get po -l app
NAME     READY   STATUS    RESTARTS   AGE
nginx1   1/1     Running   0          16m
nginx2   1/1     Running   0          16m
nginx3   1/1     Running   0          16m

#5 Soln
[ ~ (⎈|minikube:mynamespace)]$ k get po -l app=v2
NAME     READY   STATUS    RESTARTS   AGE
nginx2   1/1     Running   0          14m

#6 Soln
[~ (⎈|minikube:mynamespace)]$ k label po -l "app in (v1.v2)" teir=web

#7 Soln
[~ (⎈|minikube:mynamespace)]$ k annotate po -l "app=v2" owner=marketing
pod/nginx2 annotated

#8 Soln
[~ (⎈|minikube:mynamespace)]$ k label po nginx{1..3} app-
pod/nginx1 unlabeled
pod/nginx2 unlabeled
pod/nginx3 unlabeled

#9 Soln
[~ (⎈|minikube:mynamespace)]$ k annotate po nginx{1..3} description=mydescription
pod/nginx1 annotated
pod/nginx2 annotated
pod/nginx3 annotated

#10 Soln
[~ (⎈|minikube:mynamespace)]$ k annotate po nginx1 --list
description=mydescription

#11 Soln
[ts-akshay.rao@JP-FVFZ91DHL414 ~ (⎈|minikube:mynamespace)]$ k annotate po nginx{1..3} description-
pod/nginx1 annotated
pod/nginx2 annotated
pod/nginx3 annotated

Enter fullscreen mode Exit fullscreen mode

Top comments (0)