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
💨 Fast, Async-ready, Openapi, type hints based framework for building APIs

Fast to learn, fast to code, fast to run Django Ninja - Fast Django REST Framework Django Ninja is a web framework for building APIs with Django and P

Vitaliy Kucheryaviy 3.8k Jan 01, 2023
Django models and endpoints for working with large images -- tile serving

Django Large Image Models and endpoints for working with large images in Django -- specifically geared towards geospatial tile serving. DISCLAIMER: th

Resonant GeoData 42 Dec 17, 2022
A Django application that provides country choices for use with forms, flag icons static files, and a country field for models.

Django Countries A Django application that provides country choices for use with forms, flag icons static files, and a country field for models. Insta

Chris Beaven 1.2k Dec 31, 2022
A real-time photo feed using Django and Pusher

BUILD A PHOTO FEED USING DJANGO Here, we will learn about building a photo feed using Django. This is similar to instagram, but a stripped off version

samuel ogundipe 4 Jan 01, 2020
Simple API written in Python using FastAPI to store and retrieve Books and Authors.

Simple API made with Python FastAPI WIP: Deploy in AWS with Terraform Simple API written in Python using FastAPI to store and retrieve Books and Autho

Caio Delgado 9 Oct 26, 2022
Add Chart.js visualizations to your Django admin using a mixin class

django-admincharts Add Chart.js visualizations to your Django admin using a mixin class. Example from django.contrib import admin from .models import

Dropseed 22 Nov 22, 2022
Auto-detecting the n+1 queries problem in Python

nplusone nplusone is a library for detecting the n+1 queries problem in Python ORMs, including SQLAlchemy, Peewee, and the Django ORM. The Problem Man

Joshua Carp 837 Dec 29, 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
This is a repository for a web application developed with Django, built with Crowdbotics

assignment_32558 This is a repository for a web application developed with Django, built with Crowdbotics Table of Contents Project Structure Features

Crowdbotics 1 Dec 29, 2021
Forward and backwards compatibility layer for Django 1.4, 1.7, 1.8, 1.9, 1.10, and 1.11

django-compat Forward and backwards compatibility layer for Django 1.4 , 1.7 , 1.8, 1.9, 1.10 and 1.11 Consider django-compat as an experiment based o

arteria GmbH 106 Mar 28, 2022
Twitter Bootstrap for Django Form - A simple Django template tag to work with Bootstrap

Twitter Bootstrap for Django Form - A simple Django template tag to work with Bootstrap

tzangms 557 Oct 19, 2022
Modular search for Django

Haystack author: Daniel Lindsley date: 2013/07/28 Haystack provides modular search for Django. It features a unified, familiar API that allows you to

Daniel Lindsley 4 Dec 23, 2022
Example project demonstrating using Django’s test runner with Coverage.py

Example project demonstrating using Django’s test runner with Coverage.py Set up with: python -m venv --prompt . venv source venv/bin/activate python

Adam Johnson 5 Nov 29, 2021
Django server-side adapter for Inertia.js

django-inertia Django server-side new adapter for Inertia.js. Getting Started Install the package pip install django-inertia Configure your project A

Samuel Girardin 14 Sep 16, 2022
A simple E-commerce shop made with Django and Bulma

Interiorshop A Simple E-Commerce app made with Django Instructions Make sure you have python installed Step 1. Open a terminal Step 2. Paste the given

Aditya Priyadarshi 3 Sep 03, 2022
An airlines clone website with django

abc_airlines is a clone website of an airlines system the way it works is that first you add flights to the website then the users can search flights

milad 1 Nov 16, 2021
A calendaring app for Django. It is now stable, Please feel free to use it now. Active development has been taken over by bartekgorny.

Django-schedule A calendaring/scheduling application, featuring: one-time and recurring events calendar exceptions (occurrences changed or cancelled)

Tony Hauber 814 Dec 26, 2022
Logan is a toolkit for building standalone Django applications

Logan Logan is a toolkit for running standalone Django applications. It provides you with tools to create a CLI runner, manage settings, and the abili

David Cramer 206 Jan 03, 2023
Mobile Detect is a lightweight Python package for detecting mobile devices (including tablets).

Django Mobile Detector Mobile Detect is a lightweight Python package for detecting mobile devices (including tablets). It uses the User-Agent string c

Botir 6 Aug 31, 2022
🌟 A social media made with Django and Python and Bulma. 🎉

Vitary A simple social media made with Django Installation 🛠️ Get the source code 💻 git clone https://github.com/foxy4096/Vitary.git Go the the dir

Aditya Priyadarshi 15 Aug 30, 2022