The resources that we create in a kubernetes cluster can be organised by using labels. Before we talk about field selector in Kubernetes, let us walk through quickly about labels.
Labels are key value pairs that can be used to identify, or group the resources in Kubernetes. In other words, labels can be used to select resources from a list.
You can label Kubernetes native resources as well as Custom Resources. To understand it more clearly, let us do some hands on practice on labels.
The tutorial will assume that you have a working minikube setup or a Kubernetes cluster setup.
Following is link to the yaml . It’s application will create a pod.
The yaml looks following:
- name: label-example
- containerPort: 8000
Notice the bold text in above yaml. That is one way to add labels to a resource by specifying in yaml.
Let us create a pod by executing following command:
You can use above command directly or copy the content to save it on your local machine in a file , say
ashutosh@miracle:~/Desktop/artifacts/samples$ kubectl apply -f sample-pod.yaml
pod/example-pod createdashutosh@miracle:~/Desktop/artifacts/samples$ kubectl get po
NAME READY STATUS RESTARTS AGE
example-pod 1/1 Running 0 3m
Now, let us run the following commands to check for labels in the pod.
ashutosh@miracle:~/Desktop/artifacts/samples$ kubectl get pod example-pod --show-labelsNAME READY STATUS RESTARTS AGE LABELSexample-pod 1/1 Running 0 3m env=development
As you can see in the above output
example-pod is having a label of key value pair as
You can also do a
kubectl get pod example-pod -o yaml to see all the fields along with labels.
Let us add another label to the above pod…
Read the complete article in MayaData’s Blog