Local Django on Kubernetes with Minikube

Minikube
  • I switched all the Replication Controllers to the new Deployments, which are pretty much the same thing with a more declarative update system
  • Persistent Volumes and Persistent Volume Claims
  • Minikube services vs External LoadBalancers
  • Port forwarding and why it’s useful
  • Hot reloading your code in development with host mounts

Minikube tips

Another big 2016 announcement was Docker for Mac, which is super great for those of us who didn’t like running VirtualBox and futzing with docker-machine. The default Minikube driver is still VirtualBox though, so if you’re using Docker for Mac, make sure you specify the xhyve driver (xhyve is the hypervisor that drives Docker for Mac):

$ minikube start --vm-driver=xhyve
$ minikube config set vm-driver=xhyve
$ eval $(minikube docker-env)
$ gcloud container cluster get-credentials mycluster # Container Engine context$ kubectl config use-context minikube # Minikube context

Persistent Volumes and Persistent Volume Claims

In the original project, I attached a GCE Persistent Disk directly to the Postgres Pod as a Volume:

volumes:
- name: postgresdata
gcePersistentDisk:
# your disk name here
pdName: pg-data
fsType: ext4
- name: secrets
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: postgres-data
spec:
accessModes:
  - ReadWriteOnce
resources:
  requests:
    storage: 5Gi
volumes:
- name: postgresdata
persistentVolumeClaim:
  claimName: postgres-data
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv0001
spec:
accessModes:
- ReadWriteOnce
capacity:
storage: 5Gi
gcePersistentDisk:
  pdName: pg-data
  fsType: ext4
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv0001
spec:
accessModes:
  - ReadWriteOnce
capacity:
  storage: 5Gi
hostPath:
  path: /data/pv0001/

Load Balancers

Our frontend that serves web traffic used a Service of type: LoadBalancer, which on Container Engine provisions a Google Compute Engine L7 Load Balancer. That provided us with an External IP when we ran :

$ kubectl get services
$ minikube service guestbook

Port forwarding

During development, it’s often useful to be able to make changes to code and immediately have them reflected in the browser. On the other hand, Kubernetes expects immutable images, so we need to do a Docker build in order to update our app. It would be nice if we have our code changes be reflected immediately and be able to use our real Postgres database and Redis cache.

$ kubectl port-forward <posgtres-pod> 5432:5432 &$ kubectl port-forward <redis-pod> 6379:6379 &

Hot reloads via host mounts

With port-forwarding, you’re still running the Django code on your local machine rather than on a Kubernetes cluster, which might not exactly be what you want, especially if you have things installed on your Docker image for your frontend that you don’t necessarily have on your workstation. Fortunately, it’s also pretty easy to have your code hot reloaded but still run in your Docker container.

gunicorn --reload -b :$PORT mysite.wsgi
# in guestbook container volumeMounts:
- name: reload
  mountPath: /app
volumes:
  - name: reload
  hostPath:
path: /Users/waprin/code/django_postgres_redis/guestbook

Getting In Touch

As always, file an issue on my Github repo or mention me on Twitter.

Google Cloud Platform - Community

A collection of technical articles published or curated by Google Cloud Platform Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Bill Prin

Written by

Bill Prin

Python, Data,Infrastructure, etc.

Google Cloud Platform - Community

A collection of technical articles published or curated by Google Cloud Platform Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.