在 Kubernetes cronjob 中从私有注册表中提取映像失败

问题描述 投票:0回答:2

我正在从 gitlab 上的私有存储库中提取图像并在 kubernetes 中运行 cronjob。由于它是一个私人存储库,我还必须提供 imagePullSecrets。但我注意到它给出了一个错误,因为 cronjob 不接受 imagePullSecrets 标签。它给出以下错误。这是否意味着我无法在 cronjob 中使用私有存储库中的图像?

error: error validating "cron.yml": error validating data: ValidationError(CronJob.spec.jobTemplate.spec.template.spec.containers[0]): unknown field "imagePullSecrets" in io.k8s.api.core.v1.Container; if you choose to ignore these errors, turn validation off with --validate=false
docker kubernetes gitlab kubectl
2个回答
11
投票

imagePullSecrets
字段不是每个容器字段 - 您需要将其设置为
CronJob.spec.jobTemplate.spec.template.spec.imagePullSecrets
而不是
CronJob.spec.jobTemplate.spec.template.spec.containers.imagePullSecrets
。您可以在此处查看 Pod 示例。


0
投票

您的 cronjob yaml 文件应如下所示:

注意:它包含占位符,因此请替换为实际值。

apiVersion: batch/v1
kind: CronJob
metadata:
  name: <job_name>
spec:
  schedule: <schedule>
  concurrencyPolicy: <concurrencyPolicy>
  successfulJobsHistoryLimit: <successfulJobsHistoryLimit>
  failedJobsHistoryLimit: <failedJobsHistoryLimit>
  startingDeadlineSeconds: <startingDeadlineSeconds>
  jobTemplate:
    spec:
      ttlSecondsAfterFinished: <ttlSecondsAfterFinished>
      template:
        spec:
          imagePullSecrets: 
            - name: abc
          containers:
            - name: <containerName>
              image: <image>:<image_tag>
              imagePullPolicy: IfNotPresent
              args:
                - /bin/sh
                - -c
                - <FullPath_script>
              volumeMounts:
                - name: script
                  mountPath: "/script"
          volumes:
            - name: script
              configMap:
                name: <your-configmap>
                defaultMode: 0777
          restartPolicy: <restartPolicy>

秘密文件也应该在那里,按照上面的示例,它应该命名为 abc.yaml (您可以根据用法使用任何有效名称)。

kind: Secret
apiVersion: v1
metadata:
  name: abc
data:
  .dockerconfigjson: >-
    <paste image pull secret here>
type: kubernetes.io/dockerconfigjson
© www.soinside.com 2019 - 2024. All rights reserved.