Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I add initContainers to a kubernetes-cronjob?

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.

like image 373
Florian Avatar asked Dec 23 '22 22:12

Florian


2 Answers

Yes, you can use InitContainers in a CronJob template.

Like this:

apiVersion: batch/v1beta1
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
like image 67
switchboard.op Avatar answered Dec 31 '22 17:12

switchboard.op


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

like image 29
todaynowork Avatar answered Dec 31 '22 19:12

todaynowork