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.
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
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
Task: Check labels on all worker nodes
kubectl get nodes --show-labels
Task: Apply label dc=south on node2
kubectl label node node2 dc=south
Task: Apply label dc=east on node3
kubectl label node node3 dc=east
Task: Find nodes with label dc=south
kubectl get nodes -l dc=south
Task: Find all nodes which did not have label dc=south
kubectl get nodes -l dc!=south
Task: Create a pod with below details
name: web
image: nginx
kubectl run web --image=nginx
Task: Check labels on all pods in default namespace
kubectl get pods --show-labels
Task: Apply label environment=prod on pod web
kubectl label pods web environment=prod
Task: Confirm the label you applied in previous step
kubectl get pods --show-labels
Task: Create a pod using kubectl and following details
name: web1
image: nginx
label:
environment: stage
kubectl run web1 --image=nginx -l environment=stage
Task: Confirm the label you applied in previous step
kubectl get pods --show-labels
Task: Apply annotation release=1.0 on pod web
kubectl annotate pods web release=1.0
Task: Check annotation on pod web
kubectl get pods web -o yaml | grep -A 4 annotations
Task: Change annotations on pod web
kubectl annotate pods web release=1.1 --overwrite
Task: Apply label tier=prod and type=web environment=prod on pod web
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
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
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