【问题标题】:Kubernetes blocking until job stops runningKubernetes 阻塞直到作业停止运行
【发布时间】:2020-07-26 22:53:59
【问题描述】:

我想使用 kubectl wait 阻止脚本,直到我启动的作业或 pod 完成或失败。我想做类似kubectl wait --for=condition=* 的事情,但我找不到关于不同条件选项的好文档。 kubectl wait --for=condition=completed 似乎不适用于处于错误状态的作业。

【问题讨论】:

    标签: kubernetes


    【解决方案1】:

    请注意,目前 kubectl wait 在 kuberenetes 文档页面上被标记为实验性命令,因此它可能是开发中的一个功能,尚未完成。
    来自kubectl wait --helphttps://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#wait 的示例:

    Examples:
      # Wait for the pod "busybox1" to contain the status condition of type "Ready".
      kubectl wait --for=condition=Ready pod/busybox1
      
      # Wait for the pod "busybox1" to be deleted, with a timeout of 60s, after having issued the "delete" command.
      kubectl delete pod/busybox1
      kubectl wait --for=delete pod/busybox1 --timeout=60s
    

    文档还指出,--for 参数的唯一选项目前是 deletecondition=condition-name


    condition 字段出现在

    kubectl wait --for condition=condition-name pod pod-name
    

    pod.status.conditions中的条目。
    所以我们可以启动一个 pod 并查看 pod.status.conditions 中的字段,了解我们想要检查的条件。
    例如,我使用kubectl get pod pod-name -o yaml 查看了我的一个 pod 的 yaml,发现了以下条件

    status:
      conditions:
      - lastProbeTime: null
        lastTransitionTime: "2020-07-25T21:14:32Z"
        status: "True"
        type: Initialized
      - lastProbeTime: null
        lastTransitionTime: "2020-07-25T21:14:41Z"
        status: "True"
        type: Ready
      - lastProbeTime: null
        lastTransitionTime: "2020-07-25T21:14:41Z"
        status: "True"
        type: ContainersReady
      - lastProbeTime: null
        lastTransitionTime: "2020-07-25T21:14:32Z"
        status: "True"
        type: PodScheduled
    

    所以我们可以创建 pod 并阻塞,直到满足这些条件中的任何一个:

    kubectl wait --for condition=Initialized pod my-pod-name
    kubectl wait --for condition=Ready pod my-pod-name
    kubectl wait --for condition=ContainersReady pod my-pod-name
    kubectl wait --for condition=PodScheduled pod my-pod-name
    

    同样适用于工作:
    我创建了一个job,等待它完成,然后查看它的yaml,发现如下

    status:
      completionTime: "2020-07-28T17:24:09Z"
      conditions:
      - lastProbeTime: "2020-07-28T17:24:09Z"
        lastTransitionTime: "2020-07-28T17:24:09Z"
        status: "True"
        type: Complete
      startTime: "2020-07-28T17:24:06Z"
      succeeded: 1
    

    因此我们可以使用job.status.conditions中的条件,比如

    kubectl wait --for condition=Complete job job-name
    

    如果我们想处理作业无法完成的情况,我们当然可以添加超时

    kubectl wait --for condition=Complete job job-name --timeout 60s
    

    【讨论】:

    • 谢谢马克,如果我没看错你的评论,就没有好办法等待工作完成吗?我看到其他一些stackoverflow帖子引用条件=完成stackoverflow.com/questions/53536907/…。但我找不到有关其工作原理以及是否计入失败的详细信息
    • @software_intern 我已经更新了答案,举了一个等待工作完成的例子。简而言之,使用kubectl wait --for condition=complete job job-name --timeout 60s。但是您可能会发现您创建的工作中存在更多条件,只需使用kubectl get job job-name -o yaml 进行检查并查看 status.conditions
    猜你喜欢
    • 2013-07-05
    • 1970-01-01
    • 1970-01-01
    • 2010-09-20
    • 1970-01-01
    • 2019-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多