【问题标题】:Kubernetes Cronjob with env variables带有环境变量的 Kubernetes Cronjob
【发布时间】:2021-07-10 12:08:39
【问题描述】:

我想运行 kuberenetes cronjob,但我的 cronjob 命令依赖于要定义的环境变量,否则它将无法工作。当我在 cronjob yaml 中设置环境变量时,它提到这是无效的 YAML,并带有以下消息:

错误:解析 mapping_rake.yaml 时出错:将 YAML 转换为 JSON 时出错: yaml:第 77 行:没有找到预期的密钥

第 77 行是定义命令的行(命令:["rake", "mapping:check"])。我不确定为什么包含 env 变量会使传递给将被实例化以执行 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 的容器定义环境变量?为什么或者为什么不?有没有其他方法可以通过 cronjob 来做到这一点?如果不是,我可以采用不同的方式,但这对于我的用例来说更惯用,所以我想尝试一下。

【问题讨论】:

  • restartPolicy 在我看来是错误的缩进级别。我认为应该是同一级别的容器?
  • @AndD 是对的,您只需从 restartPolicy 行中删除前两个空格

标签: kubernetes cron


【解决方案1】:

您的 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

【讨论】:

    猜你喜欢
    • 2018-09-06
    • 1970-01-01
    • 1970-01-01
    • 2020-07-22
    • 2019-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多