Deploy Any Python Project to Kubernetes by Martin Heinz ( https://fediverse.org/Martin_Heinz_ )

Introduction

As your project grows, it might get to the point that it becomes too hard to handle with just single VM or some simple SaaS solution.

You can solve that by switching to more robust solution like Kubernetes.

That might however, be little too complex if you are not familiar with it’s concepts or if just never used it before.

So, to help you out - in this article - we will go over all you need to get yourself started and have your Python project deployed on cluster, including cluster setup, all the Kubernetes manifests and some extra automation to make your life easier down the roadbv!

This is a follow up to previous article(s) about Automating Every Aspect of Your Python Project, so you might want check that out before reading this one.

TL;DR: Here is my repository with full source code and docs: https://github.com/MartinHeinz/python-project-blueprint

Comfy Development Setup

To be productive in your development process, you need to have comfy local development setup. Which in this case means having simple to use Kubernetes on local, closely mirroring your real, production cluster and for that, we will use KinD:

KinD (Kubernetes-in-Docker) , as the name implies, runs Kubernetes clusters in Docker containers.

It is the official tool used by Kubernetes maintainers for Kubernetes v1.11+ conformance testing. It supports multi-node clusters as well as HA clusters. Because it runs K8s in Docker, KinD can run on Windows, Mac, and Linux. So, you can run it anywhere, you just need Docker installed.

So, let’s install KinD:

curl -Lo ./kind https://github.com/kubernetes-sigs/kind/releases/download/v0.7.0/kind-$(uname)-amd64
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   629  100   629    0     0   5376      0 --:--:-- --:--:-- --:--:--  5376
100 9136k  100 9136k    0     0  1552k      0  0:00:05  0:00:05 --:--:-- 1793k


::

~ $ chmod +x ./kind
~ $ sudo mv ./kind /usr/local/bin/kind
~ $ kind --version
kind version 0.7.0

With that, we are ready to setup our cluster. For that we will need following YAML file:

 1 kind: Cluster
 2 apiVersion: kind.x-k8s.io/v1alpha4
 3 nodes:
 4 - role: control-plane
 5   kubeadmConfigPatches:
 6   - |
 7     kind: InitConfiguration
 8     nodeRegistration:
 9       kubeletExtraArgs:
10         node-labels: "ingress-ready=true"
11         authorization-mode: "AlwaysAllow"
12   extraPortMappings:
13   - containerPort: 80
14     hostPort: 80
15     protocol: TCP
16   - containerPort: 443
17     hostPort: 443
18     protocol: TCP
19 - role: worker
20 - role: worker

This manifest describes our cluster.

It will have 3 nodes - control plane (role: control-plane) and 2 workers role: worker.

We are also giving it a few more settings and arguments to make it possible to setup ingress controller later, so that we can have HTTPS on this cluster.

All you need to know about those settings, is that extraPortMappings tells cluster to forward ports from the host to an ingress controller running on a node.

Note: All the manifests for both cluster and Python application are available in my repo here in k8s directory .