How to schedule a cronjob which executes a kubectl command?

后端 未结 2 795
刺人心
刺人心 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:36

    Long story short BusyBox doesn' have kubectl installed.

    You can check it yourself using kubectl run -i --tty busybox --image=busybox -- sh which will run a BusyBox pod as interactive shell.

    I would recommend using bitnami/kubectl:latest.

    Also keep in mind that You will need to set proper RBAC, as you will get Error from server (Forbidden): services is forbidden

    You could use something like this:

    kind: Role
    apiVersion: rbac.authorization.k8s.io/v1
    metadata:
      namespace: jp-test
      name: jp-runner
    rules:
    - apiGroups:
      - extensions
      - apps
      resources:
      - deployments
      verbs:
      - 'patch'
    
    ---
    kind: RoleBinding
    apiVersion: rbac.authorization.k8s.io/v1beta1
    metadata:
      name: jp-runner
      namespace: jp-test
    subjects:
    - kind: ServiceAccount
      name: sa-jp-runner
      namespace: jp-test
    roleRef:
      kind: Role
      name: jp-runner
      apiGroup: ""
    
    ---
    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: sa-jp-runner
      namespace: jp-test
    
    ---
    apiVersion: batch/v1beta1
    kind: CronJob
    metadata:
      name: hello
    spec:
      schedule: "*/5 * * * *"
      jobTemplate:
        spec:
          template:
            spec:
              serviceAccountName: sa-jp-runner
              containers:
              - name: hello
                image: bitnami/kubectl:latest
                command:
                - /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
    
    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题