How to schedule a cronjob which executes a kubectl command?

后端 未结 2 799
刺人心
刺人心 2020-12-17 03:07

How to schedule a cronjob which executes a kubectl command?

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

kubectl patch deployme         


        
2条回答
  •  清酒与你
    2020-12-17 03:41

    You need to make the CronJob's container to download the cluster configuration so then you can run kubectl commands against it. Here is an example:

    apiVersion: batch/v1beta1
    kind: CronJob
    metadata:
      name: drupal-cron
    spec:
      schedule: "*/5 * * * *"
      concurrencyPolicy: Forbid
      jobTemplate:
        spec:
          template:
            spec:
              containers:
                - name: drupal-cron
                  image: juampynr/digital-ocean-cronjob:latest
                  env:
                    - name: DIGITALOCEAN_ACCESS_TOKEN
                      valueFrom:
                        secretKeyRef:
                          name: api
                          key: key
                  command: ["/bin/bash","-c"]
                  args:
                    - doctl kubernetes cluster kubeconfig save drupster;
                      POD_NAME=$(kubectl get pods -l tier=frontend -o=jsonpath='{.items[0].metadata.name}');
                      kubectl exec $POD_NAME -c drupal -- vendor/bin/drush core:cron;
              restartPolicy: OnFailure
    

    I posted an answer describing how I did this in a different thread: https://stackoverflow.com/a/62321138/1120652

提交回复
热议问题