
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 the 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.
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
Pre-requisites:
- In GCP console, on the top right toolbar, click the Open Cloud Shell button. 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:
gcloud is the command-line tool for Google Cloud Platform. It comes pre-installed on Cloud Shell and supports tab-completion.
2. You can list the active account name with this command:

Output:

3. You can list the project ID with this command:

Output:

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
- In Cloud Shell, type the following command to set the environment variable for the zone and cluster name.
export my_zone=us-central1-a
export my_cluster=standard-cluster-1

2. 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 — zone $my_zone


3. In Cloud Shell enter the following command to clone the repository to the lab Cloud Shell.
git clone https://github.com/GoogleCloudPlatform/training-data-analyst

4. Create a soft link as a shortcut to the working directory.
ln -s ~/training-data-analyst/courses/ak8s/v1.1 ~/ak8s

5. Change to the directory that contains the sample files for this lab.
cd ~/ak8s/Jobs_CronJobs

Create and run a Job
You will create a job using a sample deployment manifest called example-job.yaml
that has been provided for you. This Job computes the value of Pi to 2,000 places and then prints the result.


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


2. To check the status of this Job, execute the following command:


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.

Few Minutes Later, Job gets successfully completed.

3. 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. Make a note of one of the Pod names.
output:

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 to interact with them.
- To get a list of the Jobs in the cluster, execute the following command:


2. 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:

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

3. To delete the Job, execute the following command:


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.
Create and run a CronJob
The CronJob manifest file example-cronjob.yaml
has been provided for you. This CronJob deploys a new container every minute that prints the time, date and “Hello, World!”.



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


2. To get a list of the Jobs in the cluster, execute the following command:


3. To check the status of this Job, execute the following command, where [job_name]
is the name of your job:


4. Make a note of the name of the Pod that was used by this job.

5. 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.


6. To view all job resources in your cluster, including all of the Pods created by the CronJob which have completed, execute the following command:


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 job are retained so this list will only contain the most recent three of four jobs.
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.
- To delete all these jobs, execute the following command:


2. To verify that the jobs were deleted, execute the following command:

The output will show that all the Jobs were removed.
This concludes our Lab demonstration for — Deploying Jobs on Google Kubernetes Engine
好文章!你的文章对我帮助非常大。谢谢!你会允许吗?我想把你的文章分享到我的网站:
gate.io 交易 所
thank you