Eric
Eric

Reputation: 46

502 (Bad Gateway) when trying to access to my Flask api with kubernetes

I try to deploy my application on GCP. I have a frontend in Vuejs and an api with Flask and a Postgre database

Everything is deployed in a Kubernetes cluster on GCP. I can accès to my frontend without any problems, but I cannot access to the api, I have a 502 bad gatteway. I think a made a mistake in my configuration. Here is my configuration :

**flask-service.yml**

apiVersion: v1
kind: Service
metadata:
  name: flask
  labels:
    service: flask
spec:
  type: NodePort
  selector:
    app: flask
  ports:
  - protocol: TCP
    port: 5000
    targetPort: 5000

vue-service.yml file

**vue-service.yml**

apiVersion: v1
kind: Service
metadata:
  name: vue
  labels:
    service: vue
spec:
  type: NodePort
  selector:
    app: vue
  ports:
  - protocol: TCP
    port: 8080
    targetPort: 8080

ingress.yml file

ingress.yml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress
spec:
  rules:
  - http:
      paths:
      - path: /*
        backend:
          serviceName: vue
          servicePort: 8080
      - path: /api/*
        backend:
          serviceName: flask
          servicePort: 5000

My flask app is deployed with gunicord

gunicorn -b 0.0.0.0:5000 manage:app

screeshot of my GCP cluster services

Do you know where I've made a mistake ? I'm a beginner in kubernetes

Here is my Dockerfile

FROM python:3.8.1-slim

# install netcat
RUN apt-get update && \
    apt-get -y install netcat && \
    apt-get clean

# set working directory
WORKDIR /usr/src/app

# add and install requirements
RUN  pip install --upgrade pip
RUN pip install -U gunicorn
COPY ./requirements.txt /usr/src/app/requirements.txt
RUN pip install -r requirements.txt

# add entrypoint.sh
COPY ./entrypoint.sh /usr/src/app/entrypoint.sh
RUN chmod +x /usr/src/app/entrypoint.sh

# add app
COPY . /usr/src/app

# run server
CMD ["/usr/src/app/entrypoint.sh"]

And my entrypoint.sh

echo "Waiting for postgres..."

while ! nc -z postgres 5432; do
  sleep 0.1
done

echo "PostgreSQL started"

gunicorn -b 0.0.0.0:5000 manage:app

One more edit. In GCP, when I check backend services, there are 3 backends, and one of them doesn't work But why do I have 3 backend ? I should have juste two no (flask and vue)?

When I check, I have 2 backend services with Flask, and on of them doesn't work

The backend services (flask) with problems

the other backend services (flask)

My Flask Image logs in GCP show an error. Do you know why ?

GCP log of my flask image

Upvotes: 0

Views: 1193

Answers (1)

Eric
Eric

Reputation: 46

I founded the solution. It was a problem with my ingress, I forgot to add an Ingress Controler. (I didn't know anything about Ingress Controler...)

Now I've added an Nginx Ingress Controler and everything works fine !

Upvotes: 1

Related Questions