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.
Sign in to Qwiklabs using an incognito window.
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.
When ready, click Start lab.
Note your lab credentials (Username and Password). You will use them to sign in to the Google Cloud Console.
Click Open Google Console.
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.
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.
In Cloud 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. It comes pre-installed on Cloud Shell and supports tab-completion.
You can list the active account name with this command:
Let's create a sample Job that computes the value of Pi to 2,000 places and then prints the result.
Create and open a file called example-job.yaml with nano using the following command:
nano example-job.yaml
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
Press Ctrl+O, and then press Enter to save your edited file.
Press Ctrl+X to exit the nano text editor.
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
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:
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.
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
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.
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!".
Create and open a file called example-cronjob.yaml with nano using the following command:
nano example-cronjob.yaml
Once nano has opened, paste the following into the example-cronjob.yaml file:
Press Ctrl+O, and then press Enter to save your edited file.
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
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
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
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:
Make a note of the name of the Pod that was used by this job.
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!
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.
To delete all these jobs, execute the following command:
kubectl delete cronjob hello
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.
Les ateliers créent un projet Google Cloud et des ressources pour une durée déterminée.
Les ateliers doivent être effectués dans le délai imparti et ne peuvent pas être mis en pause. Si vous quittez l'atelier, vous devrez le recommencer depuis le début.
En haut à gauche de l'écran, cliquez sur Démarrer l'atelier pour commencer.
Utilisez la navigation privée
Copiez le nom d'utilisateur et le mot de passe fournis pour l'atelier
Cliquez sur Ouvrir la console en navigation privée
Connectez-vous à la console
Connectez-vous à l'aide des identifiants qui vous ont été attribués pour l'atelier. L'utilisation d'autres identifiants peut entraîner des erreurs ou des frais.
Acceptez les conditions d'utilisation et ignorez la page concernant les ressources de récupération des données.
Ne cliquez pas sur Terminer l'atelier, à moins que vous n'ayez terminé l'atelier ou que vous ne vouliez le recommencer, car cela effacera votre travail et supprimera le projet.
Ce contenu n'est pas disponible pour le moment
Nous vous préviendrons par e-mail lorsqu'il sera disponible
Parfait !
Nous vous contacterons par e-mail s'il devient disponible
Un atelier à la fois
Confirmez pour mettre fin à tous les ateliers existants et démarrer celui-ci
Utilisez la navigation privée pour effectuer l'atelier
Ouvrez une fenêtre de navigateur en mode navigation privée pour effectuer cet atelier. Vous éviterez ainsi les conflits entre votre compte personnel et le compte temporaire de participant, qui pourraient entraîner des frais supplémentaires facturés sur votre compte personnel.
Architecting with Google Kubernetes Engine: Deploying Jobs on Kubernetes Engine
Durée :
8 min de configuration
·
Accessible pendant 60 min
·
Terminé après 60 min