Deploy Flask App on Kubernetes - 2
Tags: python, kubernetes, docker
Published: 2020-08-08 20:48:42

This is second part of [this article](https://dev.to/yogendra_tamang/deploy-flask-app-on-kubernetes-1-39k0), 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](https://kubernetes.io/docs/setup/learning-environment/minikube/) or [Docker Desktop](https://www.docker.com/products/docker-desktop)
For my ubuntu 18.04 machine, I followed [this tutorial](https://ubuntu.com/kubernetes/install#single-node)

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`
```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. 
```yaml
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](https://dev-to-uploads.s3.amazonaws.com/i/0ht88s0pjqcwu42hr58o.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/vbfiuwj0sei7jmoj0c5l.png)