Live Workshop: How to Measure ROI of Developer Experience
Register now!

Accelerate Serverless Development with Cloud Run and Okteto

Google recently introduced Cloud Run, a new solution for deploying your code as containers with no infrastructure management. It is a step forward for serverless platforms, eliminating most of the architectural restrictions that Lambda functions have.

There is an official Cloud Run contract for supported containers that we can summarize in the following points:

  • Compile your container for 64-bit Linux;
  • Your container listens on port 8080 for HTTP requests;
  • Your HTTP must be ready within four minutes after receiving a request;
  • The available memory per request is 2GB;
  • Computation is stateless and scoped to a single request.

If your application meets these requirements it will work in Cloud Run. Note that there is no restriction on the programming language used by your application.

Cloud Run runs on top of Kubernetes, but you don’t need to know anything about Kubernetes to deploy your applications in Cloud Run. Let’s see how easy it is with a sample application, the Voting App.

Deploy the Voting App to Cloud Run

I assume you are already familiar with GCP (Google Cloud Platform) and Cloud Run. If you are not, check this excellent blog post to get a sense of it. For the purpose of this sample, we will just need a Project ID on which to deploy Cloud Run applications.

Get a local version of the Voting App, by executing the following commands from your terminal:

$ git clone https://github.com/okteto/vote.git
$ cd vote

You now have a functioning git repository that contains a simple python 3 application and a Dockerfile to generate the associate Docker Image. The Voting App consists of a flask app that allows you to vote for your favorite animals. Build the Docker image by executing:

$ gcloud builds submit --tag gcr.io/[project-id]/vote

After about 30 seconds you will have your Docker image built and uploaded to the Google Container Registry. Deploy your image to Cloud Run by executing the command below:

$ gcloud beta run deploy --image gcr.io/[project-id]/vote
Service name: (vote):
Deploying container to Cloud Run service [vote] in project [project-id] region [us-central1]
Allow unauthenticated invocations to new service [vote]? (y/N)?  y
✓ Deploying new service... Done.
✓ Creating Revision...
✓ Routing traffic...
Done.
Service [vote] revision [vote-00001] has been deployed and is serving traffic at https://vote-cg2bjntyuq-uc.a.run.app

After another 30 seconds or so you will be able to browse to the generated URL and see the Voting App online! Really cool, isn’t it?

Develop the Voting App with Okteto

Now it is time to do some work on the Voting App. Building and deploying the Voting App to Cloud Run takes about 1 minute for every change we want to test. If you don’t want to kill your productivity, you will need to take a different approach.

Let me introduce you to Okteto. Okteto provides instant cloud-based development environments to code and collaborate. Instead of having to build and deploy a container every time you want to see your changes in action, Okteto lets you develop your applications directly in the cloud.

The first thing we need to do is install the Okteto CLI:

MacOS/Linux

$ curl https://get.okteto.com -sSfL | sh

Windows

Download https://downloads.okteto.com/cli/okteto.exe and add it to your $PATH.

Once the CLI is installed, run the okteto context command to create your Okteto account and get an API token for your workstation.

$ okteto context

Now activate your development container by executing the okteto up command:

$ okteto up
Deployment 'vote' doesn't exist. Do you want to create a new one? [y/n]: y
  ✓  Development container activated
  ✓  Files synchronized
     Namespace: pchico83
     Name:      vote
     Forward:   8080 -> 8080

* Serving Flask app "app" (lazy loading)
* Environment: production
  WARNING: Do not use the development server in a production environment.
  Use a production WSGI server instead.
* Debug mode: on
* Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 117-959-944

After a couple of seconds the Voting App will be deployed. Browse http://0.0.0.0:8080/ or go to https://cloud.okteto.com and browse to the generated URL to see the Voting App online!

Now you are ready to see the power of Okteto in action. Open your local IDE, go to app.py and modify the getOptions function with the code below:

def getOptions():
    optionA = 'Otters'
    optionB = 'Dogs'
    return optionA, optionB

Go to the browser again and reload the page. Your changes were applied instantly. No commit, build or push required 😎!

Edit the source code as many times as you need. With Okteto you can iterate in your code instantly, instead of wasting minutes building and deploying images. This is possible because Okteto instantly synchronizes your local filesystem to your development container.

Once you are happy with your changes, deploy them to production with Cloud Run:

$ gcloud builds submit --tag gcr.io/[project-id]/vote
$ gcloud beta run deploy --image gcr.io/[project-id]/vote

Conclusion

We have shown how easy is to deploy applications in Cloud Run, and how to use Okteto to do efficient development in the cloud. And what it is even more awesome, is that you have been able to efficiently develop, build and deploy a Docker-based application without typing a single docker command, thanks to the combined powers of Cloud Run and Okteto!

Okteto provides replicable, cloud-based development environments, enabling a new level of team collaboration and integration with the rest of your cloud services. Try Okteto for free and learn how we can help you develop cloud-native applications faster than ever.

Pablo Chico de GuzmanCTO & Co-founder View all posts

How Developers Can Seamlessly Collaborate When Building Microservice Apps

Building microservices based applications is inherently challenging. Given the multitude of components involved, it is unrealistic to expect any individual...

October 10, 2023
Avatar of Arsh SharmaAvatar of Arsh SharmaArsh Sharma

Slash Your Kubernetes Costs During Development

Using Kubernetes can be quite costly, to be frank. Regardless of the cloud provider you choose for your cluster, there are expenses associated with the...

September 26, 2023
Avatar of Arsh SharmaAvatar of Arsh SharmaArsh Sharma