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/

  • 相关阅读:
    DDD之3实体和值对象
    DDD之2领域概念
    DDD之1微服务设计为什么选择DDD
    SOFA入门
    COLA的扩展性使用和源码研究
    kafka可插拔增强如何实现?
    请设计一个核心功能稳定适合二开扩展的软件系统
    如何保证kafka消息不丢失
    kafka高吞吐量之消息压缩
    kafka消息分区机制原理
  • 原文地址:https://www.cnblogs.com/oskb/p/10244470.html
Copyright © 2011-2022 走看看