<p>I have a simple python flask app named hwrequest. I can run it from PyCharm IDE and it works fine.
I've dockerized this app using following Dockerfile</p>
<pre><code>FROM python:3.8-alpine
LABEL maintainer=&quot;John Smith, john.smith@mycompany.com&quot;
RUN apk update &amp;&amp; apk add bash curl
COPY . /hwrequest
WORKDIR /hwrequest
RUN pip install -r app/requirements.txt
EXPOSE 5000
ENTRYPOINT [&quot;python&quot;]
CMD [&quot;/hwrequest/app/app.py&quot;]
</code></pre>
<p>I can exec -it into container and when I call successfully <code>curl 127.0.0.1:5000</code>
Now I'am trying to deploy this app to Kubernetes and expose it with Ingress.</p>
<p>This is my YAML files:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: &quot;hwrequest&quot;
  name: &quot;hwrequest&quot;
spec:
  replicas: 1
  selector:
    matchLabels:
      app: &quot;hwrequest&quot;
  template:
    metadata:
      labels:
        app: &quot;hwrequest&quot;
    spec:
      containers:
      - name: &quot;hwrequest&quot;
        image: &quot;myregistry.com/hwrequest:0.0.4&quot;
        imagePullPolicy: Always
        ports:
        - containerPort: 5000
</code></pre>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: &quot;hwrequest&quot;
  labels:
    app: &quot;hwrequest&quot;
spec:
  type: ClusterIP
  ports:
    - port: 5000
      targetPort: 5000
  selector:
    app: &quot;hwrequest&quot;
</code></pre>
<pre><code>apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: &quot;hwrequest&quot;
spec:
  rules:
  - http:
      paths:
      - backend:
          serviceName: &quot;hwrequest&quot;
          servicePort: 5000
    host: &quot;hwrequest.mycompany.com&quot;
</code></pre>
<p>When I call <code>curl hwrequest.mycompany.com</code> I get <code>502 Bad Gateway</code></p>
<p>What am I doing wrong?</p>

<p>Exposing any application on kubernetes using ingress is non trivial task and here are few things to look for</p>
<ol>
<li>Make sure that the application is listening on <code>0.0.0.0</code> instead of <code>127.0.0.1</code></li>
<li>The curl request should have <code>Host</code> header i.e <code>-H &quot;Host: hwrequest.mycompany.com&quot;</code> because this is how ingress controller will know which rule to apply as per the rules defined in ingress resource.</li>
<li>There need to a be an ingress controller such as nginx running in the cluster and the ingress controller pods need to be exposed via <code>NodePort</code> or <code>LoadBalancer</code> type service or can be deployed with <code>hostNetwork: true</code></li>
<li>You need to use <code>NODEIP</code>(kubernetes node) and <code>NODEPORT</code> in curl if <code>NodePort</code> service was used so it would look like <code>curl http://&lt;NODEIP&gt;:NODEPORT -H &quot;Host: hwrequest.mycompany.com&quot;</code>.</li>
</ol>
<p>If you used <code>hostNetwork</code> to expose nginx ingress controller pods then curl should be curl http:// -H &quot;Host: hwrequest.mycompany.com&quot;`</p>
<p>If you used <code>LoadBalancer</code> type service(works on cloud providers such as AWS,Azure,GCP) then curl should be curl http:// -H &quot;Host: hwrequest.mycompany.com&quot;`</p>
<p>If you are just starting I would suggest to look at this <a href="https://kubernetes.io/docs/tasks/access-application-cluster/ingress-minikube/" rel="nofollow noreferrer">guide</a> on using nginx ingress on Minikube</p>
