gpt4 book ai didi

kubernetes - Kubernetes阻塞直到作业停止运行

转载 作者:行者123 更新时间:2023-12-02 12:00:31 24 4
gpt4 key购买 nike

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

最佳答案

请注意,当前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
出现在
kubectl wait --for condition=condition-name pod pod-name
引用 condition中的条目。
因此,我们可以启动一个pod并查看 pod.status.conditions中的字段以了解我们要检查的条件。
例如,我使用 pod.status.conditions查看了我的一个 pods 的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

同样适用于工作:
我创建了一个工作,等待它完成,然后查看它的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
因此,我们可以使用 kubectl get pod pod-name -o yaml中的条件,例如
kubectl wait --for condition=Complete job job-name
如果我们想处理作业无法完成的情况,我们当然可以添加超时
kubectl wait --for condition=Complete job job-name --timeout 60s

关于kubernetes - Kubernetes阻塞直到作业停止运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63106603/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com