Kubernetes如何进行部署以更新映像 [英] Kubernetes how to make Deployment to update image

查看:185
本文介绍了Kubernetes如何进行部署以更新映像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确实使用单个pod进行了部署,并使用了我的自定义docker镜像,例如:

I do have deployment with single pod, with my custom docker image like:

containers:
  - name: mycontainer
    image: myimage:latest

在开发过程中,我想推送新的最新版本并更新Deployment. 如果没有明确定义标签/版本并为每个版本增加它,就找不到方法.

During development I want to push new latest version and make Deployment updated. Can't find how to do that, without explicitly defining tag/version and increment it for each build, and do

kubectl set image deployment/my-deployment mycontainer=myimage:1.9.1

推荐答案

您可以为Pod配置宽限期(例如30秒或更长时间,具体取决于容器启动时间和图像大小),并设置"imagePullPolicy: "Always".并使用kubectl delete pod pod_name. 将创建一个新容器,并自动下载最新的映像,然后终止旧容器.

You can configure your pod with a grace period (for example 30 seconds or more, depending on container startup time and image size) and set "imagePullPolicy: "Always". And use kubectl delete pod pod_name. A new container will be created and the latest image automatically downloaded, then the old container terminated.

示例:

spec:
  terminationGracePeriodSeconds: 30
  containers:
  - name: my_container
    image: my_image:latest
    imagePullPolicy: "Always"

我目前正在使用Jenkins进行自动构建和图像标记,看起来像这样:

I'm currently using Jenkins for automated builds and image tagging and it looks something like this:

kubectl --user="kube-user" --server="https://kubemaster.example.com"  --token=$ACCESS_TOKEN set image deployment/my-deployment mycontainer=myimage:"$BUILD_NUMBER-$SHORT_GIT_COMMIT"

另一种技巧是初始运行:

Another trick is to intially run:

kubectl set image deployment/my-deployment mycontainer=myimage:latest

然后:

kubectl set image deployment/my-deployment mycontainer=myimage

它实际上会触发滚动更新,但是请确保您还设置了imagePullPolicy: "Always".

It will actually be triggering the rolling-update but be sure you have also imagePullPolicy: "Always" set.

更新:

我发现的另一个技巧是,无需更改图像名称,而是更改将触发滚动更新的字段的值,例如terminationGracePeriodSeconds.您可以使用kubectl edit deployment your_deploymentkubectl apply -f your_deployment.yaml或使用类似以下的补丁程序来完成此操作:

another trick I found, where you don't have to change the image name, is to change the value of a field that will trigger a rolling update, like terminationGracePeriodSeconds. You can do this using kubectl edit deployment your_deployment or kubectl apply -f your_deployment.yaml or using a patch like this:

kubectl patch deployment your_deployment -p \
  '{"spec":{"template":{"spec":{"terminationGracePeriodSeconds":31}}}}'

只需确保您始终更改数字值即可.

Just make sure you always change the number value.

这篇关于Kubernetes如何进行部署以更新映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆