Lab 06 - Labels, selectors and annotations

Lab 06 - Labels, selectors and annotations

Infodiscover

Goal: slice a pile of Pods any way you need. Chapter: Labels, selectors and annotations

Set-up - six Pods with different labels:

cd ~
kubectl create ns shop
for p in "web-1 app=web,env=prod,tier=frontend" "web-2 app=web,env=dev,tier=frontend" \
         "api-1 app=api,env=prod,tier=backend"  "api-2 app=api,env=test,tier=backend" \
         "db-1 app=db,env=prod"                 "cache-1 app=cache,env=dev"; do
  set -- $p
  kubectl run $1 -n shop --image nginx:1.27 --labels $2
done
kubectl config set-context --current --namespace=shop

Task 1. List all Pods with their labels. Then show only app and env as columns.

kubectl get pods --show-labels
kubectl get pods -L app,env

Task 2. Show only production Pods. Expected: web-1, api-1, db-1.

kubectl get pods -l env=prod

Task 3. Show production frontend Pods. Expected: web-1.

kubectl get pods -l env=prod,tier=frontend

Task 4. Show Pods in dev or test. Expected: 3 Pods.

kubectl get pods -l 'env in (dev,test)'

Task 5. Show Pods that have no tier label. Expected: db-1, cache-1.

kubectl get pods -l '!tier'

Task 6. Promote api-2 from test to prod, and give db-1 the label tier=data.

kubectl label pod api-2 env=prod --overwrite
kubectl label pod db-1 tier=data
kubectl get pods -L env,tier

Without --overwrite kubectl refuses to change an existing label.


Task 7. Remove the env label from cache-1.

kubectl label pod cache-1 env-

Task 8. Add the annotation owner=team-a@example.com to every web Pod, then read it back from one of them.

kubectl annotate pods -l app=web owner=team-a@example.com
kubectl get pod web-1 -o jsonpath='{.metadata.annotations.owner}{"\n"}'

Task 9. Try to select Pods by the annotation with -l owner=.... Why does it return nothing?

Annotations are not indexed and cannot be selected. If you need to select by it, it is a label.


Task 10. Delete all dev Pods with one command.

kubectl delete pods -l env=dev

Selectors work for delete too - check with get first.

Clean up

kubectl config set-context --current --namespace=default
kubectl delete ns shop