Why is there a need of defining one's deployment's labels multiple times? Just to explain my question fast I'll give an example.
apiVersion: apps/v1
kind: Deployment
metadata:
name: app
# 1st Time
labels:
app: app
tier: backend
spec:
selector:
# 2nd Time
matchLabels:
app: app
tier: backend
template:
metadata:
# 3rd time
labels:
app: app
tier: backend
My first issue is that I really couldn't find any good reason for the spec.selector.matchlabels
to exist at all. spec.selector.matchlabels
must match spec.template.metadata.labels
right?
DESCRIPTION:
Label selector for pods. Existing ReplicaSets whose pods are selected by this will be the ones affected by this deployment. It must match the pod template's labels.
So why do you have to explicitely define them, if they should be fixed to a specific value that kubernetes knows anyway and will throw an error anyways if one does not match another?
My second issue, which might have a more reasonable answer, is why do you have to explicitely define deployment's labels and template's labels? Aren't they supposed to match each other as well or are there any cases where they might not match to each other? Am I just using them wrong? There's no clear documentation over their use against template's labels. :/
In case I might be doing or understand something wrong please do mention! :D