Unable to schedule a cronjob which executes a kubectl command


#1

I would like to run the following kubectl command every 5 minutes:

kubectl patch deployment runners -p '{"spec":{"template":{"spec":{"containers":[{"name":"jp-runner","env":[{"name":"START_TIME","value":"'$(date +%s)'"}]}]}}}}' -n jp-test

For this, I have created a cronjob as below:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "*/5 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            args:
            - /bin/sh
            - -c
            - kubectl patch deployment runners -p '{"spec":{"template":{"spec":{"containers":[{"name":"jp-runner","env":[{"name":"START_TIME","value":"'$(date +%s)'"}]}]}}}}' -n jp-test
          restartPolicy: OnFailure

But it is failing to start the container, showing the message :

Back-off restarting failed container

And with the error code 127:

State:          Terminated
      Reason:       Error
      Exit Code:    127

From what I checked, the error code 127 says that the command doesn’t exist. How could I run the kubectl command then as a cron job ? Am I missing something?


#2

The image you’re trying to use, does not have Kubectl installed so you’ll get the error.

(⎈ |minikube:default)rrackow@LM-TXL-14510176 ~ $ docker run -ti --rm busybox
    / # kubectl
    sh: kubectl: not found
    / #

If you really want to do this, you’d have to have Kubectl installed in the image the you’re using.