将相同的标签推送到容器注册表时自动进行 Kubernetes 滚动更新

Automated Kubernetes rolling update when the same tag is pushed to a container registry

我经常使用 Azure 容器注册表上的相同标签更新 Docker 图像。我想在将新标签推送到我的 ACR 时自动 运行 滚动更新,因此 pods 会自动更新为最新版本的图像。

ACR 提供了一个 Webhook 功能,可以在推送新标签时自动调用 webhook。

我的第一种方法是在 Kubernetes 中创建通过入口公开并可从 ACR 调用的 webhook。 Webhook 将执行滚动更新,但它需要访问 Kubernetes API.

可能吗?我的方向是否正确,或者是否有更方便的方式来回答我的要求?

谢谢!

是的,这是可能的。如果 ACR 可以在每次推送新图像(即使具有相同标签)时调用 webhook,则您可以在集群中调用 webhook 服务。例如,该服务然后可以更新 Deployment 的 PodTemplateSpec 中的注释,这会触发 Pods.

的滚动更新

但是需要container的imagePullPolicy设置为Always,因为镜像的tag保持不变。否则,Kubernetes 会认为它仍然是同一个镜像,并会使用它之前拉取的镜像。

要访问 Kubernetes API,您可以在 PodSpec 中使用 ServiceAccount to which you assign the necessary Kubernetes API permissions with Roles and RoleBindings. Finally, you assign this ServiceAccount to your Pods through the serviceAccountName 字段。