Quick and Easy GKE Cluster Deployment with Cloud Build

Quick and Easy GKE Cluster Deployment with Cloud Build


3 min read

In this project, we will walk through the steps to deploy a Flask application on a Google Kubernetes Engine (GKE) cluster using Cloud Build. We'll have separate environments for development and production, each with its own GKE namespace, and use Cloud Build triggers to automate deployments from the respective branches in a Git repository.


  • A Google Cloud Platform (GCP) project.

  • Cloud Build, Artifact Registry GKE APIs enabled.

  • Basic knowledge of Kubernetes and GKE.

  • A Git repository with main and development branches.

  • Dockerfile for the Flask application.

  • Google Cloud SDK installed and configured.

Create GKE Cluster:

Ensure that a GKE cluster is created. If not, you can create one using the following command:

gcloud container clusters create gcpdevops --zone us-central1-c --num-nodes 3

Set Up Git Repository:

Prepare a Git repository with main and development branches. Push your Flask application's code to both branches

Create Flask App:

Create a simple hello world flask `app.py`.

from flask import Flask
app = Flask(__name__)

def hello_world():
    return 'Hello, This is the GCP DevOps Project production environment!'

Dockerize Flask App:

Create Dockerfile for the flask app.

FROM python:slim-buster

COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
COPY . .
CMD [ "python3", "-m" , "flask", "run", "--host="]

Create Cloud Build Triggers:

Create two Cloud Build triggers, one for the main branch and one for the development branch. These triggers will use the respective cloudbuild.yaml files to deploy the application.

Configure Namespaces in GKE:

Create namespaces for development and production environments:

kubectl create namespace gcp-devops-dev
kubectl create namespace gcp-devops-prod

Cloud Build Configuration:

Development Environment (cloudbuild.yaml for mainbranch):

  # Build and push image to GCR
  - name: 'gcr.io/cloud-builders/docker'
    args: ['build', '-t', 'gcr.io/$PROJECT_ID/gcpdevops', '.']
  # Push image to GCR
  - name: 'gcr.io/cloud-builders/docker'
    args: ['push', 'gcr.io/$PROJECT_ID/gcpdevops']
  # Deploy image to GKE
  - name: 'gcr.io/cloud-builders/kubectl'
      - apply
      - -f
      - gke.yaml
      - 'CLOUDSDK_COMPUTE_ZONE=us-central1-c'

GKE Deployment Configuration (gke.yaml):

Create a gke.yaml file to define the Kubernetes deployment and service for the production environment. This file should be placed in the root directory of your project.

apiVersion: apps/v1
kind: Deployment
  name: gke-devops
      app: gke-devops
        app: gke-devops
      - name: gke-devops
        image: gcr.io/sunlit-charge-404811/gcpdevops:latest
        resources: {}
        - containerPort: 5000
        - name: PORT
          value: "5000"
apiVersion: v1
kind: Service
  name: gke-devops
  type: LoadBalancer
  - port: 5000
    app: gke-devops

For the development environment, you can use a similar gke.yaml file, with appropriate changes to the namespace and image name.

Testing the Deployment:

After setting up the Cloud Build triggers and pushing changes to the respective branches (main for production and development for development), Cloud Build will automatically build the Docker images and deploy the application to the GKE cluster.

To verify the deployment, you can use the following commands:

kubectl get services -n gcp-devops-dev
kubectl get services -n gcp-devops-prod

You should see the services created with external IP addresses, which you can use to access the Flask application.


By following these steps, you've successfully set up a continuous deployment pipeline using Cloud Build to deploy a Flask application on a GKE cluster. This setup ensures that your application is automatically built and deployed whenever there are changes in the respective Git branches, promoting a seamless and efficient development workflow.

For full repo: https://github.com/omarsamyi/GCP-DevOps-Project