Deploy Django into Production with Kubernetes, Docker, & Github Actions. Complete Tutorial Series
HTML-код
- Опубликовано: 28 июн 2024
- Learn how to deploy a production-ready Django application into a Kubernetes cluster.
We partnered with DigitalOcean to bring you this series. Sign up now for a $100 credit: do.co/cfe-youtube
Kubernetes is a tool to manage containers. This type of management is called orchestration because it does more than just manage the containers such as it will scale up and scale down resources as needed (among many other things).
Since Kubernetes is a massive tool, we're going to be covering a practical implementation using a Docker-based Django application.
Django is a way to build web applications rapidly using Python. There are many ways to get Django into production so the question is why use K8S at all? There's many reasons we'll cover throughout this series but here are a few:
● Gracefully deploy Docker-based Django projects
● Scale up (or scale down) your web apps with ease
● Use a few or a lot of microservices internally or externally
● Self-heal when services go down
● Simplify app deployment (after initial configuration)
● Streamline your backend Infrastructure (provision virtual machines and let k8s do the rest)
Kubernetes can do a lot which makes it a great tool to learn but also a daunting one at that. To make it more approachable here's what we'll be doing:
● Start a Python Virtual Environment
● Create a bare bones production-ready Django project
● Create a Dockerfile to describe our environment (much like this blog post: www.cfe.sh/blog/django-on-docker)
● Use a managed Database service from DitgitalOcean
● Install Kubernetes Command Line tool locally (kubectl)
● Start a Kubernetes Cluster on DigitalOcean
● Provision a Deployment and Service for our Django project
● Implement Github Action Workflows to automate future deployments
▶︎ References
● Django on Docker guide: cfe.sh/blog/django-on-docker
● Docker & Docker Compose tutorial series: cfe.sh/projects/docker-and-do...
● Github Actions guide: cfe.sh/blog/django-github-act...
● DigitalOcean $100 credit for new accounts: do.co/cfe-youtube
● Project Code: github.com/codingforentrepren...
★ Subscribe: cfe.sh/youtube ★
⦿ New to Django? Checkout the Try Django Tutorial Series: • Try Django 3.2 - Pytho...
▶︎ Chapters
00:00:00 Welcome to Django & Kubernetes
00:02:08 Requirements & Recommendations
00:07:12 Install Kubernetes Command Line Tool Kubectl on macOS
00:12:49 Install Kubernetes Command Line Tool Kubectl on Windows
00:19:08 Create the Python Virtual Environment
00:24:28 Install Requirements & Start Django Project
00:33:01 Environment Variables with a dotenv file
00:36:10 Setup django dotenv to read our dotenv file
00:41:54 Update Django settings for Database & Environment Variables
00:47:29 Docker, Dockerfile & dockerignore
01:01:13 Create a migration script
01:09:46 Docker Compose Part 1
01:32:48 Docker Compose Part 2
01:41:43 Provision Kubernetes on DigitalOcean
01:55:20 Connect to Kubernetes with kubectl & kubeconfig
02:03:41 Deploy your First Container on Kubernetes
02:14:00 Expose your Deployment with a Load Balancer
02:26:11 Deploy a Minimal FastAPI App
02:34:24 DigitalOcean Container Registry
02:38:36 Build & Push Docker Container to DO Container Registry
02:45:08 Managed PostgreSQL Database
02:52:44 Kubernetes Secrets from env
02:57:17 Django Deployment & Service
03:11:21 Full Deployment & Fix Errors
03:24:55 Deployment Guide
03:38:57 Github Actions Test Django Automatically
03:55:31 Github Actions Build & Push Docker Container to DigitalOcean
04:11:09 Github Actions Django Deployment on Kubernetes
04:29:01 Django Static Files on DigitalOcean Spaces
04:54:28 New Django App, Models, & Tests
05:10:55 Thank you
Thank you for checking this one out! Code linked below as well as chapters -- skip around!
Code: github.com/codingforentrepreneurs/Django-Kubernetes
00:00:00 Welcome to Django & Kubernetes
00:02:08 Requirements & Recommendations
00:07:12 Install Kubernetes Command Line Tool Kubectl on macOS
00:12:49 Install Kubernetes Command Line Tool Kubectl on Windows
00:19:08 Create the Python Virtual Environment
00:24:28 Install Requirements & Start Django Project
00:33:01 Environment Variables with a dotenv file
00:36:10 Setup django dotenv to read our dotenv file
00:41:54 Update Django settings for Database & Environment Variables
00:47:29 Docker, Dockerfile & dockerignore
01:01:13 Create a migration script
01:09:46 Docker Compose Part 1
01:32:48 Docker Compose Part 2
01:41:43 Provision Kubernetes on DigitalOcean
01:55:20 Connect to Kubernetes with kubectl & kubeconfig
02:03:41 Deploy your First Container on Kubernetes
02:14:00 Expose your Deployment with a Load Balancer
02:26:11 Deploy a Minimal FastAPI App
02:34:24 DigitalOcean Container Registry
02:38:36 Build & Push Docker Container to DO Container Registry
02:45:08 Managed PostgreSQL Database
02:52:44 Kubernetes Secrets from env
02:57:17 Django Deployment & Service
03:11:21 Full Deployment & Fix Errors
03:24:55 Deployment Guide
03:38:57 Github Actions Test Django Automatically
03:55:31 Github Actions Build & Push Docker Container to DigitalOcean
04:11:09 Github Actions Django Deployment on Kubernetes
04:29:01 Django Static Files on DigitalOcean Spaces
04:54:28 New Django App, Models, & Tests
05:10:55 Thank you
I don't know many how can you post such high quality content for free. Salute.
Because.... mostly but Not everyone is greedy
May be digitalocean sponsored? But still great content
@@sethurk okay
WAAOO!!! You just did it.... Was missing this K8s thing. and finally sir you did it. Thanks a lot.
I am stoked!!! Started looking into kubernetes recently... Glad to be subscribed to your channel. You just saved me loads of hustle
Great to see you still in the game killing it
I'm a subscriber since 2019, It's unbelievable how informatic your channel is! Great Content Great Mastery. You have no idea how many times your channel just saved my a**! Especially this one! I mean someone with a good portion of time can individually start a startup just by watching your videos!
just randomly clicked from suggestion! but Man I Love you!!! Never found this much detailed and insane explanation. Subbed
I am truly impressed. I've been working as a software engineer for two years, and this tutorial is unbelievable!
Once I get a job i will purchase subscription for sure. This is brutal free content. Life saver.
Wow thanks for doing this. Really helpful.
The best tutorial I've seen so far :)
The video was so helpful. Thank you very much.
QUALITY, this is what everyone got used to from Justin Michel. THANK YOU
Thank you!
Would've been a lifesafer 2 weeks back when I was ramming my head deploying an application at work, anyway thanks and keep up the good work!
I finally found the time to go over this material, and OMG, my mind is blown away by what you are showing us. Justin, there are no words but the utmost Gratitude towards you and your teachings. I'm blown away by this material again I'm only two and a half hours in, and my mind just Exploded like confetti. I will admit the Kluster will be more expensive than Dokku, I think - but I'm anxious about seeing this lesson to the end
I felt that excited when I was coding this project just as you my friend, this classes are like treasure for us
always be number one Amazing tutorial
You're a star mate.
Hey!
I know you from 2019 with Django full course for Beginners - that was a point where i've started lear python and Django.
So now because of you i know Django ) Thank you very much for your work, dude!!!!!
Great to hear it! Keep it up
Salute Justin!
Awesome work💯
Thank you!!
This video is AWESOME. Thank you very much!!!!!!
Thank you!
Awesome 🎉🎉🎉🎉🎉… after a very long time saw some end to end implementation with complete application development lifecycle with Devops implementation
Thank you, Justin! Your Django tutorial is always the best! If I can give you more than one thumbs up, I will do it without hesitation!
Thank you
I don't know why your RUclips is growing slowly, you are the best
Learned a ton!!!
im in the middle of this now and i just want to say this is some dope work....i hope one day ill be able to master my skills like you sir!! kudos!
Thank you! Subscribe and little by little you will.
Really High Quality Content for free🎉❤️
Great help. Thanks man
Thanks! Amazing content. High quality
Thank you! Is helping me a lot in my work.
Glad to hear it!
you should add another series where you explain in very short time for experienced programmers.
Awesome !!
Salute thank you so much this helped me a lot !
Awesome!
This is the absolute best tutorial for a beginner like myself on how to deploy a django website onto cloud. Thank you so much. My only humble request is if you can make a video or point me in the direction of how to deploy on AWS instead of DigitalOcean?
The Complete Tutorial as much as possible. Great work👍🏻
Good work👍🏻👍🏻
What an amazing video. Really can't thank you enough. I've struggled to find info about using docker for django in production. A couple of notes in case it's helpful to anyone else. I've faced a few issues along the way caused by using an M1 Mac.
---- Postgres Error : scram authentication requires libpq version 10 or above -
This error us cause by a bug that causes an older version to get built when docker makes containers for M1 macs. To resolve you can ask docker build the container for amd64 by typing "export DOCKER_DEFAULT_PLATFORM=linux/amd64 " (this will slow things down a lot tho).
--- General failure of the image. Running my image in digital ocean apps failed (as did my kubernetes pods). I found a stockoverflow stating that when images are build on M1s it can cause issues. To solve change the first line of the Dockerfile to: FROM --platform=linux/amd64 python:3.10-slim to tell docker to build the image for amd not your local arm.
from VIETNAMESE , you are wonderful
I just came here to see who are these people with the super power of watching a 5hour tutorial.. I'm wowed
I request you (CodingEntrepreneurs)to please make PLAYLIST related videos thatswhy everyone can learn in well way. Thanks
Just a few minutes in and I’m loving this already. Can’t believe this is free!
Thank you. Be sure to show DigitalOcean some love too (they sponsored this) do.co/cfe-sh/ 🦾
Digital gold!
You're doing an amazing job. All these quality contents are for free? Incredible
Thanks for this
Any time
Thanks a lot😊
Thanks 🙏🏻
Thanks.... Great job...
Thank you too!
Ohh thank you very much....
Thanks!
Thank you
Thank you!
Great video and thanks mate . I did a research and found out that you don't need virtualenv if you're using docker because there won't be another app in same container
But! Containers may have conflicts when it comes to Python packages - even if it’s unlikely I still think virtual environments are a must when deploying to production!
Loveee youuuuuuuuuuuuuu mannnnnnnnnnnnn
2:37:57 "on Windows..." Reason enough to install WSL2. Even if you prefer Windows, there are just some cases where having access to a *nix environment for tooling is very helpful.
Great vid. Not sure if I would bother setting up a venv in a container though. Didnt make any sense to me. But I would definitely pip freeze my dev. Venv when deploying into production. After unit testing is done.
Os-Python has different packages then a clean venv. So yeah, always use a virtual env
This is a fantastic k8 deployment example. I got a question @3:36:38, Don't we need to migrate database changes on each of the pod?
i dont think so. we're using digital ocean managed db. so at pod creation or deletion that db staying persistent because it running separately and it has nothing to do with Kube cluster.
Hello sir, I wish to thank you for your kind work, I am looking forward to take on this project 1 year after it has been posted and wish to know if the hosting platforms are free and open source to use still?
Very nice tutorial, still haven't finished. The only thing I thought curious is the use of virtual environment inside docker, not sure why to do it. Docker itself serves as a general virtual environment
Even the docker container can have Python-based dependencies. Having a Python virtual environment ensures no conflicts can arise.
Hi, Please make a video on how to use Docker and kubernetes on the ubuntu located in Hypervisor inorder to deploy the Django Project onto the server.
Just in case if you are using Windows and stuck on RUN command in Dockerfile and by "docker compose up" you get error 127, check your path, it should be like in the video with bin/ and not Scripts/ as I have thought
Nice clarification! Thanks
Create series, would like to add that you can run wsl2 wich is basiclly linux hypervisor terminal in windows
Hey men, i'm a big fan of you when i start learn coding and i choice python django. I learned from you so much, but when i made first product, its so slow, i fell slow than js framwork. I hear that Rust is faster than python and learning actix framework. I hope you can teach us build production api web in Rust using actix like django rest framwork. Or how to change from django rest api framwork to actix. Thank you so much.
What if there are different Repos, for instance, one for the backend which is Django, and the other one is the front-end repo which is React then how Kubernetes and CI/CD should be handled?
What are the differences for using asgi instead of wsgi that would happen to the code, should I just replace wsgi with asgi and make sure to do the dotenv in the asgi?
Just to clear things out, do we use docker-compose in development and Kubernetes in production?
Because the postgres_db is not resolving locally in the last part of the tutorial where we create a new Django app. And you don't seem to use docker-compose.
한국인 손!
Thank you so much!
The workspace config file, can you show how to do it on pycharm ? Some of use pycharm
You don't have to worry about python path on Windows at all. Just remember during installer, click on that add to path. Simple and easy
you also don't have to worry about virtual env either. what's the point of using docker if you're just going to create another env inside an env. just run your python environment raw inside the container. that's what it's for.
Can you please suggest how to run celery with redis on this configuration?
Amazing tutorial and outstading series! I have a question: what if my github repo is shared between our devs and I don't want them to access the secrets. Any tips on this? Thanks!
You can pull the secrets from a separate repository in that case...
I am rewatching it and just got a quick question. I am wondering what the reason why you are still using virtual environment when building and runing a docker. Would it be easier to directly install the requirements into docker and run the command directly without using venv? Thanks!
When in doubt, use a virtual environment with Python projects.
When in doubt, use local node_modules with a JavaScript project.
System-wide dependencies change all the time, this kind of isolation ensures your app is almost always unaffected.
not able to conect with postgress db web service is running db is also running but not able to connect.
getting the error [[connection to server at "localhost" (::1), port 5434 failed: FATAL: database "myproject" does not exist]]
Can you guide how to add https support to it, including obtaining the certbot certificate
Could you make a tutorial on how to integrate the ELK stack with kubernetes on DO?
Simple Application Enter Your Details
ruclips.net/video/DWn5-Ej8R-M/видео.html
Can I proceed with the course with just local kubectl for practice
i have a question am having a problem with entrypoint.sh after doing docker built -t . and trying docker run .... it always say exec /ap/entrypoint.sh no such file or directory ... do you have a solution for this problem. cloned your repo and tried the command
When debug is false my css doesn't work. What are the solution
How can we deploy the same on AWS EKS ?
Great content, I do have a question
Why not use Git Bash in Windows? and do Linux Commands?
Why use Chocolatey? is there a reason other than cute chocolate command names?
Great points! I’ll consider this for the next time around. I actually prefer the subsystem for Linux (WSL) to run and use all the things we need in Docker.
I tend to think chocolatey is easiest for the most people
My staticfiles are working for django project but not for django app in production. Can you help me ?
I got into this error while creating superuser "error: unrecognized arguments: -- noinput"
django deployment is easy, but k8s make it so complex...
Beynimi yakıyorsun gakko :)
1:29:00
Doubt ?? In connect to cluster - I made my cluster on azure and setup az aks -get-credentials ****** and so on. Now i m able to access it globally on my computer since a config now exist in my laptop in file"/.kube/config". So to limit the access to current workspace should i remove this file and do workspace settings like u did in the video.
Because as long as that config file exists in the system inside .kube directory, i can access it from any terminal or any workspace.
Pls answer this as for the best practice pov.
RBAC!
When we setup the docker in the windows machine, the virtual environment file has no bin but has rather Scripts folder. So, if I replace the bin with Scripts in all of your docker setting up parts. It should work as intended right?
In this, Docker builds to Linux regardless of what os is crafting the dockerfile
@@CodingEntrepreneurs Oh okay, thank you for reply. I appreciate your time.
how you serve staticfiles without whitenoise or nginx
what's the need for python virtual environment inside docker container?
It will make sure that there will be no clashes between existing Python packages installed through the OS on the Docker container.
It’s unlikely to happen but it can, using a venv will prevent that.
2:08:00
hey can anyone help me out , I'm getting error on git command I have installed git. i can see the version in the cmd but I cant run any git command in visual studio its showing like this "PS C:\Users\91924\Dev\django-k8s> git --version
git : The term 'git' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of
the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ git --version
+ ~~~
+ CategoryInfo : ObjectNotFound: (git:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException"
gunicorn does not work on windows, when i do the step of 58:25, got a error "No module named 'fcntl' "
So did you go ahead and complete the project and was it a success?? I am also doing it on windows
@@arjavkanadia9730 Nop, It didint work, i had to find a new way, to be fair, i just reset all the project
@@arjavkanadia9730 Yes, i worked for a startup in that time, and i and other co-worker complete the project
@@arjavkanadia9730 I was using windows and my co-worker using Linux, i dont remember what we did but a thing that i learned ia, always have other way to do things with Django
Did anyone else get an error message when they tried to install psycopg2-binary about the pg_config file not being found? "Error: pg_config executable not found"
Just try psycopg2
getting 400 bad request when i docker compose --build, don't know why
I'm just wondering how you can run docker compose up without any UserWarning in the terminal at 1:13:16. I ran into a UserWarning in the terminal that said "Not reading /app/.env - it doesnt exist" . I see that you're ignoring it in .dockerignore and that's why the file didn't copied into the image. I'm still confused.
Docker compose does not copy the .env file, it injects it into the runtime for docker.
think of it like
docker run -env-file .env my-image-tag
@@CodingEntrepreneurs alright I understand now. I missed the point of using env_file in the docker-compose.yml file. Thank you so much, Justin ! I learned a lot from you ! There's no word I can say about how grateful I am to know that your channel does exists. What a quality content ! :)
@@rijaltanjung9506 How did you resolve the issue, I keep getting the same userWarning, and when I try to access django admin with the password in my environment it''s not working, someone please help me with this
hi! im getting an error: load balancer droplets are down. Crashloopback off. followed all steps.
Did you check the logs of your deployment?
@@CodingEntrepreneurs yes, gunicorn is working. however, when i check on describe pod, liveness and readineness probe failed with statuscode: 400.
@@CodingEntrepreneurs hi!. it works now. thank you
@@migy220 Awesome! Nice work
men when you go out is like another chapter of the Mandalorian haaaaaaaaaaaaaaaaaaaa!!!!!!!
Hahaha!
I AM COMING HERE IN 3DAYS
Prove it
@@CodingEntrepreneurs Sir, I am Just Finalizing My website with Django as backend and then I will push it. But currently, I only know Django with docker. And as I also voted in Forum for Kubernetes with Django. And it's the latest tech too. And the biggest thing. No one is teaching this on the whole web. Except for One And only Sir Justin Mitchell.
Present, Going to start. Let's see, Is that I could be able to Implement it or not.
I could not remember how he populated .gitignore file
Just googling "github Python gitignore" haha
Thanks for this great tutorial. Out of interest, could you please explain why you are creating a virtual environment within docker container? I ask this because I never do since the python you are using in that docker container is really only gonna be used within that container and installing virtual environment spins up another python interpreter from global python (installed in docker) which is an extra overhead.
Virtual environments isolate packages from the global python packages. It’s always a good idea to do this even within a container because a container may have global python packages for other OS-level installed tools
Hello, I'm facing below issue while running docker-compose. Please help me out
File "/app/django_k8s/wsgi.py", line 14, in
from Tkinter import CURRENT
ModuleNotFoundError: No module named 'Tkinter'
[2022-02-13 06:06:26 +0000] [11] [INFO] Worker exiting (pid: 11)
[2022-02-13 06:06:27 +0000] [10] [INFO] Shutting down: Master
[2022-02-13 06:06:27 +0000] [10] [INFO] Reason: Worker failed to boot.
requirements.txt
django>=3.2,