Ingress Controller on Minikube not routing correctly flask POST request

9/29/2019

I have a simple Flask app. It worked fine when I connected to it via port-forwarding to send the HTTP Post request directly to the Service.

from flask import Flask, request
import redis
from rq import Queue
from worker import job_worker

UPLOAD_FOLDER = './uploads/'

app = Flask(__name__)

r = redis.Redis()
q = Queue(connection = r)

@app.route('/', methods=['POST'])
def upload():
    scale = int(request.form['scale'])
    q.enqueue(job_worker, scale)
    return ""

if __name__ == "__main__":
    app.run()

I also have a simple index.html file in an nginx container which is served at port 80. It does an ajax POST request to "/upload". Which if you look at the ingress controller, should convert that to a port 5000 request and strip away the "upload" The flask app gets served at port 5000

Here is the ingress controller:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: emoji-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - http:
      paths:
      - path: /upload
        backend:
          serviceName: emoji-backend
          servicePort: 5000
      - path: /
        backend:
          serviceName: emoji-frontend
          servicePort: 80

And for completeness, the emoji-backend service:

apiVersion: v1
kind: Service
metadata:
  name: emoji-backend
  labels:
    app: emoji-backend
    tier: backend
spec:
  type: LoadBalancer
  ports:
  - port: 5000
  selector:
    app: emoji-backend
    tier: backend

I get a 502 bad gateway without really any indication except the ingress log does say this:

2019/09/29 21:41:04 [error] 2021#2021: *78651 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.64.1, server: _, 
request: "POST /upload HTTP/2.0", upstream: "http://172.17.0.4:5000/", host: "192.168.64.5", referrer: "https://192.168.64.5/"

"http://172.17.0.4:5000/" is the correct endpoint and port for the emoji-backend service.

-- JonathanC
flask
kubernetes
kubernetes-ingress
minikube

1 Answer

9/30/2019

Adding the following line fixed it:

app.run(debug=True,host='0.0.0.0',port=5000)

However, it took me a while to figure that out because at first when I tried it my docker image was not updating when I re-deployed.

-- JonathanC
Source: StackOverflow