Launch a Kubernetes cluster on Google Kubernetes Engine, run a containerized web application, and then roll out a new version of the application across the cluster.
-
You will need a Google Cloud Platform account with an active billing account. If you don't have one, you can sign up for a free trial to get $300 worth of GCP credits, which are valid for 12 months, at https://cloud.google.com/free/.
-
This tutorial requires the
kubectl
andgcloud
command-line tools. Cloud Shell provides an environment with these tools preinstalled.
For each step below, run the command in a terminal window.
Enter "Y" to use the recommended project id.
gcloud projects create --name=gke-demo
Replace [PROJECT-ID]
with the id returned in Step 1.
gcloud config set project [PROJECT-ID]
Make a note of the billing account ID in the format XXXXXX-XXXXXX-XXXXXX. If you see 0 items listed, you can sign up for the Free Trial to get $300 worth of GCP credits, which are valid for 12 months.
gcloud alpha billing accounts list
Replace [ACCOUNT-ID]
with the id from step 3.
gcloud beta billing projects link [PROJECT-ID] --billing-account=[ACCOUNT-ID]
This tutorial requires both the Compute Engine API and Kubernetes Engine API. This may take a couple of minutes to complete.
gcloud services enable compute.googleapis.com; gcloud services enable container.googleapis.com
Creating the cluster may take a few minutes.
gcloud container clusters create demo-1 -z us-central1-b
A sample container image, us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0
, is stored in
Google Artifact Registry.
kubectl run web-app --image us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0 --port 8080
Re-run this command at any time to view the current pod state.
kubectl get pods
kubectl scale deployment web-app --replicas 3; kubectl get pods
kubectl expose deployment web-app --port=80 --target-port=8080 --type=LoadBalancer
Make a note of the external IP address that is returned. If the external IP address is pending, wait a few moments and then re-run the command.
kubectl get service web-app
Replace [IP-ADDRESS]
with the external IP address returned in the previous step.
curl http://[IP-ADDRESS]
kubectl set image deployment web-app web-app=us-docker.pkg.dev/google-samples/containers/gke/hello-app:2.0
watch kubectl get pods
Kubernetes will terminate the old pods and re-launch them with the new version.
Appending the watch
command to Step 13 ensures that the watch window has
time to open before the deployment completes.
When the changes are complete, click Ctrl+C to return to the main terminal.
curl http://[IP-ADDRESS]
This prevents further charges from accruing to your GCP account.
kubectl delete services,deployment web-app
Wait about 30 seconds for the Load Balancer to finish deleting in the background, then move to the next step.
This deletes the cluster and its nodes hosted on Compute Engine.
gcloud container clusters delete demo-1 --zone us-central1-b
Replace [PROJECT-ID]
with the id returned in Step 1. This prevents further
charges from accruing to your GCP account.
gcloud projects delete [PROJECT-ID]
FURTHER EXPLORATION:
- Google Kubernetes Engine (GKE)
- GKE tutorials
- Kubernetes comic
- Kubernetes documentation
- kubectl command syntax