The simple Django poll app example used in the Django beginner tutorial. Code adapted graciously from here.
In order to effectively use Docker with k8s you will need a Docker registry account. A docker registry is a remote file server that contains docker images stored in your repository. For this tutorial we are using the free DockerHub account, which only allows public repositories. This means that anyone else can search and use the docker images you have uploaded. This is great for learning k8s best practices of never baking credentials into your images!
Minikube + Docker Gotcha
When you use Minikube on your local laptop remember the docker daemon that you want to interact with (e.g. for building images) runs within that minikube VM.
Make sure your laptop host docker client points to the docker daemon running inside the minikube VM. Run this command on the host
minikube docker-env
Then read the last line of the output (differs depending on your OS platform) and execute the suggested command. Now you have your docker client pointing to the docker runtime inside your minikube VM.
We assume you have a functional local minikube installation for development. So the Docker runtime is running within the minikube VM. You can point your host Docker client installation to control the Docker runtime inside the VM (instead of controlling the Docker runtime on the host machine itself).
minikube docker-env
#output:
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.39.134:2376"
export DOCKER_CERT_PATH="/home/myuseraccount/.minikube/certs"
export DOCKER_API_VERSION="1.35"
# Run this command to configure your shell:
# eval $(minikube docker-env)
#See comment above: this command is host-OS dependent.
$ eval $(minikube docker-env)
You should check that docker commands now run on the minikube VM:
docker ps
# You should see many docker containers running k8s internals - dns, etcd, controllers etc.
Commit the application code in this directory (to git). We will use the git hash to create tags for our docker images so that we have a way to get from the docker image tag to the application code version hash at a later time.
git commit -a -m "My awesome application update"
Build the docker file and tag it with a short git hash
docker build --tag kubernetes101/django_image:`git rev-parse --short HEAD` .
You can run your container using docker (only for quick debugging):
docker run -it -p 8000:8000 kubernetes101/django_image:`git rev-parse --short HEAD`
# Hit ^C thrice (number of gunicorn processes) to exit
If you want to use the container on your production k8s cluster, you will need to put this image into the docker registry. Log into your docker registry account
docker login
#Enter your docker registry credentials
docker push kubernetes101/django_image:`git rev-parse --short HEAD`
#May take a bit to upload
Note the repository,name and tag of the container (kubernetes101/django_image:git rev-parse --short HEAD
) - you will need this when telling k8s which container to run.
The Jenkins pipelines implement a simple workflows of updating the code, building a new container image, and updating the deployment (the steps discussed above) and also operational procedures like scaling the nuber of pods. Read through pipeline files (these are just bash scripts)
Static files are collected and stored in an S3 bucket in production, as discussed here.
Log into a running pod to run manage.py commands. For example:
kubectl get pods
kubectl exec django-deployment-xxxxxx bash
cd kube101
python manage.py createsuperuser
exit