0
0
GCPcloud~5 mins

Cloud Run service concept in GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to run your app without managing servers. Cloud Run lets you run your app in containers that start and stop automatically, so you only pay when your app is running.
When you want to deploy a web app quickly without setting up servers.
When your app needs to scale up and down automatically based on traffic.
When you want to run a containerized app without managing infrastructure.
When you want to pay only for the time your app is handling requests.
When you want to deploy microservices that respond to HTTP requests.
Config File - service.yaml
service.yaml
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: example-service
  namespace: default
spec:
  template:
    spec:
      containers:
        - image: gcr.io/cloudrun/hello
          ports:
            - containerPort: 8080

This file defines a Cloud Run service named example-service.

The image field points to the container image to run.

The service listens on port 8080 inside the container.

Cloud Run will create and manage the service based on this spec.

Commands
This command creates or updates the Cloud Run service using the configuration file. It tells Google Cloud to deploy the container as a managed service in the us-central1 region.
Terminal
gcloud run services replace service.yaml --region us-central1 --platform managed
Expected OutputExpected
Service [example-service] replaced in region [us-central1]
--region - Specifies the region where the service will run
--platform - Specifies that the service runs on fully managed Cloud Run
This command shows details about the deployed Cloud Run service, including its URL and status.
Terminal
gcloud run services describe example-service --region us-central1 --platform managed
Expected OutputExpected
apiVersion: serving.knative.dev/v1 kind: Service metadata: name: example-service namespace: default status: url: https://example-service-abcdefg-uc.a.run.app conditions: - type: Ready status: "True"
--region - Specifies the region of the service
--platform - Specifies the managed Cloud Run platform
This command sends a request to the Cloud Run service URL to check if it responds correctly.
Terminal
curl https://example-service-abcdefg-uc.a.run.app
Expected OutputExpected
Hello World!
Key Concept

If you remember nothing else from this pattern, remember: Cloud Run runs your containerized app automatically without managing servers, scaling it up and down based on traffic.

Common Mistakes
Not specifying the correct region when deploying or describing the service
Cloud Run services are regional, so commands fail or show no results if the wrong region is used
Always include the --region flag with the region where your service is deployed
Using a container image that does not listen on the port Cloud Run expects (default 8080)
Cloud Run routes traffic to the port specified in the service config; if the app listens on a different port, it won't receive requests
Ensure your container listens on port 8080 or update the service config to match your app's port
Trying to deploy without enabling the Cloud Run API in the Google Cloud project
The deployment commands will fail because the service cannot be created without the API enabled
Enable the Cloud Run API in your Google Cloud Console before deploying
Summary
Create a Cloud Run service by defining a YAML file with the container image and port.
Deploy or update the service using gcloud run services replace with the config file.
Check the service status and URL with gcloud run services describe.
Test the service by sending an HTTP request to its URL.