Django nginx gunicorn in kubernetes


#1

I am trying to run my django app in kubernetes.I use docker-compose to build application and nginx.I am able to run the application using proxy server nginx in docker compose.

docker-compose build - successful

docker-compose up - successful

http://aggre.mabh.io - successful(able to load the application)

when i try to deploy the image of application and nginx, I am getting error in kubernetes dashboard saying

Error in kubernetes

pod has unbound PersistentVolumeClaims (repeated 2 times) (for both nginx and application)

I am using kompose up to build and deploy the application.

How to deploy the the application in kubernetes and access the application through nginx kubernetes external end-point?

docker-compose.yml

version: '3'
services:
  djangoapp:
    build: .
    image: sigmaaggregator/djangoapp
    labels:
      - kompose.service.type=NodePort
    volumes:
      - .:/djangoapp
      - static_volume:/djangoapp/static
    ports:
      - 4000:4000
    networks:
      - nginx_network

  nginx:
    image: nginx:1.13
    ports:
      - 80:80
    volumes:
      - ./config/nginx/conf.d:/etc/nginx/conf.d
      - static_volume:/djangoapp/static
    depends_on:
      - djangoapp
    networks:
      - nginx_network

networks:
  nginx_network:
    driver: bridge

volumes:
  static_volume:

Dockerfile

    FROM python:3.6
    
    RUN mkdir -p /djangoapp
    WORKDIR /djangoapp
    COPY . /djangoapp
    
    RUN pip install -r requirements.txt
    
    # copy our project code
    RUN python manage.py collectstatic --no-input
    
    CMD exec /bin/bash -c "trap : TERM INT; sleep infinity & wait"
   
    # expose the port 4000
    EXPOSE 4000
    
   # define the default command to run when starting the container
   CMD ["gunicorn", "--bind", ":4000", "aggre.wsgi:application"]

config/nginx/conf.d/local.conf

upstream hello_server {
   server djangoapp:4000;
}
server {
   listen 80;
   server_name aggre.mabh.io;

   location /static/ {
     alias /djangoapp/static/;
   }

   location / {
    proxy_pass http://hello_server;
    proxy_set_header Host $host;
    #proxy_ssl_server_name on;
    #proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    #proxy_set_header X-Forwarded-Proto $scheme;
    proxy_redirect off;
  }
}

#2

Sadly I am not familar with how Kompose translates compose files to K8s deployments. I have a feeling something might have gotten lost in translation, possibly with the static_volume.

That said it sounds like k8s is trying to provision a PersistentDrive and failing. If you run kubectl get pvc -n <namespace> you will get a list of the claims, you can then inspect them to find out what is going on kubectl describe pvc <pvcname> -n <namespace> . If you can get the output of that it would make troubleshooting a little easier.