A simple demonstration of how a django-based website can be set up for local development with microk8s

Overview

Django with MicroK8s

Start Building Your Project

This project provides a Django web app running as a single node Kubernetes cluster in microk8s. It is meant to make it as easy as possible to begin building a project with a minimum of initial configuration.

This is both a reference and an actual github template that can be used to quickly start development. After installing prerequisites and downloading this repository, only one command is needed to have a working, browsable application.

Provided Tools

Prerequisites

You'll need to install the following software:

Note that the system has only been tested on Ubuntu 18+, but should theoretically work for any host OS on which Microk8s and docker can be installed.

Initial Operation

  1. Install pre-requisites.

  2. Download the repository:

    git clone [email protected]:bluemoo/django-with-microk8s.git
    
  3. Install pre-commit hooks:

    pre-commit install
    
  4. Start Microk8s and enable required add-ons:

    microk8s.start
    microk8s enable helm3 dns storage registry host-access ingress
    
  5. Start the system:

    ./dev.sh start
    
  6. Check for successful startup with:

    ./dev.sh status
    
  7. Navigate to http://localhost:8000 and you should see Django tell you it was installed successfully!

Example System Start

$ microk8s.start
Started.

$ microk8s enable helm3 dns storage registry host-access ingress
Enabling Helm 3
Fetching helm version v3.5.0.
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 11.7M  100 11.7M    0     0  8430k      0  0:00:01  0:00:01 --:--:-- 8430k
Helm 3 is enabled
Addon dns is already enabled.
Addon storage is already enabled.
Addon registry is already enabled.
Addon host-access is already enabled.
Addon ingress is already enabled.


$ ./dev.sh start
Checking microk8s configuration
Build image
Sending build context to Docker daemon  7.146MB
<-- Snipped Steps -->
Successfully built aeb2100b9b07
Successfully tagged server:development-server
Pushing image to registry
The push refers to repository [localhost:32000/server]
<-- Snip Pushing Image -->
Setting up Secrets
<-- Snip -->
Installing database
<-- Snip -->
Wait for database to be ready
pod/postgres-statefulset-0 condition met
Installing server
<-- Snip -->
Configuring Ingress
configmap/nginx-ingress-tcp-microk8s-conf unchanged
daemonset.apps/nginx-ingress-microk8s-controller unchanged

$ ./dev.sh status
NAME                                  READY   STATUS      RESTARTS   AGE
development-server-7f5c56ff54-wmgf5   1/1     Running     0          9s
migration-job-gqd65                   0/1     Completed   0          13s
postgres-statefulset-0                1/1     Running     0          18s
redis-statefulset-0                   1/1     Running     0          9s

$ wget http://localhost:8000/
2021-05-11 20:07:00 (84,3 MB/s) - ‘index.html’ saved [10697/10697]

$ grep "The install worked successfully! Congratulations!" index.html 
        <title>The install worked successfully! Congratulations!</title>
        <h1>The install worked successfully! Congratulations!</h1>

$ ./dev.sh test
.System check identified no issues (0 silenced).
..
----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK

Development Workflows

dev.sh

The project comes with a shell script dev.sh that contains helpful commands to interact with the running system. In particular, it has options to install/uninstall the system, run django management commands, and run django tests. For a full set of options, simply type: ./dev.sh help

Hot code reloading

The development server mounts files directly from your file system, so any changes to python files will be automatically noticed and result in the server automatically reloading the code:

$ ./dev.sh server-logs
10.1.97.1 - - [11/May/2021 18:33:12] "GET / HTTP/1.1" 200 -
10.1.97.1 - - [11/May/2021 18:33:12] "GET / HTTP/1.1" 200 -
 * Detected change in '/opt/project/server/appsecrets.py', reloading
 * Restarting with stat
Performing system checks...

System check identified no issues (0 silenced).

Django version 3.2.2, using settings 'settings'
Development server is running at http://0.0.0.0:8000/
Using the Werkzeug debugger (http://werkzeug.pocoo.org/)
Quit the server with CONTROL-C.

Thus, you can edit, save, and see your changes in your app very quickly.

Database migrations

As this is a development environment, migrations are automatically applied on system start up, so you'll have an empty django database schema from the beginning. You can perform all other database interactions as you would normally via the Django manage command provided by dev.sh:

$ ./dev.sh manage makemigrations
No changes detected

$ ./dev.sh manage migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  No migrations to apply.

Testing

While django tests can be run via the manage command, the dev.sh file provides a small shortcut, allowing you to simply use ./dev.sh test. You can also pass one or more test labels:

$ ./dev.sh test
...
----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
System check identified no issues (0 silenced).
$ ./dev.sh test tests.test_appsecrets.AppSecretsIntegrationTest
.
----------------------------------------------------------------------
System check identified no issues (0 silenced).
Ran 1 test in 0.000s

OK

Next steps

This development environment should server you well until you're ready to actually deploy your app to production. However, if you're new to Django, you can easily pick up the tutorial from Creating the Polls app. Just remember that instead of typing python manage.py startapp polls you should use ./dev.sh manage startapp polls instead. Good luck!

Notes

If you want to run docker as non-root user then you need to add it to the docker group. https://stackoverflow.com/questions/48957195/how-to-fix-docker-got-permission-denied-issue

Owner
Noah Jacobson
Noah Jacobson
🏭 An easy-to-use implementation of Creation Methods for Django, backed by Faker.

Django-fakery An easy-to-use implementation of Creation Methods (aka Object Factory) for Django, backed by Faker. django_fakery will try to guess the

Flavio Curella 93 Oct 12, 2022
Integarting Celery with Django to asynchronous tasks 📃

Integrating 🔗 Celery with Django via Redis server ,To-Do asynchronously 👀task without stopping the main-flow 📃 of Django-project . It increase your speed 🚀 and user experience 🤵 of website

Rushi Patel 4 Jul 15, 2022
Django Persistent Filters is a Python package which provide a django middleware that take care to persist the querystring in the browser cookies.

Django Persistent Filters Django Persistent Filters is a Python package which provide a django middleware that take care to persist the querystring in

Lorenzo Prodon 2 Aug 05, 2022
A fresh approach to autocomplete implementations, specially for Django.

A fresh approach to autocomplete implementations, specially for Django. Status: v3 stable, 2.x.x stable, 1.x.x deprecated. Please DO regularely ping us with your link at #yourlabs IRC channel

YourLabs 1.6k Dec 22, 2022
Probably the best abstract model / admin for your tree based stuff.

django-treenode Probably the best abstract model / admin for your tree based stuff. Features Fast - get ancestors, children, descendants, parent, root

Fabio Caccamo 360 Jan 05, 2023
A Django app for managing robots.txt files following the robots exclusion protocol

Django Robots This is a basic Django application to manage robots.txt files following the robots exclusion protocol, complementing the Django Sitemap

Jazzband 406 Dec 26, 2022
Super simple bar charts for django admin list views visualizing the number of objects based on date_hierarchy using Chart.js.

Super simple bar charts for django admin list views visualizing the number of objects based on date_hierarchy using Chart.js.

foorilla LLC 4 May 18, 2022
:couple: Multi-user accounts for Django projects

django-organizations Summary Groups and multi-user account management Author Ben Lopatin (http://benlopatin.com / https://wellfire.co) Status Separate

Ben Lopatin 1.1k Jan 01, 2023
A small Django app to easily broadcast an announcement across a website.

django-site-broadcasts The site broadcast application allows users to define short messages and announcements that should be displayed across a site.

Ben Lopatin 12 Jan 21, 2020
Django React Flight Rezervation

Django Intro & Installation python -m venv venv source ./venv/Scripts/activate pip install Django pip install djangorestframework pip install python-d

HILMI SARIOGLU 2 May 26, 2022
The magical reactive component framework for Django ✨

Unicorn The magical full-stack framework for Django ✨ Unicorn is a reactive component framework that progressively enhances a normal Django view, make

Adam Hill 1.4k Jan 05, 2023
Basic Form Web Development using Python, Django and CSS

thebookrain Basic Form Web Development using Python, Django and CSS This is a basic project that contains two forms - borrow and donate. The form data

Ananya Dhulipala 1 Nov 27, 2021
A simple demonstration of integrating a sentiment analysis tool in a django project

sentiment-analysis A simple demonstration of integrating a sentiment analysis tool in a django project (watch the video .mp4) To run this project : pi

2 Oct 16, 2021
DCM is a set of tools that helps you to keep your data in your Django Models consistent.

Django Consistency Model DCM is a set of tools that helps you to keep your data in your Django Models consistent. Motivation You have a lot of legacy

Occipital 59 Dec 21, 2022
Serve files with Django.

django-downloadview django-downloadview makes it easy to serve files with Django: you manage files with Django (permissions, filters, generation, ...)

Jazzband 328 Dec 07, 2022
A starter template for building a backend with Django and django-rest-framework using docker with PostgreSQL as the primary DB.

Django-Rest-Template! This is a basic starter template for a backend project with Django as the server and PostgreSQL as the database. About the templ

Akshat Sharma 11 Dec 06, 2022
A Minimalistic Modern Django Boilerplate

A Minimalistic Modern Django Boilerplate This boilerplate is mainly for educational purposes. It is meant to be cloned as a starter code for future tu

Jonathan Adly 21 Nov 02, 2022
Compresses linked and inline javascript or CSS into a single cached file.

Django Compressor Django Compressor processes, combines and minifies linked and inline Javascript or CSS in a Django template into cacheable static fi

2.6k Jan 03, 2023
Boilerplate Django Blog for production deployments!

CFE Django Blog THIS IS COMING SOON This is boilerplate code that you can use to learn how to bring Django into production. TLDR; This is definitely c

Coding For Entrepreneurs 26 Dec 09, 2022
A simple plugin to attach a debugger in Django on runserver command.

django-debugger A simple plugin to attach a debugger in Django during runserver Installation pip install django-debugger Usage Prepend django_debugger

Sajal Shrestha 11 Nov 15, 2021