Why is a Kubernetes Deployment YAML file so repetitive?

3/2/2019

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:

  1. metadata.labels,
  2. spec.selector.matchLabels, and
  3. spec.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
-- spro
kubernetes

1 Answer

3/2/2019

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.

-- coderanger
Source: StackOverflow