gpt4 book ai didi

尝试挂载卷时出现 python kubernetes API 错误

转载 作者:行者123 更新时间:2023-12-01 00:40:49 27 4
gpt4 key购买 nike

我正在将 python 与 python-kubernetes 一起使用,并在本地运行 minikube,例如,不存在云问题。

我正在尝试创建一个作业并为其提供要运行的数据。我想为其提供带有本地计算机数据的目录挂载。

我正在使用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 response body: {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"Job in version \"v1\" cannot be handled as a Job: v1.Job.Spec: v1.JobSpec.Template: v1.PodTemplateSpec.Spec: v1.PodSpec.Containers: []v1.Container: v1.Container.VolumeMounts: []v1.VolumeMount: readObjectStart: expect { or n, but found \", error found in #10 byte of ...|ounts\": [\"/home/user|..., bigger context ...| \"image\": \"perl\", \"name\": \"pi\", \"volumeMounts\": [\"/home/user/data\"]}], \"restartPolicy\": \"Never\"}}}}|...","reason":"BadRequest","code":400

我在这里缺少什么?

还有其他方法可以向作业公开数据吗?

编辑:尝试使用 client.V1Volumemount我正在尝试添加此代码,并在不同的初始化函数中添加挂载对象,例如。

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) Reason: Unprocessable Entity HTTP response headers: HTTPHeaderDict({'Content-Type': 'application/json', 'Date': 'Tue, 06 Aug 2019 06:19:13 GMT', 'Content-Length': '401'}) HTTP response body: {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"Job.batch \"pi\" is invalid: spec.template.spec.containers[0].volumeMounts[0].name: Not found: \"d\"","reason":"Invalid","details":{"name":"pi","group":"batch","kind":"Job","causes":[{"reason":"FieldValueNotFound","message":"Not found: \"d\"","field":"spec.template.spec.containers[0].volumeMounts[0].name"}]},"code":422}

最佳答案

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]))
....

引用文献:

关于尝试挂载卷时出现 python kubernetes API 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57365094/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com