DEV Community

Yogendra Tamang
Yogendra Tamang

Posted on

Deploy Flask App on Kubernetes - 2

This is second part of this article, where we did following:

  1. Created Simple Flask App
  2. Built Docker Image of the App
  3. Pushed the image to DockerHub.

In this post, I will show you how you can deploy your app in kubernetes cluster. If you do not have access to multinode Cluster, you can install single node cluster, like Minikube or Docker Desktop
For my ubuntu 18.04 machine, I followed this tutorial

Lets first understand few resources in Kubernetes. For simplicity, I am making simple Book-Shop Scenario here.

Imagine you work in a book shop. Customers come and ask for the book. Its your job to fetch the book requested. Lets say a customer comes and asks for Harry Porter book, you go to the fantasy novel section, find there are 3 copies, take one of them and sell it to the customer.
Now you have 2 copies remaining. However, your shop has a rule, every book must have three copies. That means, you need bring another copy from the stock and put that along those two other books.

1. Pod

In above scenario, each book is a Pod. Like the book "Harry Porter" has its own contents, pods also run their own application. Pods are instances which run actual application.

2. Deployment

In above scenario, it was a rule that there must be three copies of every book. This rule controlled desired state of number of books in the shop. Like this, we have deployment in Kubernetes which controls how many pods should be running.

3. Service

There were 3 copies of "Harry Porter". You could have taken any one of them (It does not matter since they are same copies). Simply put, "Harry Porter" means any of those three books. Services are similar like this. It identifies group of pods as single unit.

All resources in Kubernetes are created using yaml configurations. We usually define pod inside deployment so need two yaml files. Lets create them.

  1. 1_deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: simple-flask-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: simple-flask-app
  template:
    metadata:
      labels:
        app: simple-flask-app
    spec:
      containers:
        - name: simple-flask-app
          image: yogen48/simple-flask-app
          ports:
            - containerPort: 8083

2.2_service
Lets create a service.

apiVersion: v1
kind: Service
metadata: 
  name: simple-flask-app
spec:
  selector:
    app: simple-flask-app
  type: ClusterIP
  ports:
    - name: http
      port: 9999
      targetPort: 8083

Now as per these yaml files, lets create resources in kubernetes

kubectl apply -f 1_deployment.yaml
kubectl apply -f 2_service.yaml

Now, lets check if the there are pods, deployments and services.

kubectl get all
kubectl get all

Now, lets check if we got our app running or not by port forwarding the service.

kubectl port-forard svc/simple-flask-app 8888:9999

Now lets curl

curl localhost:8888

Curl output

Top comments (0)