
Before you begin
- Labs create a Google Cloud project and resources for a fixed time
- Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
- On the top left of your screen, click Start lab to begin
Deploy GKE clusters
/ 30
Deploy Pods to GKE clusters
/ 35
Deploy a new pod using a Yaml file
/ 35
In this lab, you use the command line to build GKE clusters. You inspect the kubeconfig file, and you use kubectl
to manipulate the cluster.
In this lab, you learn how to perform the following tasks:
kubectl
to build and manipulate GKE clusterskubectl
and configuration files to deploy PodsFor each lab, you get a new Google Cloud project and set of resources for a fixed time at no cost.
Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is the Lab Details panel with the following:
Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).
The lab spins up resources, and then opens another tab that shows the Sign in page.
Tip: Arrange the tabs in separate windows, side-by-side.
If necessary, copy the Username below and paste it into the Sign in dialog.
You can also find the Username in the Lab Details panel.
Click Next.
Copy the Password below and paste it into the Welcome dialog.
You can also find the Password in the Lab Details panel.
Click Next.
Click through the subsequent pages:
After a few moments, the Google Cloud console opens in this tab.
After you complete the initial sign-in steps, the project dashboard appears.
You will do most of the work in Cloud Shell. Cloud Shell is a command-line environment running in Google Cloud. This Debian-based virtual machine is loaded with all the management tools you need (such as docker
, gcloud,gsutil
, and kubectl
) and provides a persistent 5GB home directory.
After a moment of provisioning, the Cloud Shell prompt appears:
In this task, you use Cloud Shell to deploy GKE clusters.
This form of the command sets most options to their defaults. To view the entire set of possible options, refer to the gcloud container clusters create reference.
You will see a number of warnings highlighting changes to default GKE cluster settings that were introduced as newer versions of Kubernetes have been adopted by GKE.
When deployment is complete, the Google Cloud console Kubernetes Engine > Clusters page should look like this screenshot:
Click Check my progress to verify the objective.
In this task, you use Cloud Shell to authenticate to a GKE cluster and then inspect the kubectl configuration files.
Authentication in Kubernetes applies both to communicating with the cluster from an external client through the kube-APIserver running on the master and to cluster containers communicating within the cluster or externally.
In Kubernetes, authentication can take several forms. For GKE, authentication is typically handled with OAuth2 tokens and can be managed through Cloud Identity and Access Management across the project as a whole and, optionally, through role-based access control which can be defined and configured within each cluster.
In GKE, cluster containers can use service accounts to authenticate to and access external resources.
kubectl
command-line tool), execute the following command:This command creates a .kube
directory in your home directory if it doesn't already exist. In the .kube
directory, the command creates a file named config
if it doesn't already exist, which is used to store the authentication and configuration information. The config file is typically called the kubeconfig file.
You can now examine all of the authentication and endpoint configuration data stored in the file. Information for the cluster should appear. The information was populated during cluster creation.
kubectl
commands manipulate) is indicated by the current-context
property.
You don't have to run the gcloud container clusters get-credentials
command to populate the kubeconfig file for clusters that you created in the same context (the same user in the same environment), because those clusters already have their details populated when the cluster is created. But you do have to run the command to connect to a cluster created by another user or in another environment. The command is also an easy way to switch the active context to a different cluster.
In this task, you use Cloud Shell and kubectl to inspect a GKE cluster.
After the kubeconfig file is populated and the active context is set to a particular cluster, you can use the kubectl
command-line tool to execute commands against the cluster. Most such commands ultimately trigger a REST API call against the master API server, which triggers the associated action.
The sensitive certificate data is replaced with DATA+OMITTED.
The output describes the active context cluster.
Output:
A line of output indicates the active context cluster.
Output:
PROJECT_ID
is your project ID. This information is the same as the information in the current-context
property of the kubeconfig file.
Several lines of output indicate details about the cluster you created and indicate which is the active context cluster. In general, this command lists some details of the clusters present in the user's kubeconfig file, including any other clusters that were created by the user as well as any manually added to the kubeconfig file.
In this case you have only one cluster, so this command didn't change anything.
However, in the future you may have more than one cluster in a project. You can use this approach to switch the active context when your kubeconfig file has the credentials and configuration for several clusters already populated. This approach requires the full name of the cluster, which includes the gke
prefix, the project ID, the location, and the display name, all concatenated with underscores.
kubectl
:This command produces no output.
The shell outputs all the possible commands:
The shell outputs all commands starting with "co" (or any other text you type).
In this task, you use Cloud Shell to deploy Pods to GKE clusters.
Kubernetes introduces the abstraction of a Pod to group one or more related containers as a single entity to be scheduled and deployed as a unit on the same node. You can deploy a Pod that is a single container from a single container image. Or a Pod can contain many containers from many container images.
This command creates a Pod named nginx with a container running the nginx image. When a repository isn't specified, the default behavior is to try to find the image either locally or in the Docker public registry. In this case, the image is pulled from the Docker public registry.
The output should look like the following example, but with a slightly different Pod name.
Output:
The output should look like the following example.
Output:
metrics not available yet
, re-run the above command.
Another top
command (kubectl top pods
) shows similar information across all the deployed Pods in the cluster.
[your_pod_name]
:Example:
Output:
The output should look like the following example. Details of the Pod, as well as its status and conditions and the events in its lifecycle, are displayed.
Output:
To be able to serve static content through the nginx web server, you must create and place a file into the container.
test.html
in the nano text editor:test.html
file:Press CTRL+X, then press Y and enter to save the file and exit the nano editor.
In Cloud Shell, execute the following command to place the file into the appropriate location within the nginx container in the nginx Pod to be served statically:
This command copies the test.html
file from the local home directory to the /usr/share/nginx/html
directory of the first container in the nginx Pod. You can specify other containers in a multi-container Pod by using the -c
option, followed by the name of the container.
A service is required to expose a Pod to clients outside the cluster. Services are discussed elsewhere in the course and used extensively in other labs. You can use a simple command to create a service to expose a Pod.
This command creates a LoadBalancer service, which allows the nginx Pod to be accessed from internet addresses outside of the cluster.
The output should look like the following example. You use the external IP address in the next step.
Output:
The Kubernetes service is one of the default services created or used by the cluster. The nginx service that you created is also displayed.
You may need to re-run this command several times before the external IP address is displayed.
Output:
Click Check my progress to verify the objective.
You replace [EXTERNAL_IP] with the external IP address of your service that you obtained from the output of the previous step.
The file contents appear in the output. You can go to the same address in your browser to see the file rendered as HTML.
Example:
Output:
In this task, you connect to a Pod to adjust settings, edit files, and make other live changes to the Pod.
The preferred way of deploying Pods and other resources to Kubernetes is through configuration files, which are sometimes called manifest files. Configuration files are typically written in the YAML syntax, specifying the details of the resource. With configuration files, you can more easily specify complex options than with a long line of command-line arguments.
YAML syntax is similar to, but more concise than, JSON syntax and it enables the same kind of hierarchical structuring of objects and properties. The source repository for the lab contains sample YAML files that have been prepared for you.
A sample manifest YAML file for a Pod called new-nginx-pod.yaml
has been provided for you:
Click Check my progress to verify the objective.
The output should look like the example.
Output:
You can see your new nginx Pod as well as the one we created earlier in the lab.
Some container images include a shell environment that you can launch. This shell environment might be more convenient than executing individual commands with kubectl
. For instance, the nginx image includes a bash shell. In this task you use shell redirection to connect to the bash shell in your new nginx pod to carry out a sequence of actions.
A new shell prompt appears.
Output:
You have started an interactive bash shell in the container of the new-nginx Pod. If the Pod had several containers, you could specify one by name with the -c
option.
Because the nginx container image has no text editing tools by default, you need to install one.
When prompted with Do you want to continue (Y/n), press Y to confirm.
You need to create a test.html
file in the static served directory on the nginx container.
test.html
file:To connect to and test the modified nginx container (with the new static HTML file), you could create a service. An easier way is to use port forwarding to connect to the Pod directly from Cloud Shell.
The output should look like the example.
Output:
This is a foreground process, so you need to open another Cloud Shell instance to test.
A second Cloud Shell session appears in your Cloud Shell window. You can switch between sessions by clicking the titles in the menu bar.
The HTML text you placed in the test.html
file is displayed.
A third Cloud Shell session appears in your Cloud Shell window. As before, you can switch sessions by clicking them in the menu bar.
When you have completed your lab, click End Lab. Google Cloud Skills Boost removes the resources you’ve used and cleans the account for you.
You will be given an opportunity to rate the lab experience. Select the applicable number of stars, type a comment, and then click Submit.
The number of stars indicates the following:
You can close the dialog box if you don't want to provide feedback.
For feedback, suggestions, or corrections, please use the Support tab.
Copyright 2022 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.
This content is not currently available
We will notify you via email when it becomes available
Great!
We will contact you via email if it becomes available
One lab at a time
Confirm to end all existing labs and start this one