[kubernetes]-helm通过hook在upgrade前清理job

导语:当我们使用helm upgrade时,job资源会因为不能修改而中断。

当我们使用before-hook-creation的时候,job资源会在存在的情况下先删除,再次创建。避免因为不能修改而中断。

yaml添加部分为annotations块

apiVersion: batch/v1
kind: Job
metadata:
  name: aiserverconfig-migration
  labels:
    job-name: aiserverconfig-migration
    k8s-med-type: job
  annotations:
    # This is what defines this resource as a hook. Without this line, the
    # job is considered part of the release.
    "helm.sh/hook": pre-upgrade
    "helm.sh/hook-weight": "-5"
    # before-hook-creation,hook-succeeded 表示在创建新的 {{ .Release.Name }}-db-migration 钩子之前和在钩子成功执行之后都将删除该钩子。
    #"helm.sh/hook-delete-policy": hook-succeeded
    "helm.sh/hook-delete-policy": before-hook-creation
spec:
 template:
  metadata:
      labels:
        job-name: aiserverconfig-migration
  spec:
   initContainers:
     - command: ['sh', '-c', "until nc -w 3  -zv mysql 3306 ; do echo waiting for mydb; sleep 5; done"]
       image: harbor.deepwise.com/base/busybox:stable-wait-for
       name: wait-for-mysql
   containers:
   - name: aiserverconfig-migration
     image: {{ .Values.images.aiserverconfig_migration }}
     command: ["sh","-c","sh /srv/aiserverconfig-migration/startup.sh"]
     env:
       - name: TZ
         value: Asia/Shanghai
     volumeMounts:
       - name: migration-persistent-storage
         mountPath: /root/migration/
   restartPolicy: OnFailure
   volumes:
   - name: migration-persistent-storage
     persistentVolumeClaim:
       claimName: migration-pvc

初次执行时job名称似乎没有显示,重复执行几次之后查看job名并无问题。

image-20240604155106216

并未经详细验证和测试。仅记录一下

参考

https://helm.sh/docs/topics/charts_hooks/#hook-deletion-policies

https://www.jianshu.com/p/1a865909d20b

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爷来辣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值