Apply Labels to Kubernetes Deployment

This post will show you how to apply labels to kubernetes deployment and list deployments based on labels.


In Kubernetes, we can apply labels to deployment, pods and any object we deploy. Labels are bases on key/value pairs.


In the following example, I have a basic deployment of A Pod two key/value pair.

apiVersion: v1
kind: Pod
   name:  nginx
      environment: "dev"
      app: "nginx"
    - image: nginx:latest
      name: nginx
       - 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"