简体   繁体   English

"使用 helm 更新 Kubernetes 作业"

[英]Kubernetes job update with helm

I have a Kubernetes cluster and I'm deploying my app there with Helm.我有一个 Kubernetes 集群,我正在使用 Helm 在那里部署我的应用程序。 Everything works fine, but one aspect, the Job update.一切正常,但一方面,工作更新。 As I've read, the Jobs are immutable and that's why they can't be updated, but I don't get, why is helm not creating a new job as it does for the Pods?正如我所读到的,作业是不可变的,这就是它们无法更新的原因,但我不明白,为什么 helm 没有像为 Pod 那样创建新作业?

In the end, I want to achieve that my app code is deployed as a job, that runs DB migrations.最后,我想实现我的应用程序代码被部署为一个运行数据库迁移的作业。 I tried to do it as a Pod, but for pods, the restart policy can be only "Always", "never" is not supported, even though the doc says otherwise.我尝试将其作为 Pod 进行,但对于 pod,重启策略只能是“始终”,不支持“从不”,即使文档另有说明。 How can I achieve this, so the Migration can be updated with every deployment (new image tag) and it runs once and not restarts?我怎样才能做到这一点,以便每次部署(新图像标签)都可以更新迁移并且它运行一次而不重新启动?

You can use helm hooks here.您可以在此处使用 helm hooks。 Official Link: https://helm.sh/docs/topics/charts_hooks/官方链接: https ://helm.sh/docs/topics/charts_hooks/

Once job is completed with "helm install", helm hook should delete it.使用“helm install”完成作业后,helm hook 应将其删除。 Once you perform "helm upgrade", a new job should be triggered.执行“helm upgrade”后,应该会触发一个新作业。 Application logic should handle install and upgrade scenarios.应用程序逻辑应处理安装和升级方案。

Below are some concepts related to helm hooks.下面是一些与 helm hooks 相关的概念。

Types of Helm Hooks舵钩的类型

  • pre-install : hooks run after templates are rendered and before any resources are created in a Kubernetes cluster pre-install :在渲染模板之后和在 Kubernetes 集群中创建任何资源之前运行挂钩
  • post-install : hooks run after all Kubernetes resources have been loaded post-install :在加载所有 Kubernetes 资源后运行挂钩
  • pre-delete : hooks run before any existing resources are deleted from Kubernetes pre-delete :在从 Kubernetes 中删除任何现有资源之前运行挂钩
  • post-delete : hooks run after all Kubernetes resources have been deleted post-delete :在删除所有 Kubernetes 资源后运行钩子
  • pre-upgrade : hooks run after chart templates have been rendered and before any resources are loaded into Kubernetes pre-upgrade : 在图表模板渲染之后和任何资源加载到 Kubernetes 之前运行钩子
  • post-upgrade : hooks run after all Kubernetes resources have been upgraded post-upgrade :在所有 Kubernetes 资源升级后运行钩子
  • pre-rollback : hooks run after templates have been rendered and before any resources are rolled back pre-rollback :在模板渲染之后和任何资源回滚之前运行的钩子
  • post-rollback : hooks run after all resources have been modified post-rollback :在修改所有资源后运行挂钩
  • test : hooks run when helm test subcommand is executed test : 执行 helm test 子命令时运行钩子

NOTE: One resource can implement multiple hooks:注意:一个资源可以实现多个钩子:

Eg: annotations: "helm.sh/hook": post-install,post-upgrade例如:注释:“helm.sh/hook”:安装后,升级后

How Helm Chart Hooks Are Executed Helm Chart Hooks 是如何执行的

  • When a Helm chart containing hooks is executed, components like pods or jobs pertaining to hooks are not directly applied in a Kubernetes environment.当执行包含钩子的 Helm 图表时,与钩子相关的 pod 或作业等组件不会直接应用于 Kubernetes 环境。 Instead when a hook is executed, a new pod is created corresponding to the hook.相反,当执行钩子时,会创建一个与钩子对应的新 pod。 If successfully run, they will be in "Completed" state.如果成功运行,它们将处于“已完成”状态。
  • Any resources created by a Helm hook are un-managed Kubernetes objects. Helm hook 创建的任何资源都是非托管的 Kubernetes 对象。 In other words, uninstalling a Helm chart using "helm uninstall" will not remove the underlying resources created by hooks.换句话说,使用“helm uninstall”卸载 Helm 图表不会删除由钩子创建的底层资源。 A separate deletion policy needs to be defined in the form of annotation if those resources need to be deleted.如果需要删除这些资源,则需要以注释的形式定义单独的删除策略。
  • Any hook resources that must never be deleted should be annotated with "helm.sh/resource-policy: keep".任何不能删除的钩子资源都应该用“helm.sh/resource-policy:keep”注释。

Helm Hook Annotations Helm Hook 注解

  • "helm.sh/hook": post-install “helm.sh/hook”:安装后
  • "helm.sh/hook-weight": "-5" ## NOTE: This MUST be string "helm.sh/hook-weight": "-5" ## 注意:这必须是字符串
  • "helm.sh/hook-delete-policy": hook-succeeded “helm.sh/hook-delete-policy”:钩子成功
  • "helm.sh/resource-policy": keep “helm.sh/resource-policy”:保持

Hook Deletion Policies挂钩删除策略

  • “helm.sh/hook-delete-policy" annotation to be used.使用“helm.sh/hook-delete-policy”注解。

Three different deletion policies are supported which will decide when to delete the resources:支持三种不同的删除策略,它们将决定何时删除资源:

  • before-hook-creation : Delete the previous resource before a new hook is launched before-hook-creation :在启动新钩子之前删除以前的资源
  • hook-succeeded : Delete the resource after the hook is successfully executed hook-succeeded :钩子执行成功后删除资源
  • hook-failed : Delete the resource if the hook failed during execution hook-failed :如果钩子在执行过程中失败,则删除资源

NOTE: If no hook deletion policy annotation is specified, the before-hook-creation behavior is applied by default.注意:如果没有指定钩子删除策略注释,则默认应用钩子创建之前的行为。

Hook Weights钩重

  • "helm.sh/hook-weight" annotation to be used.要使用的“helm.sh/hook-weight”注释。
  • Hook weights can be positive or negative numbers but must be represented as strings.挂钩权重可以是正数或负数,但必须表示为字符串。
  • When Helm starts the execution cycle of hooks of a particular Kind it will sort those hooks in ascending order.当 Helm 开始执行特定种类的钩子时,它会按升序对这些钩子进行排序。

Hook weights ensure below:挂钩重量确保如下:

  • execute in the right weight sequence以正确的权重顺序执行
  • block each other互相阻挡
  • all block main K8s resource from starting all 阻止主 K8s 资源启动

Complete Execution Flow Example完整的执行流程示例

  1. Step-1: Create post-install and post-install hook YAML files步骤 1:创建安装后和安装后挂钩 YAML 文件

pre-install.yaml预安装.yaml

apiVersion: v1
 kind: Pod
 metadata:
   name: hook-preinstall
   annotations:
     "helm.sh/hook": "pre-install"          ## Without this line, this becomes a normal K8s resource.
 spec:
   containers:
   - name: hook1-container
     image: busybox
     imagePullPolicy: IfNotPresent
     command: ['sh', '-c', 'echo The pre-install hook Pod is running  - hook-preinstall && sleep 15']
   restartPolicy: Never
   terminationGracePeriodSeconds: 0

post-install.yaml安装后.yaml

apiVersion: v1
 kind: Pod
 metadata:
   name: hook-postinstall
   annotations:
     "helm.sh/hook": "post-install"         ## Without this line, this becomes a normal K8s resource.
 spec:
   containers:
   - name: hook2-container
     image: busybox
     imagePullPolicy: IfNotPresent
     command: ['sh', '-c', 'echo post-install hook Pod is running - hook-postinstall && sleep 10']
   restartPolicy: Never
   terminationGracePeriodSeconds: 0

  1. Step-2: Install Helm Chart (Assuming other K8s resources are defined under /templates/ directory) Step-2:安装Helm Chart(假设/templates/目录下定义了其他K8s资源)

  1. Get Pods:获取 Pod:

$ kubectl get pods
NAME                                 READY   STATUS      RESTARTS   AGE
demohook-testhook-5ff88bb44b-qc4n2   1/1     Running     0          5m45s
hook-postinstall                     0/1     Completed   0          5m45s
hook-preinstall                      0/1     Completed   0          6m2s
$

  1. Describe Pods and notice Started & Finished time of the pods:描述 Pod 并注意 Pod 的开始和完成时间:

$ kubectl describe pod demohook-testhook-5ff88bb44b-qc4n2 | grep -E 'Anno|Started:|Finished:'
$ kubectl describe pod hook-postinstall | grep -E 'Anno|Started:|Finished:'              
$ kubectl describe pod hook-preinstall | grep -E 'Anno|Started:|Finished:'

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

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