Kubernetes NGINX can't find uWSGI application server but working with docker-compose

9/13/2019

I have an application with two images, one running NGINX and the other one running Flask/uWSGI. It works as expected using docker-compose. Now I try to deploy my application on a Kubernetes cluster, but I am unable to make a connection between NGINX and my uWSGI application server.

The logs is my nginx deployment say:

2019/09/13 11:29:53 [error] 6#6: *21 upstream timed out (110: Connection timed out) while connecting to upstream, client: 10.244.0.1, server: , request: "GET / HTTP/1.1", upstream: "uwsgi://10.0.232.218:8080", host: "52.166.xxxxxx"

However, it appears my flask service is running correctly.

NAMESPACE              NAME                        TYPE           CLUSTER-IP     EXTERNAL-IP     PORT(S)          AGE
default                flask                       NodePort       10.0.232.218   <none>          8080:32043/TCP   23m   
default                kubernetes                  ClusterIP      10.0.0.1       <none>          443/TCP          19h
default                nginx                       LoadBalancer   10.0.162.165   52.166.xxxxx   80:31669/TCP     23m  

I have tried changing the settings in the services, but without success. I used kompose to convert my docker-compose.yaml to *-deployment, *-service.yaml files. Notably during the process kompose did not provide a flask-service.yaml. I had to create it manually.

Here is my code: docker-compose.yaml

version: "3"

services:

flask:
  image: registry.azurecr.io/model_flask
  build: ./flask
  container_name: flask
  restart: always
  environment:
    - APP_NAME=fundamentalmodel
  expose:
    - 8080

nginx:
  image: registry.azurecr.io/model_nginx
  build: ./nginx
  container_name: nginx
  restart: always
  ports:
    - "80:80"

The Dockerfile for Flask

# Dockerfile to build glpk container images
# Based on Ubuntu

# Set the base image to Ubuntu
FROM ubuntu:latest

# Switch to root for install
USER root

# Install wget
RUN apt-get update -y && apt-get install -y \
wget \
build-essential \
python3 \
python3-pip \
python3.6-dev \
--no-install-recommends \
&& rm -rf /var/lib/apt/lists/*

RUN pip3 install setuptools Cython numpy wheel 
RUN pip3 install uwsgi


# Create a user
ENV HOME /home/user
RUN useradd --create-home --home-dir $HOME user \
&& chmod -R u+rwx $HOME \
&& chown -R user:user $HOME


# switch back to user
WORKDIR $HOME
USER user

COPY . /usr/src/app
WORKDIR /usr/src/app
RUN pip3 install -r requirements.txt
RUN pip3 install .

RUN export LC_ALL=C.UTF-8
RUN export LANG=C.UTF-8
RUN export FLASK_APP=server
CMD ["uwsgi", "uwsgi.ini"]

My nginx Dockerfile

# Use the Nginx image
FROM nginx

# Remove the default nginx.conf
RUN rm /etc/nginx/conf.d/default.conf

# Replace with our own nginx.conf
COPY nginx.conf /etc/nginx/conf.d/

Now my Kubernetes configuration files:

flask-deployment.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: kompose convert
    kompose.version: 1.17.0 (a74acad)
  creationTimestamp: null
  labels:
    io.kompose.service: flask
  name: flask
spec:
  replicas: 1
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        io.kompose.service: flask
    spec:
      containers:
      - env:
        - name: APP_NAME
          value: fundamentalmodel
        image: registry.azurecr.io/fundamentalmodel_flask
        name: flask
        resources: {}
        ports:
          - containerPort: 8080
      restartPolicy: Always
status: {}

flask-service.yaml

apiVersion: v1
kind: Service
metadata:
  labels:
    name: flask
  name: flask
spec:
type: NodePort
  selector:
    app: flask
  ports:
  - name: http
    port: 8080
    targetPort: 8080

nginx-deployment.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: kompose.exe convert
    kompose.version: 1.17.0 (a74acad)
  creationTimestamp: null
  labels:
    io.kompose.service: nginx
  name: nginx
spec:
  replicas: 1
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        io.kompose.service: nginx
    spec:
      containers:
      - image: registry.azurecr.io/fundamentalmodel_nginx
        name: nginx
        ports:
        - containerPort: 80
        resources: {}
      restartPolicy: Always
status: {}

nginx-service.yaml

apiVersion: v1
kind: Service
metadata:
  annotations:
    kompose.cmd: kompose.exe convert
    kompose.version: 1.17.0 (a74acad)
  creationTimestamp: null
  labels:
    io.kompose.service: nginx
  name: nginx
spec:
  type: LoadBalancer
  ports:
  - name: "80"
    port: 80
    targetPort: 80
  selector:
    io.kompose.service: nginx
status:
  loadBalancer: {}

nginx.conf

server {

    listen 80;

    location / {
        include uwsgi_params;
        uwsgi_pass flask:8080;
    }

}

uswgi.ini

[uwsgi]
wsgi-file = run.py
callable = app
socket = :8080
processes = 4
threads = 2
master = true
chmod-socket = 660
vacuum = true
die-on-term = true
-- lammy
docker-compose
flask
kubernetes
uwsgi

1 Answer

9/17/2019

I think the solution here is that you change a couple of settings, namely your service should use a ClusterIP as NodePort serves a different purpose (read more here https://kubernetes.io/docs/concepts/services-networking/service/#nodeport):

apiVersion: v1
kind: Service
metadata:
  labels:
    name: flask
  name: flask
spec:
type: ClusterIP
  selector:
    app: flask
  ports:
  - name: http
    port: 8080
    targetPort: 8080

Also your UWSGI config should specify 0.0.0.0:8080

[uwsgi]
wsgi-file = run.py
callable = app
socket = 0.0.0.0:8080
processes = 4
threads = 2
master = true
chmod-socket = 660
vacuum = true
die-on-term = true
-- Spazzy757
Source: StackOverflow