<p>I am learning Kubernetes at the moment. I have built a simple python application that uses Flask to expose rest APIs. Flask by default uses port 5000 to run the server. My API look like -</p>
<pre><code>http://0.0.0.0:5000/api
</code></pre>
<p>Application is built into a docker image</p>
<pre><code>FROM python:3.8.6-alpine

COPY . /app
WORKDIR /app
RUN \
    apk add --no-cache python3 postgresql-libs &amp;&amp; \
    apk add --no-cache --virtual .build-deps gcc python3-dev musl-dev postgresql-dev &amp;&amp; \
    python3 -m pip install -r requirements.txt --no-cache-dir &amp;&amp; \
    apk --purge del .build-deps

ENTRYPOINT [&quot;python3&quot;]
CMD [&quot;app.py&quot;]
</code></pre>
<p>I deploy this in a Kubernetes pod with pod definition</p>
<pre><code>apiVersion: v1
kind:  Pod
metadata:
  name: python-webapp
  labels:
    type: web
    use: internal
spec:
  containers:
    - name: python-webapp
      image: repo/python-webapp:latest
</code></pre>
<p>Everything works fine and I am able to access the api on the pod directly and through Kubernetes service. I am boggled how does the POD know that the application in the container is running on port 5000? Where is the mapping for a port on the container to port on the pod?</p>

Jaspreet
<blockquote>
<p>I am boggled how does the POD know that the application in the container is running on port 5000?</p>
</blockquote>
<p>The pod does not know that. The app in the container, in the pod can respond to any request on any port.</p>
<p>But to expose this to outside the cluster, you likely will forward traffic from a specific port to a specific port on your app, via a <a href="https://kubernetes.io/docs/concepts/services-networking/service/" rel="nofollow noreferrer">service</a> (that can map to different ports) and a <a href="https://kubernetes.io/docs/concepts/services-networking/" rel="nofollow noreferrer">load balancer</a>.</p>
<p>You can use <a href="https://kubernetes.io/docs/concepts/services-networking/network-policies/" rel="nofollow noreferrer">Network Policies</a> to restrict traffic in your cluster, e.g. to specific ports or services.</p>
