简体   繁体   English

从 OpenShift 运行 Kubernetes Cron 作业以定期调用 REST 端点

[英]Run a Kubernetes Cron Job from OpenShift to call a REST endpoint Periodically

I'm doing research on how to run a Spring Batch job on RedHat OpenShift as a Kubernetes Scheduled Job.我正在研究如何在 RedHat OpenShift 上将 Spring Batch 作业作为 Kubernetes 计划作业运行。 Steps have done,步骤已经完成,

1) Created a sample Spring Batch app that reads a .csv file that does simple processing and puts some data into in-memory h2 DB. 1) 创建了一个示例 Spring Batch 应用程序,该应用程序读取一个 .csv 文件,该文件进行简单处理并将一些数据放入内存中的 h2 DB。 The job launcher is called upon as a REST endpoint ( /load ).作业启动器作为 REST 端点 ( /load ) 被调用。 The source code can be found here .源代码可以在这里找到。 Please see the README file for the endpoint info.有关端点信息,请参阅 README 文件。

2) Created the Docker Image and pushed into DockerHub 2) 创建 Docker Image 并推送到DockerHub

3) Deployed using that image to my OpenShift Online cluster as an app 3) 使用该映像作为应用程序部署到我的 OpenShift Online 集群

What I want to do is,我想做的是,

Run a Kubernetes Cron Job from OpenShift to call /load REST endpoint which launches the SpringBatch job periodically从 OpenShift 运行 Kubernetes Cron 作业以调用/load REST 端点,该端点定期启动 SpringBatch 作业

Can someone please guide me here on how can I achieve this?有人可以在这里指导我如何实现这一目标吗?

Thank you谢谢

Samme萨姆

The easiest way would be to curl your /load REST endpoint.最简单的方法是卷曲您的/load REST 端点。 Here's a way to do that:这是一种方法:

The Pod definition that I used as replacement for you application (for testing purposes):我用来替代您的应用程序的 Pod 定义(用于测试目的):

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
spec:
  containers:
  - name: myapp-container
    image: mendhak/http-https-echo

I used this image because it sends various HTTP request properties back to client.我使用这个图像是因为它将各种 HTTP 请求属性发送回客户端。

Create a service for pod :为 pod 创建一个服务

apiVersion: v1
kind: Service
metadata:
  name: myapp-service
spec:
  selector:
    app: myapp the selector
  ports:
    - protocol: TCP
      port: 80 #Port that service is available on
      targetPort: 80 #Port that app listens on

Create a CronJob:创建一个 CronJob:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: curljob
spec:
  jobTemplate:
    metadata:
      name: curljob
    spec:
      template:
        metadata:
        spec:
          containers:
          - command:
            - curl
            - http://myapp-service:80/load
            image: curlimages/curl
            imagePullPolicy: Always
            name: curljobt
          restartPolicy: OnFailure
  schedule: '*/1 * * * *'

Alternatively you can use command to launch it:或者,您可以使用命令来启动它:

kubectl create cronjob --image curlimages/curl curljob -oyaml --schedule "*/1 * * * *" -- curl  http://myapp-service:80/load 

When "*/1 * * * *" will specify how often this CronJob would run."*/1 * * * *"将指定此CronJob运行的频率时。 I`ve set it up to run every one minute.我已经设置为每分钟运行一次。 You can see more about how to setup cron job here and here您可以在此处此处查看有关如何设置 cron 作业的更多信息

Here is the result of the kubectl logs from one of the job`s pod:这是来自作业的 pod 之一的 kubectl 日志的结果:

{
  "path": "/load",
  "headers": {
    "host": "myapp-service",
    "user-agent": "curl/7.68.0-DEV",
    "accept": "*/*"
  },
  "method": "GET",
  "body": "",
  "fresh": false,
  "hostname": "myapp-service",
  "ip": "::ffff:192.168.197.19",
  "ips": [],
  "protocol": "http",
  "query": {},
  "subdomains": [],
  "xhr": false,
  "os": {
    "hostname": "myapp-pod"

As you can see the application receives GET request with path: /load .正如您所看到的,应用程序通过路径接收GET请求: /load

Let me know if that helps.如果这有帮助,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM