Django, Nginx, uwsgi, Kubernetes: unable to connect nginx to uwsgi stream

9/27/2018

I have a django container and an Ngix container. they work fine with docker-compose, and now im trying to use the images with kubernetes. Everything works fine, except the fact that the nginx container cannot connect to the uwsgi upstream. No response is being returned.

Here are my configuration:

# Nginx congifuration
upstream django {
    server admin-api-app:8001 max_fails=20 fail_timeout=10s;  # for a web port socket (we'll use this first),
}

server {
    # the port your site will be served on
    listen  80;
    server_name server localhost my-website-domain.de;
    charset     utf-8;


    location / {
        uwsgi_pass django;
        include     /etc/nginx/uwsgi_params;
    }
}

# Uwsgi file
module          = site_module.wsgi

master          = true
processes       = 5
socket          = :8001
enable-threads  = true
vacuum=True

# Kubernetes

# Backend Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: container-backend
  labels:
    app: backend
spec:
  replicas: 1
  selector:
    matchLabels:
      app: backend
  template:
    metadata:
      labels:
        app: backend
    spec:
      containers:
      - name: container-backend
        image: my-djangoimage:latest
        command: ["./docker/entrypoint.sh"]
        ports:        
        - containerPort: 8001        
          name: uwsgi        

      - name: nginx
        image: my-nginx-image:latest
        imagePullPolicy: Always
        ports:
        - containerPort: 80
          name: http        

---

# Backend Service
kind: Service
apiVersion: v1
metadata:
  name: admin-api-app
spec:
  selector:
    app: backend
  ports:
  - port: 80
    targetPort: 80
  type: LoadBalancer
-- anyavacy
django
docker
kubernetes
uwsgi

1 Answer

9/27/2018

You probably need to change host in your django upstream because, as far as I understand, you want to connect to your django app located in the same pod where is nginx so try to change:

server admin-api-app:8001 max_fails=20 fail_timeout=10s;

to

server localhost:8001 max_fails=20 fail_timeout=10s;

Edit: To make it work you need to change socket to http-socket but it can be painful/pointless as described here: Should I have separate containers for Flask, uWSGI, and nginx?

-- Jakub Bujny
Source: StackOverflow