<p>I'm doing research on how to run a Spring Batch job on RedHat OpenShift as a Kubernetes Scheduled Job.
Steps have done,</p>

<p>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. The job launcher is called upon as a REST endpoint (<strong>/load</strong>). The source code can be found <a href="https://github.com/samme4life/spring-batch-example" rel="nofollow noreferrer">here</a>. Please see the README file for the endpoint info.</p>

<p>2) Created the Docker Image and pushed into <a href="https://hub.docker.com/repository/docker/samme4life/spring-batch/tags?page=1" rel="nofollow noreferrer">DockerHub</a></p>

<p>3) Deployed using that image to my OpenShift Online cluster as an <a href="http://spring-batch-example-my-first-project.4b63.pro-ap-southeast-2.openshiftapps.com/health" rel="nofollow noreferrer">app</a></p>

<p>What I want to do is,</p>

<p>Run a Kubernetes Cron Job from OpenShift to call <strong>/load</strong> REST endpoint which launches the SpringBatch job periodically</p>

<p>Can someone please guide me here on how can I achieve this?</p>

<p>Thank you</p>

<p>Samme</p>

Samme
<p>The easiest way would be to curl your <code>/load</code> REST endpoint. 
Here's a way to do that: </p>

<p><strong>The Pod definition that I used as replacement for you application (for testing purposes):</strong></p>

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

<p>I used this image because it sends various HTTP request properties back to client.</p>

<p><strong>Create a service for pod</strong>: </p>

<pre><code>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
</code></pre>

<p><strong>Create a CronJob:</strong></p>

<pre><code>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 * * * *'
</code></pre>

<p><strong>Alternatively you can use command to launch it:</strong> </p>

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

<p>When <code>"*/1 * * * *"</code> will specify how often this <code>CronJob</code> would run. I`ve set it up to run every one minute. 
You can see more about how to setup cron job <a href="http://kubernetes.io/docs/tasks/job/automated-tasks-with-cron-jobs" rel="nofollow noreferrer">here</a> and <a href="https://medium.com/swlh/the-power-of-kubernetes-cron-jobs-d7f550958de8" rel="nofollow noreferrer">here</a></p>

<p><strong>Here is the result of the kubectl logs from one of the job`s pod:</strong> </p>

<pre><code>{
  "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"
</code></pre>

<p>As you can see the application receives <code>GET</code> request with path:  <code>/load</code>. </p>

<p>Let me know if that helps. </p>
