Deploying Python-Flask App With Kubernetes

Nicola Vitaly
Sep 29, 2020 · 2 min read

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 command-line tool installed on your local machine and configured to connect to your cluster. You can read more about installing in the official documentation.
  • A domain name to used by the Ingress.
  • Flask application docker image that accept connection from port .

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 :

Create 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 , which manages Pods with the .

Then we define a Service called which routes traffic to Pods with the label selector. It accepts TCP traffic on port and routes it to port , ’s port.

After that, create the Kubernetes resources using with the 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 Service is now available internally at on port . It will forward traffic to container Port 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 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 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 . Requests to host foo.example.com will be directed to the backend set up in Step 1.

The Startup

Medium's largest active publication, followed by +754K people. Follow to join our community.

Nicola Vitaly

Written by

Software Engineer. https://www.linkedin.com/in/nicola-vitaly-218399185

The Startup

Medium's largest active publication, followed by +754K people. Follow to join our community.

Nicola Vitaly

Written by

Software Engineer. https://www.linkedin.com/in/nicola-vitaly-218399185

The Startup

Medium's largest active publication, followed by +754K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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