The first and most minimal example of a Deployment in the Kubernetes documentation has the app: nginx
line that repeats itself three times. I understand it's a tag, but I haven't found anything that explains why this needs to be specified for all of:
metadata.labels
,spec.selector.matchLabels
, andspec.template.metadata.labels
The example deployment file:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
So 1 and 3 are technically unrelated. 1 is the labels for the deployment object and only matter for your own organizational purposes. 3 are the labels that will be put on the generated pods. As for why Deployments rely on manually specifying a selector against the pod labels, it is to ensure stay stateless. The deployment controller can restart at any time and things will be safe. It could be improved in the future though, if someone has a solid proposal that takes care of all the edge cases.