Deploying Django, Postgres,Redis Containers To Kubernetes

Bill Prin
Bill Prin
Feb 26, 2016 · 10 min read

Prerequisites

Introducing Kubernetes

Core Kubernetes Concepts

Step 1: Run The App Locally

$ 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

Step 2: Creating the Container

# 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

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


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

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.

Thanks to Catia.

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.