首页 > 解决方案 > 重新部署最新镜像而不使用`kubectl apply -f file`

问题描述

我们在集群上进行了部署,我们想告诉它提取最新的映像并重新部署。如果我运行kubectl apply -f deployment.yml该文件实际上并没有改变。我如何告诉集群使用更新的映像版本?

标签: kubectlamazon-eks

解决方案


根据文档:

注意:当且仅当 Deployment 的 pod 模板(即 .spec.template)发生更改时才会触发 Deployment 的推出,例如,如果模板的标签或容器映像被更新。其他更新(例如扩展部署)不会触发部署。

请考虑使用:

kubectl patch deployment my-nginx --patch '{"spec": {"template": {"spec": {"containers": [{"name": "nginx","image": "nginx:1.7.9"}]}}}}'
    
kubectl --record deployment.apps/nginx-deployment set image deployment.v1.apps/nginx-deployment nginx=nginx:1.9.1
    
kubectl edit deploy/<your_deployment> --record

有关更新部署容器映像的文档。

根据最佳实践,请注意:

在生产环境中部署容器时,您应该避免使用:latest标签,因为很难跟踪哪个版本的映像正在运行并且更难以正确回滚。

但是,如果您想始终强制拉新图像,您可以使用这些选项

   - set the imagePullPolicy of the container to Always.
   - omit the imagePullPolicy and use :latest as the tag for the image to use.
   - omit the imagePullPolicy and the tag for the image to use.
   - enable the AlwaysPullImages admission controller.

希望这有帮助。


推荐阅读