在Kubernetes创造就业机会后,Pod大小归零

yiytaume  于 2023-01-25  发布在  Kubernetes
关注(0)|答案(1)|浏览(82)

我们正在使用java kubernates客户端API(V:5.12.2)创建kubernates作业,如下所示。
我想到两个地方。有人能帮忙吗?
1.以下代码片段中的podList.getItems(). size()有时返回零,即使我看到pod已创建并具有其他现有作业。
1.如何指定特定的标签到工作舱?

KubernetesClient kubernetesClient = new DefaultKubernetesClient();
String namespace = System.getenv(POD_NAMESPACE);
String jobName = TextUtils.concatenateToString("flatten" + Constants.HYPHEN+ flattenId);
Job jobRequest = createJob(flattenId, authValue);

var jobResult = kubernetesClient.batch().v1().jobs().inNamespace(namespace)
    .create(jobRequest);
PodList podList = kubernetesClient.pods().inNamespace(namespace)
    .withLabel("job-name", jobName).list();

// Wait for pod to complete
var pods = podList.getItems().size();
var terminalPodStatus = List.of("succeeded", "failed");
_LOGGER.info("pods created size:" + pods);
if (pods > 0) {
// returns zero some times. 
var k8sPod = podList.getItems().get(0);
var podName = k8sPod.getMetadata().getName();
kubernetesClient.pods().inNamespace(namespace).withName(podName)
    .waitUntilCondition(pod -> {
      var podPhase = pod.getStatus().getPhase();
       //some logic 
      return terminalPodStatus.contains(podPhase.toLowerCase());
    }, JOB_TIMEOUT, TimeUnit.MINUTES);
kubernetesClient.close();
}
private Job createJob(String flattenId, String authValue) {
       return new JobBuilder()
        .withApiVersion(API_VERSION)
        .withNewMetadata().withName(jobName)
        .withLabels(labels)
        .endMetadata()
        .withNewSpec()
        .withTtlSecondsAfterFinished(300)
        .withBackoffLimit(0)
        .withNewTemplate()
        .withNewMetadata().withAnnotations(LINKERD_INJECT_ANNOTATIONS)
        .endMetadata()
        .withNewSpec()
        .withServiceAccount(Constants.TEST_SERVICEACCOUNT)
        .addNewContainer()
        .addAllToEnv(envVars)
        .withImage(System.getenv(BUILD_JOB_IMAGE))
        .withName(“”test)
        .withCommand("/bin/bash", "-c", "java -jar test.jar")
        .endContainer()
        .withRestartPolicy(RESTART_POLICY_NEVER)
        .endSpec()
        .endTemplate()
        .endSpec()
        .build();
  }
gjmwrych

gjmwrych1#

创建作业后不会立即创建Pod:作业控制器需要激活并相应地创建Pod。根据控制平面上的负载和作业示例的数量,您可能需要等待或长或短的时间。

相关问题