0
0
GCPcloud~5 mins

Serverless vs GKE decision in GCP - CLI Comparison

Choose your learning style9 modes available
Introduction
Choosing between serverless and Google Kubernetes Engine (GKE) helps you decide how to run your app. Serverless lets you run code without managing servers, while GKE gives you control over containers and infrastructure.
When you want to quickly run small apps or functions without worrying about servers or scaling.
When you need full control over containerized apps and want to manage scaling and updates yourself.
When your app has unpredictable traffic and you want automatic scaling without manual setup.
When you have complex apps that need multiple containers working together with custom networking.
When you want to reduce operational work and pay only for the exact compute time your code uses.
Commands
Deploys a container image to Cloud Run, which is a serverless platform. This runs your app without managing servers and scales automatically.
Terminal
gcloud run deploy my-serverless-app --image=gcr.io/my-project/my-image --region=us-central1 --platform=managed
Expected OutputExpected
Deploying container to Cloud Run service [my-serverless-app] in project [my-project] region [us-central1] Done. Service [my-serverless-app] revision [my-serverless-app-00001-abc] has been deployed and is serving traffic at https://my-serverless-app-abc.a.run.app
--image - Specifies the container image to deploy
--region - Sets the region where the service runs
--platform - Chooses the managed serverless platform
Grants your Google account admin rights on the GKE cluster so you can manage resources.
Terminal
kubectl create clusterrolebinding cluster-admin-binding --clusterrole cluster-admin --user $(gcloud config get-value account)
Expected OutputExpected
clusterrolebinding.rbac.authorization.k8s.io/cluster-admin-binding created
Creates a GKE cluster with 3 nodes in the specified zone. This cluster lets you run and manage containerized apps with control over infrastructure.
Terminal
gcloud container clusters create my-gke-cluster --zone us-central1-a --num-nodes 3
Expected OutputExpected
Creating cluster my-gke-cluster in us-central1-a... Created [https://container.googleapis.com/v1/projects/my-project/zones/us-central1-a/clusters/my-gke-cluster]. To inspect the contents of your cluster, go to: https://console.cloud.google.com/kubernetes/workload_/gcloud/us-central1-a/my-gke-cluster?project=my-project kubeconfig entry generated for my-gke-cluster NAME LOCATION MASTER_VERSION MASTER_IP MACHINE_TYPE NODE_VERSION NUM_NODES STATUS my-gke-cluster us-central1-a 1.26.8-gke.100 35.233.123.45 e2-medium 1.26.8-gke.100 3 RUNNING
--zone - Specifies the zone for the cluster
--num-nodes - Sets the initial number of nodes
Lists the nodes in your GKE cluster to verify the cluster is ready and nodes are running.
Terminal
kubectl get nodes
Expected OutputExpected
NAME STATUS ROLES AGE VERSION my-gke-cluster-default-pool-1a2b3c4d-5e6f RUNNING <none> 2m v1.26.8-gke.100
Key Concept

If you remember nothing else from this pattern, remember: serverless is best for simple, auto-scaling apps without server management, while GKE is best when you need full control over containerized apps and infrastructure.

Common Mistakes
Trying to deploy a complex multi-container app directly to Cloud Run without splitting it into services.
Cloud Run is designed for single container services and does not support multi-container pods like Kubernetes.
Use GKE for multi-container apps or split your app into multiple Cloud Run services.
Creating a GKE cluster without setting up proper permissions for kubectl access.
Without cluster-admin binding, you cannot manage resources on the cluster.
Run the clusterrolebinding command to grant your user admin rights on the cluster.
Deploying serverless apps in a region far from your users causing latency.
Serverless apps scale automatically but latency depends on region proximity.
Choose a region close to your users when deploying Cloud Run services.
Summary
Deploy serverless apps with 'gcloud run deploy' for easy scaling and no server management.
Create GKE clusters with 'gcloud container clusters create' to run complex containerized apps with control.
Use 'kubectl' commands to manage and verify your GKE cluster nodes and permissions.