Skip to content

Instantly share code, notes, and snippets.

@chirag773
Created December 22, 2019 16:40
Show Gist options
  • Save chirag773/7597a470814fff4bdfad2809d3906a65 to your computer and use it in GitHub Desktop.
Save chirag773/7597a470814fff4bdfad2809d3906a65 to your computer and use it in GitHub Desktop.
flask with kubernetes

sudo apt-get update

sudo apt-get install -y apt-transport-https

Now install docker

sudo curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add

sudo chmod 777 /etc/apt/sources.list.d/

Go to /etc/apt/sources.list.d and create a file kubernetes.list and paste deb https://apt.kubernetes.io/ kubernetes-xenial main into it

cat /etc/apt/sources.list.d/kubernetes.list

sudo apt-get update

sudo apt-get install -y kubelet kubeadm kubectl kubernetes-cni

sudo swapoff -a

sudo kubeadm init

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.config $HOME/.kube/config
sudo chown $(id -u):((id -g) $HOME/ .kube/config

sudo kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

sudo kubectl get pods --all-namespaces

now lets create flask app

app structure would be like this :

.
├── app.py
├── config.py
└── requirements.txt
#add this file into app.py
from flask import Flask

import config

app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=config.PORT, debug=config.DEBUG_MODE)

Add below code into config.py

from os import environ as env
import multiprocessing

PORT = int(env.get("PORT", 8080))
DEBUG_MODE = int(env.get("DEBUG_MODE", 1))

# Gunicorn config
bind = ":" + str(PORT)
workers = multiprocessing.cpu_count() * 2 + 1
threads = 2 * multiprocessing.cpu_count()

now create Dockerfile and add below code into it

FROM python:3.6-jessie
RUN apt update
WORKDIR /app
ADD requirements.txt /app/requirements.txt
RUN pip3 install -r /app/requirements.txt
ADD . /app
ENV PORT 8080
CMD ["gunicorn", "app:app", "--config=config.py"]

now create requirements.txt file and add below code into it

Click==7.0
Flask==1.0.2
gunicorn==19.9.0
itsdangerous==1.1.0
Jinja2==2.10
MarkupSafe==1.1.0
Werkzeug==0.14.1

now we will create docker image

docker build -t <application_name>:v1 .

Now we will push this image to docker hub

1.check you are login to docker account docker login add your credentials

2.push your image to your docker docker push <application_name>:v1

Deploy to Google Kubernetes Engine

First of all, make sure you have access to the Kubernetes cluster. To deploy to Kubernetes, create new deployment configuration called app.yaml and add the following:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: kubernetsflaskapi
  labels:
    name: kubernetsflaskapi
spec:
  replicas: 1
  selector:
    matchLabels:
      name: kubernetsflaskapi
  template:
    metadata:
      name: kubernetsflaskapi
      labels:
        name: kubernetsflaskapi
    spec:
      containers:
        - name: kubernetsflaskapi
          image: <docker Image will be go here>
          ports:
            - containerPort: 8080
          resources:
            requests:
              memory: 256Mi
            limits:
              memory: 512Mi
          env:
            - name: DEBUG_MODE
              value: "1"

Deploy to Flask app to the kubernetes cluster using the following command:

kubectl apply -f app.yaml

Expose the port using the following command:

kubectl expose deployment kubernetsflaskapi --type=LoadBalancer --port 80 --target-port 8080

You will get the internal and external IP. If you want to access the Flask app outside kubernetes cluster you can use the external IP. The next step is to enable auto-scaling using the following command:

kubectl scale deployment kubernetsflaskapi --replicas=NUMBER

kubectl autoscale deployment kubernetsflaskapi \
    --min=NUMBER --max=NUMBER \
    --cpu-ratio=FLOAT --replicas=NUMBER
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment