【问题标题】:Bulk editing Kubernetes annotations with Kustomize without pods restarting使用 Kustomize 批量编辑 Kubernetes 注释,无需重启 pod
【发布时间】:2020-11-04 22:29:03
【问题描述】:

我正在尝试找到批量编辑大量 Kubernetes 资源上的注释的最佳方法。看起来 Kustomize 可能是最好的选择:

  1. 资源已经是 kustomization.yaml 的一部分
  2. 然后我可以使用新的或修改后的注释编辑 kustomization.yaml
  3. kubectl apply -k ./ 根据需要使用新注释更新所有相关资源

不幸的是,这会使所有 pod 终止并重新启动,这有时比我希望的要长。在没有 Kustomize 的情况下应用注释,当 YAML 没有其他更改时,不需要重新部署,我很乐意做类似的事情,但批量。任何提示表示赞赏!

【问题讨论】:

    标签: kubernetes kustomize


    【解决方案1】:

    您的 pod 似乎由一些副本集(部署、守护程序集、状态集等)管理,这是正确的。更新这些将导致更新 pod 规范,从而导致重新部署。

    你可以看kubectl annotate命令。

    来自kubectl annotate --help的一些例子

    Examples:
      # Update pod 'foo' with the annotation 'description' and the value 'my frontend'.
      # If the same annotation is set multiple times, only the last value will be applied
      kubectl annotate pods foo description='my frontend'
    
      # Update a pod identified by type and name in "pod.json"
      kubectl annotate -f pod.json description='my frontend'
    
      # Update pod 'foo' with the annotation 'description' and the value 'my frontend running nginx', overwriting any
    existing value.
      kubectl annotate --overwrite pods foo description='my frontend running nginx'
    
      # Update all pods in the namespace
      kubectl annotate pods --all description='my frontend running nginx'
    
      # Update pod 'foo' only if the resource is unchanged from version 1.
      kubectl annotate pods foo description='my frontend running nginx' --resource-version=1
    
      # Update pod 'foo' by removing an annotation named 'description' if it exists.
      # Does not require the --overwrite flag.
      kubectl annotate pods foo description-
    

    【讨论】:

    • 是的,主要使用有状态集。我最初使用的是kubectl annotate,它和kubectl apply(在编辑 YAML 之后)都很好,因为它们不需要重新部署标签和注释。但是kubectl annotate 不会更改底层的 YAML,手动编辑 YAML 然后再做kubectl apply 太手动且耗时。
    • 我的意思是您可以执行以下操作 1. kubectl annotate 在正在运行的 pod 上 2. 使用 kubectl rollout pause 暂停您的 statefulesets 的推出,直到您不需要重新部署 3. 使用 kustomize 更新/创建/应用你的新 YAML。由于您在上一步中暂停了部署,因此不会发生重新部署 4. 当您准备好重新部署时,继续部署 kubectl rollout resume
    猜你喜欢
    • 2020-03-01
    • 2016-08-22
    • 1970-01-01
    • 2020-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-04
    相关资源
    最近更新 更多