Deploy Django App and Postgresql on Kubernetes With GKE

Deploying a Python Django web app and PostgresSQL database on Kubernetes deployment with Google Kubernetes Engine (GKE)

Deploy Django App and Postgresql on Kubernetes With GKE
Photo by Ian Taylor / Unsplash

Deploy Django in Kubernetes

Deploy Django App and Postgresql on Kubernetes With GKE

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.


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


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

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 First, create a file with the name Dockerfile on the root project directory.

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

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 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 by being registered as a free cloud service from the google cloud platform for 3 months on the following page

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.

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
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.

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
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.

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
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.

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
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.

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':
- 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.

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
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.


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.

GitHub Repository

To see all of the file configurations and about my Django project, visit below GitHub repository.

GitHub - regiapriandi012/web-regiapriandi-2.0 at deploykube
Halaman website Regi Apriandi termasuk halaman blog dan resume yang dibagun menggunakan framework Django. - GitHub …


Set up HTTP(S) Load Balancing with Ingress | Kubernetes Engine | Google Cloud
Whether your business is early in its journey or well on its way to digital transformation, Google Cloud can help solve…
Tutorial: Deploy Django on AKS cluster with PostgreSQL Flexible Server by using Azure CLI
APPLIES TO: Azure Database for PostgreSQL - Flexible Server In this quickstart, you deploy a Django application on…
How To Deploy a Scalable and Secure Django Application with Kubernetes | DigitalOcean
In this tutorial you'll deploy a containerized Django polls application into a Kubernetes cluster. Django is a powerful…

Read more on my Blog

Regi Apriandi Blog
regiapriandi | June 19, 2022, 7:14 p.m. Python is a programming language that is suitable for beginners to learn…

More content at Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord. Interested in Growth Hacking? Check out Circuit.