Deploy Web App built using Streamlit on Google Kubernetes Engine
Quicky and easy deployment of command line script or package to a webapp(frontend) allows backend-developer to enhance user-base, as the tools are available to the users with minimim-to-no progamming experience. Secure and reliable frontend development is itself a different game and quite a bit of technical. Often, as a computatinal biologist, I do get more excited by the logics and problem-breakdown that goes in the backend developemnt. Backend development basically applies programming script to the biological knowledege; and is often less dry.
One of the recent tools that I have been experimenting for an easy and quick frontend deployment is Streamlit. If you have experience of developing R Shiny App, you can find some similarity, but Stremlit is geared more towards python-based machine learning and data science applications.
In this tutorial, I will create a docker container for Streamlit app, then deploy it locally as well as in the Google Cloud. This app will use Streamlit for the frontend. Backend was developed in python and requires more computational resources. Thus, a typical resources provided in the Google App Engine is not the best option ( ~ although technically it works). So, instead of deploying app via Google App Engine, we will built own Kubernetes Cluster in Google Cloud (GKE), then deploy the app. Once deployed, the pulic I.P address from the kubernetes cluster will be linked with personal domain/ website.
First create an account on google cloud. Once the online account is rolled, we will install google cloud commanline tools in the local computer by creating a conda environment. We will need this for cloud deployment.
Local Deployment
First, we will run the app in the local machine. Once happy with the local deployment, we will deploy it on the cloud.
# Create a conda env
conda create -n guidemakerapp
conda activate guidemakerapp
# Create a folder to put docker file, and app script.
(guidemakerapp) π mkdir GuideMakerApp
Copy streamlit app.py and Dockerfile script to the folder. Also add other needed/supporting files specified in the Dockerfile and app.py. In our case these are: paramter_dictionary.md, introduction.md and requirements.txt.
Once all the files are in place, we can build the docker container and deploy the app locally. NOTE: Make sure docker is installed and running!!!!
(guidemakerapp) π docker build -f Dockerfile -t guidemakerapp .
(guidemakerapp) π docker image ls
(guidemakerapp) π docker run -p 8501:8501 guidemakerapp
If all the deployment goes file, then we will be able to run the app at: http://localhost:8501
Try running the app locally. The input follow the similar pattern as in the command line, only difference is that you will be uploading file then sliding/selecting input paramters via the web app. You can use the provided Carsonella_ruddii.gbk genome or use your own genome of interest. You can also learn more about the input parameter by checking Parameter Dictionary.
Once you hit SUBMIT button, you will also be able to see how the software passed the input parameters from the web app to the backend scprit.
Running:: 'guidemaker -i a35a02d6-7ac5-4eaa-b8b6-1fc30683bab6 -p NGG --guidelength 20 --strand both --lcp 10 --dist 2 --outdir 710b06d8-5daf-11eb-9d1a-0242ac110002 --log 710b06d8-5daf-11eb-9d1a-0242ac110002_log.txt --threads 2'
Now if we are happy with the local deployment, we can set up and deploy the app on google cloud using kubernetes cluster.
# install google cloud command line tool-kit.
(guidemakerapp) π conda install -c conda-forge google-cloud-sdk
# initiate google cloud and configure gcloud. Also create a new project called guidemakerapp. It will ask question regarding the configuration of google cloud. Just follow the instruction.
(guidemakerapp) π gcloud init
Deploy the Container to Google Cloud Registry.
(guidemakerapp) π docker tag guidemakerapp gcr.io/guidemakerapp/app
(guidemakerapp) π docker push gcr.io/guidemakerapp/app
If your project is not enable to register dockerfile, then you need to first enable the settings. Also, make sure that your project guidemakerapp has billing information enabled. If not container deployment to Google Clound Registry will give some error. You can enable billling information via your Google Cloud console.
(guidemakerapp) π gcloud services enable containerregistry.googleapis.com
# then run agian the deployment
(guidemakerapp) π docker push gcr.io/guidemakerapp/app
Now our container resides in Google Cloud Registry!!! Make sure by running:
(guidemakerapp) π gcloud container images list
Setting a kubernetes cluster (GKE cluster)
GuideMaker software needs some computational resources in order to predict the guides on-fly. Resources similary to the normal computer will be sufficient- such as 8 cores/threads. Additionally, one of the library - NMSLIB that GuideMaker uses run faster in processor with AVX2 setting. Thus, after checking the resources available on the standard compute machine and our need, I decided to use n1-standard-16 as this machine include Intel Broadwell CPU platform, which has AVX2 settings.
Let build the machine now!!
(guidemakerapp) π gcloud container clusters create guidemakerapp --machine-type n1-standard-16 --min-cpu-platform "Intel Broadwell" --num-nodes 1 --zone us-east4-b
Now we need to get the IP address for our compute machine so that we can communicate input and output from machine to web app. Once the IP address is created it will be reserved. We will also be using this IP address to link with personal domain.
(guidemakerapp) π gcloud compute addresses create guidemakerapp-ip --global
(guidemakerapp) π gcloud compute addresses describe guidemakerapp-ip --global
Once you have reserved IP address, you can copy that and set-it-up with personal website. For this, you just need to go to the domain site (for example- i am using https://www.godaddy.com, and have a domain called seedxpresss.com). In your domain site, just need to adda record to point to the IP address from guidemakerapp.
SSL Certificate
So far, we have app ready to deploy in the GKE or have an app that ran locally. Once in the cloud, the internet path/link between the user to the web app must be secure. For this we need to add SSL certificates so that our weblink move from HTTP to HTTPS. Here we will be using Google-managed-SSL certificate. You can read more about the SSL certificates here.
Open certificate.yaml file, which looks like:
apiVersion: networking.gke.io/v1beta1
kind: ManagedCertificate
metadata:
name: streamlit-certificate
spec:
domains:
- seedxpresss.com
Apply ssl certificate.
(guidemakerapp) π kubectl apply -f certificate.yaml
Deployment to GKE
In kubernetes, container is deployed as a pod. Pods are the smallest deployable units of computing that you can create and manage in Kubernetes. Additinally, if more than one replicas need to be deployed, then that can be managed with ReplicaSets in deployment.yaml file. More on kubernetes pod and ReplicaSets.
Open deployment.yaml file
apiVersion: apps/v1
kind: Deployment
metadata:
name: streamlitweb
labels:
app: streamlit
spec:
selector:
matchLabels:
app: streamlit
tier: web
template:
metadata:
labels:
app: streamlit
tier: web
spec:
containers:
- name: streamlit-app
image: gcr.io/guidemakerapp/app
ports:
- containerPort: 8501
Deployment:
(guidemakerapp) π kubectl apply -f deployment.yaml
How to expose application? Ingress Configuration
SO far, we have added SSL certificates and deployed our app in GKE. Now, we need to expose our application to the outside of your Kubernetes cluster, so that user can see/use the app. This can be done with Kubernetes Ingress. Basically, INgress is an API object that manages external access to the services in a cluster, typically HTTP. More on Kubernetes Ingress and option can be found at here.
Configuration for ingress can be found in ingress.yaml file.
(guidemakerapp) π kubectl apply -f ingress.yaml
It takes sometime to get the deployed app to be in action. Once all set, your webapp should be running at:
https://seedxpresss.com
NOTE:
-
Change the default
TIMEOUTvia loadbalancer in the google cloud console. Just navigate to GKE then click on the running app. The default was 30 seconds. If it takes more than 30 seconds to upload the file, then the app will lost connection with the backend. or more at here -
By the time you will be reading this, you might not be able to run app at https://seedxpresss.com- I will shut-down the app, as it is costly to run cluster for long time. How to deploy app cost effectively? Will be the next topicβ¦.