k8s-----14、controller-Job和Cronjob

Job和Cronjob

  • 1、Job(一次性任务)
  • 2、CronJob(定时任务)

1、Job(一次性任务)

[root@master controlleryaml]# cat job.yaml 
apiVersion: batch/v1
kind: Job
metadata:
  name: pi
spec:
  template:
    spec:
      containers:
      - name: pi
        image: perl   
        command: ["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]
      restartPolicy: Never
  backoffLimit: 4
[root@master controlleryaml]# kubectl create -f job.yaml 
[root@master controlleryaml]# kubectl get pods  #Completed表示任务已完成
NAME                  READY   STATUS      RESTARTS   AGE
pi-274c4              0/1     Completed   0          2m52s
[root@master controlleryaml]# kubectl get jobs   ##查看任务
NAME   COMPLETIONS   DURATION   AGE
pi     1/1           2m50s      4m4s
[root@master controlleryaml]# kubectl logs pi-274c4   ##查看相应pod的日志信息
[root@master controlleryaml]# kubectl delete -f job.yaml   #删除一次性任务

2、CronJob(定时任务)

[root@master controlleryaml]# cat cronjob.yaml 
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            args:
            - /bin/sh
            - -c
            - date; echo Hello from the Kubernetes cluster
          restartPolicy: OnFailure

[root@master controlleryaml]#  kubectl get pods
NAME                     READY   STATUS      RESTARTS   AGE
hello-1652154720-tcx6z   0/1     Completed   0          38s

[root@master controlleryaml]# kubectl get cronjobs.batch 
NAME    SCHEDULE      SUSPEND   ACTIVE   LAST SCHEDULE   AGE
hello   */1 * * * *   False     0        47s             92s
[root@master controlleryaml]# kubectl logs hello-1652154720-tcx6z
Tue May 10 03:52:17 UTC 2022
Hello from the Kubernetes cluster

你可能感兴趣的:(k8s新学习目录,kubernetes,容器,云原生)