我在本地运行minikube的python-kubernetes中使用python,例如没有云问题。
我正在尝试创建一个作业,并为其提供要运行的数据。我想为它提供一个包含本地计算机数据的目录。
我正在使用this示例,并尝试添加装载卷 这是添加关键字volume_mounts后的代码(我尝试了多个位置,使用了多个关键字,但无济于事)
from os import path
import yaml
from kubernetes import client, config
JOB_NAME = "pi"
def create_job_object():
# Configureate Pod template container
container = client.V1Container(
name="pi",
image="perl",
volume_mounts=["/home/user/data"],
command=["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"])
# Create and configurate a spec section
template = client.V1PodTemplateSpec(
metadata=client.V1ObjectMeta(labels={
"app": "pi"}),
spec=client.V1PodSpec(restart_policy="Never",
containers=[container]))
# Create the specification of deployment
spec = client.V1JobSpec(
template=template,
backoff_limit=0)
# Instantiate the job object
job = client.V1Job(
api_version="batch/v1",
kind="Job",
metadata=client.V1ObjectMeta(name=JOB_NAME),
spec=spec)
return job
def create_job(api_instance, job):
# Create job
api_response = api_instance.create_namespaced_job(
body=job,
namespace="default")
print("Job created. status='%s'" % str(api_response.status))
def update_job(api_instance, job):
# Update container image
job.spec.template.spec.containers[0].image = "perl"
# Update the job
api_response = api_instance.patch_namespaced_job(
name=JOB_NAME,
namespace="default",
body=job)
print("Job updated. status='%s'" % str(api_response.status))
def delete_job(api_instance):
# Delete job
api_response = api_instance.delete_namespaced_job(
name=JOB_NAME,
namespace="default",
body=client.V1DeleteOptions(
propagation_policy='Foreground',
grace_period_seconds=5))
print("Job deleted. status='%s'" % str(api_response.status))
def main():
# Configs can be set in Configuration class directly or using helper
# utility. If no argument provided, the config will be loaded from
# default location.
config.load_kube_config()
batch_v1 = client.BatchV1Api()
# Create a job object with client-python API. The job we
# created is same as the `pi-job.yaml` in the /examples folder.
job = create_job_object()
create_job(batch_v1, job)
update_job(batch_v1, job)
delete_job(batch_v1)
if __name__ == '__main__':
main()
我收到此错误
HTTP响应正文: {“种类”:“状态”,“ apiVersion”:“ v1”,“元数据”:{},“状态”:“故障”,“消息”:“作业 版本“ v1”中的版本不能作为Job:v1.Job.Spec: v1.JobSpec.Template:v1.PodTemplateSpec.Spec:v1.PodSpec。容器: [] v1.Container:v1.Container.VolumeMounts:[] v1.VolumeMount: readObjectStart:期望{或n,但找到\“,在#10字节中发现错误 of || ounts \“:[\” / home / user | ...,更大的上下文... | \“图片\”: \“ perl \”,\“名称\”:\“ pi \”,\“ volumeMounts \”:[\“ / home / user / data \”]}]], \“ restartPolicy \”:\“从不”,“}}}} | ...”,“原因”:“ BadRequest”,“代码”:400
我在这里想念什么?
还有另一种向工作公开数据的方法吗?
编辑:尝试使用client.V1Volumemount 我试图添加此代码,并在不同的init函数中添加安装对象,例如。
mount = client.V1VolumeMount(mount_path="/data", name="shai")
client.V1Container
client.V1PodTemplateSpec
client.V1JobSpec
client.V1Job
在多个关键字下都会导致错误,这是使用正确的对象吗?我如何使用它的外壳?
编辑:尝试将volume_mounts作为列表传递,答案中建议使用以下代码:
def create_job_object():
# Configureate Pod template container
container = client.V1Container(
name="pi",
image="perl",
volume_mounts=["/home/user/data"],
command=["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"])
# Create and configurate a spec section
template = client.V1PodTemplateSpec(
metadata=client.V1ObjectMeta(labels={
"app": "pi"}),
spec=client.V1PodSpec(restart_policy="Never",
containers=[container]))
# Create the specification of deployment
spec = client.V1JobSpec(
template=template,
backoff_limit=0)
# Instantiate the job object
job = client.V1Job(
api_version="batch/v1",
kind="Job",
metadata=client.V1ObjectMeta(name=JOB_NAME),
spec=spec)
return job
仍然出现类似错误
kubernetes.client.rest.ApiException:(422)原因:无法处理 实体HTTP响应标头:HTTPHeaderDict({'Content-Type': 'application / json','Date':'Tue,06 Aug 2019 06:19:13 GMT', 'Content-Length':'401'})HTTP响应正文: {“种类”:“状态”,“ apiVersion”:“ v1”,“元数据”:{},“状态”:“故障”,“消息”:“ Job.batch \“ pi \”无效: spec.template.spec.containers [0] .volumeMounts [0] .name:找不到: \“ d \”“,”原因“:”无效“,”详细信息“:{”名称“:” pi“,”组“:”批处理“,”种类“:”工作“,”原因“:[{ “原因”:“ FieldValueNotFound”,“消息”:“不 发现: \“ d \”“,” field“:” spec.template.spec.containers [0] .volumeMounts [0] .name“}]},” code“:422}
答案 0 :(得分:2)
V1Container调用需要一个用于volume_mounts参数的V1VolumeMount对象列表,但是您传入了一个字符串列表:
代码:
def create_job_object():
volume_mount = client.V1VolumeMount(
mount_path="/home/user/data"
# other optional arguments, see the volume mount doc link below
)
# Configureate Pod template container
container = client.V1Container(
name="pi",
image="perl",
volume_mounts=[volume_mount],
command=["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"])
# Create and configurate a spec section
template = client.V1PodTemplateSpec(
metadata=client.V1ObjectMeta(labels={
"app": "pi"}),
spec=client.V1PodSpec(restart_policy="Never",
containers=[container]))
....
参考: