<p>I tried the Service definition example from <a href="https://kubernetes.io/docs/concepts/services-networking/service/#defining-a-service" rel="nofollow noreferrer">here</a>.</p>
<p>So, I created below Service:</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: service-simple-service
spec:
  selector:
    app: MyApp
  ports:
    - protocol: TCP
      port: 80
      targetPort: 9376
</code></pre>
<p>And then to test the concept, I created below Pod:</p>
<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: service-simple-service-pod
  labels:
    app: MyApp
spec:
  containers:
  - name: service-simple-service-pod-container-1
    image: nginx:alpine
    ports:
      - containerPort: 9376
</code></pre>
<p>And I can see that a new Endpoint for this Pod is created, so all good till now, below is the output:</p>
<pre><code>C:\Users&gt;kubectl describe service/service-simple-service
Name:              service-simple-service
Namespace:         default
Labels:            &lt;none&gt;
Annotations:       &lt;none&gt;
Selector:          app=MyApp
Type:              ClusterIP
IP:                10.98.246.70
Port:              &lt;unset&gt;  80/TCP
TargetPort:        9376/TCP
Endpoints:         10.244.0.8:9376
Session Affinity:  None
Events:            &lt;none&gt;
</code></pre>
<p>Then to test negative concept, I created below Pod.</p>
<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: service-simple-service-pod-nouse
  labels:
    app: MyApp
spec:
  containers:
  - name: service-simple-service-pod-nouse-container-1
    image: nginx:alpine
    ports:
      - containerPort: 9378
</code></pre>
<p>But to my surprise this Pod was also picked:</p>
<pre><code>C:\Users&gt;kubectl describe service/service-simple-service
Name:              service-simple-service
Namespace:         default
Labels:            &lt;none&gt;
Annotations:       &lt;none&gt;
Selector:          app=MyApp
Type:              ClusterIP
IP:                10.98.246.70
Port:              &lt;unset&gt;  80/TCP
TargetPort:        9376/TCP
Endpoints:         10.244.0.10:9376,10.244.0.8:9376
Session Affinity:  None
Events:            &lt;none&gt;
</code></pre>
<p>My understanding of Service I created above was that Scheduler will look for any Pod having label as <code>app: MyApp</code> and running on port <code>9376</code>, so my expectation was that since this Pod is running on port <code>9378</code> so it will not be picked up. So, my question is that why this &quot;service-simple-service-pod-nouse&quot; was picked up?</p>
<p>If someone says that my understanding was incorrect and Service only selects Pod based on Label, then my question is that since &quot;service-simple-service-pod-nouse&quot; Pod is listening on port <code>9378</code> then how &quot;service-simple-service&quot; Service can send traffic to this Pod?</p>

pjj
<p>Sevice will picked all the pods that are labeled as the label selector of that service. <code>service-simple-service</code> service will select all the pods that are labeled as <code>MyApp</code> because you tell in the service selector (<code>app: MyApp</code>). This is the common and expected behavior of label-selector, you can see the k8s official <a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/" rel="nofollow noreferrer">doc</a></p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: service-simple-service
spec:
  selector:
    app: MyApp
  ports:
    - protocol: TCP
      port: 80
      targetPort: 9376
</code></pre>
<h1>Update</h1>
<p>Basically, a service get the requests and then it serves the traffic to the pods (those are labeled as the service selector), when a service take a pod then it opens a endpoint for that pod, when traffic comes to the service it sends those traffics in one of it endpoints(which is basically going to a pod). And the container port is basically the port inside the pod where the container is running.</p>
