<p>I've gone over the following docomentation page: <a href="https://kubernetes.io/docs/concepts/workloads/controllers/deployment/" rel="noreferrer">https://kubernetes.io/docs/concepts/workloads/controllers/deployment/</a></p>

<p>The example deployment yaml is as follows:</p>

<pre><code>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
</code></pre>

<p>We can see here three different times where the label <code>app: nginx</code> is mentioned.</p>

<p>Why do we need each of them? I had a hard time understanding it from the official documentation.</p>

<p>The <strong>first label</strong> is for deployment itself, it gives label for that particular deployment. Lets say you want to delete that deployment then you run following command:</p>

<pre><code>kubectl delete deployment -l app=nginx
</code></pre>

<p>This will delete the entire deployment.</p>

<p>The <strong>second label</strong> is <code>selector: matchLabels</code> which tells the resources(service etc) to match the pod according to label. So lets say if you want to create the service which has all the pods having labels of <code>app=nginx</code> then you provide following definition:</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  type: LoadBalancer
  ports:
    - port:  80
  selector:
    app: nginx
</code></pre>

<p>The above service will look for the matchLabels and bind pods which have label <code>app: nginx</code> assigned to them</p>

<p>The <strong>third label</strong> is <code>podTemplate</code> labels, the <code>template</code> is actually <code>podTemplate</code>. It describe the pod that it is launched. So lets say you have two replica deployment and k8s will launch 2 pods with the label specified in <code>template: metadata: labels</code>. This is subtle but important difference, so you can have the different labels for deployment and pods generated by that deployment.</p>
