Anshul Tripathi
Anshul Tripathi

Reputation: 599

Pulling an Image from Private Registry in Kubernetes cronjob fails

I am pulling an image from a private repository on gitlab and running a cronjob in kubernetes. Since it is a private repo I would also have to supply the imagePullSecrets. but I noticed it gives an error because a cronjob doesn't accept the imagePullSecrets tag. It gives the following error. Does that mean I can't use an image from a private repository in a 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

Upvotes: 4

Views: 11388

Answers (2)

Rakesh Chauhan
Rakesh Chauhan

Reputation: 342

Your cronjob yaml file should look like below:

NOTE: This sample cronjob is to execute a shell script documented inside configmap, also it contains placeholders so replace with actual values.

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>

and secret file should also be there, as per example above it should be named as abc.yaml (you can use any valid name as per usage).

kind: Secret
apiVersion: v1
metadata:
  name: abc
data:
  .dockerconfigjson: >-
    <paste image pull secret here>
type: kubernetes.io/dockerconfigjson

Upvotes: 0

dippynark
dippynark

Reputation: 3003

The imagePullSecrets field is not a per container field - you need to set that at CronJob.spec.jobTemplate.spec.template.spec.imagePullSecrets instead of CronJob.spec.jobTemplate.spec.template.spec.containers.imagePullSecrets. You can see an example for a Pod here.

Upvotes: 11

Related Questions