[英]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 相关的概念。
NOTE: One resource can implement multiple hooks:注意:一个资源可以实现多个钩子:
Eg: annotations: "helm.sh/hook": post-install,post-upgrade例如:注释:“helm.sh/hook”:安装后,升级后
NOTE: If no hook deletion policy annotation is specified, the before-hook-creation behavior is applied by default.注意:如果没有指定钩子删除策略注释,则默认应用钩子创建之前的行为。
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
$ 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
$
$ 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.