Kubernetes implements cronjob to execute shell scripts regularly
Kubernetes cronjob regularly executes the role of shell scripts
Many existing tutorials only explain how to regularly execute a command through cronjob, but in the actual production process of using cronjob, especially when a large number of shell commands need to be executed, it often cannot meet the requirements. By executing a shell script associated with configmap, you can flexibly adjust the commands in the shell script and satisfy cronjob to run more complex shell commands.
A CronJob that periodically executes a shell script in Kubernetes has many uses, such as:
- Regular maintenance: CronJob can be set to perform certain maintenance tasks on a regular basis, such as cleaning up old log files, updating databases, maintaining indexes, etc.
- Data backup and recovery: You can create a CronJob to periodically back up the database or file system, and perform recovery operations if necessary.
- Periodic data processing: If your system needs to process data on a regular basis (eg every night), you can create a CronJob to process these tasks.
- Monitoring and reporting: CronJob can periodically perform monitoring tasks, such as checking the health of the system, or generating reports.
- Automatic Scaling and Scaling: You can create CronJobs that automatically scale up or down the number of running Pods as needed based on predicted system load.
- CronJob: CronJob can also be used to execute any task you want to run at a specific time.
1. Implementation process
1.1 Define configmap
cat <<EOF > script-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: script-configmap
data: #shell 脚本的命令,可以根据实际情况创建更复杂的命令
myscript.sh: |
#!/bin/bash
echo "Hello, Kubernetes!"
EOF
1.2 Define cronjob
cat <<EOF > my-cronjob.yaml
apiVersion: batch/v1 #kubernetes 1.21版本后请使用batch/v1,1.21前使用batch/v1beta1
kind: CronJob
metadata:
name: my-cronjob
spec:
schedule: "*/1 * * * *" #每分钟执行一次
concurrencyPolicy: Allow #并发策略,允许并发运行
jobTemplate:
spec:
template:
spec:
volumes:
- name: script-volume
configMap:
name: script-configmap
containers:
- name: my-container
image: centos
volumeMounts:
- name: script-volume
mountPath: /scripts
command: ["/bin/bash", "/scripts/myscript.sh"]
restartPolicy: OnFailure
EOF
Commonly configured api parameters for cronjob:
- spec.schedule: Scheduling, required field, specifies the task running cycle, the format is the same as Cron
- spec.jobTemplate: Job template, required field, specifies the task to be run, the format is the same as Job
- spec.startingDeadlineSeconds : The deadline for starting the Job (in seconds), this field is optional. If the scheduled time is missed for any reason, the job that missed the execution time will be considered to have failed. If not specified, no deadline
- spec.concurrencyPolicy: Concurrency policy, this field is also optional. It specifies how to handle concurrent execution of jobs created by Cron Job. Only one of the following policies is allowed to be specified:
a.Allow (default): Allow concurrent running of jobs
b.Forbid: Forbid concurrent running, if the previous one has not been completed, skip the next one directly
c.Replace: Cancel the current running job Job, replace it with a new one
Note that the current policy can only be applied to jobs created by the same cron job. If there are multiple cron jobs, the jobs they create are always allowed to run concurrently.
- spec.suspend : suspend, this field is also optional. If set to true, all subsequent executions will be suspended. It has no effect on jobs that have already started executing. The default value is false
- spec.successfulJobsHistoryLimit and .spec.failedJobsHistoryLimit: history limit, is an optional field. They specify how many completed and failed jobs can be kept
1.3 Check whether the cronjob is running correctly
#查看cronjob状态
kubectl get cronjob
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
my-cronjob */5 * * * * False 2 19s 23m
#查看job状态
kubectl get jobs
NAME COMPLETIONS DURATION AGE
my-cronjob-28096067 1/1 34s 3m38s
my-cronjob-28096068 1/1 34s 2m38s
my-cronjob-28096069 1/1 34s 98s
#查看pod状态
kubectl get po
NAME READY STATUS RESTARTS AGE
my-cronjob-28096067-qxtqg 0/1 Completed 0 5m54s
my-cronjob-28096068-7scqd 0/1 Completed 0 4m54s
my-cronjob-28096069-gtlzg 0/1 Completed 0 3m54s