In this practical post , you create a Google Kubernetes Engine cluster containing several containers, each containing a web server. You place a load balancer in front of the cluster and view its contents.
Objectives
In this post, you learn how to perform the following tasks:
- Provision a Kubernetes cluster using Kubernetes Engine.
- Deploy and manage Docker containers using
kubectl
.
Task 1. Sign in to the FREE Google Cloud Trial
Google Cloud is offering new customers $300 in free credit on Google Cloud and up to $1,000 in free credit on Google Cloud.
There is a short process involved in claiming your free credit from Google Cloud. Please follow the instructions below carefully, and please check you are eligible before you begin.
- Note your lab credentials (Username and Password). You will use them to sign in to the Google Cloud Console.
- Click Open Google Console.
- Copy/paste credentials for this lab into the prompts.
- Accept the terms and skip the recovery resource page.
Task 2. Confirm that needed APIs are enabled
- Activate these two APIs using Gcloud :
- Kubernetes Engine API
- Container Registry API
$ gcloud services enable container.googleapis.com $ gcloud services enable containerregistry.googleapis.com
Task 3. Start a Kubernetes Engine cluster
- In Google Cloud console, on the top right toolbar, click the Activate Cloud Shell button.
- Click Continue.
- At the Cloud Shell prompt, type the following command to export the environment variable called
MY_ZONE
.export MY_ZONE=”europe-west1-b” - Start a Kubernetes cluster managed by Kubernetes Engine. Name the cluster webfrontend and configure it to run 2 nodes: gcloud container clusters create webfrontend –zone $MY_ZONE –num-nodes 2. It takes several minutes to create a cluster as Kubernetes Engine provisions virtual machines for you.
- After the cluster is created, check your installed version of Kubernetes using the
kubectl version
command: kubectl versionThegcloud container clusters create
command automatically authenticatedkubectl
for you. - View your running nodes in the GCP Console. On the Navigation menu (
), click Compute Engine > VM Instances.Your Kubernetes cluster is now ready for use.
Task 4. Run and deploy a container
- From your Cloud Shell prompt, launch a single instance of the nginx container. (Nginx is a popular web server.). kubectl create deploy nginx –image=nginx:1.17.10 In Kubernetes, all containers run in pods. This use of the
kubectl create
command caused Kubernetes to create a deployment consisting of a single pod containing the nginx container. A Kubernetes deployment keeps a given number of pods up and running even in the event of failures among the nodes on which they run. In this command, you launched the default number of pods, which is 1. - View the pod running the nginx container:kubectl get pods
- Expose the nginx container to the Internet:kubectl expose deployment nginx –port 80 –type LoadBalancer. Kubernetes created a service and an external load balancer with a public IP address attached to it. The IP address remains the same for the life of the service. Any network traffic to that public IP address is routed to pods behind the service: in this case, the nginx pod.
- View the new service: kubectl get services . You can use the displayed external IP address to test and contact the nginx container remotely.It may take a few seconds before the External-IP field is populated for your service. This is normal. Just re-run the
kubectl get services
command every few seconds until the field is populated. - Open a new web browser tab and paste your cluster’s external IP address into the address bar. The default home page of the Nginx browser is displayed.
- Scale up the number of pods running on your service:kubectl scale deployment nginx –replicas 3 Scaling up a deployment is useful when you want to increase available resources for an application that is becoming more popular.
- Confirm that Kubernetes has updated the number of pods:kubectl get pods
- Confirm that your external IP address has not changed: kubectl get services
- Return to the web browser tab in which you viewed your cluster’s external IP address. Refresh the page to confirm that the nginx web server is still responding.
Conclusion
In this lab, you configured a Kubernetes cluster in Kubernetes Engine. You populated the cluster with several pods containing an application, exposed the application, and scaled the application.