Redeploy Flask App on GKE(Google Kubernete Engine)

2023/03/12 12:02 PM posted in  Coding Notes   comments
Tags:  #Docker #Kubernetes

Some things need to take attention

Docker does not support overwrite images!!!
To update code, we need to set up new image tags.

My Code to auto-deploy Local Flask to GKE

#!/bin/bash

while getopts "t:" opt; do
  case $opt in
    t)
      arg="$OPTARG"
      ;;
    \?)
      echo "Invalid option" >&2
      exit 1
      ;;
  esac
done

echo "The tag is: $arg"    # add this line

# docker rmi khipu_flask
docker build -t khipu_flask:$arg .
docker tag khipu_flask:$arg gcr.io/flaskkhipu/khipu_flask:$arg

# gcloud container images delete gcr.io/flaskkhipu/khipu_flask --force-delete-tags
docker push gcr.io/flaskkhipu/khipu_flask:$arg
# docker kubectl expose deployment khipu-deploy  --type=LoadBalancer --port 80 --target-port 8080
kubectl set image deployment/khipu-deploy khipu-flask=gcr.io/flaskkhipu/khipu_flask:$arg

Run use

sh kubernetes_deploy.sh -t [tag_name]

Also for the Docker file and Deploy.yaml:

FROM python:3.9
WORKDIR /khipu_flask
ADD . /khipu_flask
COPY main.py /khipu_flask
RUN pip install --trusted-host pypi.python.org -r /khipu_flask/requirements.txt
RUN pip install gunicorn
EXPOSE 8080
CMD ["gunicorn", "main:app", "--config=config.py"]
apiVersion: apps/v1
kind: Deployment
metadata:
  name: khipu-deploy
spec:
  replicas: 3
  selector:
    matchLabels:
      app: app
  template:
    metadata:
      labels:
        app: app
    spec:
      containers:
      - name: khipu-flask
        image: gcr.io/flaskkhipu/khipu_flask
        ports:
        - containerPort: 8080

Other Useful Commands

  1. gcloud container images list to see available images on gcloud
  2. kubectl rollout status deployment/khipu-deploy to check the rollout status