A Guide to Deploy Flask App on Google Kubernetes Engine
This is a simple guide that helps you to deploy Flask App on Google Kubernetes Engine. This guide should be relevant on any Kubernetes cluster.
Suppose that we want to deploy the following Flask app to Kubernetes:
.
├── app.py
├── config.py
└── requirements.txt
This app is a simple HTTP server that listen to specified PORT
number. We can enable/disable debug mode by setting the DEBUG_MODE
environment variable. We can run the HTTP server locally using the following command:
gunicorn app:app --config=config.py
Dockerize the Flask App
We need to to dockerize the Flask app before deploying it to Google Kubernetes Engine. Create new file called Dockerfile
and add the following configuration:
FROM python:3.6-jessieRUN apt updateWORKDIR /app
ADD requirements.txt /app/requirements.txt
RUN pip install -r /app/requirements.txtADD . /appENV PORT 8080
CMD ["gunicorn", "app:app", "--config=config.py"]
Build the docker image on Google Cloud Platform, using the following command:
gcloud builds --project YOUR_PROJECT_NAME \
submit --tag gcr.io/YOUR_PROJECT_NAME/flask-app:v1 .
Save the tag name for the next step.
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/v1beta2
kind: Deployment
metadata:
name: flask-app-tutorial
labels:
name: flask-app-tutorial
spec:
replicas: 1
selector:
matchLabels:
name: flask-app-tutorial
template:
metadata:
name: flask-app-tutorial
labels:
name: flask-app-tutorial
spec:
containers:
- name: flask-app-tutorial
image: gcr.io/kumparan-data-staging/flask-app:v1
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 flask-app-tutorial \
--type=LoadBalancer --port 80 --target-port 8080
You an get the deployed Flask app URL using the following command:
kubectl get services -l name=flask-app-tutorial
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 flask-app-tutorial --replicas=NUMBERkubectl autoscale deployment flask-app-tutorial \
--min=NUMBER --max=NUMBER \
--cpu-ratio=FLOAT --replicas=NUMBER
If you want to re-deploy, you need to build the new image then run the following command:
kubectl set image deployment/flask-app-tutorial \
flask-app-tutorial=NEW_IMAGE_TAG
Done.
Secret Environment Variables
Suppose that you want to store some secrets (Secret token, etc), you can use kubernetes secrets to handle this. First of all, you need to encode your secret token as base64. You can use the following command:
echo -n "YOUR_SECRET_TOKEN" | base64
The next step is to create yaml file called a secret.yaml
then add the following config:
apiVersion: v1
kind: Secret
metadata:
name: flask-app-secrets
type: Opaque
data:
secret_token: $YOUR_ENCODED_BASE64_TOKEN
Run the following command to create the secret:
kubectl apply -f secret.yaml
Now you can access it via environment variable in your deployment configuration like the following:
apiVersion: apps/v1beta2
kind: Deployment
metadata:
name: flask-app-tutorial
labels:
name: flask-app-tutorial
spec:
replicas: 1
selector:
matchLabels:
name: flask-app-tutorial
template:
metadata:
name: flask-app-tutorial
labels:
name: flask-app-tutorial
spec:
containers:
- name: flask-app-tutorial
image: gcr.io/kumparan-data-staging/flask-app:v1
ports:
- containerPort: 8080
resources:
requests:
memory: 256Mi
limits:
memory: 512Mi
env:
- name: DEBUG_MODE
value: "1"
- name: SECRET_TOKEN
valueFrom:
secretKeyRef:
name: flask-app-secrets
key: secret_token
Now you can access you secret token via SECRET_TOKEN
environment variable.
The source code is avalilable here: pyk/k8s-flask-tutorial.