Google Cloud: Introduction to Cloud Run

Share At:

What is a GCP Cloud Run?

  • Cloud Run is a GCP service provided which allows you to run your stateless HTTP containers without worrying about the provisioning of machines.
  • In Cloud Run, you begin from a “container image” to a fully managed application which auto-scales with requests using a single command. You only pay when a request is handled.
  • Cloud Run — Managed: By default, It allows 5 minutes timeout, can be configured up to 15 min.
  • Cloud run utilizes stateless applications inside Docker containers. The service can scale up and down automatically and will scale to 0 when not in use whereas the App Engine cannot scale down to 0.
  • In Cloud Run, you are only paying when you are processing requests, and the billing granularity is 0.1 seconds.
  • In Cloud Run, based on “time serving requests” on each instance. If your service handles multiple requests simultaneously, you do not pay for them separately. (This is a cost saver!)

How is it different From App Engine Flexible?

  • App Engine can allow up to 24hours timeout based on the various factors like auto-scaling, manual scaling.
  • In Google App Engine, There is always running at least 1 instance at all times, even if your application is not getting any requests, you will be still paying for that instance.
  • The minimum Billing time is 1 minute.
  • App Engine is always a fully managed service, with auto-scaling features.
  • The drawback is, App Engine Flexible is running on VMs, that’s why it is a bit slower than Cloud Run to deploy a new revision of your app and scale-up. Cloud Run deployments are faster as they are not running on VMs.
  • Google App Engine Flexible and Cloud Run have a lot of similarities also. They both accept container images as the deployment input, they both auto-scales, and manage your infrastructure.

Lab Details:

  1. This lab walks you through GCP Cloud Run, its advantages over App Engine.
  2. You will be creating a Hello World function in Python and then containerize your application.
  3. Finally, you will deploy your application to Cloud Run

Lab Tasks:

  1. Login into GCP Console.
  2. Starting Cloud Shell.
  3. Creating a Hello World application.
  4. Containerize and Deploy your application.
  5. Testing the API.

Creating a Simple Application using Cloud Run

  1. Click on the hamburger icon on the top left corner
  2. Click on Cloud Run.
  1. Click on the Cloud Shell icon in the top right corner as shown below.
  2. The Cloud Shell window will load at the bottom of your screen. Enter command mkdir cloudrun-whizlabs to create a directory named cloudrun-whizlabs.
  3. Enter command cd cloudrun-whizlabs to change the directory to your created directory.
  1. Enter command nano app.py to open a text editor to enter the code of your application.
  2. Enter the given code which includes parameters also, whatever you will pass as a parameter in the URL will be printed like Hello <parameter>!. If you don’t mention any parameter Hello Whizlabs will be printed. After writing the code, press CTRL + O to save then press Enter. Press CTRL + X to exit the editor.

8. Enter command nano Dockerfile to open a text editor to enter the required details of your container.

9. Enter the given code in the Dockerfile, you can ignore the comments which is for your understanding only. After writing the code, press CTRL + O to save then press Enter. Press CTRL + X to exit the editor.

10. Enter the given command to create you Container image gcloud builds submit — tag gcr.io/<project_id>/cloudrun-whizlabs Your image id is gcr.io/<project_id>/cloudrun-whizlabs , Please replace your project id which is assigned to you.

gcloud builds submit — tag gcr.io/student-00004/cloudrun-whizlabs

11. You will see a success message, which means your image is successfully created.

12. Now, you will deploy you image using the command, gcloud run deploy — image=<image_id> — platform managed — region us-central1 — allow-unauthenticated cloudrun-whizlabs.

Here cloudrun-whizlabs is your service name. Replace your Image id created in the above steps e.g. gcr.io/<project_id>/cloudrun-whizlabs

gcloud run deploy — image=gcr.io/student-00004/cloudrun-whizlabs — platform managed — region us-central1 — allow-unauthenticated cloudrun-whizlabs

13. After your previous command, a service URL will be displayed. Enter the command curl <url>. It should display Hello Whizlabs.

14. You may also open in Browser:

14. You can enter the parameter as shown below, whatever you will mention in the parameter will be printed like Hello <parameter>!.

curl https://cloudrun-whizlabs-exxudb3fpa-uc.a.run.app/?who=World

15. Refresh the page, you will see your services here.

Completion and Conclusion:

  1. In this lab, you have created an application in python returning Hello along with your parameters.
  2. You have containerized your application.
  3. You have deployed your application on Cloud Run

Happy Learning !!!


Share At:
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Back To Top

Contact Us