Ayashmantha Perera

Deploying Django-Rest application in kubernetes with production grade server apache

In a previous tech guide relating to serving django-rest framework in apache and mod_wsgi, I configured a newly created Django app and walked through all of the configurations needed to serve it on apache2 with mod_wsgi.

logo

In a previous tech guide relating to serving django-rest framework in apache and mod_wsgi, I configured a newly created Django app and walked through all of the configurations needed to serve it on apache2 with mod_wsgi.

In this blog post I will be focusing on deploying this application upon a cloud container engine.

Container engines can be specific to a cloud provider and some have their own container engines where docker images can be deployed on. Since docker started prioritising kubernetes as their native clustering framework, leaving behind docker swarm,  nearly all cloud platforms have shifted focus to kubernetes.

Amazon Web Services (AWS) also released a kubernetes cluster support service late last year. So as it seems to be going the kubernetes way I thought it appropriate to use kubernetes for the container deployment. From our previous tech guide we already have an apache2 and mod_wsgi supported serving django rest api project.

Here, we will focus on deploying in Google kubernetes cluster. I used google kubernetes cluster since it was the first and also because google owns one of the most mature kubernetes clusters in comparison to other cloud providers. Google Kubernetes cluster also provides enormous support along with in-detail product documentation.

Let’s get started with the deployment.

First, we need to have a docker image that includes our project in order to achieve the deployment in kubernetes. For that, we will need a docker file that can do that for us.

Here’s the docker file  I wrote that will do the trick for us.

 

FROM ubuntu

RUN apt-get update

RUN apt-get install -y apt-utils vim curl apache2 apache2-utils

RUN apt-get -y install python-pip apache2 libapache2-mod-wsgi

RUN pip install — upgrade pip

RUN pip install djangorestframework

ADD ./requirements.txt ./requirements.txt

RUN pip install -r requirements.txt

ADD ./demo_site.conf /etc/apache2/sites-available/000-default.conf

ADD ./www/ /var/www/html

RUN chmod 664 /var/www/html/tutorial/tutorial/db.sqlite3

RUN chmod 775 /var/www/html/tutorial/tutorial

RUN chown :www-data /var/www/html/tutorial/tutorial/db.sqlite3

RUN chown :www-data /var/www/html/tutorial/tutorial

EXPOSE 80 3500

CMD [“apache2ctl”, “-D”, “FOREGROUND”]

(Code snippet 1 : Docker file to achieve deployment in kubernetes)

 

This will be the source for us to build the docker image for the deployment of the django rest project.

 

sudo docker build -t tutorial/tutorial:1.0.0 .

 

And also, before going in to Google Kubernetes Engine (GKE) you can run it locally from docker for testing purposes.

 

docker run -d -p 8005:82 -p 3500:3500 -p 8006:81 -t tutorial/tutorial:1.0.0

 

Okays. So now we have our docker image with us. Now let’s move on to the deployment in kubernetes.

First, you have to login to the cloud console of google cloud and then create a kubernetes cluster there.

(Figure 1: creating a Kubernetes cluster)

 

Next, we tag our docker image and push it to the google cloud container registry before deployment. Google container registry is a service that works as a container bucket which will hold all the docker images that we are using to deploy in the kubernetes cluster.

As for the deployment, we are using a docker compose file. From this docker compose file we can deploy our docker image to docker swarm or kubernetes.

Right now, you may be wondering as to why I’m using a docker compose file for the deployment of kubernetes. Actually it’s possible to do so thanks to a great tool named ‘kompose’.

I found this tool from kubernetes official site.

First, we must have a reverse proxy associated with the kubernetes cluster that we spin up for this deployment. Kompose also provides a great user interface for the kubernetes-dashboard where we see the deployments.

Go to the folder which contains your docker compose file and enter below command as follows:

 

kompose up


This will do the deployment for you and we can see it from the dashboard provided by kubernetes.

(Figure 2 : ‘Kompose up’ executes deployment)

 

We will be using Node port to expose our deployment to the outside world. We can see the main port that exposes our deployment is as 31904. Since it is a Node port deployment, we can access it through the cluster ip and the main port 31904 which exposes the core of the django-rest application.

 

(Figure 3: Node Port 31904)

 

That ends  the story of deploying django-rest in kubernetes.

Thank you for reading this Tech Guide. We hope you will also read our next tutorial so that we can help you solve some more interesting problems.

Ayeshmantha-Perera

Ayeshmantha Perera

Software Engineer | Mitra Innovation