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.
- This lab walks you through GCP Cloud Run, its advantages over App Engine.
- You will be creating a Hello World function in Python and then containerize your application.
- Finally, you will deploy your application to Cloud Run
- Login into GCP Console.
- Starting Cloud Shell.
- Creating a Hello World application.
- Containerize and Deploy your application.
- Testing the API.
Creating a Simple Application using Cloud Run
- Click on the hamburger icon on the top left corner
- Click on Cloud Run.
- Click on the Cloud Shell icon in the top right corner as shown below.
- The Cloud Shell window will load at the bottom of your screen. Enter command mkdir cloudrun-whizlabs to create a directory named cloudrun-whizlabs.
- Enter command cd cloudrun-whizlabs to change the directory to your created directory.
- Enter command nano app.py to open a text editor to enter the code of your application.
- 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>!.
15. Refresh the page, you will see your services here.
Completion and Conclusion:
- In this lab, you have created an application in python returning Hello along with your parameters.
- You have containerized your application.
- You have deployed your application on Cloud Run