GKE Cheatsheet
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 clusterTypical command:
Shorter command:
With auto-scaling
note
The default value for --num-nodes
is 3
tip
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 clusterTypical command
#
Getting cluster infoGet all pods running in the cluster
Get all services running in the cluster
Get credentials for cluster
Cite
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 deploymentTo 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.
tip
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 deploymentTo 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 deploymentTypical example
With auto-scaling
#
Getting deployment infoTypical example
#
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'