<p>I have built a flask app that I would like to add to a Kubernetes ingress. Currently, I have 2 questions I cannot seem to get my head around: </p>

<ol>
<li>In order for the flask app to be able to handle several requests, I figured I would add gunicorn. Do I need this, or can I mitigate this by using some kind of automatic horizontal scaling and the ingress routing layer handle it?  I am new to Kubernetes, and perhaps the solution is simpler than what I am trying below.</li>
<li><p>With the presumption that I do need gunicorn, I have proceeded and added it to the flask docker. The problem I have with this is that I now get a 502 Bad Gateway Error nginx and the log of the pod have not printed any error. If I create a load balancer service instead of the clusterIP I use with the ingress, the flask app with unicorn works fine, just as the flask app does on the ingress <strong>without</strong> adding gunicorn. I have no idea why hence writing this question. The dockerfile installs all dependencies to run flask and finishes with:         </p>

<pre><code>EXPOSE 8080

CMD ["gunicorn", "--config", "/flaskapp/gunicorn_config.py", "run:app"]
</code></pre>

<p>I have configured my ingress like this:</p>

<pre><code>apiVersion: v1
items:
- apiVersion: extensions/v1beta1
  kind: Ingress
 metadata:
   annotations:
     ingress.bluemix.net/client-max-body-size: 128m
     ingress.bluemix.net/rewrite-path: serviceName=flask-service rewrite=/; 
spec:
  rules:
  - host: &lt;my-domain&gt;
    http:
      paths:
      - backend:
        serviceName: flask-service
        servicePort: 8080
      path: /flask/
  tls:
  - hosts:
    - &lt;my-domain&gt;
    secretName: &lt;my-secret&gt;
status:
  loadBalancer:
    ingress:
    - ip: &lt;ip&gt;
</code></pre>

<p>The service looks like this:</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: flask-service
  labels:
    app: flask-service
spec:
  type: ClusterIP
  ports:
  - port: 8080
    protocol: TCP
  selector:
    app: flask
</code></pre>

<p>The deployment is also very simple specifying the correct image and port.</p></li>
</ol>

<p>Given that I need gunicorn(or similar), how can I solve the 502 Bad Gateway Error I get?</p>

<ol>
<li><p>IMO, you don't need gunicorn scaling (it's an overkill) since an HPA will do the scaling if your single application instances already. This depending on CPUs, memory or <a href="https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/#support-for-custom-metrics" rel="nofollow noreferrer">custom metrics</a>.</p></li>
<li><p>The 502 errors seem to me it's more of how gunicorn is configured issue (is there a limit on the workers? can you see the workers to just 1 to test? how is it scaling inside the container? What are the resource limits on the container?). Hard to tell without looking at logs or the environment, but it could be that you gunicorn workers are thrashing in the container thus returning an invalid response. You might want to try <a href="http://docs.gunicorn.org/en/stable/settings.html#loglevel" rel="nofollow noreferrer">--log-level debug</a> on the gunicorn command line.</p></li>
</ol>

<p>Hope it helps.</p>
