Deploying Python-Flask App With Kubernetes

Nicola Vitaly
The Startup
Published in
2 min readSep 29, 2020

Introduction

In this guide, we’ll deploy a simple python Flask app in a Kubernetes cluster and create some Ingress Resources to route traffic to the flask’s service.

Prerequisites

Before you begin with this guide, you should have the following available to you:

  • A Kubernetes 1.16+ cluster with Kubernetes Nginx Ingress Controller installed.
  • The kubectl command-line tool installed on your local machine and configured to connect to your cluster. You can read more about installing kubectl in the official documentation.
  • A domain name to used by the Ingress.
  • Flask application docker image that accept connection from port 80.

Step 1 — Setting Up Python Flask Deployment and Service

Create a Deployment and Service for the Flask app using a docker image, we are gonna call it flask-image:

Create flask.yaml file with the following content

apiVersion: apps/v1
kind: Deployment
metadata:
name: flask-deployment
spec:
selector:
matchLabels:
app: flask
replicas: 1
template:
metadata:
labels:
app: flask
spec:
containers:
- name: flask
image: flask-image
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: flask-service
spec:
ports:
- port: 80
targetPort: 80
name: http
selector:
app: flask

First we define a Deployment, also called flask-deployment, which manages Pods with the app: flask .

Then we define a Service called flask-service which routes traffic to Pods with the app: flask label selector. It accepts TCP traffic on port 80 and routes it to port 80, flask-image’s port.

After that, create the Kubernetes resources using kubectl apply with the -f flag, specifying the file you just saved as a parameter:

kubectl apply -f flask.yaml

You should see the following output:

Outputservice/flask-service created
deployment.apps/flask-deployment created

Verify that the Service started correctly by confirming that it has a ClusterIP, the internal IP on which the Service is exposed:

kubectl get svc flask-service

You should see the following output:

OutputNAME              TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)   AGE
flask-service ClusterIP 172.21.9.6 <none> 80/TCP 60s

This indicates that the flask-service Service is now available internally at 172.21.9.6 on port 80. It will forward traffic to container Port 80 on the Pods it selects.

Step 2— Creating the Ingress Resource

In this guide, we’ll use the domain foo.example.com. You should substitute this with the domain name you own. We’ll create a simple rule to route traffic all traffic to foo.example.com

Create nginx-ingress.yaml file with the following content

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: flask-ingress
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/backend-protocol: "HTTP"
spec:
rules:
- host: foo.example.com
http:
paths:
backend:
serviceName: flask-service
servicePort: http

You can now create the Ingress using kubectl:

kubectl apply -f nginx-ingress.yaml

You’ll see the following output confirming the Ingress creation:

Outputingress.networking.k8s.io/flask-ingress created

At this point , we’ve created a nginx Ingress Resource called flask-ingress . Requests to host foo.example.com will be directed to the flask-service backend set up in Step 1.

--

--