zoukankan      html  css  js  c++  java
  • Creating a Cron Job in K8S

    Creating a Cron Job

    Cron jobs require a config file. This example cron job config .spec file prints the current time and a hello message every minute:

    application/job/cronjob.yaml 
    apiVersion: batch/v1beta1
    kind: CronJob
    metadata:
      name: hello
    spec:
      schedule: "*/1 * * * *"
      jobTemplate:
        spec:
          template:
            spec:
              containers:
              - name: hello
                image: busybox
                args:
                - /bin/sh
                - -c
                - date; echo Hello from the Kubernetes cluster
              restartPolicy: OnFailure
    

    Run the example cron job by downloading the example file and then running this command:

    $ kubectl create -f ./cronjob.yaml
    cronjob "hello" created

    Alternatively, you can use kubectl run to create a cron job without writing a full config:

    $ kubectl run hello --schedule="*/1 * * * *" --restart=OnFailure --image=busybox -- /bin/sh -c "date; echo Hello from the Kubernetes cluster"
    cronjob "hello" created

    After creating the cron job, get its status using this command:

    $ kubectl get cronjob hello
    NAME      SCHEDULE      SUSPEND   ACTIVE    LAST-SCHEDULE
    hello     */1 * * * *   False     0         <none>

    As you can see from the results of the command, the cron job has not scheduled or run any jobs yet. Watch for the job to be created in around one minute:

    $ kubectl get jobs --watch
    NAME               DESIRED   SUCCESSFUL   AGE
    hello-4111706356   1         1         2s

    Now you’ve seen one running job scheduled by the “hello” cron job. You can stop watching the job and view the cron job again to see that it scheduled the job:

    $ kubectl get cronjob hello
    NAME      SCHEDULE      SUSPEND   ACTIVE    LAST-SCHEDULE
    hello     */1 * * * *   False     0         Mon, 29 Aug 2016 14:34:00 -0700

    You should see that the cron job “hello” successfully scheduled a job at the time specified in LAST-SCHEDULE. There are currently 0 active jobs, meaning that the job has completed or failed.

    Now, find the pods that the last scheduled job created and view the standard output of one of the pods. Note that the job name and pod name are different.

    # Replace "hello-4111706356" with the job name in your system
    $ pods=$(kubectl get pods --selector=job-name=hello-4111706356 --output=jsonpath={.items..metadata.name})
    
    $ echo $pods
    hello-4111706356-o9qcm
    
    $ kubectl logs $pods
    Mon Aug 29 21:34:09 UTC 2016
    Hello from the Kubernetes cluster

     https://kubernetes.io/docs/tasks/job/automated-tasks-with-cron-jobs/

  • 相关阅读:
    使用samba实现linux和windows文件共享
    使用li列举属性表中的某一属性
    popuptemplate的使用
    html中自动分配界面
    div中移除和添加元素
    使用v-html绑定数据,实现图片的动态转换
    使用js下载数据
    使用FeatureTable对FeatureLayer中的数据进行显示
    使用ant的checkboxGroup将列表信息添加为多选框,并根据多选框的转换进行操作
    arcgis api绘制多个点
  • 原文地址:https://www.cnblogs.com/oskb/p/10244470.html
Copyright © 2011-2022 走看看