You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Python Kubernetes API实现Kubernetes Job替换操作

问题:用Python Kubernetes API实现Kubectl Job替换操作

我有如下kubectl命令:

kubectl get job <JOB-NAME> -o json | jq 'del(.spec.selector)' | jq 'del(.spec.template.metadata.labels)' | kubectl replace --force -f -

这条命令可以将现有Kubernetes Job替换为自身(删除指定字段后重新应用)。请问能否通过Python Kubernetes API实现同样的操作?

我当前尝试的代码如下:

import kubernetes
from kubernetes import client, config

# 加载kube配置
config.load_kube_config()

configuration = kubernetes.client.Configuration()
name = "job-1"
namespace = "default"

v1 = client.CoreV1Api()

# 根据Job名称获取对应的Pod名称
metabase_create_readonly_user_name = ''
pods = v1.list_namespaced_pod(namespace, label_selector='job-name={}'.format(name), watch=False)
for pod in pods.items:
    job_name = pod.metadata.name

# 这里无法正常工作!
v1.replace_namespaced_pod(job_name, namespace)

但replace_namespaced_pod需要传入body参数,我不知道该传入什么内容。有没有办法获取现有Job的完整配置作为body传入,或者有其他正确的实现方式?

内容的提问来源于stack exchange,提问作者jipot

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 11:50:23