DevOps

Cloud Deploy with Cloud Run

Google Cloud Deploy is a service to continuously deploy to Google Cloud Application runtimes. It has supported Google Kubernetes Engine(GKE) so far, and now is starting to support Cloud Run. This post is about a quick trial of this new and exciting support in Cloud Deploy. 

It may be simpler to explore the entire sample which is available in my github repo here – https://github.com/bijukunjummen/clouddeploy-cloudrun-sample 

End to end Flow

The sample attempts to do the following:

A Cloud Build based build first builds an image. This image is handed over to Cloud Deploy which deploys to Cloud Run. A “dev” and “prod” target is simulated by the Cloud Run applications having names prefixed with the environment name.

Building an image

There are way too many ways to build a container image, my personal favorite is  the excellent Google jib tool which requires a simple plugin to be in place to create AND publish a container image. Once an image is created, the next task is to get the tagged image name for use with say a Kubernetes deployment manifest. 

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-skaffold-gke-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: hello-skaffold-gke
  template:
    metadata:
      labels:
        app: hello-skaffold-gke
    spec:
      containers:
        - name: hello-skaffold-gke
          image: published-image-name-here
          ports:
            - containerPort: 8080

Skaffold does a great job of orchestrating these two steps, creating an image and rendering the application runtime manifests with the image locations. Since the deployment is to a Cloud Run environment, the manifest looks something like this:

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: cloudrun-sample
spec:
  template:
    spec:
      containers:
      - image: clouddeploy-cloudrun-app-image

Now, manifest for each target environment may look a little different, so for eg in my case the application name targeted towards dev environment has a “dev-” prefix and for prod environment has a “prod-” prefix. This is where another tool called Kustomize fits in. Kustomize is fairly intuitive, it expresses the variations for each environment as a patch file, so for eg, in my case where I want to prefix the name of the application in the dev environment with a “dev-“, the Kustomize configuration looks something like this:

namePrefix: dev-
resources:
- ../../base

So now, we have 3 tools:

  1. For building an image – Google Jib
  2. Generating the manifests based on environment – Kustomize
  3. Rending the image name in the manifests – Skaffold

Skaffold does a great job of wiring all the tools together, and looks something like this for my example:

apiVersion: skaffold/v3alpha1
kind: Config
metadata:
  name: clouddeploy-cloudrun-skaffold
manifests:
  kustomize:
    paths:
    - manifests/base
build:
  artifacts:
  - image: clouddeploy-cloudrun-app-image
    jib: {}
profiles:
- name: dev
  manifests:
    kustomize:
      paths:
        - manifests/overlays/dev
- name: prod
  manifests:
    kustomize:
      paths:
        - manifests/overlays/prod
deploy:
  cloudrun:
    region: us-west1-a

Deploying the Image

In the Google Cloud Environment, Cloud Build is used for calling Skaffold and building the image, I have a
cloudbuild.yaml file available with my sample, which shows how skaffold is invoked and the image built.

Let’s come to the topic of the post, about deploying this image to Cloud Run using Cloud Deploy. Cloud Deploy uses a configuration file to describe where the image needs to be deployed, which is Cloud Run in this instance and how the deployment needs to be promoted across environments. The environments are referred to as “targets” and look like this in my configuration:

apiVersion: deploy.cloud.google.com/v1
---
kind: Target
metadata:
  name: dev
description: Cloud Run Dev environment
run:
  location: projects/sampleproject/locations/us-west1

---
apiVersion: deploy.cloud.google.com/v1
kind: Target
metadata:
  name: prod
description: Cloud Run Prod Environment
requireApproval: true
run:
  location: projects/sampleproject/locations/us-west1

They point to the project and region for the Cloud Run service.

Next is the configuration to describe how the pipeline will take the application through the targets:

apiVersion: deploy.cloud.google.com/v1
kind: DeliveryPipeline
metadata:
  name: clouddeploy-cloudrun-sample
description: Delivery Pipeline for a sample java app
serialPipeline:
  stages:
    - targetId: dev
      profiles: [dev]
    - targetId: prod
      profiles: [prod]

This simply shows that application will be first deployed to the “dev” target and then promoted to the “prod” target after approval.

The “profiles” in the each of the stages show the profile that will be activated in skaffold, which simply determines which overlay of kustomize will be used to create the manifest.

That covers the entire Cloud Deploy configuration. The next step once the configuration file is ready is to create the deployment pipeline, which is done using a command which looks like this:

gcloud deploy apply --file=clouddeploy.yaml --region=us-west1

and registers the pipeline with Cloud Deploy service.

So just to quickly recap, I now have the image built by Cloud Build, the manifests generated using skaffold, kustomize, and a pipeline registered with Cloud Deploy, the next step is to trigger the pipeline for the image and the artifacts, which is done through another command, which is hooked up to Cloud Build:

gcloud deploy releases create release-$SHORT_SHA --delivery-pipeline clouddeploy-cloudrun-sample --region us-west1 --build-artifacts artifacts.json

This would trigger the deploy to the different Cloud Run targets – “dev” in my case to start with:

Once deployed, I have a shiny Cloud Run app all ready to accept requests!

This can now be promoted to my “prod” target with a manual approval process:

Conclusion

Cloud Deploy’s support for Cloud Run works great, it takes a familiar tooling with Skaffold typically meant for Kubernetes manifests and uses it cleverly for Cloud Run deployment flows. I look forward to more capabilities in Cloud Deploy with support for Blue/Green, Canary deployment models.

The entire working code is available in  my github repo here –  https://github.com/bijukunjummen/clouddeploy-cloudrun-sample 

Published on Java Code Geeks with permission by Biju Kunjummen, partner at our JCG program. See the original article here: Cloud Deploy with Cloud Run

Opinions expressed by Java Code Geeks contributors are their own.

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button