Bird
Raised Fist0
GCPcloud~5 mins

Cloud Run service concept in GCP - Commands & Configuration

Choose your learning style10 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.

Practice

(1/5)
1. What is the main benefit of using Cloud Run for your applications?
easy
A. You must manually scale your application.
B. You don't need to manage servers or infrastructure.
C. It only runs applications written in Python.
D. You need to configure virtual machines yourself.

Solution

  1. Step 1: Understand Cloud Run's server management

    Cloud Run automatically manages the servers, so you don't have to.
  2. Step 2: Compare other options

    Manual scaling and VM configuration are not needed with Cloud Run.
  3. Final Answer:

    You don't need to manage servers or infrastructure. -> Option B
  4. Quick Check:

    Serverless = No server management [OK]
Hint: Cloud Run means no server setup needed [OK]
Common Mistakes:
  • Thinking you must manage servers manually
  • Believing Cloud Run only supports one language
  • Confusing Cloud Run with VM services
2. Which gcloud command correctly deploys a container image to Cloud Run?
easy
A. gcloud run deploy --image gcr.io/my-project/my-image
B. gcloud compute instances create --image gcr.io/my-project/my-image
C. gcloud app deploy --image gcr.io/my-project/my-image
D. gcloud container clusters create --image gcr.io/my-project/my-image

Solution

  1. Step 1: Identify the correct service for deploying containers

    Cloud Run uses gcloud run deploy to deploy container images.
  2. Step 2: Eliminate other commands

    compute instances is for VMs, app deploy is for App Engine, and container clusters create is for Kubernetes clusters.
  3. Final Answer:

    gcloud run deploy --image gcr.io/my-project/my-image -> Option A
  4. Quick Check:

    Cloud Run deploy = gcloud run deploy [OK]
Hint: Use 'gcloud run deploy' for Cloud Run containers [OK]
Common Mistakes:
  • Using compute or app commands instead of run
  • Confusing Kubernetes cluster commands with Cloud Run
  • Missing the --image flag
3. Given this Cloud Run service configuration snippet, what happens when traffic increases?
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: hello-service
spec:
  template:
    spec:
      containers:
      - image: gcr.io/my-project/hello-image
        resources:
          limits:
            cpu: 1
            memory: 512Mi
medium
A. Cloud Run automatically creates more instances to handle the traffic.
B. The service crashes because CPU is limited to 1.
C. You must manually add more instances in the config.
D. Traffic is rejected when CPU limit is reached.

Solution

  1. Step 1: Understand Cloud Run scaling behavior

    Cloud Run automatically scales container instances based on incoming traffic.
  2. Step 2: Analyze resource limits

    CPU and memory limits set max per instance but do not prevent scaling.
  3. Final Answer:

    Cloud Run automatically creates more instances to handle the traffic. -> Option A
  4. Quick Check:

    Auto scaling = more instances on traffic rise [OK]
Hint: Cloud Run scales instances automatically with traffic [OK]
Common Mistakes:
  • Thinking CPU limit stops scaling
  • Believing manual scaling is required
  • Assuming traffic is rejected on resource limits
4. You deployed a Cloud Run service but get an error: Container failed to start. What is a likely cause?
medium
A. The container image is too large to deploy.
B. You forgot to set the CPU limit in the deployment.
C. The container image does not have a listening port configured.
D. Cloud Run requires a VM instance to be created first.

Solution

  1. Step 1: Check container requirements for Cloud Run

    Cloud Run requires the container to listen on a port for incoming requests.
  2. Step 2: Evaluate other options

    CPU limits are optional, Cloud Run is serverless (no VM setup), and image size usually causes warnings, not start failure.
  3. Final Answer:

    The container image does not have a listening port configured. -> Option C
  4. Quick Check:

    Listening port missing = start failure [OK]
Hint: Containers must listen on a port for Cloud Run [OK]
Common Mistakes:
  • Assuming VM setup is needed
  • Blaming CPU limits for start failure
  • Thinking image size causes start errors
5. You want to deploy a Cloud Run service that scales down to zero when idle but also supports up to 10 concurrent requests per instance. Which configuration is correct?
hard
A. Set --concurrency=1 and --min-instances=10 to scale down to zero.
B. Set --min-instances=1 and --concurrency=10 to scale to zero.
C. Set --max-instances=10 and --concurrency=1 to allow 10 concurrent requests.
D. Set --concurrency=10 and deploy normally; Cloud Run scales to zero automatically.

Solution

  1. Step 1: Understand concurrency and scaling in Cloud Run

    Concurrency controls how many requests one instance handles; setting it to 10 allows 10 requests per instance.
  2. Step 2: Understand scaling to zero behavior

    Cloud Run automatically scales down to zero when idle unless --min-instances is set above zero.
  3. Final Answer:

    Set --concurrency=10 and deploy normally; Cloud Run scales to zero automatically. -> Option D
  4. Quick Check:

    Concurrency 10 + no min-instances = scale to zero [OK]
Hint: No min-instances means auto scale to zero [OK]
Common Mistakes:
  • Setting min-instances prevents scaling to zero
  • Confusing max-instances with concurrency
  • Assuming concurrency 1 means 10 requests
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard