A few months back Google announced something really cool, a little known project called Cloud Run — and from the initial announcement, Cloud Run caused some serious noise. Cloud Run comes out at a very interesting time. Just as developers are coming in mass to Kubernetes, Google came out with a new tool that makes it so whether you know the difference between a Deployment and an Ingress you can still create amazing tools that run built on the power of Kubernetes and Google’s GKE.
If you want to go further down the rabbit hole you’ll learn that under the covers Google is using another new project that is gaining steam called KNative. Knative uses the power of Kubernetes and — with some black magic — turns Kubernetes into a Serverless platform that allows you to ramp up your workload based on usage and ramp down your workload all the way to zero if there is no traffic.
With Cloud Run the big benefit is with just your Dockerized application and a few clicks you can have your application running on Kubernetes in an environment you can trust to scale up and down as your usage scales.
Now, in this article, I am going to quickly show you that you can have everything running in no time. Without further ado, let’s get started.
Cloud Run Example Project Using Pubsub and Scheduler
As a quick little project, we are going to make a system that uses Pubsub to trigger your Cloud Run workload. I felt the easiest way to trigger these Pubsub messages was to use Cloud Scheduler to trigger Pubsub on an interval that we can set (every minute in this example). What will this look like? Like this.
Our Cloud Run workload is written in NodeJS but if you look at the final code you’ll see that the language doesn’t really matter for this example because of how simple it is.
All of the code is viewable in my Github Repo.
You can't perform that action at this time. You signed in with another tab or window. You signed out in another tab or…
Setting Up A Basic Node Project
The NodeJS project is fairly simple. A tiny Express application and one POST endpoint for our PubSub subscription to push data into.
You can look deeper into each line of code in the getting started project in Github.
Build And Deploy The Project
To build the project into a deployable Dockerized application we just need to run one quick command to build and store the application within GCP.
This is it! This is the moment. The all-important moment where we publish our application to Cloud Run. Are you ready for this?
That’s it! That is how hard it is. Okay, it is crazy easy and now completely scalable. Seriously, this is basically production-ready.
Everything from now on is downhill to support our application.
Our application is running but nothing is really happening at this point because we don’t have Pubsub wired up. We will now add Pubsub into our GCP Project.
This script enables Pubsub and creates a Pubsub Topic.
The stage is set. We just need to set up the Cloud Scheduler dispatch the Pubsub Topic.
Add The Scheduler
Cloud Scheduler is a really slick tool that allows you to set up a Cron schedule that will fire off either an HTTP message or Pubsub message on a regular interval. For our project, we are having Cloud Scheduler send out a Pubsub message every minute to simulate an application dispatching Pubsub messages. Again, one line of bash script later and we are good to go.
Cloud Scheduler is now dispatching the topic every minute.
The last thing we need to do is set up a subscription to push the topic into our Cloud Run workload.
Add A Subscription
This is the last/most complicated part of the process. We need to create a service account, get the URL for our Cloud Run endpoint and then create the subscription to route the Pubsub messages to Cloud Run.
First, the URL. This can be found in the Cloud Run dashboard very easily.
Next, we need to create the subscription. This is done with a few lines of a bash script.
As soon as this is completed the Pubsub messages will be routed to Cloud Run. Let’s go see our handy work.
Watch The Logs Roll In
When we return to Cloud Run we can quickly see that something is already running.
And if we go to the logs for this Service we can see the data as it comes through.
And we are officially done. Everything is running through into our service using Pubsub via Cloud Scheduler. It’s pretty slick to see if all running.
Cloud Run is an amazing service. If you have a Google Kubernetes Engine (GKE) instance running you can include Cloud Run as an Add-On or you can use the managed Cloud Run instance as we’ve used here. Either way, you will have an amazing platform to build the biggest applications.
Jonathan Campos is an avid developer and fan of learning new things. I believe that we should always keep learning and growing and failing. I am always a supporter of the development community and always willing to help. So if you have questions or comments on this story please add them below. Connect with me on LinkedIn or Twitter and mention this story.