4

I want to have an initContainer that runs prior to the container my kubernetes cronjob is running. It's used to install kubectl. Is there a way of doing this?

I tried to add the initContainer-parameter to the cronjob.yaml file but it threw an error.

The code of my containerInit is the following:

initContainers:
- name: install-kubectl
  image: allanlei/kubectl
  volumeMounts:
  - name: kubectl
    mountPath: /data
  command: ["cp", "/usr/local/bin/kubectl", "/data/kubectl"]

My cronjob needs to be able to access kubectl. That is the reason I'm trying to do this. I'm grateful for any suggestions how I could solve this problem.

Florian
  • 123
  • 1
  • 9
  • 1
    initContainers are part of the PodSpec, so there should be no problem. Maybe you can include the full job deployment.yaml and the error. – Blokje5 Jun 25 '19 at 09:40
  • It should work, what was the error? Also note that nodes already come with kubectl installed on them, the important part is getting the correct KUBECONFIG so that kubectl does not contact localhost port 8080 – Patrick W Jun 25 '19 at 13:50
  • did any of the answers solve your issue? if so could you please mark the question answered? – switchboard.op Jul 27 '20 at 22:26

3 Answers3

13

Yes, you can use InitContainers in a CronJob template.

Like this:

apiVersion: batch/v1
kind: CronJob
metadata:
  name: example
  namespace: default
spec:
  schedule: '*/1 * * * *'
  jobTemplate:
    spec:
      template:
        spec:
          initContainers:
            - name: busybox
              image: busybox
              command:
                - echo
                - initialized
          containers:
            - name: pi
              image: perl
              command:
                - perl
                - '-Mbignum=bpi'
                - '-wle'
                - print bpi(2000)
          restartPolicy: OnFailure
switchboard.op
  • 1,878
  • 7
  • 26
1

You could install the kubectl into the image you used there.

todaynowork
  • 976
  • 8
  • 12
0

You can directly install kubectl inside your docker image and use that image in cronjob.

Harsh Manvar
  • 27,020
  • 6
  • 48
  • 102