arrow_back

Deploying Jobs on Google Kubernetes Engine

登录 加入
访问 700 多个实验和课程

Deploying Jobs on Google Kubernetes Engine

实验 1 小时 universal_currency_alt 5 个积分 show_chart 入门级
info 此实验可能会提供 AI 工具来支持您学习。
访问 700 多个实验和课程

Overview

In this lab, you define and run Jobs and CronJobs.

In GKE, a Job is a controller object that represents a finite task. Jobs manage a task as it runs to completion, rather than managing an ongoing desired state such as maintaining the total number of running Pods.

CronJobs perform finite, time-related tasks that run once or repeatedly at a time that you specify using Job objects to complete their tasks.

Objectives

In this lab, you learn how to perform the following tasks:

  • Define, deploy and clean up a GKE Job
  • Define, deploy and clean up a GKE CronJob

Lab setup

Access Qwiklabs

For each lab, you get a new Google Cloud project and set of resources for a fixed time at no cost.

  1. Sign in to Qwiklabs using an incognito window.

  2. Note the lab's access time (for example, 1:15:00), and make sure you can finish within that time.
    There is no pause feature. You can restart if needed, but you have to start at the beginning.

  3. When ready, click Start lab.

  4. Note your lab credentials (Username and Password). You will use them to sign in to the Google Cloud Console.

  5. Click Open Google Console.

  6. Click Use another account and copy/paste credentials for this lab into the prompts.
    If you use other credentials, you'll receive errors or incur charges.

  7. Accept the terms and skip the recovery resource page.

After you complete the initial sign-in steps, the project dashboard appears.

Activate Google Cloud Shell

Google Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud.

Google Cloud Shell provides command-line access to your Google Cloud resources.

  1. In Cloud console, on the top right toolbar, click the Open Cloud Shell button.

    Highlighted Cloud Shell icon

  2. Click Continue.

It takes a few moments to provision and connect to the environment. When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. For example:

Project ID highlighted in the Cloud Shell Terminal

gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.

  • You can list the active account name with this command:
gcloud auth list

Output:

Credentialed accounts: - @.com (active)

Example output:

Credentialed accounts: - google1623327_student@qwiklabs.net
  • You can list the project ID with this command:
gcloud config list project

Output:

[core] project =

Example output:

[core] project = qwiklabs-gcp-44776a13dea667a6 Note: Full documentation of gcloud is available in the gcloud CLI overview guide .

Task 1. Define and deploy a Job manifest

In GKE, a Job is a controller object that represents a finite task.

In this task, you create a Job, inspect its status, and then remove it.

Connect to the lab Google Kubernetes Engine cluster

  1. In Cloud Shell, type the following command to set the environment variable for the zone and cluster name:
export my_region={{{project_0.default_region | "REGION"}}} export my_cluster=autopilot-cluster-1
  1. Configure kubectl tab completion in Cloud Shell:
source <(kubectl completion bash)
  1. In Cloud Shell, configure access to your cluster for the kubectl command-line tool, using the following command:
gcloud container clusters get-credentials $my_cluster --region $my_region

Create and run a Job

Let's create a sample Job that computes the value of Pi to 2,000 places and then prints the result.

  1. Create and open a file called example-job.yaml with nano using the following command:
nano example-job.yaml
  1. Once nano has opened, paste the following into the example-job.yaml file:
apiVersion: batch/v1 kind: Job metadata: # Unique key of the Job instance name: example-job spec: template: metadata: name: example-job spec: containers: - name: pi image: perl:5.34 command: ["perl"] args: ["-Mbignum=bpi", "-wle", "print bpi(2000)"] # Do not restart containers after they exit restartPolicy: Never
  1. Press Ctrl+O, and then press Enter to save your edited file.

  2. Press Ctrl+X to exit the nano text editor.

  3. To create a Job from this file, execute the following command:

kubectl apply -f example-job.yaml

Click Check my progress to verify the objective. Create and run a Job

  1. To check the status of this Job, execute the following command:
kubectl describe job example-job

You will see details of the job, including the Pod statuses indicating how many jobs are still running, how many completed successfully and how many failed:

... Start Time: Thu, 20 Dec 2018 14:34:09 +0000 Pods Statuses: 0 Running / 1 Succeeded / 0 Failed ...
  1. To view all Pod resources in your cluster, including Pods created by the Job which have completed, execute the following command:
kubectl get pods

Your Pod name might be different from the example output:

NAME READY STATUS RESTARTS AGE example-job-sqljc 0/1 Completed 0 1m

Make a note of one of the Pod names.

Clean up and delete the Job

When a Job completes, the Job stops creating Pods. The Job API object is not removed when it completes, which allows you to view its status. Pods created by the Job are not deleted, but they are terminated. Retention of the Pods allows you to view their logs and interact with them.

  1. To get a list of the Jobs in the cluster, execute the following command:
kubectl get jobs

The output should look like the example:

NAME COMPLETIONS DURATION AGE example-job 1/1 75s 2m5s
  1. To retrieve the log file from the Pod that ran the Job execute the following command. You must replace [POD-NAME] with the node name you recorded in the last task.
kubectl logs [POD-NAME]

The output will show that the job wrote the first two thousand digits of pi to the Pod log.

  1. To delete the Job, execute the following command:
kubectl delete job example-job

If you try to query the logs again the command will fail as the Pod can no longer be found.

Task 2. Define and deploy a CronJob manifest

You can create CronJobs to perform finite, time-related tasks that run once or repeatedly at a time that you specify.

In this task, you create and run a CronJob, and then you clean up and delete the Job.

Define a CronJob manifest

This CronJob deploys a new container every minute that prints the time, date and "Hello, World!".

  1. Create and open a file called example-cronjob.yaml with nano using the following command:
nano example-cronjob.yaml
  1. Once nano has opened, paste the following into the example-cronjob.yaml file:
apiVersion: batch/v1 kind: CronJob metadata: name: hello spec: schedule: "*/1 * * * *" jobTemplate: spec: template: spec: containers: - name: hello image: busybox args: - /bin/sh - -c - date; echo "Hello, World!" restartPolicy: OnFailure
  1. Press Ctrl+O, and then press Enter to save your edited file.

  2. Press Ctrl+X to exit the nano text editor.

Note: CronJobs use the required schedule field, which accepts a time in the Unix standard crontab format.

All CronJob times are in UTC:
  • The first value indicates the minute (between 0 and 59).
  • The second value indicates the hour (between 0 and 23).
  • The third value indicates the day of the month (between 1 and 31).
  • The fourth value indicates the month (between 1 and 12).
  • The fifth value indicates the day of the week (between 0 and 6).

The schedule field also accepts * and ? as wildcard values. Combining / with ranges specifies that the task should repeat at a regular interval. In the example, */1 * * * * indicates that the task should repeat every minute of every day of every month.

Create and run a CronJob

  1. To create a Job from this file, execute the following command:
kubectl apply -f example-cronjob.yaml

Click Check my progress to verify the objective. Create and run a CronJob

  1. To get a list of the Jobs in the cluster, execute the following command:
kubectl get jobs

The output should look like the example:

NAME COMPLETIONS DURATION AGE hello-1545013620 1/1 2s 18s
  1. To check the status of this Job, execute the following command, where [job_name] is the name of your job:
kubectl describe job [job_name]

You will see details of the job, including the Pod statuses showing that one instance of this job was run:

... Start Time: Thu, 20 Dec 2018 15:24:03 +0000 Pods Statuses: 0 Running / 1 Succeeded / 0 Failed ... ...Created pod: hello-1545319920-twkhl
  1. Make a note of the name of the Pod that was used by this job.
  2. View the output of the Job by querying the logs for the Pod. Replace [POD-NAME] with the name of the Pod you recorded in the last step.
kubectl logs [POD-NAME]

This will display the output of the shell script configured in the CronJob:

Fri Jan 28 11:12:01 UTC 2022 Hello, World!
  1. To view all job resources in your cluster, including all of the Pods created by the CronJob which have completed, execute the following command:
kubectl get jobs

Your job names might be different from the example output. By default Kubernetes sets the Job history limits so that only the last three successful and last failed jobs are retained so this list will only contain the most recent three of four jobs:

NAME COMPLETIONS DURATION AGE hello-27389472 1/1 1s 2m55s hello-27389473 1/1 1s 115s hello-27389474 1/1 1s 55s

Clean up and delete the Job

In order to stop the CronJob and clean up the Jobs associated with it you must delete the CronJob.

  1. To delete all these jobs, execute the following command:
kubectl delete cronjob hello
  1. To verify that the jobs were deleted, execute the following command:
kubectl get jobs

The output should look like the example:

No resources found in default namespace.

All the Jobs were removed.

End your lab

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:

  • 1 star = Very dissatisfied
  • 2 stars = Dissatisfied
  • 3 stars = Neutral
  • 4 stars = Satisfied
  • 5 stars = Very satisfied

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.

准备工作

  1. 实验会创建一个 Google Cloud 项目和一些资源,供您使用限定的一段时间
  2. 实验有时间限制,并且没有暂停功能。如果您中途结束实验,则必须重新开始。
  3. 在屏幕左上角,点击开始实验即可开始

使用无痕浏览模式

  1. 复制系统为实验提供的用户名密码
  2. 在无痕浏览模式下,点击打开控制台

登录控制台

  1. 使用您的实验凭证登录。使用其他凭证可能会导致错误或产生费用。
  2. 接受条款,并跳过恢复资源页面
  3. 除非您已完成此实验或想要重新开始,否则请勿点击结束实验,因为点击后系统会清除您的工作并移除该项目

此内容目前不可用

一旦可用,我们会通过电子邮件告知您

太好了!

一旦可用,我们会通过电子邮件告知您

一次一个实验

确认结束所有现有实验并开始此实验

使用无痕浏览模式运行实验

请使用无痕模式或无痕式浏览器窗口运行此实验。这可以避免您的个人账号与学生账号之间发生冲突,这种冲突可能导致您的个人账号产生额外费用。