如何在kubernetes中更新一组pods 运行?

How to update a set of pods running in kubernetes?

更新由 kubernetes 中的复制控制器控制的一组 pods(例如,在进行代码更改并将底层 docker 映像推送到 docker 集线器之后)的首选方法是什么集群?

我可以看到两种方式:

  1. 正在手动删除和重新创建复制控制器
  2. 使用kubectl rolling-update

对于 rolling-update 我必须更改复制控制器名称。由于我将复制控制器定义存储在 YAML 文件中而不是手动生成它,因此必须更改文件以推出代码更新似乎会带来坏习惯,例如在复制控制器的 2 个名称(例如 controllerA 和 controllerB)之间交替使用避免名称冲突。

更好的方法是什么?

更新:kubectl rolling-update 已弃用,替换命令为 kubectl rollout。另请注意,自从我写了原始答案以来,部署资源已被添加并且是比 ReplicaSets 更好的选择,因为滚动更新是在服务器端而不是客户端执行的。


你应该使用 kubectl rolling-update。我们最近添加了一项功能来执行 "simple rolling update",它将更新复制控制器中的图像而不重命名它。这是 kubectl help rolling-update 输出中显示的最后一个示例:

// Update the pods of frontend by just changing the image, and keeping the old name
$ kubectl rolling-update frontend --image=image:v2

此命令还支持恢复——如果您取消更新并稍后重新启动,它将从停止的地方恢复。尽管它在幕后创建了一个新的复制控制器,但在更新结束时,新的复制控制器采用了旧复制控制器的名称,因此它看起来是纯粹的更新,而不是切换到一个全新的复制控制器。

目前我找到的最佳选择是 Skaffold, which automatically builds the image, pushes it the image registry and updates the corresponding pods/controllers. It can even watch for code changes and rebuild the image as soon as changes are saved with skaffold dev command. This only requires adding a simple skaffold.yaml that specifies the image on the registry and path to the Kubernetes manifests. This workflow is described in details in the Getting Started guide

The following explanations are from Kubernetes In Action's book

Deleting & re-creating replication controller manually

进行滚动更新手动laboriouserror-prone.根据副本的数量,您需要 运行 按正确顺序执行十几个或更多命令才能执行更新过程。幸运的是 ,Kubernetes 允许您执行使用单个命令滚动更新。

Using kubectl rolling-update

您可以让 kubectl 执行滚动更新,而不是手动使用 ReplicationControllers 执行滚动更新。使用 kubectl 执行更新使这个过程变得更加容易,但是,现在这是一种过时的更新应用程序的方式。

为什么执行这样的更新不如预期的好是因为它势在必行。 Kubernetes 是如何告诉它系统的理想状态,并让 Kubernetes 自己实现该状态,通过找出最好的方法来做到这一点。

Using Deployments for updating apps declaratively --THE BEST ALTERNATIVE--

Deployment 是一种更高级别的资源,用于部署应用程序并以声明方式更新它们,而不是通过 ReplicationController 或 ReplicaSet 来完成,这两者都被认为是较低级别的概念。

使用 Deployment 而不是较低级别的构造使更新应用程序变得更加容易,因为您通过单个 Deployment 资源定义所需的状态并让 Kubernetes 处理其余部分。

还有一件事,回滚 部署是可能的。