{"id":3981,"date":"2024-07-26T13:09:07","date_gmt":"2024-07-26T07:39:07","guid":{"rendered":"https:\/\/cloudwithease.com\/?p=3981"},"modified":"2024-07-26T13:10:57","modified_gmt":"2024-07-26T07:40:57","slug":"google-cloud-run","status":"publish","type":"post","link":"https:\/\/cloudwithease.com\/google-cloud-run\/","title":{"rendered":"An Overview of Google Cloud Run"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block has-background\" style=\"background-color:#daf8f9\" id=\"rank-math-toc\"><h2>Table of Contents<\/h2><nav><ul><li><a href=\"#what-is-google-cloud-run\">What is Google Cloud Run<\/a><ul><li><a href=\"#features-of-cloud-run\">Features of Cloud Run<\/a><\/li><li><a href=\"#use-cases-for-google-cloud-run\">Use Cases for Google Cloud Run<\/a><\/li><\/ul><\/li><li><a href=\"#how-to-deploy-google-cloud-run\">How to deploy Google Cloud Run?<\/a><\/li><li><a href=\"#how-google-cloud-run-works\">How Google Cloud Run works<\/a><ul><li><a href=\"#1-http-requests\">1. HTTP Requests<\/a><\/li><li><a href=\"#2-g-rpc\">2. gRPC<\/a><\/li><li><a href=\"#3-pub-sub\">3. Pub\/Sub<\/a><\/li><li><a href=\"#4-cloud-scheduler\">4. Cloud Scheduler<\/a><\/li><li><a href=\"#5-web-sockets\">5. Web Sockets<\/a><\/li><li><a href=\"#6-cloud-tasks\">6. Cloud Tasks<\/a><\/li><li><a href=\"#7-firebase\">7. Firebase<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n\n\n\n<p><a href=\"https:\/\/cloudwithease.com\/serverless-vs-microservices\/\" target=\"_blank\" rel=\"noreferrer noopener\">Serverless computing<\/a> is a buzz word and used by many organizations to get rid of management of infrastructure and for automatic scaling of business applications. Usage of such technology in computing environments help organizations to focus on core business implementations such as building applications, software and others while management aspects are taken care by the cloud service provider. Infrastructure management tasks automation help in running stateless containers in a fully managed environment.<\/p><div id=\"cloud-2053860117\" class=\"cloud-content cloud-entity-placement\"><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-1375203873676133\" crossorigin=\"anonymous\"><\/script><ins class=\"adsbygoogle\" style=\"display:block; text-align:center;\" data-ad-client=\"ca-pub-1375203873676133\" \ndata-ad-slot=\"8195412531\" \ndata-ad-layout=\"in-article\"\ndata-ad-format=\"fluid\"><\/ins>\n<script> \n(adsbygoogle = window.adsbygoogle || []).push({}); \n<\/script>\n<\/div>\n\n\n\n<p>In today\u2019s topic we will learn about Google Cloud Run, its key features, use cases, how to deploy Google cloud run and how it works.\u00a0<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-google-cloud-run\">What is Google Cloud Run<\/h2>\n\n\n\n<p>Google cloud run is a managed computing platform to run containers which are invoked with requests and events. It operates on a serverless container architecture. Due to its serverless architecture all infrastructure management tasks are abstracted so that users can focus on building good applications.<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"800\" height=\"454\" data-id=\"3988\" data-src=\"https:\/\/cloudwithease.com\/wp-content\/uploads\/2024\/07\/An-Overview-of-Google-Cloud-Run-1.jpg\" alt=\"\" class=\"wp-image-3988 lazyload\" data-srcset=\"https:\/\/cloudwithease.com\/wp-content\/uploads\/2024\/07\/An-Overview-of-Google-Cloud-Run-1.jpg 800w, https:\/\/cloudwithease.com\/wp-content\/uploads\/2024\/07\/An-Overview-of-Google-Cloud-Run-1-300x170.jpg 300w, https:\/\/cloudwithease.com\/wp-content\/uploads\/2024\/07\/An-Overview-of-Google-Cloud-Run-1-768x436.jpg 768w, https:\/\/cloudwithease.com\/wp-content\/uploads\/2024\/07\/An-Overview-of-Google-Cloud-Run-1-600x341.jpg 600w\" data-sizes=\"(max-width: 800px) 100vw, 800px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 800px; --smush-placeholder-aspect-ratio: 800\/454;\" \/><\/figure>\n<\/figure>\n\n\n\n<p>Cloud run takes up a container with a pre-built application to deploy it on a serverless environment and eliminates the requirement of managing the resources or infrastructure creation for the IT team. The logic embedded within containers should be stateless, and one should specify the combination of CPU vs memory resources along with all concurrencies. A persistent volume needs to be attached to the container when it is running to prevent deletion of any data stored within the container.&nbsp;<\/p>\n\n\n\n<p>There are specific requirements for containers which need to be added to cloud run as under:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>64-bit Linux compiled containers<\/li>\n\n\n\n<li>Ability to listen all HTTP requests over port 8080<\/li>\n\n\n\n<li>Ability to fit within a memory scale of 2GB.<\/li>\n\n\n\n<li>All container instances start from an HTTP server within the next 4 minutes of receiving a request.<\/li>\n\n\n\n<li>The applications need to be container ready\u00a0<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"features-of-cloud-run\">Features of Cloud Run<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Users can use any programming languages to build their applications\u00a0<\/li>\n\n\n\n<li>There is no infrastructure management required to deploy application and cloud run will manage all services for you<\/li>\n\n\n\n<li>It automatically scales up and down applications based on network traffic<\/li>\n\n\n\n<li>It is built on an open source project named <a href=\"https:\/\/github.com\/knative\" target=\"_blank\" rel=\"noreferrer noopener\">Knative<\/a> to enable workload portability across platforms<\/li>\n\n\n\n<li>It maps services to domain of users<\/li>\n\n\n\n<li>It provides a simple command line interface to help in quick deployment and management of services<\/li>\n\n\n\n<li>It runs its services globally and auto replicates across multiple zones<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"use-cases-for-google-cloud-run\">Use Cases for Google Cloud Run<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Webservices \u2013 <\/strong>Backoffice administration requires documentation, spreadsheets or vendor supplied web applications. Hosting containerized internal web applications on cloud run is suited for always ready to use and billed only when in use.<\/li>\n\n\n\n<li><strong>Data processing applications<\/strong> can be built using google cloud run to extract, structured data and store in Big Query tables.<\/li>\n\n\n\n<li><strong>Website web services<\/strong> can be build using mature technologies such as nginx, ExpressJS, Django to render dynamic HTMPL pages<\/li>\n\n\n\n<li><strong>REST API backend<\/strong> hosted on Google cloud run allows developers to persist data reliability for managed databases.\u00a0<\/li>\n\n\n\n<li><strong>Automation for scheduled document generation \u2013 <\/strong>using this feature jobs can be scheduled such as invoice generation and will be billed on when invoice is generated.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-deploy-google-cloud-run\">How to deploy Google Cloud Run?<\/h2>\n\n\n\n<p>To use google cloud run you need to have a google account<\/p>\n\n\n\n<p><strong>Step 1: <\/strong>Go to google cloud console and login with registered ID<\/p>\n\n\n\n<p><strong>Step 2: <\/strong>In console click on navigation menu and click on cloud run<\/p>\n\n\n\n<p><strong>Step 3:<\/strong> Click on create a service<\/p>\n\n\n\n<p><strong>Step 4:<\/strong> The cloud run will be enabled automatically when create a service option is clicked<\/p>\n\n\n\n<p><strong>Step 5: <\/strong>In create service provide following details and click on next to continue<\/p>\n\n\n\n<p><em>Service name: K21-cloudrun-service<br>Deployment platform: cloud run<\/em><br><em>Region: us-central1 (choose region closed to your location)<\/em><\/p>\n\n\n\n<p><strong>Step 6: <\/strong>in configure service first revision enter following details and click next<br>Choose deploy one revision from existing container image<br>For the container image use <em>us-docker.pkg.dev\/cloudrun\/container\/hello<\/em><\/p>\n\n\n\n<p><strong>Step 7: <\/strong>Now configure how this service will be triggered from Choose option Allow all traffic and Allow unauthenticated invocations in the respective fields. Click on Create to deploy the image to Cloud Run and let deployment finish.<\/p>\n\n\n\n<p><strong>Step 8: <\/strong>Deployment will be completed in sometime<\/p>\n\n\n\n<p><strong>Step 9:<\/strong> To verify deployment status copy URL generated in cloud run details page<\/p>\n\n\n\n<p><strong>Step 10: <\/strong>Copy URL in browser and you will see successfully deployed container image on cloud run<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-2 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"800\" height=\"454\" data-id=\"3989\" data-src=\"https:\/\/cloudwithease.com\/wp-content\/uploads\/2024\/07\/An-Overview-of-Google-Cloud-Run-2.jpg\" alt=\"\" class=\"wp-image-3989 lazyload\" data-srcset=\"https:\/\/cloudwithease.com\/wp-content\/uploads\/2024\/07\/An-Overview-of-Google-Cloud-Run-2.jpg 800w, https:\/\/cloudwithease.com\/wp-content\/uploads\/2024\/07\/An-Overview-of-Google-Cloud-Run-2-300x170.jpg 300w, https:\/\/cloudwithease.com\/wp-content\/uploads\/2024\/07\/An-Overview-of-Google-Cloud-Run-2-768x436.jpg 768w, https:\/\/cloudwithease.com\/wp-content\/uploads\/2024\/07\/An-Overview-of-Google-Cloud-Run-2-600x341.jpg 600w\" data-sizes=\"(max-width: 800px) 100vw, 800px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 800px; --smush-placeholder-aspect-ratio: 800\/454;\" \/><\/figure>\n<\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-google-cloud-run-works\">How Google Cloud Run works<\/h2>\n\n\n\n<p>Cloud Run service can be triggered\/invoked by:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-http-requests\">1. HTTP Requests<\/h3>\n\n\n\n<p>You can directly make HTTP requests to your Cloud Run service.<\/p>\n\n\n\n<p>Example using curl:<\/p>\n\n\n\n<pre class=\"wp-block-code has-background\" style=\"background-color:#daf8f9\"><code>curl -X POST https:\/\/YOUR_CLOUD_RUN_SERVICE_URL -H \"Authorization: Bearer $(gcloud auth print-identity-token)\" -H \"Content-Type: application\/json\" -d '{\"key\":\"value\"}'<\/code><\/pre>\n\n\n\n<p>Example using Python with requests:<\/p>\n\n\n\n<pre class=\"wp-block-code has-background\" style=\"background-color:#daf8f9\"><code>import requests\n\nfrom google.auth import default\n\nfrom google.auth.transport.requests import Request\n\n# Get default credentials and project ID\n\ncredentials, project = default()\n\n# Get an identity token for the Cloud Run service\n\nauth_req = Request()\n\ncredentials.refresh(auth_req)\n\ntoken = credentials.id_token\n\n# Cloud Run service URL\n\nservice_url = \"https:\/\/YOUR_CLOUD_RUN_SERVICE_URL\"\n\n# Make a request to the service\n\nresponse = requests.post(\n\n\u00a0\u00a0\u00a0 service_url,\n\n\u00a0\u00a0\u00a0 headers={\"Authorization\": f\"Bearer {token}\"},\n\n\u00a0\u00a0\u00a0 json={\"key\": \"value\"}\n\n)\n\nprint(response.json())<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-g-rpc\">2. gRPC<\/h3>\n\n\n\n<p>Cloud Run supports gRPC, a high-performance RPC framework.<\/p>\n\n\n\n<p>Example using gRPC in Python:<\/p>\n\n\n\n<pre class=\"wp-block-code has-background\" style=\"background-color:#daf8f9\"><code>Define your service in a .proto file.\n\nGenerate the gRPC client and server code using protoc.\n\nImplement the server and deploy it to Cloud Run.\n\nWrite a gRPC client to invoke the service.<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-pub-sub\">3. Pub\/Sub<\/h3>\n\n\n\n<p><a href=\"https:\/\/cloudwithease.com\/google-cloud-pub-sub\/\" target=\"_blank\" rel=\"noreferrer noopener\">Google Cloud Pub\/Sub<\/a> can trigger Cloud Run services asynchronously.<\/p>\n\n\n\n<p>Create a Pub\/Sub topic:<\/p>\n\n\n\n<pre class=\"wp-block-code has-background\" style=\"background-color:#daf8f9\"><code>gcloud pubsub topics create YOUR_TOPIC\n\nDeploy your Cloud Run service with Pub\/Sub trigger:\n\ngcloud run deploy --image gcr.io\/YOUR_PROJECT\/YOUR_IMAGE --platform managed --trigger-topic YOUR_TOPIC\n\nPublish a message to the topic:\n\nfrom google.cloud import pubsub_v1\n\npublisher = pubsub_v1.PublisherClient()\n\ntopic_path = publisher.topic_path('YOUR_PROJECT', 'YOUR_TOPIC')\n\ndata = 'Hello, World!'\n\npublisher.publish(topic_path, data.encode('utf-8'))<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-cloud-scheduler\">4. Cloud Scheduler<\/h3>\n\n\n\n<p>Cloud Scheduler can be used to schedule HTTP calls to your Cloud Run service.<\/p>\n\n\n\n<p>Create a Cloud Scheduler job:<\/p>\n\n\n\n<pre class=\"wp-block-code has-background\" style=\"background-color:#daf8f9\"><code>gcloud scheduler jobs create http YOUR_JOB_NAME --schedule \"*\/5 * * * *\" --uri \"https:\/\/YOUR_CLOUD_RUN_SERVICE_URL\" --http-method POST --oidc-service-account-email \"YOUR_SERVICE_ACCOUNT\"<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-web-sockets\">5. Web Sockets<\/h3>\n\n\n\n<p>Cloud Run currently does not support<a href=\"https:\/\/ipwithease.com\/web-socket-vs-http\/\" target=\"_blank\" rel=\"noreferrer noopener\"> <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-accent-color\">WebSockets<\/mark><\/a> directly. You would need to use a proxy like Cloud Run for Anthos or use another service like Firebase Realtime Database or Firestore for real-time capabilities.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-cloud-tasks\">6. Cloud Tasks<\/h3>\n\n\n\n<p>Google Cloud Tasks can be used to enqueue HTTP tasks to your Cloud Run service.<\/p>\n\n\n\n<p>Create a task queue:<\/p>\n\n\n\n<pre class=\"wp-block-code has-background\" style=\"background-color:#daf8f9\"><code><em>gcloud tasks queues create YOUR_QUEUE<\/em><\/code><\/pre>\n\n\n\n<p>Create a task:<\/p>\n\n\n\n<pre class=\"wp-block-code has-background\" style=\"background-color:#daf8f9\"><code>from google.cloud import tasks_v2\n\nfrom google.protobuf import timestamp_pb2\n\nimport datetime\n\nclient = tasks_v2.CloudTasksClient()\n\nproject = 'YOUR_PROJECT'\n\nqueue = 'YOUR_QUEUE'\n\nlocation = 'YOUR_LOCATION'\n\nurl = 'https:\/\/YOUR_CLOUD_RUN_SERVICE_URL'\n\npayload = 'Hello, World!'\n\nparent = client.queue_path(project, location, queue)\n\ntask = {\n\n\u00a0\u00a0\u00a0 'http_request': {\u00a0\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 'http_method': tasks_v2.HttpMethod.POST,\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 'url': url,\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 'headers': {\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 'Content-Type': 'application\/json'\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 },\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 'body': payload.encode()\n\n\u00a0\u00a0\u00a0 }\n\n}\n\nresponse = client.create_task(parent, task)\n\nprint('Created task {}'.format(response.name))<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7-firebase\">7. Firebase<\/h3>\n\n\n\n<p>Firebase can be used in conjunction with Cloud Run for various use cases.<\/p>\n\n\n\n<p>Example using Firebase Functions to invoke Cloud Run:<\/p>\n\n\n\n<pre class=\"wp-block-code has-background\" style=\"background-color:#daf8f9\"><code>const functions = require('firebase-functions');\n\nconst fetch = require('node-fetch');\n\nexports.invokeCloudRun = functions.https.onRequest(async (req, res) => {\n\n\u00a0\u00a0\u00a0 const response = await fetch('https:\/\/YOUR_CLOUD_RUN_SERVICE_URL', {\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 method: 'POST',\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 headers: {\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 'Authorization': `Bearer ${await getAccessToken()}`,\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 'Content-Type': 'application\/json'\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 },\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 body: JSON.stringify({ key: 'value' })\n\n\u00a0\u00a0\u00a0 });\n\n\u00a0\u00a0\u00a0 const data = await response.json();\n\n\u00a0\u00a0\u00a0 res.send(data);\n\n});\n\nasync function getAccessToken() {\n\n\u00a0\u00a0\u00a0 const { GoogleAuth } = require('google-auth-library');\n\n\u00a0\u00a0\u00a0 const auth = new GoogleAuth({\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 scopes: 'https:\/\/www.googleapis.com\/auth\/cloud-platform'\n\n\u00a0\u00a0\u00a0 });\n\n\u00a0\u00a0\u00a0 const client = await auth.getClient();\n\n\u00a0\u00a0\u00a0 const token = await client.getAccessToken();\n\n\u00a0\u00a0\u00a0 return token;\n\n}<\/code><\/pre>\n\n\n\n<p>These methods cover a wide range of scenarios for invoking a Cloud Run service. Each method has its own use cases and can be chosen based on the requirements of your application.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Serverless computing is a buzz word and used by many organizations to get rid of management of infrastructure and for &#8230; <a title=\"An Overview of Google Cloud Run\" class=\"read-more\" href=\"https:\/\/cloudwithease.com\/google-cloud-run\/\" aria-label=\"Read more about An Overview of Google Cloud Run\">Read more<\/a><\/p>\n","protected":false},"author":3,"featured_media":3988,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pmpro_default_level":"","footnotes":""},"categories":[20],"tags":[57],"class_list":["post-3981","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-google-cloud-gcp","tag-gcp","pmpro-has-access"],"_links":{"self":[{"href":"https:\/\/cloudwithease.com\/wp-json\/wp\/v2\/posts\/3981","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cloudwithease.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cloudwithease.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cloudwithease.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/cloudwithease.com\/wp-json\/wp\/v2\/comments?post=3981"}],"version-history":[{"count":8,"href":"https:\/\/cloudwithease.com\/wp-json\/wp\/v2\/posts\/3981\/revisions"}],"predecessor-version":[{"id":3991,"href":"https:\/\/cloudwithease.com\/wp-json\/wp\/v2\/posts\/3981\/revisions\/3991"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cloudwithease.com\/wp-json\/wp\/v2\/media\/3988"}],"wp:attachment":[{"href":"https:\/\/cloudwithease.com\/wp-json\/wp\/v2\/media?parent=3981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cloudwithease.com\/wp-json\/wp\/v2\/categories?post=3981"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cloudwithease.com\/wp-json\/wp\/v2\/tags?post=3981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}