Getting started with Google Kubernetes Engine¶
Last updated on: 30 August, 2020
Compiled by: Evan Tay
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:
1 |
|
Shorter command:
1 |
|
With auto-scaling
1 |
|
Info
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 cluster¶
Typical command
1 |
|
Getting cluster info¶
Get all pods running in the cluster
1 |
|
Get all services running in the cluster
1 |
|
Get credentials for cluster
1 |
|
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 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.
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 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
1 |
|
With auto-scaling
1 |
|
Getting deployment info¶
Typical example
1 |
|
Resources¶
- 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