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.

Prerequisites:

  • 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__)

@app.route('/')
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

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

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):

steps:
  # 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'
    args:
      - apply
      - -f
      - gke.yaml
    env:
      - 'CLOUDSDK_COMPUTE_ZONE=us-central1-c'
      - 'CLOUDSDK_CONTAINER_CLUSTER=gcpdevops'

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
metadata:
  name: gke-devops
spec:
  selector:
    matchLabels:
      app: gke-devops
  template:
    metadata:
      labels:
        app: gke-devops
    spec:
      containers:
      - name: gke-devops
        image: gcr.io/sunlit-charge-404811/gcpdevops:latest
        resources: {}
        ports:
        - containerPort: 5000
        env:
        - name: PORT
          value: "5000"
---
apiVersion: v1
kind: Service
metadata:
  name: gke-devops
spec:
  type: LoadBalancer
  ports:
  - port: 5000
  selector:
    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.

Conclusion

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