# 官方提供@yunTaoScripts JOB 🔥🔥

loading

# 创建Job

kubectl create job my-job --image=busybox --dry-run=client -oyaml > myjob1.yam

apiVersion: batch/v1
kind: Job
metadata:
  creationTimestamp: null
  name: my-job
spec:
  backoffLimit: 6 
  completions: 10 
  parallelism: 4
  template:
    metadata:
      creationTimestamp: null
    spec:
      containers:
      - image: busybox
        command: ['sh','-c','echo "hello $(date)"']
        imagePullPolicy: IfNotPresent
        name: my-job
        resources: {}
      restartPolicy: Never
status: {}
  • 计算圆周率
apiVersion: batch/v1
kind: Job
metadata:
  creationTimestamp: null
  name: pi
spec:
  activeDeadlineSeconds: 10
  backoffLimit: 6 
  completions: 10 
  parallelism: 1
  template:
    metadata:
      creationTimestamp: null
    spec:
      terminationGracePeriodSeconds: 0
      containers:
      - image: perl
        command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(20000)"]
        imagePullPolicy: IfNotPresent
        name: pi
        resources: {}
      restartPolicy: Never
status: {}

# 创建CronJob

kubectl create cronjob my-cronjob --image=nginx --schedule="*/1 * * * *" -- echo "hello"

apiVersion: batch/v1
kind: CronJob
metadata:
  creationTimestamp: "2022-05-21T07:23:04Z"
  generation: 1
  name: my-cronjob
  namespace: 09-job
  resourceVersion: "1196989"
  uid: eeb9e3d3-7327-4a00-8fe5-4b5947e54b42
spec:
  concurrencyPolicy: Allow
  failedJobsHistoryLimit: 1
  jobTemplate:
    metadata:
      creationTimestamp: null
      name: my-cronjob
    spec:
      template:
        metadata:
          creationTimestamp: null
        spec:
          containers:
          - command:
            - echo
            - hello
            image: nginx
            imagePullPolicy: Always
            name: my-cronjob
            resources: {}
            terminationMessagePath: /dev/termination-log
            terminationMessagePolicy: File
          dnsPolicy: ClusterFirst
          restartPolicy: OnFailure
          schedulerName: default-scheduler
          securityContext: {}
          terminationGracePeriodSeconds: 30
  schedule: '*/1 * * * *'
  successfulJobsHistoryLimit: 3
  suspend: false
status: {}

参数解释

  • job的restart策略只能是:
    • Nerver 只要任务没有完成,则是新创建pod运行,直到job完成会产生多个pod
    • OnFailure 只要pod没有完成,则会重启pod,直到job完成
  • parallelism: N
    • 一次性运行N个pod
  • completions: M
    • job结束需要成功运行的Pod个数,即状态为Completed的pod数
  • backoffLimit: N
    • 如果job失败,则重试几次 这里parallelism的值指的是一次性运行几个pod,这个值不会超过completions的值。
  • activeDeadlineSeconds: 10
    • pod 执行时间,超时删除。 在job失败的情况下,job必须要完成一次