【发布时间】:2020-07-26 22:53:59
【问题描述】:
我想使用 kubectl wait 阻止脚本,直到我启动的作业或 pod 完成或失败。我想做类似kubectl wait --for=condition=* 的事情,但我找不到关于不同条件选项的好文档。 kubectl wait --for=condition=completed 似乎不适用于处于错误状态的作业。
【问题讨论】:
标签: kubernetes
我想使用 kubectl wait 阻止脚本,直到我启动的作业或 pod 完成或失败。我想做类似kubectl wait --for=condition=* 的事情,但我找不到关于不同条件选项的好文档。 kubectl wait --for=condition=completed 似乎不适用于处于错误状态的作业。
【问题讨论】:
标签: kubernetes
请注意,目前 kubectl wait 在 kuberenetes 文档页面上被标记为实验性命令,因此它可能是开发中的一个功能,尚未完成。
来自kubectl wait --help 和https://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 参数的唯一选项目前是 delete 和 condition=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
【讨论】:
kubectl wait --for condition=complete job job-name --timeout 60s。但是您可能会发现您创建的工作中存在更多条件,只需使用kubectl get job job-name -o yaml 进行检查并查看 status.conditions