无法更新 K8S 中 ReplicationController 的 pod 镜像

Not able to update the pod images of a ReplicationController in K8S

我使用以下命令创建了一个 ReplicationController。

kubectl run nginx --image=nginx -r=2 --generator=run/v1

现在我尝试将映像升级到版本 1.7.1。

kubectl set image rc/nginx nginx=nginx:1.7.1

但是,图像似乎没有更新。

watch -n1 "kubectl describe pods | grep "Image:""

也尝试了 kubectl edit ....kubectl apply -f .... 命令,但图像没有得到更新。

如何在 K8S ReplicationController 中更新镜像?

文档中描述了如何在复制控制器上进行滚动升级https://kubernetes.io/docs/concepts/workloads/controllers/replicationcontroller/#rolling-updates

您需要知道实际上您的图像已在复制控制器中更新,但复制控制器不会杀死现有的 pods 并使用新图像生成新图像。因此,要实现这一点,您需要执行以下两个选项之一:

  1. 手动杀pods
  2. 将您的 RC 缩放到 0 以杀死 pods,然后使用以下命令将副本缩放到所需数量 kubectl scale --replicas=3 rc/nginx

复制控制器只能扩展给定 pod 的副本数量,不能进行任何更新。

有一种方法可以使用 kubectl rolling-update "update" 你的 ReplicationController,但它不会按字面意思更新它。 当你 运行 kubectl rolling-update (link1):

时发生了什么
  1. Creating a new replication controller with the updated configuration.
  2. Increasing/decreasing the replica count on the new and old controllers until the correct number of replicas is reached.
  3. Deleting the original replication controller.

Rolling updates are initiated with the kubectl rolling-update command:

$ kubectl rolling-update NAME \
    ([NEW_NAME] --image=IMAGE | -f FILE)

假设我们有一个名为 foo 的当前复制控制器,它是 运行ning 映像 image:v1 (link2)

kubectl rolling-update foo [foo-v2] --image=myimage:v2

If the user doesn't specify a name for the 'next' replication controller, then the 'next' replication controller is renamed to the name of the original replication controller.

以下是 kubectl reference 中的更多示例:

Update pods of frontend-v1 using new replication controller data in frontend-v2.json.

kubectl rolling-update frontend-v1 -f frontend-v2.json

Update pods of frontend-v1 using JSON data passed into stdin.

cat frontend-v2.json | kubectl rolling-update frontend-v1 -f -

Update the pods of frontend-v1 to frontend-v2 by just changing the image, and switching the # name of the replication controller.

kubectl rolling-update frontend-v1 frontend-v2 --image=image:v2

Update the pods of frontend by just changing the image, and keeping the old name.

kubectl rolling-update frontend --image=image:v2

Abort and reverse an existing rollout in progress (from frontend-v1 to frontend-v2).

kubectl rolling-update frontend-v1 frontend-v2 --rollback

ReplicationController (link3)

替代方案

ReplicaSet(还是不支持更新Pod的镜像)

ReplicaSet is the next-generation ReplicationController that supports the new set-based label selector. It’s mainly used by Deployment as a mechanism to orchestrate pod creation, deletion and updates. Note that we recommend using Deployments instead of directly using Replica Sets, unless you require custom update orchestration or don’t require updates at all.

Deployment(推荐)(它作为 ReplicaSets 的编排器,因此它支持设计更新)

Deployment is a higher-level API object that updates its underlying Replica Sets and their Pods in a similar fashion as kubectl rolling-update. Deployments are recommended if you want this rolling update functionality, because unlike kubectl rolling-update, they are declarative, server-side, and have additional features.

kubectl run nginx1 --image nginx --replicas=3
kubectl get deployment nginx1 --export -o yaml