Deploying Containerized Applications with Cloud Run
Master serverless container deployment with Google Cloud Run. Learn how to build, deploy, and manage containerized applications with automatic scaling and zero infrastructure management.
Deploying Applications on Google Cloud Run
Google Cloud Run is a fully managed compute platform that automatically scales your stateless containers. This guide covers everything you need to know to deploy and manage applications on Cloud Run.
Architecture Overview
Key Features
| Feature | Description | |---------|-------------| | Automatic Scaling | Scales to zero when not in use | | Request-based Billing | Pay only for actual usage | | Container Support | Run any container image | | HTTPS Endpoints | Automatic SSL/TLS certificates | | Custom Domains | Map your own domain names |
Getting Started
1. Building a Container
First, create a simple application and containerize it:
# Dockerfile FROM node:18-slim WORKDIR /app COPY package*.json ./ RUN npm install COPY . . ENV PORT=8080 EXPOSE 8080 CMD ["npm", "start"]
Example Node.js application:
// app.js const express = require('express'); const app = express(); const port = process.env.PORT || 8080; app.get('/', (req, res) => { res.send('Hello from Cloud Run!'); }); app.listen(port, () => { console.log(`Server running on port ${port}`); });
2. Building and Pushing the Image
# Build the container image gcloud builds submit --tag gcr.io/PROJECT_ID/my-app # Deploy to Cloud Run gcloud run deploy my-service \ --image gcr.io/PROJECT_ID/my-app \ --platform managed \ --region us-central1 \ --allow-unauthenticated
Configuration Options
Service Configuration
# service.yaml apiVersion: serving.knative.dev/v1 kind: Service metadata: name: my-service spec: template: spec: containers: - image: gcr.io/PROJECT_ID/my-app env: - name: NODE_ENV value: "production" resources: limits: cpu: "1" memory: "256Mi" ports: - containerPort: 8080
Environment Variables and Secrets
# Set environment variables gcloud run services update my-service \ --set-env-vars KEY1=VALUE1,KEY2=VALUE2 # Use secrets from Secret Manager gcloud run services update my-service \ --set-secrets="MY_SECRET=my-secret:latest"
Advanced Features
1. Custom Domain Mapping
# Map custom domain gcloud beta run domain-mappings create \ --service my-service \ --domain www.example.com
2. Traffic Splitting
# Split traffic between revisions gcloud run services update-traffic my-service \ --to-revisions=v1=50,v2=50
Security Best Practices
1. Authentication and Authorization
# Require authentication gcloud run services update my-service \ --no-allow-unauthenticated # Grant invoker role gcloud run services add-iam-policy-binding my-service \ --member="user:jane@example.com" \ --role="roles/run.invoker"
2. Network Security
# Configure VPC connector gcloud run services update my-service \ --vpc-connector my-connector # Configure egress settings gcloud run services update my-service \ --vpc-egress all-traffic
Monitoring and Logging
1. Viewing Logs
# View service logs gcloud logging read "resource.type=cloud_run_revision AND \ resource.labels.service_name=my-service" \ --limit 10
2. Monitoring Metrics
Key metrics to monitor:
- Request count and latency
- Container instance count
- Memory and CPU usage
- Error rates
Performance Optimization
1. Container Optimization
- Use multi-stage builds
- Minimize image size
- Optimize startup time
# Multi-stage build example FROM node:18 AS builder WORKDIR /app COPY package*.json ./ RUN npm install COPY . . RUN npm run build FROM node:18-slim WORKDIR /app COPY --from=builder /app/dist ./dist COPY package*.json ./ RUN npm install --production CMD ["npm", "start"]
2. Resource Configuration
- Set appropriate memory limits
- Configure concurrency
- Optimize cold start times
# Configure resources gcloud run services update my-service \ --memory 512Mi \ --cpu 1 \ --concurrency 80
Troubleshooting
1. Common Issues
# Check service status gcloud run services describe my-service # View revision details gcloud run revisions list \ --service my-service # Check container logs gcloud logging read "resource.type=cloud_run_revision" \ --project=PROJECT_ID
2. Health Checks
# health-check.yaml spec: template: spec: containers: - image: gcr.io/PROJECT_ID/my-app livenessProbe: httpGet: path: /health port: 8080 readinessProbe: httpGet: path: /ready port: 8080
Cost Optimization
-
Optimize Container Resources
- Right-size memory and CPU
- Use minimum instances wisely
- Implement efficient auto-scaling
-
Request Handling
- Implement caching where appropriate
- Optimize response times
- Use compression
Conclusion
Cloud Run provides a powerful platform for running containerized applications with zero infrastructure management. Key takeaways:
- Use container best practices
- Implement proper security measures
- Monitor performance and costs
- Optimize for scalability
- Leverage Cloud Run features
For more information, refer to the official Cloud Run documentation.