How To Deploy A Secure Django Application on Kubernetes
This weblog article is predicated on the documentation and instance repository written by Porter’s neighborhood member, jimcru21.
Kubernetes is a robust container orchestrator that automates deployments, administration, and scaling of containerized purposes. Despite all these advantages of Kubernetes, nonetheless, there’s usually a ton of overhead to it that’s usually not justified for easy purposes. In this tutorial, we go over how one can deploy Django purposes on main cloud suppliers’ Kubernetes choices (e.g. EKS, GKE, DOKS) in a number of clicks utilizing Porter, with out even having to containerize your purposes.
What is Porter?
Porter is a Platform as a Service (PaaS) that runs within the person’s personal cloud. If you are conversant in Heroku/Vercel/Netlify, Porter brings the convenience of use of these platforms into your personal cloud, notably into your personal Kubernetes cluster. With Porter, you’ll be able to deploy and scale Django purposes on Kubernetes with minimal overhead with out having to write down a `Dockerfile` or `YAML` recordsdata.
Porter is open supply. Check out the supply code right here.
Provisioning the Kubernetes Cluster
Before you can begin deploying a Django software on Kubernetes, you could first provision a Kubernetes cluster. With Porter, it is doable to create a cluster on AWS, GCP, and Digital Ocean with a single click on. Follow this information to provision the cluster within the cloud supplier of your selection.
Alternatively, you may as well provision a cluster by yourself and join Porter to an current cluster. These are the guides on how one can create your personal cluster for every cloud supplier:
After you’ve got created a Kubernetes cluster, you’ll be able to connect with it through the Porter CLI per this information.
Prepare Django Application
While it isn’t essential to containerize your Django software to deploy it via Porter, you could observe these steps for a profitable deployment.
1. Install `django-allow-cidr` (that is the middleware to allow the usage of CIDR IP ranges in `ALLOWED_HOSTS`)
pip set up django-allow-cidr
2. Go to Django Settings and add `os.environ.get` in allowed host.
ALLOWED_HOSTS = os.environ.get("DJANGO_ALLOWED_HOSTS", default='127.0.0.1').break up(" ")
3. Add allowed CIDR networks. Put CIDR in keeping with the Kubernetes kubelet CIDR. If you’ve got provisioned the cluster via Porter, it’s set to `10.99.0.0/16` by default. If you’ve got provisioned the cluster your self, seek the advice of your cloud console to search out the CIDR.
ALLOWED_CIDR_NETS = os.environ.get("ALLOWED_CIDR_NETS", default='10.99.0.0/16').break up(" ")
4. Add `django-allow-cidr` middleware to the applying.
MIDDLEWARE = [ 'allow_cidr.middleware.AllowCIDRMiddleware', #'django.middleware.security.SecurityMiddleware', ]
5. Add Gunicorn
6. Add static folder and add your HTML and CSS recordsdata. Locate static URL settings and add static file dirs under:
STATICFILES_DIRS = ( os.path.be a part of(BASE_DIR, 'static'), ) STATIC_ROOT = os.path.be a part of(BASE_DIR, 'staticfiles')
7. Add a `Procfile` to your repository. This is an alternate configuration to `Dockerfile` that makes use of Cloud Native Buildpacks, which have been popularized by Heroku. Porter makes use of the `Procfile` to construct your pictures if a `Dockerfile` just isn’t current.
net: gunicorn <project-name>.wsgi -b 0.0..:8989 --timeout 120
8. Lastly, generate a necessities file.
pip freeze > necessities.txt
Sample repository for the above configuration: https://github.com/jimcru21/porter-sample-django-non-docker
Deploy Django on Porter
1. Visit the Porter Dashboard. Click Web Service > Launch Template.
2. Name your software.
3. When prompted for the deployment methodology, click on Git Repository. Connect your GitHub account and choose the repo you’d wish to deploy from.
4. Select the department (important within the instance under), then hit Continue.
5. Porter will learn your `Procfile` and immediate you for the identify of the method you’d wish to run. In the instance above, the method is known as `net`. By default, Porter shops your construct artifacts within the registry that was provisioned by Porter. If you’ve got linked to an current cluster, you may as well join an current container registry to Porter utilizing our CLI per this information.
6. In Additional settings, specify the container port that you just use for `gunicorn` within the `Procfile` (within the instance above, that is set to `8989`). You can even configure customized area per this information.
7. From the Environment tab, set `DJANGO_ALLOWED_HOSTS` that we specify on Django settings. Then enter the area you could have set to your software.
8. Click Deploy then look ahead to buildpack to complete and push to porter. Porter writes a GitHub Actions file to your repository for Continuous Integration. You can examine the construct progress in your GitHub repository beneath the Action tab.
Kubernetes generally is a mountain to climb for newcomers, however builders are drawn to its advantages regardless of the steep studying curve. Porter is a platform that makes Kubernetes straightforward to the extent that the person does not should be taught something about Kubernetes, and even Docker, to begin deploying.
If you could have any questions on this tutorial, be a part of our discord neighborhood and ask away!
Previously revealed at https://weblog.getporter.dev/deploy-django-on-kubernetes-without-dockerizing/
Create your free account to unlock your customized studying expertise.