Exercise 3: Labels
In this Exercise, you will create a pod that has labels associated with it. Labels make it easy to filter the pods later. Labels play a vital role in the Kubernetes ecosystem, so it’s important to understand their proper usage.
Task 1 - Assign A New Label To A Running Pod
Assign a new label (key=value) pair to a running pod. This comes in handy when you are troubleshooting an issue and would like to distinguish between different pod(s). Assign a new label health=fair to the pod nginx-pod, which is already running.
kubectl label pod nginx-pod health=fair
kubectl label pod nginx-pod health=fair
Use
kubectl get pods
to show the labels of running pods
Task 2 - Update An Existing Label That Is Assigned To A Running Pod
Use
kubectl
to update the value of an existing label on a running pod. Change the value of the label kind=web to kind=db of the nginx-pod pod.Show the pod labels again. Notice that kind has changed from
web
todb
.kubectl get pods --show-labels
kubectl get pods --show-labels
Task 3 - Delete A Label That Is Assigned To A Running Pod
Delete the health label from the nginx-pod pod.
Run the command below to show the pod labels again. Notice that the health label is no longer included on the list of labels.
kubectl get pods --show-labels
kubectl get pods --show-labels
Task 4 - Delete Pods Based On Their Labels
Delete all the pods that have the target label with value dev.