Django on Google Container Engine (GKE)

After years of playing around with the various ways of running Docker in production, with varying success, it feels as though we now have a brilliant option in Google Container Engine.

This is a walkthrough on how to get Django up and running on Google Container Engine (GKE) using our sample starter project. It was written to accompany my talk at the London Django Meetup in March 2017. The slides from my talk are here.

Sign Up For Google's Cloud Platform

If you don't already have an account, you'll need to sign up for Google's Cloud Platform. Do that here.

Create a Project

Once you're logged in to the GKE console, from the project dropdown at the top of the screen select "Create Project". Give your project a name, and make a note of the slug generated for it.

Install the CLI Tools

We need to install the Google Cloud command line tool, gcloud, and the Kubernetes CLI tool, kubectl.

export CLOUD_SDK_REPO="cloud-sdk-$(lsb_release -c -s)"
echo "deb https://packages.cloud.google.com/apt $CLOUD_SDK_REPO main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
sudo apt-get update && sudo apt-get install google-cloud-sdk kubectl

Authenticate and Set up CLI Tools

Authenticate with the service using oauth:

gcloud auth application-default login

Set project and zone defaults:

gcloud config set project <your-project-slug>
gcloud config set compute/zone europe-west1-d

Deploy our Project to the Cluster

Create a persistent disk for the database:

gcloud compute disks create --size 200GB django-example-postgres-disk

Clone the starter and create Kubernetes objects:

git clone https://github.com/wildfish/kubernetes-django-starter/
kubectl create -f k8s/

Wait until all of the pods are listed as running, then grab the external IP:

kubectl get ing

Open the provided IP in your browser to view the running Django container.