简体   繁体   English

Kubernetes:kubectl apply在使用“latest”标签时不会更新pod

[英]Kubernetes: kubectl apply does not update pods when using “latest” tag

I'm using kubectl apply to update my Kubernetes pods: 我正在使用kubectl apply来更新我的Kubernetes pods:

kubectl apply -f /my-app/service.yaml
kubectl apply -f /my-app/deployment.yaml

Below is my service.yaml: 以下是我的service.yaml:

apiVersion: v1
kind: Service
metadata:
  name: my-app
  labels:
    run: my-app
spec:
  type: NodePort
  selector:
    run: my-app 
  ports:
  - protocol: TCP
    port: 9000
    nodePort: 30769

Below is my deployment.yaml: 下面是我的deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:  
  selector:
    matchLabels:
      run: my-app
  replicas: 2
  template:
    metadata:
      labels:
        run: my-app
    spec:
      containers:
      - name: my-app
        image: dockerhubaccount/my-app-img:latest
        ports:
        - containerPort: 9000
          protocol: TCP
      imagePullSecrets:
      - name: my-app-img-credentials
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 25%
      maxSurge: 25%

This works fine the first time, but on subsequent runs, my pods are not getting updated. 这在第一次工作正常,但在后续运行中,我的pod没有得到更新。

I have read the suggested workaround at https://github.com/kubernetes/kubernetes/issues/33664 which is: 我已经在https://github.com/kubernetes/kubernetes/issues/33664上阅读了建议的解决方法,它是:

kubectl patch deployment my-app -p "{\"spec\":{\"template\":{\"metadata\":{\"labels\":{\"date\":\"`date +'%s'`\"}}}}}"

I was able to run the above command, but it did not resolve the issue for me. 我能够运行上面的命令,但它没有解决我的问题。

I know that I can trigger pod updates by manually changing the image tag from "latest" to another tag, but I want to make sure I get the latest image without having to check Docker Hub. 我知道我可以通过手动将图像标签从“最新”更改为另一个标签来触发pod更新,但我想确保无需检查Docker Hub即可获得最新图像。

Any help would be greatly appreciated. 任何帮助将不胜感激。

If nothing changes in the deployment spec, the pods will not be updated for you. 如果部署规范中没有任何更改,则不会为您更新pod。 This is one of many reasons it is not recommended to use :latest , as the other answer went into more detail on. 这是不建议使用的众多原因之一:latest ,因为另一个答案更详细。 The Deployment controller is very simple and pretty much just does DeepEquals(old.Spec.Template, new.Spec.Template) , so you need some actual change, such as you have with the PATCH call by setting a label with the current datetime. Deployment控制器非常简单,几乎只有DeepEquals(old.Spec.Template, new.Spec.Template) ,因此您需要进行一些实际更改,例如通过使用当前日期时间设置标签来进行PATCH调用。

You're missing an imagePullPolicy in your deployment. 您在部署中缺少imagePullPolicy Try this: 尝试这个:

containers:
- name: my-app
  image: dockerhubaccount/my-app-img:latest
  imagePullPolicy: Always

The default policy is ifNotPresent which is why yours is not updating. 默认策略是ifNotPresent ,这就是您未更新的原因。

I will incorporate two notes from the link: 我将从链接中加入两个注释:

Note: You should avoid using the :latest tag when deploying containers in production as it is harder to track which version of the image is running and more difficult to roll back properly 注意:在生产中部署容器时应避免使用:latest标记,因为更难以跟踪正在运行的映像版本并且更难以正确回滚

Note: The caching semantics of the underlying image provider make even imagePullPolicy: Always efficient. 注意:底层图像提供程序的缓存语义甚至imagePullPolicy: Always使imagePullPolicy: Always高效。 With Docker, for example, if the image already exists, the pull attempt is fast because all image layers are cached and no image download is needed 例如,使用Docker,如果图像已经存在,则拉取尝试很快,因为所有图像层都被缓存并且不需要图像下载

Turns out I misunderstood the workaround command I gave from the link. 结果我误解了我从链接给出的变通方法命令。

I thought it was a one-time command that configured my deployment to treat all future kubectl apply commands as a trigger to update my pods. 我认为这是一次性命令,将我的部署配置为将所有未来的kubectl apply命令视为更新我的pod的触发器。

I actually just had to run the command every time I wanted to update my pods: 实际上,每次我想更新我的pod时,我都必须运行命令:

kubectl patch deployment my-app -p "{\"spec\":{\"template\":{\"metadata\":{\"labels\":{\"date\":\"`date +'%s'`\"}}}}}"

Many thanks to everyone who helped! 非常感谢所有帮助过的人!

There are two things here that relates the issue, 这里有两件事与这个问题有关,

  1. It is suggested to use kubectl apply for the first time while creating a resource and later recommended to use kubectl replace or kubectl edit or kubectl patch commands which subsequently call the kubectl apply . 建议在创建资源时首次使用kubectl apply ,后来建议使用kubectl replacekubectl editkubectl patch命令,随后调用kubectl apply

  2. Once you create a service using either kubectl apply or kubetcl create you cannot replace that service with a yaml file. 使用kubectl applykubetcl create服务后,您无法使用yaml文件替换该服务。 In other words, service generates a random IP that cannot be patched or replaced . 换句话说,服务生成无法 修补或替换随机IP The only option to recreate a service is to delete the service and recreate it with the same name. 重新创建服务的唯一选择是删除服务并使用相同的名称重新创建它。

NOTE: When I tried replacing a service using kubectl apply command while I was trying to create a backup and restore solution resulted this below error. 注意:当我尝试使用kubectl apply命令替换服务时,我尝试创建备份和还原解决方案时导致此错误。

kubectl apply -f replace-service.yaml -n restore-proj
The Service "test-q12" is invalid: spec.clusterIP: Invalid value: "10.102.x.x": provided IP is already allocated.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM