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/

Creating a Cron Job in K8S的更多相关文章

  1. K8S从入门到放弃系列-(1)环境初始化

    一.系统规划  主机名 IP   组件  k8s-master01 10.10.0.18 etcd.kube-apiserver.kube-controller-manager.kube-schedu ...

  2. Quartz 2.2.x CronTrigger Tutorial

    http://www.quartz-scheduler.org/documentation/quartz-2.2.x/tutorials/crontrigger.html Introduction c ...

  3. kubernetes secret 和 serviceaccount删除

    背景 今天通过配置创建了一个serviceaccounts和secret,后面由于某种原因想再次创建发现已存在一个serviceaccounts和rolebindings.rbac.authoriza ...

  4. k8s 执行 ingress yaml 文件报错: error when creating "ingress-myapp.yaml": Internal error occurred: failed calling webhook

    k8s 执行 ingress yaml 文件报错:错误如下: [root@k8s-master01 baremetal]# kubectl apply -f ingress-test.yaml Err ...

  5. k8s的基本使用

    一.kubectl的命令参数 1)kubectl 能使用的命令.即查看帮助 [root@k8s6 ~]# kubectl kubectl controls the Kubernetes cluster ...

  6. [k8s]Docker 用户使用 kubectl 命令指南-unkown排错(kubelet端口解析)

    参考:https://kubernetes.io/docs/user-guide/kubectl-cheatsheet/ https://k8smeetup.github.io/docs/user-g ...

  7. docker+k8s基础篇二

    Docker+K8s基础篇(二) docker的资源控制 A:docker的资源限制 Kubernetes的基础篇 A:DevOps的介绍 B:Kubernetes的架构概述 C:Kubernetes ...

  8. K8S(16)集成实战-使用spinnaker进行自动化部署

    K8s集成实战-使用spinnaker进行自动化部署 1 spinnaker概述和选型 1.1 概述 1.1.1 主要功能 Spinnaker是一个开源的多云持续交付平台,提供快速.可靠.稳定的软件变 ...

  9. 搭建docker私有仓库,建立k8s集群

    服务器IP角色分布 192.168.5.2 etcd server 192.168.5.2 kubernetes master 192.168.5.3 kubernetes node 192.168. ...

随机推荐

  1. python循环结构

    while循环 while 条件表达式: 语句块 while语句的条件表达式是循环条件,常用的是关系表达式或者逻辑表达式,语句块是循环执行的语句. n=1 p=1 num=int(input(&quo ...

  2. nginx gzip压缩配置

    gzip(GNU-ZIP)是一种压缩技术.经过gzip压缩后页面大小可以变为原来的30%甚至更小,这样,用户浏览页面的时候速度会块得多.gzip 的压缩页面需要浏览器和服务器双方都支持,实际上就是服务 ...

  3. 一个简单的rest_framework demo

    models.py from django.db import models class UserInfo(models.Model): username = models.CharField(max ...

  4. JavaScript之函数柯里化

    什么是柯里化(currying)? 维基百科中的解释是:柯里化是把接受多个参数的函数变换成接受一个单一参数(最初函数的第一个参数)的函数,并且返回接受余下的参数而且返回结果的新函数的技术.意思就是当函 ...

  5. P4环境搭建

    P4环境搭建 执行仓库中所有脚本,即可即可安装所有依赖项. GitHub链接 脚本执行顺序:deps,p4c-bm,bmv2,p4c

  6. 01.1 Windows环境下JDK安装与环境变量配置详细的图文教程

    01.1 Windows环境下JDK安装与环境变量配置详细的图文教程 本节内容:JDK安装与环境变量配置 以下是详细步骤 一.准备工具: 1.JDK JDK 可以到官网下载 http://www.or ...

  7. 使用Python 、 go 语言测试rabbitmq的工作机制

    1:在haproxy 和 rabbitmq上安装Python.python2-pip,默认是Python2 yum install -y python python2-pip   2:在haproxy ...

  8. Hibernate(九)

    三套查询之SQL查询 Native Sql Query原生的sql查询.要求写sql语句.SQLQuery 是 Query的子类 1.查询所有的学生 //1.查询所有的学生 @Test public ...

  9. 【uoj#244】[UER #7]短路 CDQ分治+斜率优化dp

    题目描述 给出 $(2n+1)\times (2n+1)$ 个点,点 $(i,j)$ 的权值为 $a[max(|i-n-1|,|j-n-1|)]$ ,找一条从 $(1,1)$ 走到 $(2n+1,2n ...

  10. C++解析-外传篇(3):动态内存申请的结果

    0.目录 1.动态内存申请一定成功吗? 2.new_handler() 函数 3.小结 1.动态内存申请一定成功吗? 问题: 动态内存申请一定成功吗? 常见的动态内存分配代码: C代码: C++代码: ...