0
0
GCPcloud~5 mins

GKE vs Cloud Run decision in GCP - CLI Comparison

Choose your learning style9 modes available
Introduction
Choosing between GKE and Cloud Run helps you decide how to run your applications in the cloud. GKE manages containers with more control, while Cloud Run runs containers without managing servers.
When you want full control over container orchestration and scaling for complex apps, use GKE.
When you want to deploy simple containerized apps quickly without managing infrastructure, use Cloud Run.
When your app needs to run continuously with custom networking, GKE is better.
When your app can scale down to zero and only run on demand, Cloud Run saves cost.
When you want to integrate with Kubernetes tools and ecosystem, choose GKE.
Commands
This command creates a GKE cluster named example-cluster with 3 nodes in the us-central1-a zone. It sets up the environment to run containerized apps with Kubernetes.
Terminal
gcloud container clusters create example-cluster --zone us-central1-a --num-nodes 3
Expected OutputExpected
Creating cluster example-cluster in us-central1-a... Created [https://container.googleapis.com/v1/projects/my-project/zones/us-central1-a/clusters/example-cluster]. To inspect the contents of your cluster, go to: https://console.cloud.google.com/kubernetes/workload_/gcloud/us-central1-a/example-cluster?project=my-project kubeconfig entry generated for example-cluster. NAME LOCATION MASTER_VERSION MASTER_IP MACHINE_TYPE NODE_VERSION NUM_NODES STATUS example-cluster us-central1-a 1.26.8-gke.1200 34.68.123.45 e2-medium 1.26.8-gke.1200 3 RUNNING
--num-nodes - Sets the number of nodes in the cluster
--zone - Specifies the zone where the cluster is created
This command deploys a containerized app named example-app to the GKE cluster using the specified container image and port.
Terminal
kubectl run example-app --image=gcr.io/my-project/example-app:latest --port=8080
Expected OutputExpected
pod/example-app created
--image - Specifies the container image to run
--port - Exposes the container port
This command deploys the container image to Cloud Run as a managed service named example-service in the us-central1 region, allowing public access.
Terminal
gcloud run deploy example-service --image=gcr.io/my-project/example-app:latest --region us-central1 --platform managed --allow-unauthenticated
Expected OutputExpected
Deploying container to Cloud Run service [example-service] in project [my-project] region [us-central1] ✓ Creating Revision... ✓ Routing traffic... Service [example-service] revision [example-service-00001-abc] has been deployed and is serving 100 percent of traffic. Service URL: https://example-service-abc-uc.a.run.app
--platform managed - Deploys to fully managed Cloud Run
--allow-unauthenticated - Allows public access to the service
This command lists all Cloud Run services in the us-central1 region to verify the deployment.
Terminal
gcloud run services list --region us-central1
Expected OutputExpected
SERVICE REGION URL example-service us-central1 https://example-service-abc-uc.a.run.app
--region - Specifies the region to list services
Key Concept

If you remember nothing else, remember: GKE gives you full control and complexity for container orchestration, while Cloud Run offers simple, serverless container deployment with automatic scaling.

Common Mistakes
Trying to deploy a container to Cloud Run without specifying the region or platform flags
Cloud Run needs region and platform info to know where and how to deploy the service
Always include --region and --platform managed flags when deploying to Cloud Run
Creating a GKE cluster with too few nodes for the app's resource needs
The app may not have enough resources to run properly, causing failures or slow performance
Estimate resource needs and create the cluster with enough nodes or scale nodes later
Not exposing the container port when deploying to GKE
Kubernetes won't know which port the app listens on, so traffic won't reach the app
Use the --port flag with kubectl run to expose the correct container port
Summary
Use 'gcloud container clusters create' to set up a GKE cluster for full container orchestration control.
Deploy containers to GKE with 'kubectl run' specifying image and port for app access.
Use 'gcloud run deploy' with region and platform flags to deploy containers serverlessly on Cloud Run.
List Cloud Run services with 'gcloud run services list' to verify deployments.