Kubernetes客户蟒蛇创建服务错误
问题描述:
我试图在GCE hostes命名node-js-deployment
Kubernetes集群我部署一个创建新的服务Kubernetes客户蟒蛇创建服务错误
我跟着文档,create_namespaced_service
这是业务数据:
{
"kind": "Service",
"apiVersion": "v1",
"metadata": {
"name": "node-js-service"
},
"spec": {
"selector": {
"app": "node-js"
},
"ports": [
{
"protocol": "TCP",
"port": 80,
"targetPort": 8000
}
]
}
}
这是Python函数来创建服务
api_instance = kubernetes.client.CoreV1Api()
namespace = 'default'
body = kubernetes.client.V1Service() # V1Serice
# Creating Meta Data
metadata = kubernetes.client.V1ObjectMeta()
metadata.name = "node-js-service"
# Creating spec
spec = kubernetes.client.V1ServiceSpec()
# Creating Port object
ports = kubernetes.client.V1ServicePort()
ports.protocol = 'TCP'
ports.target_port = 8000
ports.port = 80
spec.ports = ports
spec.selector = {"app": "node-js"}
body.spec = spec
try:
api_response = api_instance.create_namespaced_service(namespace, body, pretty=pretty)
pprint(api_response)
except ApiException as e:
print("Exception when calling CoreV1Api->create_namespaced_service: %s\n" % e)
错误:
Reason: Bad Request
HTTP response headers: HTTPHeaderDict({'Content-Type': 'application/json', 'Date': 'Tue, 21 Feb 2017 03:54:55 GMT', 'Content-Length': '227'})
HTTP response body: {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"Service in version \"v1\" cannot be handled as a Service: only encoded map or array can be decoded into a struct","reason":"BadRequest","code":400}
但是,如果我传递JSON正在创建的服务。不知道我做错了什么。
任何帮助非常感谢,谢谢。
答
通过阅读您的代码,您似乎错过了将元数据分配到body.metadata
。你错过了V1ServiceSpec
的ports
领域应该是一个列表,但使用单个V1ServicePort
所以没有测试我想这应该工作:
api_instance = kubernetes.client.CoreV1Api()
namespace = 'default'
body = kubernetes.client.V1Service() # V1Serice
# Creating Meta Data
metadata = kubernetes.client.V1ObjectMeta()
metadata.name = "node-js-service"
body.metadata = metadata
# Creating spec
spec = kubernetes.client.V1ServiceSpec()
# Creating Port object
port = kubernetes.client.V1ServicePort()
port.protocol = 'TCP'
port.target_port = 8000
port.port = 80
spec.ports = [ port ]
spec.selector = {"app": "node-js"}
body.spec = spec
的定义也可以直接从JSON/YAML装如官方回购中的两个示例所示 - 请参见exec.pycreate_deployment.py。然后
您的解决方案可能看起来像:
api_instance = kubernetes.client.CoreV1Api()
namespace = 'default'
manifest = {
"kind": "Service",
"apiVersion": "v1",
"metadata": {
"name": "node-js-service"
},
"spec": {
"selector": {
"app": "node-js"
},
"ports": [
{
"protocol": "TCP",
"port": 80,
"targetPort": 8000
}
]
}
}
try:
api_response = api_instance.create_namespaced_service(namespace, manifest, pretty=true)
pprint(api_response)
except ApiException as e:
print("Exception when calling CoreV1Api->create_namespaced_endpoints: %s\n" % e)
+0
传递yaml作为字典完美的作品,我已经将元数据添加到我的代码中的身体未能在问题中更新它。但就像你说的那样,问题可能在于我如何通过端口而不是列表。 – kt14
从未使用过的客户蟒蛇,因此无法帮助100%。但我猜你至少忘了将元数据对象分配给body.metadata。另外,端口应该是1个元素的数组。 –
我错过了在问题中包含元数据。在我的代码中,我确实做到了。可能是因为我试图附加端口。但最终最终使用字典进行部署和服务。这很简单,容易。 – kt14