Deploying Django, Postgres,Redis Containers To Kubernetes

Bill Prin
Bill Prin
Feb 26, 2016 · 10 min read
Image for post
Image for post

Prerequisites

This will be a multi-part tutorial, and assumes you already have some familiarity with Django and connecting it to databases and caches. If you have’t used Django before, I first recommend working through it’s excellent tutorial.

Introducing Kubernetes

Traditionally, you would probably deploy Django to some sort of virtual machine instance. The downside is that if you want to install other applications on the same machine, there might be library conflicts. It was also hard to reproduce your application state without taking a machine image, which tend to be non-portable and slow to start.

  • How does a container talk to another container, even it’s on different machine?
  • Where do I store my data and my secrets?
  • How do I scale it, manually and automatically?
  • How do I make sure my container stay healthy and are replaced on failure?
  • How do I safely deploy new containers and roll back bad ones?
  • How do I handle namespaces and authorization?

Core Kubernetes Concepts

Before we get into deploying Django, it’s good to review a few core Kubernetes concepts.

  • Pods — This is the basic unit of orchestration. A Pod has one or more container running inside it. A Pod can have an arbitrary amount of Labels, which are used by Services and Replication Controllers to match with Pods.
  • Replication Controller — A Replication Controller controls how many of a given Pod exist, based on a provided label. You might want exactly 1, or 10 of a given Pod, and a Replication Controller will monitor your Pods and start up new ones in case of failure. You can manually or automatically scale these up or down.
  • Service — A Service is how a Pod talks to another Pod, even if they’re on different Nodes. A Service provides an IP on a virtual private network (10.0.0.0/8) created by Kubernetes, and that IP will round-robin to different Pods that match a given Label. The IP for the service can be discovered using either environment variables, or the recommended Kube-DNS add-on.

Step 1: Run The App Locally

First, you’ll need to setup a Python development environment. Follow these instructions for setting it up on OS X, Linux, or Windows.

$ mkvirtualenv guestbook
$ git clone https://github.com/waprin/kubernetes_django_postgres_redis
$ cd guestbook
$ pip install -r requirements.txt
$ export NODB=1
$ python manage.py runserver
http://locahost:8000
Image for post
Image for post

Step 2: Creating the Container

To deploy our app to Kubernetes, the first step is to make a Docker image to deploy.

# https://github.com/GoogleCloudPlatform/python-docker
FROM
gcr.io/google_appengine/python
# Create a virtualenv for the application dependencies.
# If you want to use Python 3, add the -p python3.4 flag.
RUN
virtualenv /env

# Set virtualenv environment variables. This is equivalent to running
# source /env/bin/activate. This ensures the application is executed within
# the context of the virtualenv and will have access to its dependencies.
ENV
VIRTUAL_ENV /env
ENV PATH /env/bin:$PATH

# Install dependencies.
ADD
requirements.txt /app/requirements.txt
RUN pip install -r /app/requirements.txt
ENV NODB 1
CMD export DJANGO_PASSWORD=$(cat /etc/secrets/djangouserpw); gunicorn -b :$PORT mysite.wsgi
$ docker-machine create --driver virtualbox default
$ docker-machine start dev
$ eval $(docker-machine env dev)
$ docker build -t waprin/my-guestbook-app .
$ docker run -p 8080:8080 waprin/my-guestbook-app
$ docker-machine ip
http://192.168.99.100:8080

Step 3: Creating a Kubernetes Cluster

For this tutorial, we’ll use Google’s managed Kubernetes service called Container Engine. Alternatively, you can run Kubernetes yourself on Google Compute Engine or other cloud providers like Amazon Web Services. The advantage of Container Engine is that Google manages your Kubernetes master for you, significantly simplifying the operations of a Kubernetes cluster.

Image for post
Image for post
Here kube-django-prac is the name of the Project, as well as the Project ID
gcloud components update # make sure it’s up to date
gcloud config set project <project-id>
# Create the cluster with 2 nodes
gcloud container clusters create guestbook --scopes "https://www.googleapis.com/auth/userinfo.email","cloud-platform" --num-nodes 2
# Configure kubectl with the right context
gcloud container clusters get-credentials guestbook
# Only run this when you're done the tutorial
gcloud container clusters delete guestbook
$ kubectl get services$ kubectl get pods

Step 4: Deploy Our App To Kubernetes

Now that we have an application container and a Kubernetes cluster.


apiVersion: v1
kind: ReplicationController
metadata:
name:
frontend
labels:
name:
frontend
spec:
replicas:
3
template:
metadata:
labels:
name:
frontend
spec:
containers:
- name: guestbook
# Replace with your project ID or use `make template`
image: waprin/my-guestbook-app

# This setting makes nodes pull the docker image every time before
# starting the pod. This is useful when debugging, but should be turned
# off in production.
imagePullPolicy: Always
ports:
- containerPort: 8080
# replace with the image you tagged earlier
$ docker push waprin/my-guestbook-app
# Replace with your image id in frontend.yaml
image: waprin/my-guestbook-app
$ kubectl scale rc frontend --replicas=3
apiVersion: v1
kind: Service
metadata:
name:
frontend
labels:
name:
frontend
spec:
type:
LoadBalancer
ports:
- port: 80
targetPort: 8080
selector:
name:
frontend
$ kubectl create -f kubernetes_configs/frontend.yaml
$ kubectl get services
$ kubectl get servicesNAME CLUSTER_IP EXTERNAL_IP PORT(S) SELECTOR AGEfrontend 10.67.240.157 104.197.69.33 80/TCP name=frontend 1mkubernetes 10.67.240.1 <none> 443/TCP <none> 10m

Wrapping Up

Please note that while I work for Google Cloud, this is not an official Google product or company statement.

Google Cloud - Community

Google Cloud community articles and blogs

Thanks to Catia

Bill Prin

Written by

Bill Prin

Python, Data,Infrastructure, etc.

Google Cloud - Community

A collection of technical articles and blogs published or curated by Google Cloud 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 - Community

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

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store