Let’s start to build & deploy with Cloud Run
Follow these 3 simple steps to deploy your serverless app with Google Cloud Run
Cloud Run brings the flexibility of containers to the simplicity of serverless. Here, I mentioned the term“serverless”;
So, what’s serverless? In the normal approach of running an application, both the server hardware and server processes should be running somewhere. The difference in serverless applications compared to the normal approach is that the organization building and supporting a ‘serverless’ application is not looking after that hardware or those processes. They are outsourcing this responsibility to someone else.
Given you have some idea on serverless, let's move onto Cloud Run; If I run my code in a VM or a container, I need to choose;
- Any Programming Language
- Any Framework
- Any Libraries
And also I need to configure runtime environment, networking, operating system, and other infrastructure (All operational activities).
However, if I choose serverless we don’t have to worry about the infrastructure because, serverless provides you Invisible Infrastructure, Automatic Scaling and Pay-per-use.
I mentioned about Automatic Scaling above.
So, what is Automatic Scaling in an infrastructure scenario?
Scaling is mainly divided into two categories;
- Horizontal automatic scaling - Automatically creates more VMs(virtual machines/instances) when demand increases and automatically removes instances when demand decreases.
- Vertical scaling - Adding more RAM, CPU etc. when demand increases and remove RAM, CPU when demand decreases.
In this case, what Cloud Run does is Horizontal scaling ie. adding VMs. So, simply Cloud Run is a combination of simplicity and flexibility.
When you finish writing your app you can deploy it to Cloud Run in three simple steps.
- Define - Writing a Dockerfile. (If you don’t know how it works; you can find it here). A Dockerfile is similar to this;
2. Publish- Publish your docker container to GCP Container Registry; For this, you need to follow these steps,
i) After you have successfully built the docker container you need to copy it to your Google Cloud project. What I recommend is, you can make directories and add your simple app with your Dockerfile included in it, using Linux commands.
ii) After that, once again build your docker image.
iii) After you have successfully built it you can run it in the cloud shell. You can open another cloud shell and test the container with the curl command.
iv) Then if it’s running properly you can push it to the container registry. First of all, you need to tag it. For that follow this command;
docker tag [image-name:version] gcr.io/[project-id]/[image-name:version]
v) After that, you can simply push your docker container into the Container Registry. For that follow this command,
docker push gcr.io/[project-id]/[image-name:version]
vi) Finally, you have successfully pushed it into the Container Registry; To find your image, navigate via the console to Tools > Container Registry or visit:
http://gcr.io/[project-id]/[image-name]. You should land on a similar page:
3. Deploy- Deploy your published container to Cloud Run. To do that, follow this command in your cloud shell;
gcloud beta run deploy \ --image gcr.io/[project-id]/[image-name]
On successful deployment, the command line will display the service URL of your app.
And also Cloud Run supports you to enable Kubernetes clusters in your Cloud Run service, also known as enabling GKE (Google Kubernetes Engine). For that, you need to follow this command;
gcloud beta run deploy \ — image gcr.io/[project-id]/[image-name] — cluster=[cluster-name]
Above command only works for existing GKE clusters. If you don’t have a GKE cluster you need to create it.
Finally; In the above scenario, I mentioned something called “Kubernetes”. In simple terms, Kubernetes is a container orchestrating framework; What it does is, it manages containers at scale.
Cloud Run gives you a platform for the Dev environment. And it will take the responsibility of Operations (Ops part). Simply, it is considered a PaaS (Platform as a Service). It enables you to run stateless containers that are invocable via web requests or Cloud Pub/Sub events. It lets you choose to run your containers either fully managed with Cloud Run, or in your Google Kubernetes Engine cluster with Cloud Run on GKE.