Deploy Flask App On Google Kubernetes Engine
Introduction
Google has streamlined the way to containerize and deploy your project to a Kubernetes cluster. Today we will take a look at how we can create a brand new Flask application and deploy it to GKE.
Technology
- Visual Studio Code
- Python
- Flask
- Git/Github
- Google Cloud Service
- Docker
- GKE
Flask
Flask is a very simple, lightweight micro-framework written in the Python language. It comes with a development server as well as a debugger and it is extremely easy to start up a web server with. We will be using Flask to host our web server.
Let's create a project folder to hold all files related to your project. Create a file called app.py (This python file will contain our Flask code). Enter the following code.
from flask import Flask, render_template app = Flask(__name__) @app.route('/') def home(): return render_template("home.html")
if __name__ == "__main__": app.run(host='0.0.0.0', port=80, debug=True)
Run the following command on the VS Code terminal to install Flask.
pip install Flask
Code Explanation
In our Flask code, we are creating a route decorator that will trigger if the URL is hit. We set the URL to '/' which is the home URL. When the home URL is hit, the function which is bound to it (home()), will trigger and run a hello world command. All flask app, by default, runs on port 5000. We modified this so our server will run on port 80 instead. If we run our app.py and in a browser enter localhost:80, we should see our "Hello, world!" output. After the code is working properly push your project folder into a new GitHub repository.
Google Cloud
Log into the google cloud console. On the top menu, create a new project and then activate the cloud shell. Pull your project folder from GitHub and change into that directory.
Create a new file called Dockerfile and enter the following.
FROM python:3 WORKDIR /app ADD . /app RUN pip install -r requirements.txt EXPOSE 80
CMD ["python", "app.py"]
Create a new file called requirements.txt and enter the following.
Flask
Then we are going to dockerize our project by building a docker image. Enter the following command on gcloud.
docker build -t app-image .
We also have to tag our image.
docker tag app-image gcr.io/<project-id>/app-image:v1.0
We are now ready to push the Docker image to the container registry.
docker push gcr.io/<project-id>/app-image:v1.0
After a successful push, try pulling to see if everything works.
docker push gcr.io/<project-id>/app-image:v1.0
If there are no errors, then you are ready to deploy your app to GKE.
Code Explanation
Our Dockerfile is copying our project to a docker container as well as installing flask, exposing port 80, and running our app in that container. After that, we gave commands to simply build our docker image and push it to the cloud. You can also push your project to GitHub.
GKE
Make sure you have your docker image pulled and up-to-date before continuing forward. The first step we need to do is create a new GKE cluster. Type in this command in the gcloud shell.
gcloud container clusters create flask-cluster \ --num-nodes 1 \ --enable-basic-auth \ --issue-client-certificate \ --zone <enter your zone>
After your clusters are running, you have to create a couple of YAML files. One for deployment and one for service. Here is deployment.yaml.
apiVersion: extensions/v1beta1 kind: Deployment metadata: name: my-app spec: replicas: 1 selector: matchLabels: app: my-app template: metadata: labels: app: my-app spec: containers: - name: my-app image: "gcr.io/<project-name>/app-image:version1.0" ports: - containerPort: 80 env: - name: PORT
value: 80
Here is service.yaml
apiVersion: v1 kind: Service metadata: name: my-app spec: type: LoadBalancer selector: app: my-app ports: - port: 80 targetPort: 80
After writing these YAML files, we are going to apply them. Enter the following commands on the gcloud shell.
kubectl apply -f deployment.yaml kubectl apply -f service.yaml
After you have created the service type in the following command in the gcloud shell to get the external IP address.
kubectl get svc
Code Explanation
After creating our cluster we had to set up a deployment YAML and a service YAML. both of these files are not necessarily needed as these steps can be done within the gcloud console itself instead of the shell. Doing it in the console would generate similar deployment and service files. After our service starts we should be able to run the get svc command to see our external IP and reach it on port 80 to see our working app.
Congratulations! You have successfully deployed a Flask app to GKE. If you liked this tutorial and are interested in learning more like this please visit us at beCloudReady.