How to Deploy a Django Application With Kubernetes | by Kanat Akylson | Apr, 2022

Deploy a scalable Django app right into a Kubernetes cluster

On this tutorial, we’ll deploy a containerized Django utility with Kubernetes (K8s).

Django is a Python-based free and open-source internet framework that follows the mannequin–template–views architectural sample.

Kubernetes, also called K8s, is an open-source system for automating the deployment, scaling, and administration of containerized purposes.

Let’s create a brand new Django utility:

$django-admin startproject djangokubernetesproject

Navigate to the djangokubernetesproject listing:

$cd djangokubernetesproject

After that, we have to create a brand new Dockerfile which Docker will use to construct our container picture:

$vim Dockerfile

Dockerfile:

FROM ubuntu:20.04RUN apt-get replace && apt-get set up -y tzdata && apt set up -y python3.8 python3-pipRUN apt set up python3-dev libpq-dev nginx -yRUN pip set up django gunicorn psycopg2ADD . /appWORKDIR /appEXPOSE 8000CMD ["gunicorn", "--bind", ":8000", "--workers", "3", "djangokubernetesproject.wsgi"]

This Dockerfile makes use of the official Ubuntu 20.04 docker picture as a base and installs Django, Gunicorn, Python3.8. Lastly, it exposes that port 8000 can be used to just accept incoming container connections, and runs gunicorn with 3 staff and listening on port 8000.

Now, let’s construct our picture utilizing docker construct:

$docker construct -t djangokubernetesproject.

We named the picture djangokubernetesproject utilizing the -t flag and go within the present listing as a construct context, the set of recordsdata to reference when containerize the picture.

After Docker builds and tags the picture, listing out there photographs utilizing docker photographs:

$docker photographs

It’s best to see the djangokubernetesproject picture listed:

REPOSITORY                                                                                                                           IMAGE ID       CREATED         SIZE
djangokubernetesproject newest

Within the subsequent step, we’ll run the configured container regionally.

With the container constructed and configured, use docker run to override the CMD set within the Dockerfile and create the database schema utilizing the handle.py makemigrations and handle.py migrate instructions

$docker run -i -t djangokubernetesproject sh

This may offer you a shell immediate inside the working container

#python3 handle.py makemigrations && python3 handle.py migrate

In case you’re working this it’s best to see:

OutputOperations to carry out:
Apply all migrations: admin, auth, contenttypes, classes
Operating migrations:
Making use of contenttypes.0001_initial... OK
Making use of auth.0001_initial... OK
Making use of admin.0001_initial... OK
Making use of admin.0002_logentry_remove_auto_add... OK
Making use of admin.0003_logentry_add_action_flag_choices... OK
Making use of contenttypes.0002_remove_content_type_name... OK
Making use of auth.0002_alter_permission_name_max_length... OK
Making use of auth.0003_alter_user_email_max_length... OK
Making use of auth.0004_alter_user_username_opts... OK
Making use of auth.0005_alter_user_last_login_null... OK
Making use of auth.0006_require_contenttypes_0002... OK
Making use of auth.0007_alter_validators_add_error_messages... OK
Making use of auth.0008_alter_user_username_max_length... OK
Making use of auth.0009_alter_user_last_name_max_length... OK
Making use of auth.0010_alter_group_name_max_length... OK
Making use of auth.0011_update_proxy_permissions... OK
Making use of auth.0012_alter_user_first_name_max_length... OK
Making use of classes.0001_initial... OK

This reveals that the database schema has efficiently been created.

#python3 handle.py createsuperuser

Enter a username, e-mail deal with, and password to your superuser, and after creating the superuser, hit CTRL+D to stop the container and kill it.

Now let’s run our docker container .

$docker run -p 80:8000 djangokubernetesproject

it’s best to see :

Output[2022-04-18 06:40:37 +0000] [1] [INFO] Beginning gunicorn 20.1.0
[2022-04-18 06:40:37 +0000] [1] [INFO] Listening at: http://0.0.0.0:8000 (1)
[2022-04-18 06:40:37 +0000] [1] [INFO] Utilizing employee: sync
[2022-04-18 06:40:37 +0000] [9] [INFO] Booting employee with pid: 9
[2022-04-18 06:40:37 +0000] [10] [INFO] Booting employee with pid: 10
[2022-04-18 06:40:37 +0000] [11] [INFO] Booting employee with pid: 11

Right here, we run the default command outlined within the Dockerfile, gunicorn --bind :8000 --workers 3 djangokubernetesproject.wsgi:utility, and expose container port 8000 in order that port 80 in your native machine will get mapped to port 8000 of the djangokubernetesproject container.

It’s best to now be capable to navigate to the djangokubernetesproject app utilizing your internet browser by typing http://localhost within the URL bar.

Navigate to http://localhost to see the djangoapp:

If you end up completed exploring, hit CTRL+C within the terminal window working the Docker container to kill the container.

To deploy your utility to Kubernetes, your app picture should be uploaded to a docker hub registry. Kubernetes will retrieve the applying picture from its repository after which deploy it to your cluster.

You should utilize a publicly out there Docker registry, corresponding to Docker Hub. Docker Hub additionally means that you can create non-public Docker repositories. A public repository permits anybody to view and retrieve container photographs, and a personal repository means that you can limit entry to you and your staff members.

On this tutorial, we’ll push a Django picture to the general public Docker Hub repository.

Start by logging in to Docker Hub in your native machine:

$docker login

Enter your Docker Hub username and password to login. After you efficiently logged in it’s best to see:

OutputLogin Succeeded

The Django picture has the djangokubernetesproject:newest tag. To push it to your Docker Hub repository, re-tag the picture along with your Docker Hub username and repo identify:

$docker tag djangokubernetesproject:newest your_dockerhub_username/your_dockerhub_repo_name:newest

Push the picture to the repo:

$docker push your_dockerhub_username/your_dockerhub_repo_name:newest

You’ll see some output that updates as picture layers are pushed to Docker Hub.

Now that your picture is accessible to Kubernetes on Docker Hub, you possibly can start deploy it in your cluster.

On this step you’ll create a Deployment to your Django app. A Kubernetes Deployment is a controller that can be utilized to handle stateless purposes in your cluster. A controller is a management loop that regulates workloads by scaling them up or down. Controllers additionally restart and filter failed containers.

Deployments management a number of Pods, the smallest deployable unit in a Kubernetes cluster. Pods enclose a number of containers. To study extra in regards to the several types of workloads you possibly can launch, please evaluate An Introduction to Kubernetes.

Start by opening a file known as django-deployment.yaml in your favourite editor:

$vim django-deployment.yaml

Paste within the following Deployment manifest:

apiVersion: apps/v1
sort: Deployment
metadata:
identify: django-app
labels:
app: django
spec:
replicas: 3
selector:
matchLabels:
app: django
template:
metadata:
labels:
app: django
spec:
containers:
- picture: your_dockerhub_username/app_repo_name:newest
identify: django
ports:
- containerPort: 8000
identify: gunicorn

Fill within the applicable container picture identify, referencing the Django mission picture you pushed to Docker Hub in Step 2.

Right here we outline a Kubernetes Deployment known as django-app and label it with the key-value pair app: django. We specify that we’d prefer to run three replicas of the Pod outlined beneath the template area.

Lastly, we expose containerPort 8000 and identify it gunicorn.

To study extra about configuring Kubernetes Deployments, please seek the advice of Deployments from the Kubernetes documentation.

While you’re executed enhancing the file, save and shut it.

Create the Deployment in your cluster utilizing kubectl apply -f:

$kubectl apply -f django-deployment.yaml

it’s best to see:

deployment.apps/django-app created

Verify that the Deployment rolled out accurately utilizing kubectl get:

$kubectl get deploy django-app

Output:

NAME        READY   UP-TO-DATE   AVAILABLE   AGE
django-app 3/3 3 3 3m21s

In case you encounter an error or one thing isn’t fairly working, you should use kubectl describe to examine the failed Deployment:

$kubectl describe deplo

You possibly can examine the 2 Pods utilizing kubectl get pod:

$kubectl get pod

Output:

NAME                         READY   STATUS    RESTARTS   AGE
django-app-7c55868755-4wglz 1/1 Operating 0 2m5s
django-app-7c55868755-7tpjd 1/1 Operating 0 2m5s
django-app-7c55868755-9s4s8 1/1 Operating 0 2m5s

Three replicas of your Django app at the moment are up and working within the cluster. To entry the app, you could create a Kubernetes Service, which we’ll do subsequent.

On this step, you’ll create a Service to your Django app. A Kubernetes Service is an abstraction that means that you can expose a set of working Pods as a community service. Utilizing a Service you possibly can create a secure endpoint to your app that doesn’t change as Pods die and are recreated.

There are a number of Service sorts, together with ClusterIP Companies, which expose the Service on a cluster-internal IP, NodePort Companies, which expose the Service on every Node at a static port known as the NodePort, and LoadBalancer Companies, which provision a cloud load balancer to direct exterior visitors to the Pods in your cluster (by way of NodePorts, which it creates routinely). To study extra about these, please see Service from the Kubernetes docs.

Start by making a file known as django-svc.yaml utilizing your favourite editor:

$vim django-svc.yaml

Paste within the following Service manifest:

apiVersion: v1
sort: Service
metadata:
identify: django
labels:
app: django
spec:
sort: NodePort
selector:
app: django
ports:
- port: 8000
targetPort: 8000

Right here we create a NodePort Service known as django and provides it the app: django label. We then choose backend Pods with the app: django label and goal their 8000 ports.

While you’re executed enhancing the file, save and shut it.

Roll out the Service utilizing kubectl apply:

$kubectl apply -f django-svc.yaml

Output:

service/django created

Affirm that your Service was created utilizing kubectl get svc:

$kubectl get svc django

Output:

NAME    TYPE       CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
django NodePort 10.107.211.249 <none> 8000:30306/TCP 15s

This output reveals the Service’s cluster-internal IP and NodePort (30306). To hook up with the service, we’d like the exterior IP addresses for our cluster nodes:

In your internet browser, go to your Django app utilizing http://localhost:30306

It’s best to see the identical Django app interface that you just accessed regionally in Step 1.

At this stage, you’ve rolled out three replicas of the Django app container utilizing a Deployment. You’ve additionally created a secure community endpoint for these three replicas, and made it externally accessible utilizing a NodePort Service.

On this tutorial, you deployed a scalable Django app right into a Kubernetes cluster. Operating Pods will be rapidly scaled up or down utilizing the replicas area within the django-app Deployment manifest.

Subsequent time I’ll present you the way to deploy your individual mission from the GitHub repository.

More Posts