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
A simple page with paypal payment and confiramtion in django

django-paypal a simple page with paypal payment and confiramtion in django Youtube Video : Paypal Smart Button : https://developer.paypal.com/demo/che

Mahmoud Ahmed 5 Feb 19, 2022
Simply integrate Summernote editor with Django project.

django-summernote Summernote is a simple WYSIWYG editor. django-summernote allows you to embed Summernote into Django very handy. Support admin mixins

Summernote 936 Jan 02, 2023
This repository contains django library management system project.

Library Management System Django ** INSTALLATION** First of all install python on your system. Then run pip install -r requirements.txt to required se

whoisdinanath 1 Dec 26, 2022
It takes time to start a Django Project and make it almost production-ready.

It takes time to start a Django Project and make it almost production-ready. A developer needs to spend a lot of time installing required libraries, setup a database, setup cache as well as hiding se

Khan Asfi Reza 1 Jan 01, 2022
Django Audit is a simple Django app that tracks and logs requests to your application.

django-audit Django Audit is a simple Django app that tracks and logs requests to your application. Quick Start Install django-audit pip install dj-au

Oluwafemi Tairu 6 Dec 01, 2022
A music recommendation REST API which makes a machine learning algorithm work with the Django REST Framework

music-recommender-rest-api A music recommendation REST API which makes a machine learning algorithm work with the Django REST Framework How it works T

The Reaper 1 Sep 28, 2021
Automatically upgrade your Django projects.

django-upgrade Automatically upgrade your Django projects. Installation Use pip: python -m pip install django-upgrade Python 3.8 to 3.10 supported. Or

Adam Johnson 525 Dec 29, 2022
A handy tool for generating Django-based backend projects without coding. On the other hand, it is a code generator of the Django framework.

Django Sage Painless The django-sage-painless is a valuable package based on Django Web Framework & Django Rest Framework for high-level and rapid web

sageteam 51 Sep 15, 2022
Organize Django settings into multiple files and directories. Easily override and modify settings. Use wildcards and optional settings files.

Organize Django settings into multiple files and directories. Easily override and modify settings. Use wildcards in settings file paths and mark setti

Nikita Sobolev 940 Jan 03, 2023
An automatic django's update checker and MS teams notifier

Django Update Checker This is small script for checking any new updates/bugfixes/security fixes released in django News & Events and sending correspon

prinzpiuz 4 Sep 26, 2022
django social media app with real time features

django-social-media django social media app with these features: signup, login and old registered users are saved by cookies posts, comments, replies,

8 Apr 30, 2022
Developer-friendly asynchrony for Django

Django Channels Channels augments Django to bring WebSocket, long-poll HTTP, task offloading and other async support to your code, using familiar Djan

Django 5.5k Jan 06, 2023
Automatically reload your browser in development.

django-browser-reload Automatically reload your browser in development. Requirements Python 3.6 to 3.10 supported. Django 2.2 to 4.0 supported. Are yo

Adam Johnson 254 Jan 04, 2023
Django Pickled Model

Django Pickled Model Django pickled model provides you a model with dynamic data types. a field can store any value in any type. You can store Integer

Amir 3 Sep 14, 2022
Thumbnails for Django

Thumbnails for Django. Features at a glance Support for Django 2.2, 3.0 and 3.1 following the Django supported versions policy Python 3 support Storag

Jazzband 1.6k Jan 03, 2023
This is a Django app that uses numerous Google APIs such as reCAPTURE, maps and waypoints

Django project that uses Googles APIs to auto populate fields, display maps and routes for multiple waypoints

Bobby Stearman 57 Dec 03, 2022
Sampling profiler for Python programs

py-spy: Sampling profiler for Python programs py-spy is a sampling profiler for Python programs. It lets you visualize what your Python program is spe

Ben Frederickson 9.5k Jan 01, 2023
A task management system created using Django 4.0 and Python 3.8 for a hackathon.

Task Management System A task management app for Projects created using Django v4.0 and Python 3.8 for educational purpose. This project was created d

Harsh Agarwal 1 Dec 12, 2021
Django Girls Tutorial Workshop

Django Girls Tutorial Workshop A log of activities during the workshop. this is an H2 git remote add origin https://github.com/ahuimanu/django_girls_t

Jeffry Babb 1 Oct 27, 2021
Hello world written in Django.

Learning Django 💡 create a virtual environment create python -m venv ./venv. this virtualenv file will be excluded by .gitignore activate the virtual

Dipak giri 4 Nov 26, 2021