目录
Job的创建:
首先是通过pip install kubernetes安装kubernetes,在k8s环境通过命令 kubectl api-resources 查询k8s的api-ersion,官方所发布的apiversion分别有分别有batch/v1,batch/betav1,batch等,需要查询所使用的api属于那一个。
当这些准备工作都做好之后,使用python编写生成job的代码,如下:
from configs import projectConfig
from kubernetes import client
from kubernetes.client import BatchV1Api, V1Container, V1PodTemplateSpec, V1ObjectMeta, V1PodSpec, V1JobSpec, V1Job, V1LocalObjectReference
from kubernetes.config import load_kube_config
from kubernetes.client import BatchV1Api, V1Job, V1Container, V1ObjectMeta, V1PodTemplateSpec, V1PodSpec, V1JobSpec, \
V1Volume, V1PersistentVolumeClaimVolumeSource, V1VolumeMount, V1ResourceRequirements,V1beta1CronJob,V1beta1CronJobSpec,V1beta1JobTemplateSpec,BatchV1beta1Api
def create_job_object(newSubscribeInfo,newComputingPowerSubscribeID):
# Configureate Pod template container
container = V1Container(
name=JOB_NAME,
image=IMAGE_NAME,
command=['/bin/bash'],
args=["-c", "sleep " + str(newSubscribeInfo.delayNumber)+ ";cd execScript; python createNode.py --nodeName={} --computingpowerType={} --userNumber={} --cpuLimit={} --memoryLimit={} --duration={} --computingpowerSubscribeId={} --status={};".format(
str(newSubscribeInfo.name),str(newSubscribeInfo.computingPowerType),str(newSubscribeInfo.userNumber),str(newSubscribeInfo.cpuLimit),str(newSubscribeInfo.memoryLimit),
str(newSubscribeInfo.duration),str(newComputingPowerSubscribeID),str(newSubscribeInfo.status))],
volume_mounts=[V1VolumeMount(name='job-pv-persistent-storage', mount_path='/usr/src/app/execScript', sub_path="execScript")],
resources=V1ResourceRequirements()
)
# 镜像使用
imagePullSecrets = V1LocalObjectReference(
name = "default-secret"
)
# Create and configure a spec section
template = V1PodTemplateSpec(
metadata=V1ObjectMeta(name=newSubscribeInfo.name),
spec=V1PodSpec(restart_policy="Never", containers=[container], image_pull_secrets=[imagePullSecrets],
volumes=[V1Volume(
name='job-pv-persistent-storage',
persistent_volume_claim=V1PersistentVolumeClaimVolumeSource(claim_name='job-pv-claim')
)]))
# Create the specification of deployment
spec = V1JobSpec(
template=template,
backoff_limit=4)
# Instantiate the job object
job = V1Job(
api_version="batch/v1",
kind="Job",
metadata=V1ObjectMeta(name=newSubscribeInfo.name, namespace=projectConfig.STAR_EVA_JOB_NAMESPACE),
spec=spec)
return job
def delete_job(api_instance):
api_response = api_instance.delete_namespaced_job(
name=JOB_NAME,
namespace=projectConfig.STAR_EVA_JOB_NAMESPACE,
body=client.V1DeleteOptions(
propagation_policy='Foreground',
grace_period_seconds=5))
print("Job deleted. status='%s'" % str(api_response.status))
def get_job_list(namespace):
v1 = client.BatchV1Api()
ret = v1.list_namespaced_job(namespace=namespace, pretty=True, _preload_content=False)
job_list = json.loads(ret.data)
print(f'job number={len(job_list["items"])}')
return job_list["items"]
def judge_job_exists(namespace, name):
job_list = get_job_list(namespace)
for job in job_list:
if name == job['metadata']['name']:
return True
return False
def create_job(job,namespace,name):
batch_v1 = BatchV1Api()
if judge_job_exists(namespace, name):
print(f'{name} exists, please do not repeat!')
return f'{name} exists, please do not repeat!'
else:
api_response = batch_v1.create_namespaced_job(
body=job,
namespace=namespace)
print("Job created. status='%s'" % str(api_response.status))
return "Job created. status='%s'" % str(api_response.status)
job=create_job_object(newSubscribeInfo,newComputingPowerSubscribeID)
create_job(job,projectConfig.STAR_EVA_JOB_NAMESPACE,newSubscribeInfo.name)
CronJob创建:
cronjob的创建尤为要注意kubernetes的版本,一些老版本的api是在batch/betav1,但新版本在batch/v1beta1,如api:
python在下载版本时就需要下载beta的版本,如图,带不b1后缀的python 包,可通过网址查看kubernetes · PyPI
因为我的k8s环境的cronjob的提供是v1beta1,所以下载了18.20.0b1,创建cronjob的相关代码如下:
from configs import projectConfig
from kubernetes import client
from kubernetes.client import BatchV1Api, V1Container, V1PodTemplateSpec, V1ObjectMeta, V1PodSpec, V1JobSpec, V1Job, V1LocalObjectReference
from kubernetes.config import load_kube_config
from kubernetes.client import BatchV1Api, V1Job, V1Container, V1ObjectMeta, V1PodTemplateSpec, V1PodSpec, V1JobSpec, \
V1Volume, V1PersistentVolumeClaimVolumeSource, V1VolumeMount, V1ResourceRequirements,V1beta1CronJob,V1beta1CronJobSpec,V1beta1JobTemplateSpec,BatchV1beta1Api
def create_cronjob_object(newSubscribeInfo,newComputingPowerSubscribeID):
container = V1Container(
name=JOB_NAME,
image=IMAGE_NAME,
command=['/bin/bash'],
args=["-c", "sleep " + str(newSubscribeInfo.delayNumber)+ ";cd execScript; python createNode.py --nodeName={} --computingpowerType={} --userNumber={} --cpuLimit={} --memoryLimit={} --duration={} --computingpowerSubscribeId={} --status={};".format(
str(newSubscribeInfo.name),str(newSubscribeInfo.computingPowerType),str(newSubscribeInfo.userNumber),str(newSubscribeInfo.cpuLimit),str(newSubscribeInfo.memoryLimit),
str(newSubscribeInfo.duration),str(newComputingPowerSubscribeID),str(newSubscribeInfo.status))],
volume_mounts=[V1VolumeMount(name='job-pv-persistent-storage', mount_path='/usr/src/app/execScript', sub_path="execScript")],
resources=V1ResourceRequirements()
)
# 镜像使用
imagePullSecrets = V1LocalObjectReference(
name = "default-secret"
)
template = V1PodTemplateSpec(
metadata=V1ObjectMeta(name=newSubscribeInfo.name),
spec=V1PodSpec(restart_policy="Never", containers=[container], image_pull_secrets=[imagePullSecrets],
volumes=[V1Volume(
name='job-pv-persistent-storage',
persistent_volume_claim=V1PersistentVolumeClaimVolumeSource(claim_name='job-pv-claim')
)]))
V1Job_template = V1beta1JobTemplateSpec(
metadata=V1ObjectMeta(name=newSubscribeInfo.name),
spec=V1JobSpec(template=template,backoff_limit=4)
)
spec = V1beta1CronJobSpec(
concurrency_policy="Allow",
failed_jobs_history_limit=3,
job_template=V1Job_template,
schedule=newSubscribeInfo.cronExpression,
starting_deadline_seconds=3,
successful_jobs_history_limit=3,
suspend=False
)
cronjob = V1beta1CronJob(
api_version="batch/v1beta1",
kind="CronJob",
metadata=V1ObjectMeta(name=newSubscribeInfo.name, namespace=projectConfig.STAR_EVA_JOB_NAMESPACE),
spec=spec)
return cronjob
def create_cronjob(body=None,namespace='default',name=None):
batch_v2 = BatchV1beta1Api()
if judge_crontab_exists(namespace, name):
print(f'{name} exists, please do not repeat!')
return f'{name} exists, please do not repeat!'
else:
api_response = batch_v2.create_namespaced_cron_job(
body=body,
namespace=namespace,pretty=True,_preload_content=False, async_req=False)
print("Job created. status='%s'" % str(api_response.status))
return "Job created. status='%s'" % str(api_response.status)
def judge_crontab_exists(namespace, name):
cron_job_list = get_cronjob_list(namespace)
for cron_job in cron_job_list:
if name == cron_job['metadata']['name']:
return True
return False
def get_cronjob_list(namespace):
v1 = client.BatchV1beta1Api()
ret = v1.list_namespaced_cron_job(namespace=namespace, pretty=True, _preload_content=False)
cron_job_list = json.loads(ret.data)
print(f'cronjob number={len(cron_job_list["items"])}')
return cron_job_list["items"]
cronjob=create_cronjob_object(newSubscribeInfo,newComputingPowerSubscribeID) create_cronjob(cronjob,projectConfig.STAR_EVA_JOB_NAMESPACE,newSubscribeInfo.name)
注意:其中使用了镜像、卷、复杂命令,如不需要可删除,如要使用卷,需提前在创建k8s创建好卷,同时创建好共享的文件夹。
同时官网也有创建job与cronjob的实例:
地址如下:
GitHub - kubernetes-client/python: Official Python client library for kubernetes
小编编写不易,请广大读者点赞、收藏。