<p>In the below yaml:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx
  labels:
    app: my-nginx # Line 6
spec:             # Line 7  
  replicas: 2
  selector:
    matchLabels:
      app: my-nginx    # line 11
  template:
    metadata:
      labels:
        app: my-nginx   # Line 15
    spec:                # Line 16
      containers:
      - name: my-nginx
        image: nginx:alpine
        ports:
        - containerPort: 80
        resources:
          limits:
            memory: &quot;128Mi&quot; #128 MB
            cpu: &quot;200m&quot; #200 millicpu (.2 cpu or 20% of the cpu)
</code></pre>
<hr />
<p>Deployment is given a label(<code>app: nginx</code>) at Line 6.</p>
<p>Deployment spec at Line 7 uses the Pod spec mentioned in Line 16</p>
<hr />
<ol>
<li><p>What is the purpose of <code>selector</code> field with <code>matchLabels</code>?</p>
</li>
<li><p>What is the purpose of <code>template</code> field with <code>labels</code>?</p>
</li>
</ol>

<p>Tried to add comments to explain the role of labels:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx
  labels:
    app: my-nginx # LABEL-A: &lt;--this label is to manage the deployment itself. this may be used to filter the deployment based on this label. 
spec:              
  replicas: 2
  selector:
    matchLabels:
      app: my-nginx    #LABEL-B:  &lt;--  field defines how the Deployment finds which Pods to manage.
  template:
    metadata:
      labels:
        app: my-nginx   #LABEL-C: &lt;--this is the label of the pod, this must be same as LABEL-B
    spec:                
      containers:
      - name: my-nginx
        image: nginx:alpine
        ports:
        - containerPort: 80
        resources:
          limits:
            memory: &quot;128Mi&quot; #128 MB
            cpu: &quot;200m&quot; #200 millicpu (.2 cpu or 20% of the cpu)
</code></pre>
<p><strong>LABEL-A:</strong> &lt;--this label is to manage the deployment itself. this may be used to filter the deployment based on this label. Example usage of <code>LABEL-A</code> is for deployment management, such as filtering.</p>
<pre><code>k get deployments.apps -L app=my-nginx
</code></pre>
<p><strong>LABEL-B</strong>:  &lt;--  There must be some place where we tell replication controller to manage the pods. This field defines how the Deployment finds which Pods to manage. Based on these labels of the pod, replication controller ensure they are ready.</p>
<p><strong>LABEL-C:</strong> &lt;--this is the label of the pod, which LABEL-B use to monitor. this must be same as LABEL-B</p>
