Asa*_*afG 6 javascript kubernetes kubectl kubernetes-cronjob
创建工作的等价物是什么:
kubectl -n my-ns create job --from=cronjob/my-cron-job my-job
Run Code Online (Sandbox Code Playgroud)
这就是javascript kubernetes-client的样子,在集群内的 pod 中使用(可能需要 rbac 权限,我在 k8s docker-desktop 上运行,所以不需要):
const createJobFromCronJob = async (
cronJobNamespace: string,
cronJobName: string,
jobNamespace: string,
jobName: string) => {
// next 4 lines can be initialized somewhere else
const kubeConfig = new k8s.KubeConfig();
kubeConfig.loadFromCluster();
const batchV1Api = kubeConfig.makeApiClient(k8s.BatchV1Api);
const batchV1beta1Api = kubeConfig.makeApiClient(k8s.BatchV1beta1Api);
try {
const cronJob = await batchV1beta1Api.readNamespacedCronJob(cronJobName, cronJobNamespace);
const cronJobSpec = cronJob.body.spec.jobTemplate.spec;
const job = new k8s.V1Job();
const metadata = new k8s.V1ObjectMeta();
job.apiVersion = 'batch/v1';
job.kind = 'Job';
job.spec = cronJobSpec;
metadata.name = jobName;
metadata.annotations = {
'cronjob.kubernetes.io/instantiate': 'manual',
};
job.metadata = metadata;
const result = await batchV1Api.createNamespacedJob(jobNamespace, job);
console.log('job created');
} catch (err) {
console.error(`failed to create job: ${err.message}`);
throw err;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
181 次 |
| 最近记录: |