使用命令行等待 kubernetes 作业在失败/成功时完成 [英] Wait for kubernetes job to complete on either failure/success using command line

查看:21
本文介绍了使用命令行等待 kubernetes 作业在失败/成功时完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

等待 kubernetes 作业完成的最佳方法是什么?我注意到很多使用建议:

kubectl wait --for=condition=complete job/myjob

但我认为只有在工作成功时才​​有效.如果失败,我必须执行以下操作:

kubectl wait --for=condition=failure job/myjob

有没有办法使用wait来等待这两种情况?如果没有,等待工作成功或失败的最佳方法是什么?

解决方案

kubectl wait --for=condition=<condition name 正在等待一个特定的条件,所以 afaik 它不能指定多个目前的情况.

我的解决方法是使用 oc get --wait,如果目标资源更新,--wait 会关闭命令.我将使用 oc get --wait 监视作业的 status 部分,直到 status 更新.status 部分的更新意味着 Job 在某些状态条件下完成.

如果作业成功完成,则 status.conditions.type 会立即更新为 Complete.但是,如果作业失败,则无论 restartPolicyOnFailure 还是 Never,作业 Pod 都会自动重新启动.但是如果在第一次更新后不更新为Complete,我们可以认为该作业处于Failed状态.

看看我的测试证据如下.

  • 用于测试成功的作业 yaml 完成
<前># vim job.ymlapi版本:批处理/v1种类:工作元数据:名称:pi规格:并行度:1完成数:1模板:元数据:名称:pi规格:容器:- 名称:pi图片:perl命令:["perl", "-w​​le", "exit 0"]重启策略:从不

  • 如果它成功完成工作,它会显示Complete.
<前># oc create -f job.yml &&oc get job/pi -o=jsonpath='{.status}' -w &&oc get job/pi -o=jsonpath='{.status.conditions[*].type}' |grep -i -E '失败|完成' ||回声失败"job.batch/pi 创建地图[开始时间:2019-03-09T12:30:16Z活动:1]完成

  • 用于测试的作业 yaml 失败
<前># vim job.ymlapi版本:批处理/v1种类:工作元数据:名称:pi规格:并行度:1完成数:1模板:元数据:名称:pi规格:容器:- 名称:pi图片:perl命令:["perl", "-w​​le", "exit 1"]重启策略:从不

  • 如果第一个作业更新不是Complete,它会显示Failed.删除现有作业资源后测试是否.
<前># oc 删除作业 pijob.batch "pi" 删除# oc create -f job.yml &&oc get job/pi -o=jsonpath='{.status}' -w &&oc get job/pi -o=jsonpath='{.status.conditions[*].type}' |grep -i -E '失败|完成' ||回声失败"job.batch/pi 创建地图[active:1 startTime:2019-03-09T12:31:05Z]失败

希望对你有帮助.:)

What is the best way to wait for kubernetes job to be complete? I noticed a lot of suggestions to use:

kubectl wait --for=condition=complete job/myjob

but i think that only works if the job is successful. if it fails, i have to do something like:

kubectl wait --for=condition=failure job/myjob

is there a way to wait for both conditions using wait? if not, what is the best way to wait for a job to either succeed or fail?

解决方案

kubectl wait --for=condition=<condition name is waiting for a specific condition, so afaik it can not specify multiple conditions at the moment.

My workaround is using oc get --wait, --wait is closed the command if the target resource is updated. I will monitor status section of the job using oc get --wait until status is updated. Update of status section is meaning the Job is complete with some status conditions.

If the job complete successfully, then status.conditions.type is updated immediately as Complete. But if the job is failed then the job pod will be restarted automatically regardless restartPolicy is OnFailure or Never. But we can deem the job is Failed status if not to updated as Complete after first update.

Look the my test evidence as follows.

  • Job yaml for testing successful complete

    # vim job.yml
    apiVersion: batch/v1
    kind: Job
    metadata:
      name: pi
    spec:
      parallelism: 1
      completions: 1
      template:
        metadata:
          name: pi
        spec:
          containers:
          - name: pi
            image: perl
            command: ["perl",  "-wle", "exit 0"]
          restartPolicy: Never

  • It will show you Complete if it complete the job successfully.

    # oc create -f job.yml &&
      oc get job/pi -o=jsonpath='{.status}' -w &&
      oc get job/pi -o=jsonpath='{.status.conditions[*].type}' | grep -i -E 'failed|complete' || echo "Failed" 

    job.batch/pi created
    map[startTime:2019-03-09T12:30:16Z active:1]Complete

  • Job yaml for testing failed complete

    # vim job.yml
    apiVersion: batch/v1
    kind: Job
    metadata:
      name: pi
    spec:
      parallelism: 1
      completions: 1
      template:
        metadata:
          name: pi
        spec:
          containers:
          - name: pi
            image: perl
            command: ["perl",  "-wle", "exit 1"]
          restartPolicy: Never

  • It will show you Failed if the first job update is not Complete. Test if after delete the existing job resource.

    # oc delete job pi
    job.batch "pi" deleted

    # oc create -f job.yml &&
      oc get job/pi -o=jsonpath='{.status}' -w &&
      oc get job/pi -o=jsonpath='{.status.conditions[*].type}' | grep -i -E 'failed|complete' || echo "Failed" 

    job.batch/pi created
    map[active:1 startTime:2019-03-09T12:31:05Z]Failed

I hope it help you. :)

这篇关于使用命令行等待 kubernetes 作业在失败/成功时完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆