Cronjob from the name can be seen, it is a scheduled task, and Linux in the same crontab, its format is basically crontab the same,
The format is as follows:
Minutes Hours dayofmonth Month DayofWeek yea
Support ",-*/" four characters,
*: Indicates matching any value, if used in minutes, indicates every minute
/: Indicates that the start time starts triggering and then fires once every fixed time,
For example, when minutes is set to 5/20, it means that the first trigger is at 5min, and then every 20min is triggered,
That is, the moment of 25min,45min is triggered
Example: For example, perform a task every 1min: the cron expression is as follows:
*/1 * * * *
Now write a Cronjob resource object to perform the job:
Cronjob the API version used before the Kubernetes1.8 version is BATCH/V2ALPHA1 and needs to be enabled at API Server startup:
--runtime-config=batch/v2alpha1=true
After version >1.8, the API version has been converted to BATCH/V1BETA1 and enabled by default.
Apiversion:batch/v1beta1kind:cronjobmetadata:name:hello #Cronjob的名称spec: Schedule: "*/1 * * * *" #job执行的周期, cron-formatted word Character string jobtemplate: #job模板 spec:template:spec:containers:-Name:hello-cronjob Image:10.3.1.15:5 000/ubuntu:16.04 command: ["Bash", "-C", "Date;echo Hello from the Kubernetes cluste"] #job具体执行的任务 restartpolicy: OnFailure
Create and view task status:
[Email protected]:# kubectl get cronjob NAME SCHEDULE SUSPEND ACTIVE last SCHEDULE agehello */1 * * * * False 0 <none> #刚创建还没有活跃的工作, nor any work planned
Then, every minute you execute kubectl get cronjob hello to view the status of the task, it is true that it is scheduled once per minute.
[email protected]# kubectl get cronjob hello name SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGEhello */1 * * * * False 1 Thu, 01 Feb 2018 06:22:00 +0000[email protected]:/data/yaml# kubectl get cronjob helloname schedule suspend active LAST SCHEDULE AGEhello */1 * * * * False 0 &Nbsp; thu, 01 feb 2018 06:22:00 +0000# can see that a job has been executed successfully within the specified time, At Last-schedule, there are currently 0 active jobs, which means that the job has completed or failed.
By finding the container for cronjob in node, you can see that a container is generated every other minute, and then exit normally after execution without restarting the container.
[email protected]:/# docker ps -a |grep hello-cronjob94f0e79db981 10.3.1.15:5000/ubuntu "bash -c ' Date;ech ..." 11 seconds ago Exited (0) 11 seconds ago k8s_hello-cronjob_hello-15174 ... 3d79b4e4a8f0 10.3.1.15:5000/ubuntu "bash -c ' Date;ech ..." about a minute ago exited (0) About a minute ago k8s_hello-cronjob_hello-15176...f5d99c855c77 10.3.1.15:5000/ubuntu "bash -c ' Date;ech ..." 2 minutes ago exited (0) 2 minutes ago k8s_ hello-cronjob_hello-15174 ... 0fd8f23cb624 10.3.1.15:5000/ubuntu "Bash -c&nbSP; ' Date;ech ... " 3 minutes ago Exited (0) 3 minutes ago k8s_hello-cronjob_hello-15174 ...
To view the logs for any container:
[Email protected]:/# docker logs 96f93b1dbf53thu Feb 1 06:08:19 UTC 2018Hello from the Kubernetes Cluste[email protected] :/# Docker logs 2271a37d07b4thu Feb 1 06:07:19 UTC 2018Hello from the Kubernetes cluste# two times the container is exactly one minute to execute the command.
Each time a task is executed, a container is generated, and when the task executes, it exits the container normally, according to the definition contains
Restart policy is onfailure so the container is no longer restarted, and the next time the task is executed, a container is started to execute,
This will not cause a lot of exited containers, but kubernetes
Don't let it happen, there's a field in cronjob that limits the number of history jobs:
. Spec.successfuljobshistorylimit: #成功完成的作业保存多少个, default is 3.spec.failedjobshistorylimit: #失败的作业保存多少个, the default is 1 # set to 0 is not saved. These two fields are siblings of the jobtemplate.
Use the following command to see in real time the history and status of each cron job triggering task execution on a regular basis:
[email protected]:/# kubectl get job --watch name DESIRED successful agehello-1517466900 1 1 2mhello-1517466960 1 1 1mhello-1517467500 1 1 46s where name is cronjob, each execution of the task starts with a pod successful column of 1, Indicates that the task executed successfully.
Find the pod created by the Cronjob resource object: You can use the above cronjob
[Email protected]:/# kubectl get pods--show-all-o wide |grep hello-1517467500hello-1517467500-brhlx 0/1 Com pleted 0 56s <none> 10.3.1.16
After you find the pod, look at its log:
[Email protected]:/# kubectl logs Hello-1517467980-q9dvkthu Feb 1 06:33:13 UTC 2018Hello from the Kubernetes cluste# formal front set Command output results
If you do not need this cronjob, delete it:
[Email protected]:/# kubectl Delete cronjob Hello
Cronjob is so simple.
Kubernetes's Cronjob timed task