GKE Cheatsheet
Published on December 24, 2020
This document was migrated from DigiDocs
In this section, you will find my notes on using Kubernetes on Google Cloud Platform's Kubernetes Engine (GKE). It covers a typical workflow for starting a Kubernetes cluster and deploying an application on it.
Clusters
Creating a cluster
Typical command:
gcloud container clusters create $C_NAME --zone $ZONE --num-nodes $NUM
Shorter command:
gcloud container clusters create $C_NAME
With auto-scaling
gcloud container clusters create $C_NAME --enable-autoscaling --min-nodes $MIN --max-nodes $MAX
The default value for --num-nodes
is 3
You can set the default zone for Kubernetes Engine using the following:
- For zonal clusters:
gcloud config set compute/zone $ZONE
- For regional clusters:
gcloud config set compute/region $REGION
Deleting a cluster
Typical command
gcloud container clusters delete $C_NAME
Getting cluster info
Get all pods running in the cluster
kubectl get pods
Get all services running in the cluster
kubectl get services
Get credentials for cluster
gcloud container clusters get-credentials $C_NAME
This command enables switching to a specific cluster, when working with multiple clusters. It can also be used to access a previously created cluster from a new workstation.
Deployments
Creating a deployment
To create a deployment, you need to have your Docker image prepared beforehand. This image must be built and uploaded to the Container Registry before you can deploy it on your GKE cluster.
Before you proceed, you need to configure Docker to authenticate to the Container Registry: gcloud auth configure-docker
- Build your image:
docker build -t gcr.io/$PROJECT_ID/$NAME:$VER .
- Verify it was built:
docker images
- Upload your image to the registry:
docker push gcr.io/$PROJECT_ID/$NAME:$VER
- Verify it was uploaded:
docker run --rm -p $CONT_PORT:$HOST_PORT gcr.io/$PROJECT_ID/$NAME:$VER
- Create your deployment:
kubectl create deployment $D_NAME --image=gcr.io/$PROJECT_ID/$NAME:$VER
- Verify it was deployed:
kubectl get pods
- Expose the deployment to the Internet via a Service resource:
kubectl expose deployment $D_NAME --type=LoadBalancer --port $EXPOSED_PORT --target-port $HOST_PORT
- Verify the service is running:
kubectl get service
Deleting a deployment
- Delete the Service resource:
kubectl delete service $D_NAME
- Delete the cluster:
gcloud container clusters delete $C_NAME
Updating your deployment
To update your deployment with a new Docker image, you have to upload it to the Cloud Registry. Next, you can apply a rolling update of your deployment's Docker image.
- Build your new image (remember to update
$VER
):
docker build -t gcr.io/$PROJECT_ID/$I_NAME:$VER .
- Verify it was built:
docker images
- Upload your image to the registry:
docker push gcr.io/$PROJECT_ID/$I_NAME:$VER
- Verify it was uploaded:
docker run --rm -p $CONT_PORT:$HOST_PORT gcr.io/$PROJECT_ID/$I_NAME:$VER
- Apply a rolling image update:
kubectl set image deployment/$D_NAME $I_NAME=gcr.io/$PROJECT_ID/$I_NAME:$VER
Scaling a deployment
Typical example
kubectl scale deployment $D_NAME --replicas $NUM
With auto-scaling
kubectl autoscale deployment $D_NAME --max $MAX --min $MIN --cpu-percent $PERCENT
Getting deployment info
Typical example
kubectl get deployment $D_NAME
Resources
- Original copy of this document at DigiDocs
- Kubectl Reference Docs
- Documentation on 'gcloud container clusters create'
- Documentation on 'gcloud container clusters delete'
- Documentation on 'gcloud container clusters get-credentials'
- Tutorial on deploying a containerized web application
- Documentation on 'kubectl autoscale'