检查在CI / CD管道中Kubernetes部署是否成功

时间:2020-05-11 18:39:17

标签: azure kubernetes deployment continuous-integration azure-aks

我有一个使用Kubernetes版本1.14.7的AKS集群。

我已经构建了CI / CD管道,以将新创建​​的映像部署到集群。

我正在使用kubectl apply用新映像更新特定的部署。有时由于多种原因,部署会失败,例如ImagePullBackOff。

在kubectl apply命令之后是否有命令要运行以检查Pod创建和部署是否成功?

4 个答案:

答案 0 :(得分:1)

为此,Kubernetes具有kubectl rollout,您应该使用选项status

默认情况下,“发布状态”将监视最新发布的状态,直到完成为止。如果您不想等待发布完成,则可以使用--watch = false。请注意,如果在其间开始新的推出,则“推出状态”将继续观看最新的修订版。如果要固定到某个特定修订版本,并且如果该修订版本已被另一个修订版本覆盖,则中止操作,请使用--revision = N,其中N是您需要注意的修订版本。

您可以阅读完整的说明here

如果您使用kubect apply -f myapp.yaml并选中rollout status,则会看到:

$ kubectl rollout status deployment myapp
Waiting for deployment "myapp" rollout to finish: 0 of 3 updated replicas are available…
Waiting for deployment "myapp" rollout to finish: 1 of 3 updated replicas are available…
Waiting for deployment "myapp" rollout to finish: 2 of 3 updated replicas are available…
deployment "myapp" successfully rolled out

答案 1 :(得分:0)

您可以通过jq解析输出:

kubectl get pod -o=json | jq '.items[]|select(any( .status.containerStatuses[]; .state.waiting.reason=="ImagePullBackOff"))|.metadata.name'

答案 2 :(得分:0)

kubediff工具似乎很适合您的任务:

Kubediff是Kubernetes的工具,用于向您显示运行配置和版本控制配置之间的差异。

该工具可以从命令行使用,并且可以用作群集中的Pod,以连续比较配置的存储库中的YAML文件与群集的当前状态。

$ ./kubediff
Usage: kubediff [options] <dir/file>...

Compare yaml files in <dir> to running state in kubernetes and print the
differences.  This is useful to ensure you have applied all your changes to the
appropriate environment.  This tools runs kubectl, so unless your
~/.kube/config is configured for the correct environment, you will need to
supply the kubeconfig for the appropriate environment.
发现差异时,

kubediff会将状态返回到stdout并返回非零退出代码。您可以使用命令行参数更改此行为。

您可能还想查看有关验证YAML文件的好文章:

答案 3 :(得分:0)

还有另一种方法可以使用配置的超时等待部署变得可用

$1,$2,$3,$4

否则可以使用 kubectl wait --for=condition=available --timeout=60s deploy/myapp,但在极少数情况下它可能会永远卡住,如果发生这种情况,将需要手动取消管道。