kubernetes Helm 3中默认的hook-delete-policy是如何工作的?

yhuiod9q  于 12个月前  发布在  Kubernetes
关注(0)|答案(1)|浏览(161)

我有一个 Helm 图myapp与工作作为预安装和预升级挂钩。这有钩删除政策提到before-hook-creation

apiVersion: batch/v1
kind: Job
metadata:
  name: "job-{{ .Release.Name }}-{{ .Release.Revision }}"
  labels:
    app.kubernetes.io/managed-by: {{ .Release.Service | quote }}
    app.kubernetes.io/instance: {{ .Release.Name | quote }}
    app.kubernetes.io/version: {{ .Chart.AppVersion }}
    helm.sh/chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
  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-install,pre-upgrade
    "helm.sh/hook-weight": "1"
    "helm.sh/hook-delete-policy": "before-hook-creation"
spec:
  template:
    metadata:
      name: "{{ .Release.Name }}"
      labels:
        app.kubernetes.io/managed-by: {{ .Release.Service | quote }}
        app.kubernetes.io/instance: {{ .Release.Name | quote }}
        helm.sh/chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    spec:
      restartPolicy: Never
      containers:
      - name: post-install-job
        image: "alpine:3.3"
        command: ["/bin/sleep","{{ default "10" .Values.sleepyTime }}"]

字符串
根据文档,作业job-myapp-1应该在第二个helm upgrade期间删除:
before-hook-creation:在启动新钩子之前删除先前的资源(默认)
但是它没有被删除。然而,值hook-succeeded按预期工作(在作业成功完成后立即删除作业)。
是yaml有问题还是我对before-hook-creation工作原理的理解有问题?

更新:

保留注解:

"helm.sh/hook-delete-policy": before-hook-creation


更改了作业名称,以避免附加发布版本号:

name: "job-{{ .Release.Name }}"


这将在大多数版本上生成相同的作业名称。然后该作业在下一个helm upgrade上被替换,与前一个作业关联的pod也被删除。
但是,作业不会在helm uninstall上被删除,因为helm没有跟踪这些钩子资源。

yduiuuwa

yduiuuwa1#

如果它适合你的使用情况,你可以在你的作业上设置生存时间字段(.spec.ttlSecondsAfterFinished),这样如果成功,它会在设置的时间后自动删除:

  • https://kubernetes.io/docs/concepts/workloads/controllers/ttlafterfinished/#cleanup-for-finished-jobs

相关问题