from kubernetes.client import BatchV1Api, V1Job, V1Container, V1ObjectMeta, V1PodTemplateSpec, V1PodSpec, V1JobSpec, \
    V1Volume, V1PersistentVolumeClaimVolumeSource, V1VolumeMount, V1ResourceRequirements
from kubernetes.config import load_kube_config


def create_job_object():
    # 配置容器
    container = V1Container(
        name="ttt",
        image="alpine",
        command=["sleep", "10"],
        volume_mounts=[V1VolumeMount(name='data', mount_path='/data', sub_path='your/sub/path', read_only=True)],
        resources=V1ResourceRequirements()
    )
    # 配置模板
    template = V1PodTemplateSpec(
        metadata=V1ObjectMeta(labels={"app": "ttt"}),
        # 配置pod
        spec=V1PodSpec(
            restart_policy="Never",
            containers=[container],
            # 配置pvc
            volumes=[V1Volume(
                name='data',
                persistent_volume_claim=V1PersistentVolumeClaimVolumeSource(claim_name='data')
            )]
        )
    )
    # 配置任务
    spec = V1JobSpec(
        template=template,
        backoff_limit=1,
        ttl_seconds_after_finished=5,
        active_deadline_seconds=10
    )

    # 实例化Job
    return V1Job(
        api_version="batch/v1",
        kind="Job",
        metadata=V1ObjectMeta(name='ttt'),
        spec=spec
    )


load_kube_config(config_file='kube_config')
batch = BatchV1Api()

# job = create_job_object()

job = {
    'apiVersion': 'batch/v1',
    'kind': 'Job',
    'metadata': {
        'name': 'ttt'
    },
    # job配置
    'spec': {
        # pod个数
        'backoffLimit': 0,
        # 超时时间
        'activeDeadlineSeconds': 100,
        # 结果保存时间
        'ttlSecondsAfterFinished': 60,
        'template': {
            # pod配置
            'spec': {
                'restartPolicy': 'Never',
                # node选择
                'nodeSelector': {
                    "gpu": "1"
                },
                # 容器配置
                'containers': [{
                    'name': 'ttt',
                    'image': 'alpine',
                    # 'env': [{'name': 'NVIDIA_VISIBLE_DEVICES', 'value': '0'}],
                    'command': ['bash', '-c'],
                    'args': ['ls /data && ls /tmp'],
                    # 'command': ['python', '-u', '/data/ttt.py'],
                    # 'resources': {
                    #     'limits': {"nvidia.com/gpu": "1"}
                    # },
                    # 存储挂载
                    'volumeMounts': [{
                        'name': 'data',
                        'mountPath': '/data',
                        # 子目录
                        'subPath': 'your/sub/path',
                        # 只读
                        'readOnly': True
                    }]
                }],
                # 存储挂载
                'volumes': [{
                    'name': 'data',
                    'persistentVolumeClaim': {
                        'claimName': 'data'
                    }
                }]
            }
        }
    }
}
api_response = batch.create_namespaced_job(body=job, namespace="default")

r = batch.read_namespaced_job(name='ttt', namespace='default')
print(r.status)

{'active': None,
 'completion_time': datetime.datetime(2021, 2, 20, 1, 31, 16, tzinfo=tzutc()),
 'conditions': [{'last_probe_time': datetime.datetime(2021, 2, 20, 1, 31, 16, tzinfo=tzutc()),
                 'last_transition_time': datetime.datetime(2021, 2, 20, 1, 31, 16, tzinfo=tzutc()),
                 'message': None,
                 'reason': None,
                 'status': 'True',
                 'type': 'Complete'}],
 'failed': None,
 'start_time': datetime.datetime(2021, 2, 20, 1, 31, 14, tzinfo=tzutc()),
 'succeeded': 1}
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐