使用k8s和多个容器处理作业

2024-10-01 07:27:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要使用k8s在s3中处理多个文件,因此我在k8s上创建了一个作业,包含大约500个容器,每个容器具有不同的环境。然而,这项工作非常缓慢,而且多次失败

我正在使用kubernetes api python提交作业,如下所示:

def read_path():
    files = []
    suffixes = ('_SUCCESS', 'referen/')

    files_path = fs.listdir(f"s3://{path}")
    if files_path is not None:
        for num, file in enumerate(files_path, start=1):
            if not file['Key'].endswith(suffixes):
                files.append(file['Key'])
    return files

def from_containers(container, path):
    containers = []
    for num, file in enumerate(read_path(container, path), start=1):
        containers.append(client.V1Container(name=f'hcm1-{num}', image='image-python',
                                             command=['python3', 'model.py'],
                                             env=[client.V1EnvVar(name='model', value='hcm1'),
                                                  client.V1EnvVar(name='referen', value=f"s3://{file}")]))


template = client.V1PodTemplateSpec(metadata=client.V1ObjectMeta(name="hcm1"),
                                    spec=client.V1PodSpec(restart_policy="OnFailure",
                                    containers=from_containers(containers, path),
                                    image_pull_secrets=[client.V1LocalObjectReference(name="secret")]))

spec = client.V1JobSpec(template=template, backoff_limit=20)

job = client.V1Job(api_version="batch/v1", kind="Job", metadata=client.V1ObjectMeta(name="hcm1"), spec=spec)

api_response = batch_v1.create_namespaced_job(body=job, namespace="default")
print("Job created. status='%s'" % str(api_response.status))

我尝试使用一些配置,比如completions=10,concurrency=2,但是我的作业执行了多次,10*500=5000

使用多个容器在k8s上创建作业的更好方法是什么

  1. 1份工作->;1舱->;500个集装箱
  2. 1份工作->;500个吊舱(每个吊舱1个集装箱)
  3. 还有别的办法吗?**

Tags: pathnameimageclientapis3作业files
1条回答
网友
1楼 · 发布于 2024-10-01 07:27:47

你的问题有点不对劲。根据您的情况和方法,您可以找到许多解决方案。 但是David Maze提到得很好:

If you have this scripting setup already, why not 500 Jobs? (Generally you want to avoid multiple containers per pod.)

是的,如果您正在编写脚本,这可能是解决问题的一个非常好的方法。它肯定不会那么复杂,因为为1个pod创建1Job。每个吊舱将有一个集装箱

理论上,您还可以定义自己的Custom Resource并编写一个类似于Job controller的控制器,它支持多个pod模板

另见this question.

相关问题 更多 >