Kubernetes deployment.yaml for django+gunicorn+nginx

3/20/2019

I have created docker images using docker-compose.yml as below

version: '2'

services:

  djangoapp:
    build: .
    volumes:
      - .:/sig_app
      - static_volume:/sig_app
    networks:
      - nginx_network

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

networks:
  nginx_network:
    driver: bridge

volumes:
  static_volume:

I have used docker-compose build and docker-compose up. The three images are created as below

  1. kubernetes_djangoapp
  2. docker.io/python
  3. docker.io/nginx

I want to deploy the application into kubernetes using YAML file. I am new to kubernetes. Django application is running with port 8000 and Nginx with port 80

-- rakeshh92
docker
kubernetes

2 Answers

3/20/2019

Take a look at Kompose. It will allow you to simply run the command

kompose up

to immediately deploy your docker-compose configuration to your cluster.

If you want to create a .yaml from your docker-compose file first to inspect and edit, you can run

kompose convert
-- Lars S
Source: StackOverflow

3/20/2019

This should work:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: my-deploy
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: my-app
    spec:
      volumes:
      - name: django-nginx
        emptyDir: {}
      - name: nginx-host
        hostPath:
          path: /config/nginx/conf.d 
      containers:
      - name: djangoapp
        image: kubernetes_djangoapp
        volumeMounts:
        - name: django-nginx
          mountPath: /sig_app
      - name: nginx
        image: nginx:1.13
        ports:
        - containerPort: 80
        volumeMounts:
        - name: django-nginx
          mountPath: /sig_app
        - name: nginx-host
          mountPath: /etc/nginx/conf.d

Note that you will have to modify some things to make it custom yours. I am missing where the image is. You should upload it to docker hub, or any registry of your choice.

About the volumes, here both containers share a non-persistent volume (django-nginx), which maps /sig_app directory in each container with each other. And another one that shares the container nginx (etc/nginx/conf.d) with your host (/config/nginx/conf.d), to pass the config file. A better way would be to use a ConfigMap. Check on that.

So, yeah, set the image for django and let me know if it doesn't work, and we will see whats failing.

Cheers

-- suren
Source: StackOverflow