Django DevOps with Kubernetes: Django Deployment to Azure AKS using CI/CD Pipeline with Python, Terraform, GitHub Actions, and Istio Integration
Automating Django , Celery , Redis and postgres deployment to AWS EC2 / EKS / ECS / Azure AKS using Terraform ( Complete Guide)
In this article, we explore the integration of Terraform, Kubernetes, and Azure CLI within a GitHub Actions workflow to automate the django deployment and management of cloud infrastructure. With a focus on best practices for Continuous Integration and Continuous Deployment (CI/CD) pipelines, we demonstrate how to securely manage Azure credentials, configure Kubernetes access, and execute Azure CLI commands. This guide provides practical examples and step-by-step instructions, empowering DevOps professionals to streamline their workflows and ensure reliable deployments across cloud environments.
Before we get into the good stuffs, first we need to make sure we have the required services on our local machine or dev server, which are:
- Basic knowledge of Django
- AWS Account
- Github Account
- AWS CLI installed and configured.
- ECS CLI
- Docker installed locally.
- Typescript installed
- Postman
- Python 3
- NPM
- NodeJS
- Terraform
- A Domain name Hosted from any domain name provider ( Ex: AWS Route 53 )
- Basic familiarity with YAML and GitHub workflows.
- A Django project hosted in a GitHub repository
- Basic knowledge of HTML or React
- Any Browser for testing
- Intermediate knowledge in Serverless Computing ( Ex : AWS Lambda , ECS,..)
You can follow along with this source code: GitHub - joelwembo/django-multitenant-saas-ecommerce-kubernetes: Django Multi-tenant … Django Multi-tenant , microservices , Kubernetes, Jenkins, Github Actions and Multiple Databases using docker, bash… github.com
If you don’t have virtualenv installed, you can install it by running the following command in your CMD after Python was installed:
Create virtual environment for Python
python3 -m venv venv or python -m venv venv
source ./venv/bin/activate
source ./venv/bin/deactivate ( To Deactivate )
or
.\venv\Scripts\activate
268770
mkdir app
pip install django
A Django app is a self-contained component of a Django project. It is a module that provides specific functionality, such as handling authentication, managing blog posts, or serving an API. An app should represent a single, specific functionality or purpose within the overall website.
django-admin startproject django-multitenant-saas-ecommerce-kubernetes
within the django project using the following command:
python manage.py startapp testapp
Adding a new app into the project
python manage.py startapp home apps/home
python manage.py startapp finances apps/finances
python manage.py startapp snippets apps/snippets
python manage.py startapp users apps/users
python manage.py startapp payments apps/payments
python manage.py startapp products apps/products
'apps.home', 'apps.snippets', 'apps.users', 'apps.finances', 'apps.payments', 'apps.products',
python manage.py makemigrations
python manage.py migrate
python manage.py runserver
python manage.py createsuperuser
bash ./server-entrypoint.sh
bash ./run.sh
http://127.0.0.1:8585/
http://127.0.0.1:8585/swagger/
http://127.0.0.1:8585/data-browser/
python manage.py show_urls python manage.py graph_models finances -a -o finances_models.png
pip install pipenv (globally)
To activate this project's virtualenv, run pipenv shell. Alternatively, run a command inside the virtualenv with pipenv run.
pipenv install django-ledger[graphql,pdf]
python manage.py test django_ledger
pip install -r requirements.txt
python manage.py makemigrations finances
python manage.py makemigrations app
python manage.py migrate finances
python manage.py migrate app
tenant = Client(schema_name="test", name="test Company")
domain = Domain(domain="btest.localhost", tenant=tenant, is_primary=True)
A step-by-step guide for AWS EC2 provisioning using Terraform: How to set up an Nginx reverse proxy with SSL using Certbot for a Django application running in Ubuntu server - Part 17
ssh -i id_rsa.pem [email protected]
sudo certbot certonly --nginx -d prodxcloud.io -d *.prodxcloud.io -m [email protected] --agree-tos --preferred-challenges dns
if error chose this one
sudo certbot certonly --manual --preferred-challenges=dns -d prodxcloud.io -d *.prodxcloud.io -m [email protected] --agree-tos
docker-compose -f docker-compose-dev.yaml up
- Generate SSH / Copy / Replace SSH Key Pair for your ec2
ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa cat ~/.ssh/id_rsa chmod 400 ~/.ssh/id_rsa.pem
rm ~/.ssh/id_rsa vim ~/.ssh/id_rsa.pem chmod 400 ~/.ssh/id_rsa.pem
- Inventory file /etc/ansible/inventory
[hosts_to_add_key] 20.28.8.41 ansible_ssh_port=22 ansible_user=ubuntu
[hosts_to_add_key:vars] ansible_ssh_common_args="-o StrictHostKeyChecking=no"
- ansible.cfg /etc/ansible/ansible.cfg
[defaults] inventory =/etc/ansible/inventory host_key_checking = False deprecation_warnings = False remote_user = ubuntu
retries = 3
private_key_file = ~/.ssh/id_rsa
Ansible Commands : root@ip-10-0-2-236:/etc/ansible# ansible all --list-hosts hosts (1): 20.28.8.41
ansible-inventory --list -y
root@ip-10-0-2-236:/etc/ansible# ansible-inventory --list -y all: children: hosts_to_add_key: hosts: 20.28.8.41: ansible_ssh_common_args: -o StrictHostKeyChecking=no ansible_ssh_port: 22 ansible_user: ubuntu ungrouped: {}
ansible all -m ping -u root
root@ip-10-0-2-236:~# ansible all -m ping -u root 20.28.8.41 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python3" }, "changed": false, "ping": "pong" }
Next, write the playbook
- ansible-playbook -i hosts deploy_nginx.yml
- ansible-playbook -i deploy_nginx.yml # host in default
ANSIBLE_LOCALHOST_WARNING=False
ANSIBLE_INVENTORY_UNPARSED_WARNING=False
ansible-playbook deploy_nginx.yaml --tags deploy_nginx
kubectl apply -f pg_secrets.yaml kubectl apply -f pg_storage.yaml kubectl apply -f pg_deployment.yaml kubectl apply -f pg_service.yaml kubectl get pods
kubectl create -f .\django-env-configmap.yaml configmap/django-env-configmap created
kubectl describe configmaps django-env-configmap
kubectl apply -f deployment-django-dev.yaml
kubectl apply -f prodxcloud-django-web-dev-service.yaml
Thank you for Reading !! 🙌🏻, see you in the next article.🤘