【问题标题】:create an Java api that will manually trigger Kubernetes already created jobs创建一个将手动触发 Kubernetes 已创建作业的 Java api
【发布时间】:2022-10-13 11:15:36
【问题描述】:

我有一个工作已经在Kubernates 运行,计划为 4 小时。但是我需要编写一个 Java API,这样每当我想运行作业时,我只需要调用这个 API 并运行作业。

请帮助解决这个要求。

【问题讨论】:

    标签: java spring-boot api kubernetes kubectl


    【解决方案1】:
    • 有两种方法,您可以在 POD 中运行您的应用程序,为您创建 JOB,或者您编写 Java API,当您点击端点时,它将在那时创建该作业。

    对于创建,您可以使用 Java Kubernetes 客户端库。

    示例 - Click here

    Java 客户端 - Click here

    package io.fabric8.kubernetes.examples;
    
    import io.fabric8.kubernetes.api.model.PodList;
    import io.fabric8.kubernetes.api.model.batch.v1.Job;
    import io.fabric8.kubernetes.api.model.batch.v1.JobBuilder;
    import io.fabric8.kubernetes.client.ConfigBuilder;
    import io.fabric8.kubernetes.client.DefaultKubernetesClient;
    import io.fabric8.kubernetes.client.KubernetesClient;
    import io.fabric8.kubernetes.client.KubernetesClientException;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import java.util.Collections;
    import java.util.concurrent.TimeUnit;
    
    /*
     * 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 > 0) {
              configBuilder.withMasterUrl(args[0]);
            }
            try (KubernetesClient client = new DefaultKubernetesClient(configBuilder.build())) {
              final String namespace = "default";
              final Job job = new JobBuilder()
                .withApiVersion("batch/v1")
                .withNewMetadata()
                .withName("pi")
                .withLabels(Collections.singletonMap("label1", "maximum-length-of-63-characters"))
                .withAnnotations(Collections.singletonMap("annotation1", "some-very-long-annotation"))
                .endMetadata()
                .withNewSpec()
                .withNewTemplate()
                .withNewSpec()
                .addNewContainer()
                .withName("pi")
                .withImage("perl")
                .withArgs("perl", "-Mbignum=bpi", "-wle", "print bpi(2000)")
                .endContainer()
                .withRestartPolicy("Never")
                .endSpec()
                .endTemplate()
                .endSpec()
                .build();
    
              logger.info("Creating job pi.");
              client.batch().v1().jobs().inNamespace(namespace).createOrReplace(job);
    
              // Get All pods created by the job
              PodList podList = client.pods().inNamespace(namespace).withLabel("job-name", job.getMetadata().getName()).list();
              // Wait for pod to complete
              client.pods().inNamespace(namespace).withName(podList.getItems().get(0).getMetadata().getName())
                .waitUntilCondition(pod -> pod.getStatus().getPhase().equals("Succeeded"), 1, TimeUnit.MINUTES);
    
              // Print Job's log
              String joblog = client.batch().v1().jobs().inNamespace(namespace).withName("pi").getLog();
              logger.info(joblog);
    
            } catch (KubernetesClientException e) {
                logger.error("Unable to create job", e);
            }
        }
    }
    

    选项:2

    您还可以应用YAML文件

    ApiClient client = ClientBuilder.cluster().build(); //create in-cluster client
    Configuration.setDefaultApiClient(client);
    BatchV1Api api = new BatchV1Api(client);
    
    V1Job job = new V1Job();
    job = (V1Job) Yaml.load(new File("<YAML file path>.yaml")); //apply static yaml file
        
    ApiResponse<V1Job> response = api.createNamespacedJobWithHttpInfo("default", job, "true", null, null);
    

    【讨论】:

    • 上面的响应是关于如何使用 Java 创建作业。但在我的情况下,作业已经创建,我只需要一个 API 来在我需要时触发它
    猜你喜欢
    • 2021-05-23
    • 2017-03-17
    • 2017-05-14
    • 2010-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-02
    • 2018-11-03
    相关资源
    最近更新 更多