数值错误:选择器的数值无效,不能为None。

3
我是一名有帮助的助手,我可以为您翻译文本。以下是需要翻译的内容:

我对 Kubernetes 还比较陌生,我需要使用 Kubernetes python-client 创建一个 Pod。 所以为了进行实验,我正在尝试运行项目提供的示例笔记本,而不做任何更改,看看事情是如何工作的。

从第三步开始使用intro_notebook.ipynb,我遇到了一个错误:

ValueError: Invalid value for `selector`, must not be `None`

这里是代码:

from kubernetes import client, config

我改变的唯一部分是第二个单元格,因为我正在使用Ubuntu的microk8s运行Kubernetes:

config.load_kube_config('/var/snap/microk8s/current/credentials/client.config')

api_instance = client.AppsV1Api()
dep = client.V1Deployment()
spec = client.V1DeploymentSpec() # <<< At this line I hit the error!

完整的回溯信息:
ValueError                                Traceback (most recent call last)
<ipython-input-9-f155901e8381> in <module>
      1 api_instance = client.AppsV1Api()
      2 dep = client.V1Deployment()
----> 3 spec = client.V1DeploymentSpec()

~/.local/share/virtualenvs/jupyter-2hJZ4kgI/lib/python3.8/site-packages/kubernetes/client/models/v1_deployment_spec.py in __init__(self, min_ready_seconds, paused, progress_deadline_seconds, replicas, revision_history_limit, selector, strategy, template)
     76         if revision_history_limit is not None:
     77             self.revision_history_limit = revision_history_limit
---> 78         self.selector = selector
     79         if strategy is not None:
     80             self.strategy = strategy

~/.local/share/virtualenvs/jupyter-2hJZ4kgI/lib/python3.8/site-packages/kubernetes/client/models/v1_deployment_spec.py in selector(self, selector)
    215         """
    216         if selector is None:
--> 217             raise ValueError("Invalid value for `selector`, must not be `None`")  # noqa: E501
    218 
    219         self._selector = selector

ValueError: Invalid value for `selector`, must not be `None`

我正在运行 python3.8

$ pip freeze | grep -e kuber
kubernetes==11.0.0

$ snap list microk8s 
Name      Version  Rev   Tracking       Publisher   Notes
microk8s  v1.18.6  1551  latest/stable  canonical✓  classic

更新

将microk8s降级到v1.15.11并没有解决问题。


你确定那是错误吗?通常会有整个回溯,包括失败的行。 - tdelaney
@FooBar,你能分享一下你的 Kubernetes 集群运行的版本吗? - hilsenrat
@FooBar 我明白了...我查看了客户端规范,似乎selectortemplate属性不是可选的。 - hilsenrat
很抱歉让您走了弯路,我很高兴@mdaniel最终能够帮助您。 - hilsenrat
@hilsenrat NP,谢谢你的帮助 :) - FooBar
显示剩余4条评论
1个回答

1
这是因为他们将验证过程改变到构造函数中,而不是以后进行验证——这不是笔记本所期望的。您只需要将这些内部赋值项上移到创建Deployment之前即可。
name = "my-busybox"
spec = client.V1DeploymentSpec(
    selector=client.V1LabelSelector(match_labels={"app":"busybox"}),
    template=client.V1PodTemplateSpec(),
)
container = client.V1Container(
    image="busybox:1.26.1",
    args=["sleep", "3600"],
    name=name,
)
spec.template.metadata = client.V1ObjectMeta(
    name="busybox",
    labels={"app":"busybox"},
)
spec.template.spec = client.V1PodSpec(containers = [container])
dep = client.V1Deployment(
    metadata=client.V1ObjectMeta(name=name),
    spec=spec,
)

谢谢,我太迷茫了... 我是说,真的很迷茫! - FooBar
1
如果 Kubernetes Python 客户端是你第一次接触 Python,那将是一个艰难的起点。如果 Kubernetes Python 客户端是你第一次接触 Kubernetes,那肯定是错误的起点。 - mdaniel
这是第二个。 "Kubernetes Python客户端几乎是我与Kubernetes的第一次接触"。 我只有基本的Kubernetes知识,什么是集群,什么是节点或Pod。 如何运行Pod...这基本上是我从初学者课程中所了解的全部... - FooBar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接