In this tutorial, you would learn how to setup continuous deployment for an application to Cloud Run on Google Kubernetes Engine (GKE).

Google Kubernetes Engine (GKE) provides a managed environment for deploying, managing, and scaling your containerized applications using Google infrastructure.
Cloud Run is a service by Google Cloud Platform to run your stateless HTTP containers without worrying about provisioning machines, clusters or auto-scaling.

Prerequisites

In addition to ease of deployment, scalability, server-less execution among other benefits that Cloud Run provides, Cloud Run on Gooogle Kubernetes Engine also adds the benefits of complete control over every aspect of the container orchestration, such as networking, storage, monitoring.

The workflow would be quite simple, you would integrate Cloud Build, a continuous delivery tool from Google that executes your builds on Google Cloud Platform's infrastructure.

You would need to setup a source code repository which would contain the application codes, Dockerfile and build configuration file which contains instructions for Cloud Build.
You can find my own source code used in this tutorial here.

Cloud Build trigger would be integrated with the source code repository and configured to build when code is pushed. This would then trigger the build steps in the build configuration file.

The steps in the build configuration involves cloning the source codes from the repository, building and pushing the image to Google Container Registry, and then deploying to Cloud Run on Google Kubernetes Engine.

Set up a Google Kubernetes Engine cluster for Cloud Run

To create a cluster and enable it for Cloud Run, follow the steps:

  1. Go to the Google Kubernetes Engine page in the Cloud Console:
  2. Click Create cluster to open the Create a Kubernetes cluster page.
  3. Under the Cluster basics menu, set the following values:
  • Enter the name you want for your cluster.
  • Choose either Zonal or regional for the location type.
  • Select a zone or region for the cluster.

4. Under the Node pool > default-pool > Nodes , set machine type to n1-standard-2

5.  Under the Cluster>Features menu, select the checkbox Enable Cloud Run for Anthos.

6. Accept the changes and click Create to create and provision the cluster.

Set up Cloud Build Triggers for Continuous Deployment

Cloud Build trigger automatically starts a build whenever you make any changes to your source code. We'll be using the following configuration file for Cloud Build.

#cloudbuild.yaml

steps:
  # build the container image
- name: 'gcr.io/cloud-builders/docker'
  args: [ 'build', '-t', 'gcr.io/$PROJECT_ID/demo', '.' ]
  # push the container image
- name: 'gcr.io/cloud-builders/docker'
  args: [ 'push', 'gcr.io/$PROJECT_ID/demo']
  # deploy to cloud run on GKE
- name: "gcr.io/cloud-builders/gcloud"
  args: ['run', 'deploy', 'demo-ui', '--image', 'gcr.io/$PROJECT_ID/demo', '--cluster', 'cloudrun-cluster', '--cluster-location', 'us-central1-c', '--namespace', 'default', '--platform', 'gke']

Connecting to Source Repository

Configure the trigger to build your code on any changes to the source repository using the following steps:

  1. Open the Triggers page in the Google Cloud Console.
  2. Click Connect Repository.
  3. Select the repository where you've stored your source code.
  4. Click Continue.
  5. Authenticate to your source repository.
  6. From the list of available repositories, select the desired repository, then click Connect repository.
  7. Click Done (skip creating default trigger).

Creating a build trigger

  1. Open the Triggers page in the Google Cloud Console.
  2. Click Create trigger.
  3. Enter the following trigger settings:
  • Set trigger name
  • Select connect source repository
  • Configure branch, we'll be using: ^master$
  • Set Build configuration file as Cloud Build configuration file: cloudbuild.yaml

4. Configure by providing necessary details and click Create trigger.

You click on Run trigger or push to your source code repository and monitor the build logs.

Henceforth, anytime you push to your repository, Cloud Build automatically triggers a build and a deployment to your Cloud Run service running on the Google Kubernetes Engine cluster.

If you’re looking to automate more of your container workflow on Google Cloud, the following resources would be helpful.