GCP DevOps Project
Sprint 05
Deploy and validate our application on GKE
After configuring our cloudbuild.yaml
to build, push, and deploy the Docker image to a GKE cluster via gke.yaml
, we can automate the entire CI/CD workflow with a Cloud Build trigger. Once this file is committed to GitHub, the pipeline performs the following steps:
Step | Builder | Purpose |
---|---|---|
1 | gcr.io/cloud-builders/docker | Build the container image |
2 | gcr.io/cloud-builders/docker | Push the image to Container Registry |
3 | gcr.io/cloud-builders/gke-deploy | Deploy the image to our GKE cluster |
steps:
- name: "gcr.io/cloud-builders/docker"
args:
- "build"
- "-t"
- "gcr.io/$PROJECT_ID/gcpdevops"
- "."
- name: "gcr.io/cloud-builders/docker"
args:
- "push"
- "gcr.io/$PROJECT_ID/gcpdevops"
- name: "gcr.io/cloud-builders/gke-deploy"
args:
- run
- --filename=gke.yaml
- --image=gcr.io/$PROJECT_ID/gcpdevops
- --location=us-central1-c
- --cluster=gke-gcp-devops
- --namespace=gcp-devops-prod
Commit, Push, and Open a Pull Request
Use the following commands to stage, commit, and push your changes:
git add cloudbuild.yaml gke.yaml
git commit -m "Update deployment code"
git push origin <branch-name>
Then, navigate to GitHub, select your feature branch, and click Contribute → Open pull request. After reviewing the diff, click Create pull request and then Merge.
Monitoring the Build in Google Cloud Console
Once merged into main
, the configured Cloud Build trigger executes our pipeline. Monitor progress under Cloud Build → History:
Step #0: Downloading MarkupSafe-2.1.2...
Step #0: Successfully installed ...
Step #1: Pushing layers...
Step #0: Built 83c1e572684d
Step #0: Tagged gcr.io/kodekloud-gcp-training/gcpdevops:latest
...
Deployment Failure: Cluster Not Found
The deployment step failed because the specified GKE cluster name does not exist. Always verify that --cluster
matches your actual cluster in the correct zone or region.
Inspecting the logs reveals an IAM binding that references a non-existent cluster:
gcloud projects add-iam-policy-binding kodekloud-gcp-training \
--member=serviceAccount:[email protected] \
--role=roles/container.developer
ERROR: (gcloud.container.clusters.get-credentials) ResponseError: code=404, message=Not found: projects/kodekloud-gcp-training/zones/us-central1-c/clusters/gke-gcp-devops.
Verifying Your GKE Clusters
Check your actual cluster names and locations in the Kubernetes Engine section:
Tip
If you need to list clusters via the CLI, use:
gcloud container clusters list --zone us-central1-c
Correcting the Cluster Reference
Update the cloudbuild.yaml
to use the correct cluster name:
steps:
- name: "gcr.io/cloud-builders/docker"
args: ["build", "-t", "gcr.io/$PROJECT_ID/gcpdevops", "."]
- name: "gcr.io/cloud-builders/docker"
args: ["push", "gcr.io/$PROJECT_ID/gcpdevops"]
- name: "gcr.io/cloud-builders/gke-deploy"
args:
- run
- --filename=gke.yaml
- --image=gcr.io/$PROJECT_ID/gcpdevops
- --location=us-central1-c
- --cluster=gcp-devops-project
- --namespace=gcp-devops-prod
After committing and merging these changes, the deployment will succeed.
Links and References
Watch Video
Watch video content