We now have Starter Projects for Django, Flask, Nest, and Nuxt! What should we build next?

All Posts

Deploy a Django app with Kubernetes in 20 minutes

Mandy Hubbard - 2022-08-04

Web applications are a crucial part of the global digital landscape. Source code hosting and version control, container and cloud technology, and more have pushed application development to great heights. Web applications are now faster, lighter, more stable, and easier to build and manage.

Using source code hosting and version control offered by platforms like GitHub and BitBucket allows developers to collaborate easily anywhere in the world. These solutions provide a seamless opportunity to work on the same project or code simultaneously.

Kubernetes

Container technology such as Docker enables developers to build applications, package the applications into containers, and run the containers on different platforms and environments. A container is a “build once, run anywhere” concept. When a team or organization has many containers to manage in a system, they use container orchestrators like Kubernetes to deploy, run, and manage them with automation. Kubernetes is an open-source project that started in-house at Google and is the most popular container orchestration platform today. With Kubernetes, you can set up, run, manage, maintain, and scale containerized applications with ease.

Django

Django is an open-source web development framework prevalent in the Python ecosystem. It favors the rapid building of scalable and secure web applications. It powers some popular platforms like Instagram, DropBox, and Spotify. Django’s modular format enables the speedy development of reusable components. It works with the model-view-controller (MVC) architecture to help separate data and business logic.

Architect

Architect is a cloud-native platform that enables you to rapidly deploy your projects with Kubernetes while providing continuous delivery and security. CI/CD provided by Architect, a DevOps-as-a-service platform, and other CI/CD tools help automate the processes involved in deploying and releasing software. Automation helps to ship out applications to users faster and more frequently. This article shows you how to set up Architect to deploy a Django application with Kubernetes.

Deploy Django on Kubernetes

This section provides a hands-on, step-by-step guide accompanied by code to deploy your Django web application to a Kubernetes cluster using Architect.

Prerequisites

To follow this tutorial, you need:

Components, Platforms, and Environments

An Architect component defines the services of your application stack that you want to be deployed together, for example, an API and a database. Using the Architect CLI, you can register your component locally or in Architect Cloud.

A platform represents a Kubernetes cluster, whether it’s your own from a public cloud provider or the one provided by Architect Cloud, and environments are distinct entities where you deploy a component on a platform. A free preview environment on the Architect platform is included with your account.

The architect.yml file

This tutorial uses the Django Starter Project built by Architect which is hosted on GitHub and available in the Architect UI. There are also Starter Projects for Flask, Nuxt, React, and other frameworks. Each project contains an architect.yml file in the root-level directory. This file is a mandatory file containing configuration specifications necessary to use Architect to deploy your application. The file for the Django Starter Projecte includes the Django project secrets, database configuration settings, Kubernetes ingress settings, and Django-specific settings.

The configuration is defined in a declarative manner, utilizing service discovery, so your component can be built once and promoted all the way to production with no code or configuration changes.

name: django
secrets:
 django_secret_key:
    default: warning-override-for-production
 postgres_password:
   default: warning-override-for-production
 django_admin_email:
   default: noreply@architect.io
 django_admin_username:
   default: admin
 django_admin_password:
   default: password

services:
  db:
    image: postgres
    interfaces:
      main: 5432
    environment:
      POSTGRES_DB: postgres
      POSTGRES_USER: postgres
     POSTGRES_PASSWORD: ${{ secrets.postgres_password }}
  web:
    build:
      context: .
    command: |
      sh -c '
        python manage.py makemigrations
        python manage.py collectstatic --noinput
        python manage.py migrate --noinput
        python manage.py createsuperuser --noinput
        uwsgi --http "0.0.0.0:8000" --module architectexample.wsgi:application --master --processes 4 --threads 2 --static-map /static=/src/main/app/static
      '
    interfaces:
      main: 8000
    environment:
      ALLOWED_HOST: .${{ ingresses.web.host }}
      SECRET_KEY: ${{ secrets.django_secret_key }}
      POSTGRES_DB: ${{ services.db.environment.POSTGRES_DB }}
      POSTGRES_USER: ${{ services.db.environment.POSTGRES_USER }}
      POSTGRES_PASSWORD: ${{ services.db.environment.POSTGRES_PASSWORD }}
      POSTGRES_HOST: ${{ services.db.interfaces.main.host }}
      POSTGRES_PORT: ${{ services.db.interfaces.main.port }}
      DJANGO_SUPERUSER_USERNAME: ${{ secrets.django_admin_username }}
      DJANGO_SUPERUSER_EMAIL: ${{ secrets.django_admin_email }}
      DJANGO_SUPERUSER_PASSWORD: ${{ secrets.django_admin_password }}
    ${{ if architect.environment == 'local' }}:
      command: |
        sh -c '
          python manage.py makemigrations
          python manage.py collectstatic --noinput
          python manage.py migrate --noinput
          python manage.py createsuperuser --noinput
          python manage.py runserver 0.0.0.0:${{ services.web.interfaces.main.port }}
        '
      environment:
        ALLOWED_HOST: '*'
        DEBUG: 'True'
      volumes:
        web:
          mount_path: /usr/src/app
          host_path: .

interfaces:
  web:
    url: ${{ services.web.interfaces.main.url }}
    ingress:
      subdomain: app

Create a Django component

Follow these steps to create a new component using the Architect UI.

1. Navigate to the Component menu on your dashboard as shown below. Click Try It Out.

2. Click Use Template on the Django Starter Project.

3. Select Github as the Git provider for your project.

4. Architect requests access to your GitHub account so that it can create a new repository to store a clone of the Starter Project you selected. Click the Authorize architect-team button to grant access. You may be asked to confirm your password during this process.

5. Architect then asks for the GitHub organization where you want the project to reside. The drop-down box contains your personal account and any GitHub organizations to which you belong. Select the GitHub account or organization you wish to use.

Architect also asks you to name your new repository. Name the repository django-start, and select the Private checkbox to keep the repository private on GitHub. Then, click Next.

6. You can leave the default settings on the GitOps Configuration page. Scroll down, then click Clone Repo.

7. At this point, Architect clones the Django Starter Project in the GitHub account you selected. It might take a couple of minutes to complete.

8. Upon successfully creating the project, your dashboard should look like the following screenshot:

9. You should now have a new repository in your selected GitHub account that you can register as an Architect component and deploy.

Register your Kubernetes cluster

Before you can deploy to your Kubernetes cluster, you’ll need to provide Architect with the configuration details using the following steps.

1. Navigate to the Platforms menu on your dashboard, then click Add a Platform+.

2. Enter a name and description (optional) for your Kubernetes cluster and click the Next button.

3. Architect displays the CLI commands needed to finish connecting your Kubernetes cluster, including the command to install the CLI if you haven’t done so already.

Copy and paste these commands directly into your terminal. The CLI will look for your Kubernetes config file in ~/.kube/config, or you can use the --kubeconfig flag and pass the full path to your file if it resides elsewhere.

Create an environment

Create an environment, which is an isolated entity on a platform for deploying code, by following these steps.

1. Navigate to the Environments menu on your Architect dashboard, then click Create.

2. When you click Create, a list of the configured Kubernetes platforms appears. You should see the default architect platform as well as the one you just created. Select the platform you created in the previous steps.

3. In the Environments section, enter test for the name of your environment, then click Create Environment.

Register the component

Now that you have connected your Kubernetes cluster and created an environment, you are ready to register the component.

1. From your terminal, clone the GitHub repo of the newly created component using the command below. Don’t forget to replace github_username with the GitHub account you selected in the previous step. Enter your GitHub password when requested.

git clone https://github.com/github_username/django-start.git

2. Next, register the component with Architect using the following CLI command:

architect register django-start

Architect uses the above command to register the component with the configuration values declared in the architect.yml file in the Architect Cloud registry.

You should get a response similar to the following once the component is registered.

[Using locally linked django-start found at

/Users/jheikhei/Documents/prototypes/airplane-demo/django-start
[+] Building 37.9s (11/11) FINISHED                                             
...

Use docker scan to run Snyk tests on images to find vulnerabilities and learn how to fix them.

Uploading images test-again:latest with Architect Cloud…
...
End component config diff
Registering component test-again:latest with Architect Cloud...... done
Successfully registered component
Time: 79551

Deploy the application

Now that you have the application (component) and the Kubernetes cluster (platform) set up, you can deploy the application to the environment you created on the Kubernetes cluster.

1. Go to the Components menu on your dashboard and click the newly created component, django-start.

2. This page displays the dependencies, services, interfaces, and secrets this component uses. Click Deploy in the upper-right corner.

3. Select the test environment you created earlier for the deployment.

4. Skip the Expose Interfaces section unless you have preconfigured DNS records. Leave the Secrets area empty to maintain the default values specified in your Django project’s architect.yml file. Click Deploy, which takes you to the Pipeline approval page.

5. The Pipeline approval page looks like the screenshot below. It shows the gateway Architect creates to serve your application using custom URLs. It also shows the services to deploy, including the Django application and its Postgres database. Click Approve on the top-right corner to proceed.

7. Wait a few minutes for the deployment to complete. Click the Live link in the left-hand nav for the test environment. You should see the completed pipeline, as shown in the screenshot below

You can click the web or database component and get the following three options.

Clicking django-start shows the three options listed above as well as the URL of the deployed web application. You can find this URL by clicking gateway as well. Click the URL to open up the application on another browser tab. You should see the following:

Congratulations! You have successfully deployed your Django application to a Kubernetes cluster with Architect.

Conclusion

This article shows how fast and painless it is to deploy your Django application with Kubernetes. Connect Architect to your code hosted on GitHub and your Kubernetes cluster from your cloud provider. Register the component, and the application is ready to be deployed.

Architect deploys your applications with Kubernetes with ease. It helps create a seamless and secure pipeline for integration while offering you logs for visibility and customer support if you encounter challenges. With features like intelligent pipelines, built-in dependency management, and push-button rollbacks, you’ll have a pipeline with all good practices embedded. 

Want to learn more development best practices? Check out these other blog posts from Architect: