Labels are the mechanism you use to organize Kubernetes objects.
A label is a key-value pair that is meaningful and relevant to users with certain restrictions concerning length and allowed values.
-
Save the following as
label-pod.yaml
cat > label-pod.yaml <<EOF apiVersion: v1 kind: Pod metadata: name: label-pod labels: env: development spec: containers: - name: sise image: mhausenblas/simpleservice:0.5.0 ports: - containerPort: 9876 EOF
-
Run the following command to create the pod
kubectl create -f label-pod.yaml
-
Run the following to list the pod, noting the new column
kubectl get pods --show-labels NAME READY STATUS RESTARTS AGE LABELS label-pod 1/1 Running 0 10m env=development
-
Add an additional label to the pod by running
kubectl label pods label-pod owner=you kubectl get pods --show-labels NAME READY STATUS RESTARTS AGE LABELS label-pod 1/1 Running 0 16m env=development,owner=you
We can use labels for filtering a list.
-
To list all pods labeled as
owner=you
, we can use the--selector
option:kubectl get pods --selector owner=you NAME READY STATUS RESTARTS AGE label-pod 1/1 Running 0 27m
Note: The
--selector
option can be abbreviated to-l
-
To list pods labeled with
env=development
we can run:kubectl get pods -l env=development NAME READY STATUS RESTARTS AGE label-pod 1/1 Running 0 27m
-
Save the following as
label-pod2.yaml
cat > label-pod2.yaml <<EOF apiVersion: v1 kind: Pod metadata: name: label-pod2 labels: env: production owner: you spec: containers: - name: sise image: mhausenblas/simpleservice:0.5.0 ports: - containerPort: 9876 EOF
-
Create the above pod using
kubectl create -f label-pod2.yaml
-
To list all pods labeled with
env=development
orenv=production
kubectl get pods -l 'env in (production, development)' NAME READY STATUS RESTARTS AGE label-pod 1/1 Running 0 43m label-pod2 1/1 Running 0 3m
-
Other verbs also support label selection, for example, you could remove both of these pods with:
kubectl delete pods -l 'env in (production, development)'
Note: Labels are not restricted to pods. In fact, you can apply them to all sorts of objects, such as nodes
or services
.
In this excercise you will add the phone number of responsible person to the running pod. Phone number contains symbols that can't be used in the label field. So you will add a new phone
field to the annotations
.
-
Edit
annotation-pod.yaml
cat > annotation-pod.yaml <<EOF apiVersion: v1 kind: Pod metadata: name: annotation-pod labels: env: production owner: you phone: "+1 (123) 456-78-90" spec: containers: - name: sise image: mhausenblas/simpleservice:0.5.0 ports: - containerPort: 9876 EOF
-
Try to create the pod
kubectl apply -f annotation-pod.yaml
You will get an error as phone number does not satisfy formatting requirements for a label field.
-
Change phone field to annotation
cat > annotation-pod.yaml <<EOF apiVersion: v1 kind: Pod metadata: name: annotation-pod labels: env: production owner: you annotations: phone: "+1 (123) 456-78-90" spec: containers: - name: sise image: mhausenblas/simpleservice:0.5.0 ports: - containerPort: 9876 EOF
-
Create the pod
kubectl apply -f annotation-pod.yaml
-
Check the annotation was stored in the pods metadata
kubectl get pods --selector owner=you -o jsonpath='{.items[*].metadata.annotations.phone}'
-
Deploy 3 pods; each one with different labels:
version=1
,version=2
andversion=3
-
List the pods using selectors that will return all pods with versions not equal to 3
Refer to the documentation to review selector syntax.
Next: Services