/django_kubernetes

Deploy Django locally with Kubernetes (Minikube)

Deploy Django locally with Kubernetes (Minikube)

The desired state:

Deployment will include 2 pods:

  • The first running a Gunicorn/Django container and an Nginx container
  • The second running a Postgres database

Other K8s components used:

  • initContainer (to collect Django static files)
  • configmap (for environment variables)
  • secrets (for sensitive variables)
  • emptyDir (for static content)
  • persistent volume and claim (for database storage)

Watch out!

To use a local image that you've built for your Django app, you can switch to Minikube's local Docker engine as opposed to using your own local Docker engine.

minikube docker-env

## point your shell to minikube's docker-daemon
eval $(minikube -p minikube docker-env)

## undo i.e. go back to your own docker env
eval $(minikube docker-env --unset)

Deployment steps

  1. base64 encode secrets
echo -n postgresdb | base64
  1. check minikube is running
minikube status
  1. apply database templates
export LOC='/path/to/k8s/folder/'

kubectl apply -f $LOC/database/secret.yaml
kubectl apply -f $LOC/database/storage.yaml
kubectl apply -f $LOC/database/deployment.yaml
kubectl apply -f $LOC/database/service.yaml
  1. get the database local IP and place it in the application configmap
kubectl get services
nano $LOC/application/configmap.yaml
  1. apply application templates
kubectl apply -f $LOC/application/secret.yaml
kubectl apply -f $LOC/application/configmap.yaml
kubectl apply -f $LOC/application/deployment.yaml
kubectl apply -f $LOC/application/service.yaml
  1. get the app pod name and exec into container to do database migration (optional)
kubectl get pods
kubectl exec -it <pod name> -- /bin/bash
python manage.py makemigrations
python manage.py migrate
  1. port forwarding to reach the Django app from your browser
kubectl port-forward service/<service name> <local port eg. 3000>:<container port eg. 80>
  1. tear down
kubectl delete all --all