Kubernetes - 作业调度API

时间:2018-01-30 19:42:15

标签: kubernetes

我正在尝试在Kubernetes安排工作。

https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/

可以使用以下命令创建作业。

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

是否有任何REST API可用于从Java客户端创建作业?

由于

2 个答案:

答案 0 :(得分:1)

the official API reference中描述了相应的REST端点。您会在CronJob API组中找到batch/v1beta1资源。要创建新的CronJob资源,您需要对/apis/batch/v1beta1/namespaces/{namespace}/cronjobs网址进行POST调用。

相应的HTTP请求可能如下所示:

POST /apis/batch/v1beta1/namespaces/default/cronjobs HTTP/1.1
Content-Type: application/json
Content-Length: ...
Authorization: ...
[other headers]

{
  "metadata": {
    "name": "some-cron"
  },
  "spec": {
    ...
  }
}

还有相同资源的旧版本,例如batch/v2alpha1 API group。根据经验,我建议您使用最新的API版本。特别是,不要在生产中依赖alpha API;他们倾向于在发布之间相当快地弃用。

从快速阅读Java client library's documentation,{客户端库}中显然尚不支持batch/v1beta1 cron作业。但是,可能(请记住我之前关于alpha API的警告)能够使用batch/v2alpha1 cron作业API。请查看io.kubernetes.client.apis.BatchV2alpha1Api类及其createNamespacedCronJob method

答案 1 :(得分:0)

HTTP请求

POST /apis/batch/v1beta1/namespaces/{namespace}/cronjobs

您可以在此处查看API概述:cronjob-v1beta1-batch

在请求正文中添加CronJob对象。

{
  "apiVersion": "batch/v1beta1",
  "kind": "CronJob",
  "metadata": {
    "name": "hello"
  },
  "spec": {
    "schedule": "*/1 * * * *",
    "jobTemplate": {

    }
  }
}

点击此处查看其规格:writing-a-cron-job-spec

swagger.json

的一部分
"post": {
 "description": "create a CronJob",
 "consumes": [
  "*/*"
 ],
 "produces": [
  "application/json",
  "application/yaml",
  "application/vnd.kubernetes.protobuf"
 ],
 "schemes": [
  "https"
 ],
 "tags": [
  "batch_v1beta1"
 ],
 "operationId": "createBatchV1beta1NamespacedCronJob",
 "parameters": [
  {
   "name": "body",
   "in": "body",
   "required": true,
   "schema": {
    "$ref": "#/definitions/io.k8s.api.batch.v1beta1.CronJob"
   }
  }
 ],
 "responses": {
  "200": {
   "description": "OK",
   "schema": {
    "$ref": "#/definitions/io.k8s.api.batch.v1beta1.CronJob"
   }
  },
  "201": {
   "description": "Created",
   "schema": {
    "$ref": "#/definitions/io.k8s.api.batch.v1beta1.CronJob"
   }
  },
  "202": {
   "description": "Accepted",
   "schema": {
    "$ref": "#/definitions/io.k8s.api.batch.v1beta1.CronJob"
   }
  },
  "401": {
   "description": "Unauthorized"
  }
 },
 "x-kubernetes-action": "post",
 "x-kubernetes-group-version-kind": {
  "group": "batch",
  "kind": "CronJob",
  "version": "v1beta1"
 }
}

查看完整的swagger.json