Overview and What You Will Learn
Cloud Run runs a container as a fully managed service that scales automatically, including down to zero. Every deployment creates an immutable revision, and you can send each revision a percentage of traffic. In this lab, you will deploy a service, deploy a second revision with no traffic, test it through a private tag URL, shift 10% of live traffic to it, promote it, roll back, and set minimum and maximum instance counts.
Why This Matters in Production
A team at a company like Shopify deploys a new API version straight to 100% of traffic, then finds a bug affecting live checkouts within minutes. With Cloud Run, the same team could have sent 10% of traffic to the new revision first, limiting the damage, and rolled back in seconds by moving the percentages back.
Core Principles
A Cloud Run service is a stable URL in front of one or more revisions. Each deploy creates a new revision and leaves older ones in place, so the new version never has to replace the old one. Traffic is divided between revisions by percentage, and a revision can also get a tag, which gives it its own URL for testing before it receives any live traffic.
inventory-service}} S -->|90 percent| R1[Revision v1
previous version] S -->|10 percent| R2[Revision v2
new version] T([Tag URL
canary]) -.->|0 percent live| R2 classDef people fill:#D1FAE5,color:#1E293B classDef svc fill:#326CE5,color:#FFFFFF classDef old fill:#F1F5F9,color:#1E293B classDef new fill:#F97316,color:#FFFFFF class U,T people class S svc class R1 old class R2 new
Scaling is controlled by instance limits. With the default minimum of zero, an idle service costs nothing but the first request after a quiet period waits for a cold start. A minimum of one or more keeps instances warm, which removes that delay but means you pay for them continuously. The maximum caps how far the service can scale, which also caps cost and protects downstream databases.
Detailed Step-by-Step Practical Lab
Set the variables used throughout the lab. Project IDs are globally unique, so change the suffix if the create command fails.
export PROJECT_ID=gcp-cloudrun-lab-mumbai-2026export REGION=asia-south1- Create the project, link billing, and enable Cloud Run:
gcloud projects create $PROJECT_ID --name="Cloud Run Lab"gcloud config set project $PROJECT_IDgcloud billing projects link $PROJECT_ID \ --billing-account=012345-6789AB-CDEF01gcloud services enable run.googleapis.com- Deploy the first revision. A fixed revision suffix gives it a predictable name instead of a random one:
gcloud run deploy inventory-service \ --image=gcr.io/google-samples/hello-app:1.0 \ --region=$REGION \ --revision-suffix=v1 \ --allow-unauthenticated- Deploy a second revision.
--no-traffickeeps all users on v1, and--tag=canarygives v2 its own test URL:
gcloud run deploy inventory-service \ --image=gcr.io/google-samples/hello-app:2.0 \ --region=$REGION \ --revision-suffix=v2 \ --no-traffic \ --tag=canary \ --allow-unauthenticated- Confirm both revisions exist and see which one holds traffic:
gcloud run revisions list \ --service=inventory-service \ --region=$REGION gcloud run services describe inventory-service \ --region=$REGION \ --format="yaml(status.traffic)"The traffic output lists the canary tag URL. Open it or call it with curl to test v2 with no live users involved.
- Send 10% of live traffic to v2 and keep 90% on v1:
gcloud run services update-traffic inventory-service \ --region=$REGION \ --to-revisions=inventory-service-v1=90,inventory-service-v2=10- After v2 looks healthy in your logs and error rates, send it all traffic:
gcloud run services update-traffic inventory-service \ --region=$REGION \ --to-revisions=inventory-service-v2=100- To roll back, point all traffic at v1 again. No rebuild or redeploy is needed:
gcloud run services update-traffic inventory-service \ --region=$REGION \ --to-revisions=inventory-service-v1=100- Set scaling limits. One warm instance avoids cold starts, and a cap of 20 limits cost and downstream load:
gcloud run services update inventory-service \ --region=$REGION \ --min-instances=1 \ --max-instances=20Remember
--min-instances=1is a deliberate trade-off. You pay for the warm instance around the clock in exchange for no cold-start delay. It is right for latency-sensitive user-facing services, not for every workload.
- Clean up:
gcloud run services delete inventory-service --region=$REGION --quietgcloud projects delete $PROJECT_ID --quietProduction Best Practices & Common Pitfalls
Common MistakeDeploying a new revision to 100% of traffic with no gradual rollout, then finding a critical bug only after customers are affected. Use
--no-trafficwith a tag, test, then shift traffic in steps.
TipPin traffic to named revisions during a rollout, as in step 5. Using
--to-latesttells Cloud Run to send all current and future traffic to whichever revision is newest, which removes your ability to hold a canary at 10%.
Security
--allow-unauthenticatedmakes the service reachable by anyone on the internet. For internal services, omit it and grantroles/run.invokeronly to the service accounts or users that should call it.
- Rollback is a traffic change, not a redeploy. Older revisions stay available, so reversing a bad release takes seconds.
- Set a max instance count on every service. A traffic spike or a retry loop can otherwise scale to hundreds of instances and overwhelm a database or run up the bill.
- Use a minimum instance count only where cold starts hurt. Internal and rarely used services are usually better off scaling to zero.
- Prefer Artifact Registry for your own images. The public sample image is fine for a lab, but production images should come from a registry you control.
- Tune concurrency alongside instances. Each instance handles many requests at once by default, and lowering
--concurrencyfor CPU-heavy code spreads load across more instances.
Quick Reference & Troubleshooting Commands
| Command | Description |
|---|---|
gcloud run deploy SERVICE --image=IMAGE --no-traffic --tag=TAG |
Deploy a revision with a test URL and no live traffic |
gcloud run services update-traffic --to-revisions=REV=PCT |
Split traffic between revisions by percentage |
gcloud run revisions list --service=SERVICE |
List all revisions for a service |
gcloud run services describe SERVICE --format="yaml(status.traffic)" |
Show the current traffic split and tag URLs |
gcloud run services update --min-instances=N --max-instances=N |
Set warm and maximum instance counts |
gcloud run services logs read SERVICE --region=REGION |
Read recent logs when a revision misbehaves |