Cloud Run
Cloud Run
Section titled “Cloud Run”Cloud Run is a fully managed serverless platform that runs stateless containers. You bring a container image; GCP handles scaling, load balancing, TLS, and infrastructure.
Key Characteristics
Section titled “Key Characteristics”- Serverless — no VMs or clusters to manage
- Container-native — any language or runtime that fits in a Docker image
- Auto-scaling — scales from 0 to thousands of instances based on traffic
- Pay-per-use — billed only when requests are being processed
Deploying a Container
Section titled “Deploying a Container”# Build and push to Google Artifact Registrygcloud builds submit --tag gcr.io/PROJECT_ID/my-app
# Deploy to Cloud Rungcloud run deploy my-app \ --image gcr.io/PROJECT_ID/my-app \ --platform managed \ --region europe-west2 \ --allow-unauthenticatedThe --allow-unauthenticated flag makes the service publicly accessible.
Service Configuration
Section titled “Service Configuration”# Set memory and CPUgcloud run deploy my-app \ --image gcr.io/PROJECT_ID/my-app \ --memory 512Mi \ --cpu 1 \ --min-instances 0 \ --max-instances 100
# Set concurrency (requests per instance)gcloud run deploy my-app \ --image gcr.io/PROJECT_ID/my-app \ --concurrency 80
# Set request timeoutgcloud run deploy my-app \ --image gcr.io/PROJECT_ID/my-app \ --timeout 300Environment Variables
Section titled “Environment Variables”gcloud run deploy my-app \ --image gcr.io/PROJECT_ID/my-app \ --set-env-vars="NODE_ENV=production,LOG_LEVEL=info"Or from a file:
gcloud run deploy my-app \ --env-vars-file=.env.yamlSecrets from Secret Manager
Section titled “Secrets from Secret Manager”# Mount a secret as an environment variablegcloud run deploy my-app \ --image gcr.io/PROJECT_ID/my-app \ --set-secrets="DB_PASSWORD=db-password:latest"
# Mount as a volumegcloud run deploy my-app \ --set-secrets="/secrets/db=db-password:latest"Traffic Splitting
Section titled “Traffic Splitting”Cloud Run supports splitting traffic between revisions — useful for canary deployments:
gcloud run services update-traffic my-app \ --to-revisions my-app-00001-abc=90,my-app-00002-def=10Cloud Run vs Cloud Functions vs App Engine
Section titled “Cloud Run vs Cloud Functions vs App Engine”| Feature | Cloud Run | Cloud Functions | App Engine |
|---|---|---|---|
| Runtime | Any container | Event-driven functions | Language-specific |
| Scaling | 0 to N instances | 0 to N instances | 0 to N instances |
| Max request timeout | 60 min | 9 min (HTTP) | 10 min |
| Stateful warm-up | Yes (min-instances) | Limited | Yes |
| Best for | APIs, web apps | Lightweight event handlers | Traditional web apps |
CI/CD with Cloud Build
Section titled “CI/CD with Cloud Build”steps: - name: 'gcr.io/cloud-builders/docker' args: ['build', '-t', 'gcr.io/$PROJECT_ID/my-app', '.']
- name: 'gcr.io/cloud-builders/docker' args: ['push', 'gcr.io/$PROJECT_ID/my-app']
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk' entrypoint: gcloud args: - 'run' - 'deploy' - 'my-app' - '--image=gcr.io/$PROJECT_ID/my-app' - '--region=europe-west2' - '--platform=managed'Useful Commands
Section titled “Useful Commands”# List servicesgcloud run services list
# Describe a service (URL, revisions, config)gcloud run services describe my-app --region=europe-west2
# View logsgcloud logging read "resource.type=cloud_run_revision AND resource.labels.service_name=my-app" --limit 50
# Delete a servicegcloud run services delete my-app --region=europe-west2