How to deploy flask App on GKE

In this tutorial, we will deploy a simple containerized flask app on Google Kubernetes Engine(GKE). We will create a new GKE cluster and use existing containerized flask app for deploying on GKE.

Pre-requisite

This tutorial uses existing docker image of flask app, refer How to containerize flask app in GCP for creating Docker image in your project.

Login to Google Cloud Console

Activate Cloud Shell to open Cloud Shell.

Create nw GKE cluster with below command


gcloud container clusters create test-cluster \
--num-nodes 1 \
--enable-basic-auth \
--issue-client-certificate \
--zone us-central1-a

Create a new file deployment.yaml


nano deployment.yaml

Write below code in deployment.yaml, replace image with image created in your project


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-id]/my_app-image:version1.0

        ports:
        - containerPort: 80
        env:
            - name: PORT
            value: "80

image gcr.io/bestshares-api/my_app-image:version1.0 is already existing image for my project, you need to follow link in prerequisite to create image for your project

Run below command to deploy resource on cluster


kubectl apply -f deployment.yaml

Create a new file service.yaml


nano service.yaml

Write below code in service.yaml


apiVersion: v1
kind: Service
metadata:
    name: my-app
spec:
    type: LoadBalancer
    selector:
    app: my-app
    ports:
    - port: 80
    targetPort: 80

Execute below command to create service


kubectl apply -f service.yaml

Run below command multiple times, until you get external ip for service


kubectl get svc

Enter external ip in browser, you should see your flask app running

Note: If you are facing any issue issue with this set up, post the error in comment section, we will try to help you on that.


Category: GCP