0
点赞
收藏
分享

微信扫一扫

CKAD备考之一——创建CronJob

witmy 03-11 17:31 阅读 4

CronJob

#设置配置环境
[candidate@node-1] $ kubectl config use-context k8s
  1. 创建一个名为 ppi 并执行一个运行以下单一容器的 Pod 的 CronJob
- name: pi
  image: perl:5
  command: ["perl", " Mbignum=bpi", " wle", "print bpi(2000)"]
  • CronJob配置 为:
    • 每隔 5 分钟执行一次
    • 保留 2 个已完成的 Job
    • 保留 4 个失败的 Job
    • 永不重启 Pod
    • 在 8 秒后终止 Pod
  1. 为测试目的,从 CronJob ppi 中 手动创建并执行一个名为 ppi-test 的 Job 。
    • job完成与否不重要

解法: kubernetes.io网页,搜索job,选择cronjob https://kubernetes.io/zh-cn/docs/tasks/job/automated-tasks-with-cron-jobs/ 页面内会找到yaml文件,复制

apiVersion: batch/v1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "* * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox:1.28
            imagePullPolicy: IfNotPresent
            command:
            - /bin/sh
            - -c
            - date; echo Hello from the Kubernetes cluster
          restartPolicy: OnFailure

命令

[root@test01 ~]# kubectl explain cronjob.spec | grep -i history
  failedJobsHistoryLimit        <integer>
  successfulJobsHistoryLimit    <integer>
[root@test01 ~]# kubectl explain cronjob.spec | grep -i template
  jobTemplate   <JobTemplateSpec> -required-
[root@test01 ~]# kubectl explain cronjob.spec.jobTemplate.spec | grep active
  activeDeadlineSeconds <integer>
    be continuously active before the system tries to terminate it; value must
    false to true), the Job controller will delete all active Pods associated
[root@test01 ~]#
vim 01-cronjob.yaml
apiVersion: batch/v1
kind: CronJob
metadata:
  name: ppi #修改为对应的
spec:
  schedule: "*/5 * * * *" #根据题目要求修改,5分钟一次
  successfulJobsHistoryLimit: 2 # 成功记录次数
  failedJobsHistoryLimit: 4     # 失败记录次数
  jobTemplate:
    spec:
      activeDeadlineSeconds: 8  # pod存活时间
      template:
        spec:
          containers:
          - name: pi            # 名称
            image: perl:5       # 镜像
            imagePullPolicy: IfNotPresent
            command: ["perl", "Mbignum=bpi", " wle", "print bpi(2000)"] #题目命令
          restartPolicy: Never  # 重启策略
#创建
kubectl apply -f 01-cronjob.yaml
#验证
kubectl get cronjobs

image.png

#手动触发cronjob,可以用kubectl create job --help查看参数
kubectl create job ppi-test --from=cronjob/ppi
#查看结果
kubectl get job

image.png

举报

相关推荐

0 条评论