<p>Am able to create a job in the Kubernetes cluster using CLI (<a href="https://kubernetesbyexample.com/jobs/" rel="nofollow noreferrer">https://kubernetesbyexample.com/jobs/</a>)</p>
<p>Is there a way to create a job inside the cluster using Java API ?</p>

Amal Thachappilly
<p>You can use Kubernetes Java Client to create any object such as Job. Referring from the example <a href="https://github.com/fabric8io/kubernetes-client/blob/master/kubernetes-examples/src/main/java/io/fabric8/kubernetes/examples/JobExample.java" rel="nofollow noreferrer">here</a></p>
<pre class="lang-java prettyprint-override"><code>/*
 * Creates a simple run to complete job that computes π to 2000 places and prints it out.
 */
public class JobExample {
    private static final Logger logger = LoggerFactory.getLogger(JobExample.class);

    public static void main(String[] args) {
        final ConfigBuilder configBuilder = new ConfigBuilder();
        if (args.length &gt; 0) {
          configBuilder.withMasterUrl(args[0]);
        }
        try (KubernetesClient client = new DefaultKubernetesClient(configBuilder.build())) {
          final String namespace = &quot;default&quot;;
          final Job job = new JobBuilder()
            .withApiVersion(&quot;batch/v1&quot;)
            .withNewMetadata()
            .withName(&quot;pi&quot;)
            .withLabels(Collections.singletonMap(&quot;label1&quot;, &quot;maximum-length-of-63-characters&quot;))
            .withAnnotations(Collections.singletonMap(&quot;annotation1&quot;, &quot;some-very-long-annotation&quot;))
            .endMetadata()
            .withNewSpec()
            .withNewTemplate()
            .withNewSpec()
            .addNewContainer()
            .withName(&quot;pi&quot;)
            .withImage(&quot;perl&quot;)
            .withArgs(&quot;perl&quot;, &quot;-Mbignum=bpi&quot;, &quot;-wle&quot;, &quot;print bpi(2000)&quot;)
            .endContainer()
            .withRestartPolicy(&quot;Never&quot;)
            .endSpec()
            .endTemplate()
            .endSpec()
            .build();

          logger.info(&quot;Creating job pi.&quot;);
          client.batch().jobs().inNamespace(namespace).createOrReplace(job);

          // Get All pods created by the job
          PodList podList = client.pods().inNamespace(namespace).withLabel(&quot;job-name&quot;, job.getMetadata().getName()).list();
          // Wait for pod to complete
          client.pods().inNamespace(namespace).withName(podList.getItems().get(0).getMetadata().getName())
            .waitUntilCondition(pod -&gt; pod.getStatus().getPhase().equals(&quot;Succeeded&quot;), 1, TimeUnit.MINUTES);

          // Print Job's log
          String joblog = client.batch().jobs().inNamespace(namespace).withName(&quot;pi&quot;).getLog();
          logger.info(joblog);

        } catch (KubernetesClientException e) {
            logger.error(&quot;Unable to create job&quot;, e);
        } catch (InterruptedException interruptedException) {
          logger.warn(&quot;Thread interrupted!&quot;);
          Thread.currentThread().interrupt();
        }
    }
}
</code></pre>
