Why would I label Services and Deployments in kubernetes?

4/24/2018

Is there a practical justification for labelling services besides that this services can be queried?

Suppose I've got a service:

apiVersion: v1
kind: Service
metadata:
    name: nginx
    labels:
        app: nginx

Can I then use that label for something else than querying that service with kubectl get svc -l app=nginx? Are there any other common use-cases? Same for Deployments. As a k8s novice I only use labels so that services can match pods and, frankly, don't see much of a use elsewhere.

-- Nemoden
kubernetes

2 Answers

4/24/2018

One of the uses I've come across for labels is for tearing down an application end to end. So when I deploy to kubernetes, I make sure everything related to the same application is labeled as such. That way I just do kubectl delete all --selector=app=foo to delete all the foo related Services, Deployments, Crons etc.

-- Luis F Hernandez
Source: StackOverflow

4/24/2018

I would say that to be able to distinguish easily between different types of objects (in your case Service) using labels. Is the most practical use case. Say you have a cluster with different environments dev, staging, production etc. and you need to find services based on that or any other type of infrastructure rules that you need to apply to your Kubernetes cluster to differentiate between similar objects.

-- aevarisak
Source: StackOverflow