kubernetes 在命令前加上超时以确保pod创建在'kubectl wait'之前

8e2ybdfx  于 5个月前  发布在  Kubernetes
关注(0)|答案(2)|浏览(61)

在这两个指令之间是否有一个命令可以执行(如果pod还不存在,则该命令将失败),并将在指定的超时内等待pod在api-server中创建?

kubectl run mypod --image=nginx 
# Add a command which qait for 'kubectl get pod' to return mypod,
# so that 'kubectl wait' command will not fail
<wanted command>
kubectl wait pod/mypod --for=condition=Ready -l olm.catalogSource=argocd-catalog --timeout=120s

字符串

iszxjhcz

iszxjhcz1#

根据Github链接,您可以使用下面的命令作为解决方案。
kubectl waittimeout参数的文档记录不好,不适合等待多个资源。您可以在kubectl waitoc wait之前使用timeout。例如,timeout为max. 305s(timeout命令的timeout应该比kubectl命令的timeout大**一点)

timeout $((300+5)) kubectl wait --for=condition=Ready --all pod --timeout=300s

字符串

lf5gs5x2

lf5gs5x22#

您可以在命令之间使用&&(和)和||(或)创建组合命令,以实现如何根据不同条件运行。
以下组合命令尝试运行/创建mypod。如果在特定超时内创建,则将创建mypod。在||之后,delete命令不会运行,因为前一个命令工作正常。

kubectl run mypod --image=nginx && kubectl wait --for=condition=Ready pod/mypod --timeout=20s || kubectl delete pod mypod

字符串
输出量:

pod/mypod created
pod/mypod condition met

**最坏情况:**以下命令尝试运行nginx 2(由于ImagePullBackOff的原因,无法运行)。它首先创建了pod,但pod尚未就绪,因此它将在||之后运行delete命令。

kubectl run mypod --image=nginx2 && kubectl wait --for=condition=Ready pod/mypod --timeout=20s || kubectl delete pod mypod


输出量:

pod/mypod created
error: timed out waiting for the condition on pods/mypod
pod "mypod" deleted


您可以使用get pods创建类似的组合,而不是创建pod。

相关问题