<pre><code>apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: my-app
  labels:
    run: my-app
spec:
  replicas: 3
  selector:
    matchLabels:
      run: my-app
  template:
    metadata:
      labels:
        run: my-app
    spec:
      containers:
      - image: gcr.io/google-samples/hello-app:1.0
        name: my-app
        ports:
        - containerPort: 8080
</code></pre>

<p>This is a sample yaml from kubenetes site, there are so many <code>my-app</code>, do they all have to be same? what are their purpose?</p>

gacopu
<blockquote>
  <p>This is a sample yaml from kubenetes site, there are so many my-app, do they all have to be same? what are their purpose?</p>
</blockquote>

<p>No they don't have to be the same as far as the <code>name</code> field goes, that can be different. The <code>my-app</code> references seen in the <code>metadata</code> and <code>selector</code> sections 
are <a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/" rel="nofollow noreferrer">labels</a> that can be used to glue the different Kubernetes objects together or simply select a subset of objects when querying Kubernetes. They will sometimes be the same.</p>

<p>Depending on how you've created the Deployment you may have <code>run: myapp</code> throughout the Deployment and in the objects derived from it. Using <code>kubectl run my-app --image=gcr.io/google-samples/hello-app:1.0 --replicas=3</code> would create a identical Deployment you're referring to.</p>

<p>Here's a picture showing how the different <code>run: my-app</code> labels are used, using the Deployment above as an inspiration:</p>

<p><a href="https://i.stack.imgur.com/HMMTa.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HMMTa.png" alt="enter image description here"></a></p>

<p>The picture above shows you the Deployment and how the <code>template</code> box (blue) are used to create the number of specified replicas (Pods). Each Pod will get a <code>run: my-app</code> label in it's <code>metadata</code> section, from the Deployment point of view this will be used as a way of selecting the Pods it's responsible for.</p>

<p>A similar selection of a subset of Pods using <code>kubectl</code> would be:</p>

<p><code>kubectl get pods -l run=my-app</code></p>

<p>This will give you all Pods labeled <code>run: my-app</code>.</p>

<p>To sum up a bit, labels can be used to select a subset of resources when querying using e.g. <code>kubectl</code> or by other Kubernetes resources to do selections. You can create your own labels and they don't necessarily have to be the same throughout your specific Deployment but if they are it would be pretty easy to query for any resource with a specific label.</p>
