In this blog, we are going to discuss deploying stateless containers on Google Cloud Platform(GCP) with Cloud Run service. At very first we see What is Google Cloud Platform(GCP)? Then we see what is cloud run and how to deploy containers using Cloud Run.
There are some prerequisites, you need to know for deploying containers on Google Cloud Platform with Cloud Run Service.
- Google Cloud account
- gcloud command-line tool
So, now let’s start with the introduction to Google Cloud Platform
What is Google Cloud Platform?
Google Cloud Platform is a set of servers that are accessible all over the internet along with the set of databases that run on those servers.
Cloud servers are located in the data center all over the world.
Google cloud platform enables developers to build, test, and deploy the application on highly scalable and reliable infrastructure.
It is a collection of Google’s computing resources, are available via services to the general public as a public cloud offering.
Google cloud platform provides some physical assets like computers, hard disk drives, and virtual resources, such as virtual machines (VMs), that are contained in Google’s data centers.
The different resources of GCP are categorizing as follows
- Global Resources
- Zonal Resources
- Regional Resource
Global Resources are the resources that are accessible across any region and any Zone. Under the Global resources of Google Cloud Platform Networks comes
Regional Resources are the resources that are accessible across any zone within the same region. Under the regional resources, the static external IP address comes
Zonal Resources are the resources that are accessible by resources within the same zone. These zonal resources include VM instances, their types, and disks.
There are the different services provided by a google cloud platform, But in this tutorial, we see about the “Cloud Run” Service.
- It is a fully managed platform that allows you to deploy containers in seconds.
- It is a service that allows you to work on any backend code with any programming language with any dependencies into a serverless Function.
- You don’t need to worry about the infrastructure
- It means you can deploy fully manage microservices that can scale automatically.
- Cloud Run charges only for the resources which you use and need to pay when the code is running.
- Cloud run can also deploy containers on Google Kubernetes engine with the ability to specifically configure hardware requirement for your containers.
Build and Deploy Containers
In this blog, we have gone build our own containers and deploy it to the cloud run. Follow the following steps
- Set up a google cloud project.
- Create a sample application.
- Package it into a container(Build a container).
- Push it to the GCR(google cloud registry).
- And at the last deploy it to the Cloud Run.
- In the very first step go to the Google Cloud console page and select create a project. After creating a google cloud project, make a billing account enable for your project. Once the billing account is enabled verify it Here
- In the second step create your own project, here my code is pushed to the GitHub account if you want you can clone it from here
- In the third step build a container, if you don’t know how to build a container you can refer my blog. There are two different ways to build containers
- 1. Building using cloud build as follows.
- 2. Building locally and pushing using docker.
gcloud builds submit --tag gcr.io/PROJECT-ID/Image-Name
docker build --tag gcr.io/PROJECT-ID/Image-Name .
First, go to the directory where your project is and then run a command on the terminal. At the place of PROJECT-ID specify the id of your project created previously. Or by running the below command you can also get project-id.
gcloud config get-value project
And the place of Image-Name you have gives image-name that you want.
When you use 1’st approach to build container you don’t need to push the container to GCR but if you are using the 2’nd approach you need to push content that is built locally to GCR. And for that, you just need to configure docker in the gcloud as follows
gcloud auth configure-docker
- Push to the GCR. Use the following command
docker push gcr.io/PROJECT-ID/Image-Name
- Deploy to the cloud Run. There are also two different ways.
- first using the command line interface
- second using the google cloud console
Deploy using a command line
gcloud run deploy --image gcr.io/PROJECT-ID/Image-Name --platform managed
- After executing the above command you will be prompted for the service name: press Enter to accept the default name.
- You will be prompted for the region: select the region of your choice, for example, us-central1.
- You will be prompted to allow unauthenticated invocations: respond y. Then wait a few moments until the deployment is complete. On success, the command line displays the service URL.
- Open the service URL in a web browser.
Deploy using Google Cloud Console
Through the cloud console.
- First, go to “Cloud Run”
- Click Create service to display the Create service page.
- In the service page, under the source supply the URL of an image in Container Registry, for example, gcr.io/demo1-267905/play-tutorial
- Select the Cloud Run platform.
- Select the region where you want to locate your application.
- Under Authentication,
- If you are creating a public API or website, select Allow unauthenticated
- If you want a secure service protected by authentication, select Require authentication.
- Click Create to deploy the image to Cloud Run and wait for the deployment to finish.
- Click the displayed URL link to open the unique and stable endpoint of your deployed service.
For more information, if you are creating a container using the jar artifact, then you can refer to creating executable jar artifact.