<p>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.</p>

<p>The logs is my nginx deployment say:</p>

<p>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"</p>

<p>However, it appears my flask service is running correctly.</p>

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

<p>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.</p>

<p>Here is my code:
docker-compose.yaml</p>

<pre><code>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"
</code></pre>

<p>The Dockerfile for Flask</p>

<pre><code># 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 &amp;&amp; apt-get install -y \
wget \
build-essential \
python3 \
python3-pip \
python3.6-dev \
--no-install-recommends \
&amp;&amp; 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 \
&amp;&amp; chmod -R u+rwx $HOME \
&amp;&amp; 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"]
</code></pre>

<p>My nginx Dockerfile</p>

<pre><code># 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/
</code></pre>

<p>Now my Kubernetes configuration files:</p>

<p>flask-deployment.yaml</p>

<pre><code>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: {}
</code></pre>

<p>flask-service.yaml</p>

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

<p>nginx-deployment.yaml</p>

<pre><code>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: {}
</code></pre>

<p>nginx-service.yaml</p>

<pre><code>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: {}
</code></pre>

<p>nginx.conf</p>

<pre><code>server {

    listen 80;

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

}
</code></pre>

<p>uswgi.ini</p>

<pre><code>[uwsgi]
wsgi-file = run.py
callable = app
socket = :8080
processes = 4
threads = 2
master = true
chmod-socket = 660
vacuum = true
die-on-term = true
</code></pre>

lammy
<p>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 <a href="https://kubernetes.io/docs/concepts/services-networking/service/#nodeport" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/services-networking/service/#nodeport</a>):</p>

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

<p>Also your UWSGI config should specify 0.0.0.0:8080</p>

<pre><code>[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
</code></pre>
