Deploy Django App and Postgresql on Kubernetes With Google Kubernetes Engine (GKE)

regiapriandi | Aug. 20, 2023, 2:55 p.m.

Welcome to the tutorial on deploying python Django applications with PostgreSQL database on the google cloud platform (GCP) using google Kubernetes engine technology. This article has several problems and solutions that hopefully can help to learn how to deploy a Python web application on Kubernetes.

Problems

  • Why deploy a Django app on Kubernetes?

  • Why deploy Kubernetes on the google cloud platform?

  • Why deploy a Django app using the Postgres database?

Table Of Contents

  1. Create a Docker file for building an image app
  2. Push the image app to Docker registry
  3. Create a Kubernetes cluster on a google cloud platform
  4. Create a deployment for PostgreSQL with YAML config file
  5. Create a service for PostgreSQL with YAML config file
  6. Create a deployment for Django web app with the YAML config file
  7. Create a service for Django web app with the Yaml config file
  8. Create a Job for database migration in Django
  9. Create an Ingress for service that can access from outside

Preliminary

Kubernetes is a widely used container orchestration, this really needs to be used at a time when the increasing use of applications continues to increase which makes the running source of an application require very large specifications. This trend is an opportunity for students in their field to develop skills by learning Kubernetes.

Python Django is a web application framework that has a very high value because this framework runs on a very extraordinary programming language, namely python. Python has many libraries that really help developers in creating very extraordinary applications. Django runs fast and has great scalability for developing sustainable applications.

I developed my web portfolio and my simple web blog with Django, where from this development I have the goal to share my learning experiences and provide a tutorial that I hope can be useful. The advantage of developing my blog with the Django framework is that I no longer need to manually create pages to create a blog, but this problem has been solved by the python library django-summernote which is integrated with the Django admin page.

I’m trying to deploy my Django application using Kubernetes because right now I’m learning about Kubernetes and I want to implement it in deploying my Django application on Kubernetes. Besides that, deploying applications on Kubernetes also make an application run smoothly and quickly when there are a lot of requests on the server.

Google cloud platform is easy to use for ordinary people in solving problems related to deploying an application. Google Kubernetes engine service is one of the best Kubernetes services in the cloud. Besides that, Google Cloud Platform provides a trial service with a free credit of $300 for a 3-month deadline via the following link https://cloud.google.com/free.

PostgreSQL database is widely used in the development of Django web applications which is a relational database type, PostgreSQL is good for large-scale databases which are suitable for use with the Django framework which is the best framework for large application development. besides that Django framework support to use of PostgreSQL with an amazing library that is psycopg2.

1. Create a Docker file for building an image app

There are several stages in deploying Django applications in Kubernetes, in Kubernetes itself, docker is used as a container runtime interface to run applications on the pods. Therefore, before using the Django application on the Kubernetes pod, build the Django application using docker and then push the built image to the docker registry docker.io. First, create a file with the name Dockerfile on the root project directory.

FROM python:3.8.2
RUN mkdir /code
WORKDIR /code
COPY . /code/
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
CMD python manage.py runserver 0.0.0.0:8000

Before building the application image using docker, make change the database configuration on Django in the settings.py file in the project then change the previous database configuration with the following PostgreSQL database configuration.

DATABASES={
'default':{
'ENGINE':'django.db.backends.postgresql',
'NAME':os.getenv('DATABASE_NAME'),
'USER':os.getenv('DATABASE_USER'),
'PASSWORD':os.getenv('DATABASE_PASSWORD'),
'HOST':os.getenv('DATABASE_HOST'),
'PORT':'5432',
}
}

2. Push the image app to Docker registry

Build an image on the root of the project and push it to the docker hub. Hereby we have created a Django application that has been uploaded to https://hub.docker.com/ and can be used by Kubernetes.

$ docker build -t <image_name> .
$ docker tag <image_name> <dockerhub_username>/<image_name>:tag
$ docker <dockerhub_username>/<image_name>:tag

3. Create a Kubernetes cluster on a google cloud platform

Create a google Kubernetes engine service on the google cloud platform page https://console.cloud.google.com/ by being registered as a free cloud service from the google cloud platform for 3 months on the following page https://cloud.google.com/free.


Follow several input forms such as cluster naming, source location, and version of the Kubernetes control plane, fill in accordingly and then click create.


Wait a few minutes for the google cloud platform to build the Kubernetes cluster, then after the cluster is completed, open the activated cloud shell as shown in the following image, to be able to access and run Kubernetes, we can do this here. check the nodes on the terminal to make sure the nodes are running properly.



4. Create a deployment for PostgreSQL with the YAML config file

Now Kubernetes cluster is ready to use, then proceed with creating a Kubernetes config for the deployment of Django applications using a PostgreSQL database. First, create a deployment config for a PostgreSQL database with a PostgreSQL docker image configuration.

apiVersion: apps/v1
kind: Deployment
metadata:
name: postgres-deploy
labels:
name: postgres-deploy
app: webregi-v2-app
spec:
replicas: 1
selector:
matchLabels:
name: postgres-pod
app: webregi-v2-app
template:
metadata:
name: postgres-pod
labels:
name: postgres-pod
app: webregi-v2-app
spec:
containers:
- name: postgres
image: postgres
ports:
- containerPort: 5432
env:
- name: POSTGRES_USER
value: "regiapriandi"
- name: POSTGRES_PASSWORD
value: "postgres"
- name: POSTGRES_HOST_AUTH_METHOD
value: trust

Create a configuration on Kubernetes to apply the configuration file that has been created, done on the google cloud console.

$ kubectl create -f postgres-deployment.yaml
$ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
postgres-deploy 1/1 1 1 4d1h

5. Create a service for PostgreSQL with the YAML config file

Create a service configuration for PostgreSQL deployment so that the database can have a cluster IP which will later be connected to the Django application deployment, besides that it is configured with the LoadBalancer service type to be accessed from outside and can be configured easily on pgAdmin.

apiVersion: v1
kind: Service
metadata:
name: db
labels:
name: postgres-service
app: webregi-v2-app
spec:
type: LoadBalancer
ports:
- port: 5432
targetPort: 5432
selector:
name: postgres-pod
app: webregi-v2-app

By configuring the service using a YAML file, we can implement the service for PostgreSQL on Kubernetes using the following command.

$ kubectl create -f postgresql-service.yaml
$ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
db LoadBalancer <ip> <ip> 5432:30000/TCP 4d2h

6. Create a deployment for Django web app with the YAML config file

The following configuration is the deployment configuration for deploying the Django web app on Kubernetes that was previously pushed in the docker registry. For the configuration, it uses an environment that is directly connected to the deployment of PostgreSQL itself.

apiVersion: apps/v1
kind: Deployment
metadata:
name: webregi-v2-app
spec:
replicas: 1
selector:
matchLabels:
app: webregi-v2-app
template:
metadata:
labels:
app: webregi-v2-app
spec:
containers:
- name: webregi-v2-app
image: regiapriandi012/bismillah-final-deploykube:latest
ports:
- containerPort: 8000
env:
- name: DATABASE_HOST
value: "db"
- name: DATABASE_USER
value: "postgres"
- name: DATABASE_PASSWORD
value: "postgres"
- name: DATABASE_NAME
value: "postgres"

Use the Kubernetes create command to create and deploy a Django web app deployment configuration on Kubernetes.

$ kubectl create -f webregi-deployment.yaml
$ kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
postgres-deploy 1/1 1 1 4d3h
webregi-v2-app 1/1 1 1 2d11

7. Create a service for Django web app with the Yaml config file

In deploying Django applications, we need a Kubernetes service with the service type NodePort which will be accessed via ingress that will create in the next stage, it takes port 8000 and targetPort 8000 for Django to run properly on the service.

apiVersion: v1
kind: Service
metadata:
name: webregi-v2-service
spec:
type: NodePort
ports:
- protocol: TCP
port: 8000
targetPort: 8000
selector:
app: webregi-v2-app

So far, the configuration implementation for the deployment and service of the Django app and PostgreSQL database has been completed. Previously, the Kubernetes create command was executed to apply the Django app service to Kubernetes.

$ kubectl create -f webregi-service.yaml
$ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
db LoadBalancer <ip> <ip> 5432:<port>/TCP 4d5h
webregi-v2-service NodePort <ip> <none> 8000:<port>/TCP 2d16h

8. Create a Job for database migration in Django

Before Django can be used, the Django model is first migrated to the database via the makemigrations command, which is used to create configurations for python, which will then be followed by the migrate command to apply the database configuration in python to the PostgreSQL database.

Therefore, a Kubernetes job is configured to apply the Django database migration to running pods without having manually access the pod terminal to perform database migrations. The following is the config file for the job database migration for the Django app.

apiVersion: batch/v1
kind: Job
metadata:
name: webregi-migrate
spec:
template:
spec:
containers:
- name: webregi-v2-app
image: regiapriandi012/bismillah-final-deploykube:latest
command: ["/bin/sh","-c"]
args: ["python manage.py makemigrations webregi && python manage.py migrate"]
env:
- name: DATABASE_USER
value: "regiapriandi"
- name: DATABASE_PASSWORD
value: "postgres"
- name: DATABASE_HOST
value: "db"
- name: DATABASE_NAME
value: "postgres"
restartPolicy: Never
backoffLimit: 5

To be able to run database migration on Django via a Kubernetes job, use the Kubernetes create command for the previously created YAML configuration file. After the Kubernetes job is applied, the Django app pod deployment will automatically migrate the database.

$ kubectl create -f webregi-job.yaml
$ kubectl logs <pod_job>
Migrations for 'webregi':
webregi/migrations/0001_initial.py
- Create model Award
- Create model Certification
- Create model Programing
- Create model Project
- Create model Publication
- Create model Post
- Create model Photography
- Create model Comment
System check identified some issues:
Operations to perform:
Apply all migrations: admin, auth, contenttypes, django_summernote, sessions, webregi
Running migrations:
No migrations to apply.

9. Create an Ingress for service that can access from outside

With ingress on Kubernetes, we can get an external IP to be able to access our application services on Kubernetes, besides that the service with LoadBalancer type also has a purpose to get access to our application services using an external IP, but in this tutorial, I use ingress to deploy my application.

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: webregi-ingress
spec:
defaultBackend:
service:
name: webregi-v2-service
port:
number: 8000

By applying the ingress configuration file on Kubernetes, we have obtained an external IP from the ingress that is connected to the NodePort service of the previously implemented Django application. Therefore with the following command apply it to Kubernetes.

$ kubectl create -f webregi-ingress.yaml
$ kubectl get ingress
NAME CLASS HOSTS ADDRESS PORTS AGE
webregi-ingress <none> * <ip> 80 2d21h

After the configuration is applied to Kubernetes, we can see the application service on the Service & Ingress menu on the Google Cloud Platform page.


Conclusion

From the development of deploying Django applications and PostgreSQL databases on Kubernetes using the google cloud platform, obtained Django applications that have been deployed on the Google Kubernetes Engine (GKE) using Kubernetes deployment. Can be seen on the google cloud platform page to see information about the Kubernetes cluster and the results of the configurations that have previously been made on the console.

Thank you for reading my simple tutorial, hopefully, it can help in your learning. Thanks very much.

About Me

I am Regi Apriandi, Welcome to the blog page, I hope this page can be useful for readers who have visited, Thank you.

RSS

0 comments

Leave a comment