This post will show you how to apply labels to kubernetes deployment and list deployments based on labels.
Label
In Kubernetes, we can apply labels to deployment, pods and any object we deploy. Labels are bases on key/value pairs.
Example
In the following example, I have a basic deployment of A Pod two key/value pair.
apiVersion: v1 kind: Pod metadata: name: nginx labels: environment: "dev" app: "nginx" spec: containers: - image: nginx:latest name: nginx ports: - containerPort: 8080 name: http protocol: TCP
After the deployment, I can run the get code command that will display the only the pods that have dev environment label as shown below.
kubectl get pods --selector="environment=dev"
To view all the pods and thier labels I will run the following command as shown below:
kubectl get pod --show-labels
And finally, we can also apply labels to exising deployments using the following code.
kubectl label pod nginx "ver=1.0"