Deploy Flask App on GKE(Google Kubernete Engine)

2023/03/10 11:11 AM posted in  Coding Notes   comments
Tags:  #Docker #Kubernetes


For the flask app waiting for deployment, the only thing we need to focus on is the host and the port this app exposed to. For example:'', port=8080)

This command let the application run on localhost and listen on pport 8080.


After finishing the python code writing, next step we need to do is to pack it into a docker image.

First, create a Dockerfile to set up for docker.

FROM python:3.9
RUN apt update
WORKDIR /Khipu_Flask
ADD . /Khipu_Flask
RUN python -m pip install --upgrade pip
RUN pip install -r /Khipu_Flask/requirements.txt
CMD ["gunicorn", "main:app", ""]

In this, WORKDIR is the root directory we set in the docker container. Then We ADD all the file under this directory into /Khipu_Flask, which is set by us as the root dir. Then We run python command to install the requirement packages. Also, we use the gunicore to run app in

Also create a .dockerignore there is any file you do not want to pack.


Second, build the docker file under the directory where the Dockerfile is in.

docker build -t <name:tag> <path>
docker build -t myapp:1.0 /path/to/Dockerfile

-t will build a new image which name and tag specified and will take the files under into build.

Next, tag the built file with the GCR storage.

docker tag <source_image> <target_image>

Where <source_image> is the name of the image you want to tag, and <target_image> is the name and tag you want to give to the new tag.

docker tag khipu_flask:latest

This command creates a new tag under GCR. You should following the convention:<project name>

After that, push the docker image to GCR.

docker push

Just add the tag of docker image you behind the push command.


  1. docker image ls is used to show the docker image under the current docker application.
  2. docker rmi is used to remove tags. You should add <REPOSITORY:TAG>


1. Set Google Cloud Context

# Get login
gcloud auth login

# set cluster name, zone, and project name
gcloud container clusters get-credentials khipu-cluster --zone us-east1 --project flaskkhipu

# Pass to kubectl
kubectl config current-context

2. Edit .yaml File

Template file.

apiVersion: [API_VERSION]
kind: [KIND]
  name: [NAME]

[API_VERSION]: The API version of the Kubernetes resource you want to create, such as v1 or apps/v1.
[KIND]: The kind of Kubernetes resource you want to create, such as Deployment, Service, or ConfigMap.
[NAME]: The name you want to give to your Kubernetes resource.
[LABEL_KEY] and [LABEL_VALUE]: A key-value pair that you can use to label your Kubernetes resource.
[RESOURCE_SPEC]: The specification of the Kubernetes resource you want to create. This will vary depending on the kind of resource you want to create.

apiVersion: apps/v1
kind: Deployment
  name: khipu-deploy
  replicas: 3
      app: app
        app: app
      - name: khipu-flask
        - containerPort: 8080

3. Create Deployment

kubectl create -f deploy.yaml

4. Edit Expose

kubectl expose deployment khipu-deploy \
    --type=LoadBalancer --port 80 --target-port 8080

5. Get Services and Visit

➜  Khipu_Flask kubectl get services
NAME           TYPE           CLUSTER-IP    EXTERNAL-IP      PORT(S)          AGE
khipu-deploy   LoadBalancer   8080:30978/TCP   62m
kubernetes     ClusterIP     <none>           443/TCP          81m