Lang
Blog

Cloud Run- Bringing Serverless to Containers

ByRaja Nagori & Payal Mittal
February 12th . 5 min read
Cloud Run- Bringing Serverless to Containers

It‘s been a year (almost!) since Google Cloud Platform (GCP) first announced this interesting serverless SaaS product, knows as ‘Cloud Run’. It was built with the idea to bring serverless computing to fully-managed container service.

Cloud Run allows you to create dynamic web and mobile applications that automatically scale to meet traffic demands with a pay‐per‐use billing model.

That means you no longer have to pay for the times the application is not used and need not bear any hefty infrastructure costs.

Serverless Agility for Containerized Apps

Cloud Run can be defined as the combination of two biggest trends of recent times i.e. Containerization and Serverless. Serverless means no infrastructure management, usage-based pricing, and easier auto-scaling while containerization stands for serving a project with stateless containers.

In its documentation, Google defined it in the best way possible-

Run stateless HTTP containers on a fully managed environment or in your own GKE cluster.

Cloud Run was built around an open API, Knative, which enables the portability of your workloads and lets them run anywhere inside a stateless container. It allows you to store the docker images of the container with a web server within a specified memory specification/CPU resources and concurrency.

It is responsible for creating endpoints for serving over HTTP requests, routing to the container, and monitor the running container to handle the volume requests.

Key Features of Cloud Run

Being fully serverless, Cloud Run portrays a different and better scenario of using web server infrastructure. Apart from that, Cloud Run also confers numerous other wonderful features, such as-

  • Built-In Logging
  • Fast Auto-Scaling
  • HTTP URLs
  • Supports All Languages, Libraries, and Binaries
  • Monitoring Through Stackdriver
  • Compatible with CI/CD tooling
  • Stateless HTTP-Driven Containers
  • Container Workflows and Standards
  • Custom Domains
  • Integration with Stackdriver Monitoring

Install Cloud SDK

Cloud SDK is compatible with all of Linux, Windows, and macOS. It is a set of command-line tools used to get direct access to cloud storage, BigQuery, and other Google cloud services. Let’s check out its installation steps-

For Linux User

Cloud SDK is available in package format for installation on Debian and Ubuntu systems. This package contains all gcloud commands for deployment over the command-line interface.

One can manually install the Cloud SDK by following the instructions below-

echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
sudo apt-get install apt-transport-https ca-certificates gnupg
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key --keyring /usr/share/keyrings/cloud.google.gpg add –
sudo apt-get update && sudo apt-get install google-cloud-sdk
gcloud init

Reduction of the Cloud SDK version

If one wants to go back to a specific version of Cloud SDK, where VERSION has the 123.0.0 module, run:

sudo apt-get update && sudo apt-get install google-cloud-sdk=123.0.0-0

For Windows User

  • Download gcloud sdk for windows.
  • Launch the installer and follow the prompts.
  • Be vigilant for the following to be selected:
  • Start Google Cloud SDK Shell
  • Run ‘gcloud init’

Troubleshooting Tips:

  • If the Cloud SDK fails to run after installing version 274.0.0, refer to this tracking bug for the latest workarounds.
  • If your installation is unsuccessful due to the find command not being recognized, ensure that the PATH environment variable is set to include the folder containing find. Usually, this is C:\WINDOWS\system32;.
  • If you have just uninstalled Cloud SDK, you will need to reboot your system before installing Cloud SDK again.

Initialize the Cloud SDK

The gcloud init command is quite useful to perform various common SDK configuration tasks including authorization of the SDK tools, accessing Google Cloud platform, and configuring the default SDK settings.

To initialize the SDK, run the following at a command prompt:

gcloud init

To restrict the command from initiating a web browser, use instead-

gcloud init --console-only

To authorize without a web browser and in a non-interactive way, create a service account with appropriate scopes using google cloud console and gcloud auth enable-service-account with the corresponding JSON key file.

  1. Sign in with your Google user account.

  2. In the browser, log in to your Google user account.

  3. If prompted, click ‘Allow’ to grant permission to access resources on the Google Cloud Platform.

  4. At the command prompt, select a Cloud Platform project from the list, having Owner, Editor, or Viewer permissions.

  5. In case of a single project, gcloud init will select it by default.

  6. If you have the Google Compute Engine API enabled, gcloud init allows to choose a default Compute Engine zone.

  7. It also ensures that you have completed the setup process successfully: Kudos, gcloud has now been configured!

Connection to Cloud SQL from Cloud Run

Cloud SQL is a fully-managed database service that serves the configuration, maintenance, and administration of PostgreSQL and MySQL relational databases in the cloud.

To use this service in running state in an instance of Cloud Run to a Cloud SQL, one needs to have this service in the container and loaded into the container registry.

cloud-run_1.jpg

If you don’t have the container in gcloud run then follow the below-given commands to build and push the docker image-

!/usr/bin/env bash
docker build -t image-name .
docker build -t gcr.io/$(gcloud config get-value project)/simple-example:latest .
docker push gcr.io/$(gcloud config get-value project)/simple-example
gcloud beta run deploy --image gcr.io/$(gcloud config get-value project)/simple-example:latest

Connecting to Cloud SQL

Once you are done with configuring docker container correctly, the only way to connect Cloud SQL to cloud run is to use UNIX socket with cloud run instance (follow the below format for connection string):

/cloudsql / INSTANCE_CONNECTION_NAME

These connections are serviced to be automatically encrypted without any additional configuration.

Warning: Linux-based operating systems have a maximum socket path length of 107 characters. If the total path length exceeds this length, one cannot connect to a socket from Cloud Run (fully managed).

Cloud Run (fully managed) does not support connecting to the Cloud SQL instance via TCP. The code must not attempt to access the instance using an IP address such as 127.0.0.1 or 172.17.0.1. For example:

let variable;
const createVariable = async () => {
variable = await mysql.createVariable({
user: process.env.DB_USER, // e.g. 'my-db-user'
password: process.env.DB_PASS, // e.g. 'my-db-password'
database: process.env.DB_NAME, // e.g. 'my-database'
// If connecting via unix domain socket, specify the path
socketPath: `/cloudsql/${process.env.CLOUD_SQL_CONNECTION_NAME}`,
// If connecting via TCP, enter the IP and port instead
// host: 'localhost',
// port: 3306,
//...
});
};
createVariable();

That’s it folk!! I’ll wrap up here, hoping that this whole info about using cloud run services and connecting with google cloud database will be proved helpful for you, sooner or later. Stay tuned with us.

Share:
0
+0