Compute & Containers
Google Cloud Platform offers multiple compute and containerization options to suit different application requirements. This section covers serverless containers with Cloud Run, managed Kubernetes with GKE, and Kubernetes architecture patterns. This guide provides everything from container deployment basics to advanced orchestration patterns.
Prerequisites
Before working with compute and containers on GCP, ensure you have:
- A GCP project with appropriate permissions
- Docker installed and configured
- Basic understanding of containerization concepts
- gcloud CLI installed and configured
- Knowledge of application deployment workflows
Cloud Run
Deploy containerized applications with serverless execution. Cloud Run is a fully managed serverless platform for deploying containerized applications. It automatically scales from zero to handle traffic, charges only for actual resource usage, and provides built-in load balancing and health checks.
Overview
Cloud Run abstracts away infrastructure management while giving you the flexibility of containers. It’s ideal for microservices, web applications, and event-driven workloads that require automatic scaling.
Key Features
- Zero to N Scaling: Automatically scales based on incoming requests
- Pay-per-use: Only pay for actual CPU/memory consumption
- Any Container: Support for any language, runtime, or operating system
- Built-in Networking: Automatic load balancing and health checks
- Integrated Services: Seamless integration with other GCP services
Use Cases
- Deploying containerized applications without managing infrastructure
- Variable workloads with unpredictable traffic patterns
- Event-driven applications and microservices
- APIs and web services
- Background processing and batch jobs
Pros
- No infrastructure management
- Automatic scaling to zero
- Pay-per-use pricing
- Supports any container
Cons
- Cold start latency
- Execution time limits
- Less control over environment
- Potential vendor lock-in
Deployment
# Deploy to Cloud Run
gcloud run deploy my-service \
--image=gcr.io/my-project/my-image:latest \
--platform=managed \
--region=us-central1 \
--allow-unauthenticated
Configuration
# cloudbuild.yaml for Cloud Run deployment
steps:
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', 'gcr.io/$PROJECT_ID/my-service', '.']
- name: 'gcr.io/cloud-builders/docker'
args: ['push', 'gcr.io/$PROJECT_ID/my-service']
- name: 'gcr.io/cloud-builders/gcloud'
args:
- 'run'
- 'deploy'
- 'my-service'
- '--image=gcr.io/$PROJECT_ID/my-service'
- '--platform=managed'
- '--region=us-central1'
Google Kubernetes Engine (GKE)
Managed Kubernetes platform for container orchestration. GKE is a managed Kubernetes service for deploying, managing, and scaling containerized applications. It offers both Standard and Autopilot modes, with features like automatic upgrades, node auto-repair, integrated monitoring, and seamless integration with Google Cloud services.
Overview
GKE provides the full power of Kubernetes without the operational overhead. It’s ideal for complex applications requiring fine-grained control, stateful workloads, or migration of existing Kubernetes deployments.
GKE Modes
Standard Mode
- Full control over cluster configuration
- Custom node pools and machine types
- Optimized for maximum control and flexibility
- Suitable for experienced Kubernetes teams
Autopilot Mode
- Serverless Kubernetes experience
- Automatic node provisioning and management
- Pay only for pod resources
- Simplified operations and maintenance
Key Features
- Automatic Upgrades: Cluster and node pool upgrades with minimal disruption
- Node Auto-repair: Automatic detection and replacement of unhealthy nodes
- Horizontal Pod Autoscaling: Automatic scaling based on CPU/memory/custom metrics
- Cluster Autoscaling: Automatic node pool sizing based on pod requirements
- Integrated Monitoring: Built-in integration with Cloud Monitoring and Logging
Use Cases
- Applications requiring full Kubernetes control
- Complex orchestration requirements
- Migrating existing Kubernetes workloads
- Stateful applications with persistent storage
- Multi-container applications with complex networking
Pros
- Fully managed Kubernetes
- Automatic scaling and upgrades
- Integrated with Google Cloud services
- Strong security features
Cons
- Higher complexity than Cloud Run
- Requires Kubernetes expertise
- Cost management complexity
- Learning curve for operations
Cluster Creation
# Create Autopilot cluster
gcloud container clusters create my-cluster \
--region=us-central1 \
--enable-autopilot \
--num-nodes=1
# Create Standard cluster
gcloud container clusters create my-cluster \
--region=us-central1 \
--machine-type=e2-medium \
--num-nodes=3 \
--enable-autoscaling \
--min-nodes=1 \
--max-nodes=10
Deployment
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: gcr.io/my-project/my-app:latest
ports:
- containerPort: 8080
resources:
requests:
cpu: "250m"
memory: "512Mi"
limits:
cpu: "500m"
memory: "1Gi"
Kubernetes Architecture
Container orchestration patterns and best practices for GKE. This pattern covers container orchestration patterns and best practices specifically for Google Kubernetes Engine. It includes pod design, service discovery, configuration management, storage integration, security policies, and operational patterns.
Pod Design Patterns
Single Container Pods
- Simple, focused applications
- One container per pod
- Easy to manage and debug
Multi-Container Pods
- Sidecar containers (logging, monitoring)
- Ambassador containers (proxies, adapters)
- Init containers (setup, configuration)
Service Discovery
ClusterIP Services
- Internal cluster communication
- Stable network identity
- Load balancing across pods
NodePort Services
- External access via node ports
- Development and testing
- Simple external exposure
LoadBalancer Services
- External load balancer integration
- Production external access
- Automatic GCP integration
Configuration Management
ConfigMaps
- Application configuration
- Environment-specific settings
- Volume-mounted configuration files
Secrets
- Sensitive data management
- Encryption at rest
- Integration with Secret Manager
Storage Integration
Persistent Volumes
- Stateful applications
- Database storage
- File sharing between pods
Storage Classes
- Dynamic provisioning
- Performance optimization
- Cost management
Security Patterns
Network Policies
- Pod-to-pod communication control
- Namespace isolation
- Segmentation of workloads
Pod Security Policies
- Container security standards
- Privilege management
- Resource constraints
Operational Patterns
Rolling Updates
- Zero-downtime deployments
- Gradual rollout of new versions
- Automatic rollback on failure
Canary Deployments
- Traffic splitting between versions
- A/B testing capabilities
- Gradual feature rollout
Blue-Green Deployments
- Parallel version deployment
- Instant traffic switching
- Safe rollback capability
Use Cases
- Designing Kubernetes-based applications
- Implementing container orchestration patterns
- Migrating to GKE
- Building microservices architectures
- Implementing DevOps practices
Pros
- Industry-standard orchestration
- Portable across environments
- Rich ecosystem and tools
- Strong community support
Cons
- Operational complexity
- Steep learning curve
- Requires Kubernetes expertise
- Configuration management overhead
Best Practices
Resource Management
resources:
requests:
cpu: "100m"
memory: "128Mi"
limits:
cpu: "500m"
memory: "512Mi"
Health Checks
livenessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /ready
port: 8080
initialDelaySeconds: 5
periodSeconds: 5
Auto-scaling
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: my-app-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: my-app
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50
Choosing the Right Option
Cloud Run vs GKE
| Factor | Cloud Run | GKE |
|---|---|---|
| Infrastructure Management | None | Managed but configurable |
| Scaling | Automatic to zero | Manual or automatic |
| Control | Limited | Full control |
| Complexity | Low | High |
| Cost Model | Pay-per-request | Pay-per-node/pod |
| Best For | Simple services, variable load | Complex apps, stateful workloads |
Decision Framework
Choose Cloud Run when:
- You want minimal operational overhead
- Workloads have variable traffic patterns
- Applications are stateless
- Quick time-to-market is priority
Choose GKE when:
- You need full Kubernetes control
- Applications require complex networking
- Stateful workloads are needed
- Team has Kubernetes expertise
Common Issues and Troubleshooting
Cloud Run Deployment Failures
- Verify container image is accessible
- Check service account permissions
- Review resource limits and quotas
- Ensure health check endpoints are accessible
GKE Cluster Issues
- Verify cluster node status
- Check pod health and logs
- Review network policies
- Monitor resource utilization
Container Runtime Errors
- Validate container configuration
- Check application logs
- Review resource constraints
- Verify environment variables
Cleanup Commands
# Delete Cloud Run service
gcloud run services delete my-service --region=us-central1
# Delete GKE cluster
gcloud container clusters delete my-cluster --region=us-central1
# Clean up container images
gcloud container images delete gcr.io/my-project/my-image:tag
# Remove unused resources
gcloud compute instances list
gcloud compute disks list
Jump to other sections
- Explore Networking & Security for secure compute deployment
- Review Data & Analytics for data processing patterns