在每次安装 helm 时重新创建作业或删除旧作业

san*_* jk 8 kubernetes kubectl kubernetes-helm kubernetes-pod

我已经创建了 Kubernetes 作业,并且作业已创建,但部署到 Kubernetes 集群时失败。当我尝试使用 Helm 重新部署它时,作业不会重新部署(删除旧作业并重新创建新作业,与微服务部署不同)。

如何在不手动删除 Kubernetes 的情况下实现此重新部署作业?我可以告诉它重新创建容器吗?

job.yaml包含:

apiVersion: batch/v1
kind: Job
metadata:
  name: "{{ .Release.Name }}-init-job"
  namespace: {{ .Release.Namespace }}
spec:
  template:
    metadata:
      annotations:
        linkerd.io/inject: disabled
        "helm.sh/hook-delete-policy": before-hook-creation
        "helm.sh/hook": pre-install,pre-upgrade,pre-delete
        "helm.sh/hook-weight": "-5"
    spec:
      serviceAccountName: {{ .Release.Name }}-init-service-account
      containers:
        - name: app-installer
          image: some image
          command:
            - /bin/bash
            - -c
            - echo Hello executing k8s init-container
          securityContext:
            readOnlyRootFilesystem: true
      restartPolicy: OnFailure
Run Code Online (Sandbox Code Playgroud)

作业没有被重新部署

kubectl get jobs -n namespace

NAME                    COMPLETIONS   DURATION   AGE
test-init-job   0/1           13h        13h
Run Code Online (Sandbox Code Playgroud)

kubectl 描述作业 test-init-job -n test

Name:           test-init-job
Namespace:      test
Selector:       controller-uid=86370470-0964-42d5-a9c1-00c8a462239f
Labels:         app.kubernetes.io/managed-by=Helm
Annotations:    meta.helm.sh/release-name: test
                meta.helm.sh/release-namespace: test
Parallelism:    1
Completions:    1
Start Time:     Fri, 14 Oct 2022 18:03:31 +0530
Pods Statuses:  0 Running / 0 Succeeded / 1 Failed
Pod Template:
  Labels:           controller-uid=86370470-0964-42d5-a9c1-00c8a462239f
                    job-name=test-init-job
  Annotations:      helm.sh/hook: pre-install,pre-upgrade
                    helm.sh/hook-delete-policy: before-hook-creation,hook-succeeded
                    helm.sh/hook-weight: -5
                    linkerd.io/inject: disabled
  Service Account:  test-init-service-account
  Containers:
   test-app-installer:
    Image:      artifactory/test-init-container:1.0.0
    Port:       <none>
    Host Port:  <none>
    Environment:
      test.baseUrl:         baser
      test.authType:        basic
      test.basic.username:  jack
      test.basic.password:  password
    Mounts:
      /etc/test/config from test-installer-config-vol (ro)
  Volumes:
   test-installer-config-vol:
    Type:      ConfigMap (a volume populated by a ConfigMap)
    Name:      test-installer-config-files
    Optional:  false
Events:        <none>
Run Code Online (Sandbox Code Playgroud)

小智 6

作业无法重新部署,因为它们的模板是不可变的。

如果您收到消息cannot patch "..." with kind Job: Job.batch "db-sync-download-snapshot" is invalid: spec.template: Invalid value: ... field is immutable,这就是原因。

只需删除作业,或创建作业并$VERSION在名称后附加 即可。

对于 GitOps,$VERSION应该是$CI_COMMIT_HASH.

替换nameHelm 模板中的:

  metadata:
    name: "{{ .Release.Name }}-init-job-{{ .Release.revision }}"
Run Code Online (Sandbox Code Playgroud)