k8s通过API创建Job

2021-09-24  本文已影响0人  jaymz明

最近接到一个任务,需要通过页面,根据用户的输入创建k8s的Job资源。虽然只是创建job,但是基本上其他资源也是通用的。看了网上的资料,基本上是使用https://github.com/fabric8io/kubernetes-client
第一步,先要获取集群的证书,需要通过集群认证才能进行操作。主要使用ca.crt,kubectl-kube-api-client.crt,kubectl-kube-api-client.key

      config = new ConfigBuilder().withMasterUrl(master).withTrustCerts(true).build();
      config.setCaCertFile(caCertFile);
      config.setClientCertFile(clientCertFile);
      config.setClientKeyFile(clientKeyFile);
      config.setConnectionTimeout(timeout);
      config.setRequestTimeout(timeout);

第二步,使用api来创建job,首先KubernetesClient client = new DefaultKubernetesClient(kubernetesConfig.buildConfig()),获取操作client。然后组装job的yaml,最后使用 Job newJob = client.batch().jobs().createOrReplace(job);

job yaml构建如下:

           final Job job = new JobBuilder()
                    .withApiVersion("batch/v1")
                    .withNewMetadata()
                    .withNamespace("default")
                    .withName("data-population-"+jobBean.getTenantId())
                    .endMetadata()
                    .withNewSpec()
                    .withCompletions(Integer.valueOf(jobBean.getCount()))
                    .withParallelism(1)
                    .withNewTemplate()
                    .withNewSpec()
                    .addNewInitContainer()
                    .withName("pre-data")
                    .withImage("xxxx/pre-data:v2.4")
                    .withArgs("sh","-c"," "xxx"; ./replace_env_properties.sh ")
                    .addNewEnv()
                            .withNewName("authUrl")
                            .withNewValue(authUrl)
                    .endEnv()
                    .addNewEnv()
                            .withNewName("authBody")
                            .withNewValue(authBody)
                    .endEnv()
                    .addNewEnv()
                            .withNewName("metaDataUrl")
                            .withNewValue(metaDataUrl)
                    .endEnv()
                    .addNewEnv()
                            .withNewName("bulkUrl")
                            .withNewValue(bulkUrl)
                    .endEnv()
                    .addNewEnv()
                            .withNewName("emsFilterUrl")
                            .withNewValue(emsFilterUrl)
                    .endEnv()
                    .addNewEnv()
                            .withNewName("deploySingleSLTUrl")
                            .withNewValue(deploySingleSLTUrl)
                    .endEnv()
                    .addNewEnv()
                            .withNewName("applySLT")
                            .withNewValue(applySLT)
                            .endEnv()

                    .withVolumeMounts(new VolumeMountBuilder()
                            .withName("datahelix-biz")
                            .withMountPath("/root/")
                            .build())
                    .endInitContainer()
                    .addNewInitContainer()
                    .withName("datahelix")
                    .withImage("xxxx/datahelix:v1.2")
                    .withArgs("sh","-c","xxx; ./generate_csv_data.sh")
                    .withEnv(new EnvVarBuilder()
                            .withNewName("profile")
                            .withNewValue(customProfile)
                            .build())
                    .withVolumeMounts(new VolumeMountBuilder()
                            .withName("datahelix-biz")
                            .withMountPath("/root/")
                            .build())
                    .endInitContainer()
                    .addNewContainer()
                    .withName("pdi-ce")
                    .withImage("xxx/pentaho_kettle:v1.1")
                    .withCommand("sh","-c","./kitchen.sh -file xxx/data_population_e2e_job.kjb; exit 0")

                    .withVolumeMounts(new VolumeMountBuilder()
                            .withName("datahelix-biz")
                            .withMountPath("/home/pentaho/")
                            .build())
                    .endContainer()
                    .withRestartPolicy("Never")
                    .withVolumes(new VolumeBuilder()
                            .withName("datahelix-biz")
                            .withNewEmptyDir()
                            .endEmptyDir()
                            .build())
                    .endSpec()
                    .endTemplate()
                    .endSpec()
                    .build();

这部分yaml文件包含了2个initContainers,一个真正干活的container。里面涉及env的创建,command的使用,已经volume的使用。
k8s job的使用对于一次性的任务来说,效果不错。目前使用它来抛数据。通过读取制定的profile,多线程抛数据。

上一篇 下一篇

猜你喜欢

热点阅读