Deploying Python Web Apps on Google Cloud Kubernetes Engine with Terraform

@kondlawork
2 min readAug 8, 2020

Deployment workflow.

Before you get started, you need to install environment binaries. In this example the first few steps illustrate how to setup and configure binaries on Mac OSX/Linux.

Stack used: Terraform, kubectl, YAML, Redis cluster, Python & Go Lang

Here what is needed before you get started.

  1. terraform executable

2. gcloud binaries

3. kubectl/minikube

on mac using homebrew

$ brew install kubectl

Install Docker

4. Setup gcloud creds

5. It is time to write your declarative IAC code to define your GCP infrastructure provider.

6. Define your Kubernetes cluster tf declaration.

7. Initialize terraform:

8. Before apply dry run terraform with “plan” option

9. Apply myplan to continue

10. Set project name and get container authentication creds.

Deploying Redis cluster to GKE

11. Redis Master config:

12. Deploy redis master to GKE

13. Create Redis Master service

Config file:

14. Redis Slave config:

14a. Create Redis Slave service

apiVersion: v1
kind: Service
metadata:
name: redis-slave
labels:
app: redis
role: slave
tier: backend
spec:
ports:
# the port that this service should serve on
- port: 6379
selector:
app: redis
role: slave
tier: backend

Deploying WebApp container to GKE

  1. Dockerfile (webApp)

2. Create docker tag, and push it to Google Cloud Registry (GCR).

3. WebApp deployment

pod declaration :

service declaration:

Testing Redis connection

Testing Python WebApp

Automate this into CI/CD pipelines or manually schedule

Check pod logs

Troubleshooting Kube service

--

--

@kondlawork

I am a software engineering manager, and cloud architect who design, build, deploy, scale ,simplify and cost optimize platform architecture.