Java代码手动从集群触发kubernetes cronjob

标签 java kubernetes fabric8

我正在尝试使用fabric8库手动触发cronjob(未计划) 但出现以下错误:

Caused by: io.fabric8.kubernetes.client.KubernetesClientException: Failure executing: POST at: https://172.20.0.1:443/apis/batch/v1/
namespaces/engineering/jobs. Message: Job.batch "app-chat-manual-947171" is invalid: spec.template.spec.containers[0].name: Re
quired value. Received status: Status(apiVersion=v1, code=422, details=StatusDetails(causes=[StatusCause(field=spec.template.spec.co
ntainers[0].name, message=Required value, reason=FieldValueRequired, additionalProperties={})], group=batch, kind=Job, name=ap
p-chat-manual-947171, retryAfterSeconds=null, uid=null, additionalProperties={}), kind=Status, message=Job.batch "app-chat-man
ual-947171" is invalid: spec.template.spec.containers[0].name: Required value, metadata=ListMeta(_continue=null, remainingItemCount=
null, resourceVersion=null, selfLink=null, additionalProperties={}), reason=Invalid, status=Failure, additionalProperties={}).

我的代码正在集群上运行:

maven 依赖:

<dependency>
            <groupId>io.fabric8</groupId>
            <artifactId>kubernetes-client</artifactId>
            <version>6.3.1</version>
</dependency>

java代码:

public static void triggerCronjob(String cronjobName, String applicableNamespace) {
        KubernetesClient kubernetesClient = new KubernetesClientBuilder().build();

        final String podName = String.format("%s-manual-%s", cronjobName.length() > 38 ? cronjobName.substring(0, 38) : cronjobName,
                new Random().nextInt(999999));

        System.out.println("triggerCronjob method invoked, applicableNamespace: " + applicableNamespace
                + ", cronjobName: " + cronjobName + ", podName: " + podName);

        Job job = new JobBuilder()
                .withApiVersion("batch/v1")
                .withNewMetadata()
                .withName(podName)
                .endMetadata()
                .withNewSpec()
                .withBackoffLimit(4)
                .withNewTemplate()
                .withNewSpec()
                .addNewContainer()
                .withName(podName)
                .withImage("perl")
                .withCommand("perl", "-Mbignum=bpi", "-wle", "print bpi(2000)")
                .endContainer()
                .withRestartPolicy("Never")
                .endSpec()
                .endTemplate()
                .endSpec().build();

        kubernetesClient.batch().v1().jobs().inNamespace(applicableNamespace).createOrReplace(job);
        kubernetesClient.close();
        System.out.println("CronJob triggered: applicableNamespace: " + applicableNamespace + ", cronjob name: " + cronjobName);
    }

在 kubernetes 集群中执行的代码,但不形成应用程序,它是在集群中运行的外部程序。

我的目标是在给定的命名空间中触发给定的作业。

最佳答案

如果要触发已经存在的CronJob,需要在Job中为已有的CronJob提供ownerReference:

// Get already existing CronJob
CronJob cronJob = kubernetesClient.batch().v1()
       .cronjobs()
       .inNamespace(namespace)
       .withName(cronJobName)
       .get();

// Create new Job object referencing CronJob
Job newJobToCreate = new JobBuilder()
       .withNewMetadata()
       .withName(jobName)
       .addNewOwnerReference()
       .withApiVersion("batch/v1")
       .withKind("CronJob")
       .withName(cronJob.getMetadata().getName())
       .withUid(cronJob.getMetadata().getUid())
       .endOwnerReference()
       .addToAnnotations("cronjob.kubernetes.io/instantiate", "manual")
       .endMetadata()
       .withSpec(cronJob.getSpec().getJobTemplate().getSpec())
       .build();
// Apply job object to Kubernetes Cluster
kubernetesClient.batch().v1()
       .jobs()
       .inNamespace(namespace)
       .resource(newJobToCreate)
       .create();

关于Java代码手动从集群触发kubernetes cronjob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75128703/

相关文章:

java - 在下一行显示 TextView

java - 如何在 javapoet 中为 methodspec 添加修饰符?

jenkins - 在 kubernetes 中使用 kubectl set image 命令使用 CI (Jenkins) 创建部署的行业标准解决方案是什么

azure - 通过 Terraform 在支持 kubernetes RBAC 的集群的 AKS 中安装 Flux

java - Forbidden : updates to statefulset spec for fields other than 'replicas' , 'template' 和 'updateStrategy' 被禁止

java - 如何使用 Fabric8 docker-maven-plugin 从 Dockerfile 构建 docker 镜像?

apache-zookeeper - 在 Fabric8 上部署 ZooKeeper 时出错

java - 是什么导致全局Tomcat/JVM变慢?

java - 如何删除android系统栏(页脚栏)

docker - 设置资源限制时,Kubernetes Pod 仅分配给一个节点