<p>Today I confronted with a misunderstanding about <code>servicePort</code>. </p>

<p>I expected that service can be linked with ingress specifying only <code>servicePort: 80</code>, but both <code>servicePort: 80</code> and <code>servicePort: 8080</code> works.</p>

<p>Can someone help me to understand why both ports <code>port</code> and <code>targetPort</code> are exposed by Service, not only <code>port</code>?</p>

<p><em>Service (app)</em></p>

<pre><code>spec:
  type: ClusterIP
  ports:
    - port: 80
      targetPort: 8080
</code></pre>

<p><em>Ingress (ingress-nginx)</em></p>

<pre><code>spec:
  rules:
  - host: example.com
    http:
      paths:
      - path: /
        backend:
          serviceName: app
          servicePort: 8080
</code></pre>

<p>As I understand,</p>
<p>Each Pod in the cluster has an endpoint which is the <strong>IP</strong> and <strong>targetPort</strong> of Pod.</p>
<p>You can list the endpoints with the following commands.</p>
<pre><code>kubectl get endpoints
</code></pre>
<p>Now If you use service to expose your pod then they have <strong>cluster IP</strong> and <strong>Service Port</strong>.</p>
<p><code>kubectl get services</code></p>
<p>Now you can write the ingress rules to expose your pod via <strong>endpoints</strong> or <strong>cluster ip</strong>. However, There are only a few Ingress-Controller which can do this task. for example <strong>nginx-ingress-controller</strong>.</p>
<p>Why do you want to use <strong>endpoints</strong> instead of <strong>cluster-ip or Service</strong> ?</p>
<blockquote>
<p>The NGINX ingress controller does not use Services to route traffic to the pods. Instead it uses the Endpoints API in order to bypass kube-proxy to allow NGINX features like session affinity and custom load balancing algorithms. It also removes some overhead, such as conntrack entries for iptables DNAT.</p>
</blockquote>
<p>Here is the link for further research <a href="https://kubernetes.github.io/ingress-nginx/user-guide/miscellaneous/#why-endpoints-and-not-services" rel="noreferrer">Why endpoints and not services</a></p>
