当前位置: 首页 > 工具软件 > wed-job > 使用案例 >

kubernetes使用 (十三)controller控制器--job&cronjob

程和蔼
2023-12-01

案例  job 一次性任务

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    #pod执行完毕后不会重启
  backoffLimit: 4       #执行失败后的重试次数

部署

kubectl create -f job.yaml

查看

[root@k8s-master01 ~]# kubectl get pod
NAME       READY   STATUS      RESTARTS   AGE
pi-gjtwg   0/1     Completed   0          2m16s

查看日志信息

[root@k8s-master01 ~]# kubectl logs pi-gjtwg
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803
4825342117067982148086513282306647093844609550582231725359408128481117450284102701938521
1055596446229489549303819644288109756659334461284756482337867831652712019091456485669234
6034861045432664821339360726024914127372458700660631558817488152092096282925409171536436
7892590360011330530548820466521384146951941511609433057270365759591953092186117381932611
7931051185480744623799627495673518857527248912279381830119491298336733624406566430860213
9494639522473719070217986094370277053921717629317675238467481846766940513200056812714526
3560827785771342757789609173637178721468440901224953430146549585371050792279689258923542
0199561121290219608640344181598136297747713099605187072113499999983729780499510597317328
1609631859502445945534690830264252230825334468503526193118817101000313783875288658753320
8381420617177669147303598253490428755468731159562863882353787593751957781857780532171226
8066130019278766111959092164201989380952572010654858632788659361533818279682303019520353
0185296899577362259941389124972177528347913151557485724245415069595082953311686172785588
9075098381754637464939319255060400927701671139009848824012858361603563707660104710181942
9555961989467678374494482553797747268471040475346462080466842590694912933136770289891521
0475216205696602405803815019351125338243003558764024749647326391419927260426992279678235
4781636009341721641219924586315030286182974555706749838505494588586926995690927210797509
3029553211653449872027559602364806654991198818347977535663698074265425278625518184175746
7289097777279380008164706001614524919217321721477235014144197356854816136115735255213347
5741849468438523323907394143334547762416862518983569485562099219222184272550254256887671
7904946016534668049886272327917860857843838279679766814541009538837863609506800642251252
0511739298489608412848862694560424196528502221066118630674427862203919494504712371378696
09563643719172874677646575739624138908658326459958133904780275901

 

案例 CronJob 定时任务

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

部署

kubectl create -f cronjob.yaml

[root@k8s-master01 ~]# kubectl get pod
NAME                     READY   STATUS      RESTARTS   AGE
hello-1611124860-dgvkt   0/1     Completed   0          27s


#这是完成了一次
查看日志
[root@k8s-master01 ~]# kubectl logs hello-1611124860-dgvkt
Wed Jan 20 06:41:20 UTC 2021
Hello from the Kubernetes cluster


#再过1分钟
[root@k8s-master01 ~]# kubectl logs hello-1611124860-dgvkt
Wed Jan 20 06:41:20 UTC 2021
Hello from the Kubernetes cluster
[root@k8s-master01 ~]# kubectl logs hello-1611124860-dgvkt
Wed Jan 20 06:41:20 UTC 2021
Hello from the Kubernetes cluster


#pod也会被新建一个
[root@k8s-master01 ~]# kubectl get pod
NAME                     READY   STATUS      RESTARTS   AGE
hello-1611124860-dgvkt   0/1     Completed   0          98s
hello-1611124920-8fkf4   0/1     Completed   0          38s   每次执行都会新建一个任务pod

 

 类似资料: