带有环境变量的 Kubernetes Cronjob

Kubernetes Cronjob with env variables

我想 运行 一个 kuberenetes cronjob,但是我的 cronjob 命令依赖于要定义的环境变量,否则它将不起作用。当我在 cronjob yaml 中设置 env 变量时,它提到这是无效的 YAML,并显示以下消息:

error: error parsing mapping_rake.yaml: error converting YAML to JSON: yaml: line 77: did not find expected key

第 77 行是定义命令的行(命令:["rake", "mapping:check"])。我不确定为什么包含 env 变量 invalidate/make 不可能将命令传递给将被实例化以执行 cronjob 的 pod。这是我的 yaml 正文:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: bob-mapping-rake-cron
spec:
  schedule: "57 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
         volumes:
           - name: nfs-volume
             nfs:
              server: vcsnas.pvt # Change this!
              path: /ifs/ifs/AssetFlow
         containers:
              - env:
                  - name: ORIGIN_PATH
                    value: /mnt/Content/BobBarkerTesting/est_pricing
                  - name: FINAL_PATH
                    value: /mnt/Content/BobBarkerTesting/est_pricing/final
                  - name: SCANNED_PATH
                    value: /mnt/Content/BobBarkerTesting/est_pricing/scanned
                  - name: EST_BUNDLES
                    value: /mnt/Content/BobBarkerTesting/est_bundles
                  - name: SCANNED_BUNDLES
                    value: /mnt/Content/BobBarkerTesting/incoming_est_bundles/scanned
                  - name: INCOMING_BUNDLES
                    value: /mnt/Content/BobBarkerTesting/incoming_est_bundles
                  - name: INCOMING_SWAPS
                    value: /mnt/Content/BobBarkerTesting/locker_swap/ingest
                  - name: OUTPUT_FOLDER
                    value: /mnt/Content/BobBarkerTesting/locker_swap/output
                  - name: PROCESSED_FOLDER
                    value: /mnt/Content/BobBarkerTesting/locker_swap/processed
                  - name: FAILED_FOLDER
                    value: /mnt/Content/BobBarkerTesting/locker_swap/failed
                  - name: LDAP_HOST
                    value: 172.17.157.21
                  - name: LDAP_PORT
                    value: '636'
                  - name: LDAP_ATTRIBUTE
                    value: uid
                  - name: LDAP_BASE
                    value: ou=people,dc=cox,dc=net
                  - name: LDAP_USER
                    valueFrom:
                      secretKeyRef:
                        name: ldap
                        key: username
                  - name: LDAP_PASSWORD
                    valueFrom:
                      secretKeyRef:
                        name: ldap
                        key: password
                  - name: LDAP_SSL
                    value: simple_tls
                  - name: DB_HOST
                    value: mysql.bb.svc.cluster.local
                  - name: DB_USER
                    valueFrom:
                      secretKeyRef:
                        name: db
                        key: username
                  - name: DB_PW
                    valueFrom:
                      secretKeyRef:
                        name: db
                        key: password
                  - name: DB
                    value: pesto_prod
                volumeMounts:
                  - name: nfs-volume
                    mountPath: /mnt
                name: bob-barker-mapping-rake-cron
                image: repo.corp.cox.com:5005/vodcontent/bob-barker-mapping-rake:v2
                command: ["rake", "mapping:check"]
            restartPolicy: Never
  concurrencyPolicy: Replace

是否允许为将执行 Kubernetes cronjob 的容器定义 env 变量?为什么或者为什么不?有没有其他方法可以通过 cronjob 来做到这一点?如果不是,我可以采用不同的方式,但这对我的用例来说更符合习惯,所以我想尝试一下。

您的 CronJob YAML 存在一些缩进问题。这里是正确缩进的 YAML:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: bob-mapping-rake-cron
spec:
  schedule: "57 * * * *"
  concurrencyPolicy: Replace
  jobTemplate:
    spec:
      template:
        spec:
          volumes:
          - name: nfs-volume
            nfs:
              server: vcsnas.pvt # Change this!
              path: /ifs/ifs/AssetFlow
          containers:
          - name: bob-barker-mapping-rake-cron
            image: repo.corp.cox.com:5005/vodcontent/bob-barker-mapping-rake:v2
            command: ["rake", "mapping:check"]
            volumeMounts:
            - name: nfs-volume
              mountPath: /mnt
            env:
            - name: ORIGIN_PATH
              value: /mnt/Content/BobBarkerTesting/est_pricing
            - name: FINAL_PATH
              value: /mnt/Content/BobBarkerTesting/est_pricing/final
            - name: SCANNED_PATH
              value: /mnt/Content/BobBarkerTesting/est_pricing/scanned
            - name: EST_BUNDLES
              value: /mnt/Content/BobBarkerTesting/est_bundles
            - name: SCANNED_BUNDLES
              value: /mnt/Content/BobBarkerTesting/incoming_est_bundles/scanned
            - name: INCOMING_BUNDLES
              value: /mnt/Content/BobBarkerTesting/incoming_est_bundles
            - name: INCOMING_SWAPS
              value: /mnt/Content/BobBarkerTesting/locker_swap/ingest
            - name: OUTPUT_FOLDER
              value: /mnt/Content/BobBarkerTesting/locker_swap/output
            - name: PROCESSED_FOLDER
              value: /mnt/Content/BobBarkerTesting/locker_swap/processed
            - name: FAILED_FOLDER
              value: /mnt/Content/BobBarkerTesting/locker_swap/failed
            - name: LDAP_HOST
              value: 172.17.157.21
            - name: LDAP_PORT
              value: '636'
            - name: LDAP_ATTRIBUTE
              value: uid
            - name: LDAP_BASE
              value: ou=people,dc=cox,dc=net
            - name: LDAP_USER
              value: pesto_prod
            - name: LDAP_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: ldap
                  key: username
            - name: LDAP_SSL
              valueFrom:
                secretKeyRef:
                  name: ldap
                  key: password
            - name: DB_HOST
              value: simple_tls
            - name: DB_USER
              value: mysql.bb.svc.cluster.local
            - name: DB_PW
              valueFrom:
                secretKeyRef:
                  name: db
                  key: username
            - name: DB
              valueFrom:
                secretKeyRef:
                  name: db
                  key: password
          restartPolicy: Never