Table of Contents
Django on k8s
1. Sources
1.1 Complete tutorial
- https://medium.com/google-cloud/deploying-django-postgres-redis-containers-to-kubernetes-9ee28e7a146
2. App deployment
3. Postgres DB deployment
3.1 Troubleshooting
- check yaml syntax (copy paste error): https://github.com/adrienverge/yamllint#installation
4. Django migration
5. Quick start & Operation
Follow the next sections for details of the setup process. This section details the operator guide.
5.1 Deploy from source
Checkout source
git clone git@bitbucket.org:thuydang/vfossorg_pj.git
Build db, app, or both:
make build-db make build-app # both make build
Deploy with k8s
kubectl apply -f ./kubernetes/postgres kubectl apply -f ./kubernetes/django
5.2 Initialize Django app
Django version 2.1.15
Django-cms version 3.6.0
5.2.1 Migration
Migrations can also be executed from the shell of a running container using the kubectl exec command. In order to run the migrations, we need to get the name of the running pod of interest by:
kubectl get pods
Once the pod name has been found, the migrations can be run by:
kubectl exec <pod_name> -- python /app/manage.py migrate
5.2.2 Superuser
Execute pods:
kubectl exec -it -n vfoss vfoss-org-app-7bbd4ff7db-bpxx8 -- /bin/bash ./manage.py createsuperuser user: admin password: a1d3m7
5.2.3 Serving statics files
This removes error with large search icon, etc.
./manage.py collectstatic
5.3 Update Deployment
5.3.1 Deploy latest image (not working)
If you weren’t updating the Replication Controller definition and wanted to just change the image, the safest way is a rolling-update, which will spin up 1 new container at a time and only keep going if the new containers successfully start.
kubectl rolling-update frontend \
--image=gcr.io/${GCLOUD_PROJECT}/guestbook
If, like me, you like to live dangerously, and don’t mind a little downtime you can instead edit the frontend.yaml file to point to your new image, scale the replication controller down to 0 (killing all your pods) and then scale it back up.
kubectl scale replicaset -n vfoss vfoss-org-app-7bbd4ff7db --replicas=0# kill your pods kubectl scale replicaset -n vfoss vfoss-org-app-7bbd4ff7db --replicas=1 # new image