In the book Kubernetes: Up & Running, on the section "Creating Deployments", it has a yaml file that starts like this:
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
labels:
run: nginx
What is the use of applying a label to a deployment? I understand how the pods and a service interact, but when do the labels in a deployment spring into action?
Labels are useful to group inter-related apps. For example, you have a application that require a Deployment, a Service, a Database(maybe deployed with deployment or statefulset). Now,if you use similar labels to all these resources(let it is app: my-app
), then you can list,delete etc. operation based on this label.
For example, if you want to list all resources for your particular application, then you can use kubectl get all -l app=my-app
.
For more details, please read this article.