1

我们正在尝试使用 knative 和 Kubernetes 的 python 客户端库来部署服务。我们正在使用以下 yaml 文件:

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: test-{{ test_id }}
  namespace: default
spec:
  template:
    spec:
      containers:
        - image: test-deployment:latest
          resources:
            limits:
              cpu: 50m
              memory: 128Mi
            requests:
              cpu: 50m
              memory: 128Mi
      containerConcurrency: 1

如果我们使用 kubernetes 的命令行工具进行部署,它可以正常工作。

kubectl create -f test.yaml

使用 python 客户端库,我们正在做:

import kubernetes
import yaml
import uuid

from jinja2 import Template
from urllib3 import exceptions as urllib_exceptions

api = kubernetes.client.CoreV1Api(api_client=kubernetes.config.load_kube_config(context=cluster))

with open(deployment_yaml_path, 'r') as file_reader:
    file_content = file_reader.read()

deployment_template = Template(file_content)

deployment_template = yaml.safe_load(template.render({
    'test_id': str(uuid.uuid4())
}))

deployment = kubernetes.client.V1Service(
    api_version=deployment_template['apiVersion'],
    kind="Service",
    metadata=deployment_template['metadata'],
    spec=deployment_template['spec']
)

try:
    response = api.create_namespaced_service(body=deployment, namespace='default')
except (kubernetes.client.rest.ApiException, urllib_exceptions.HTTPError):
    raise TestError

但是,我们收到此错误:

Reason: Bad Request
HTTP response headers: HTTPHeaderDict({'Audit-Id': 'a1968276-e16b-44f4-a40d-5eb5eaee9d47', 'Content-Type': 'application/json', 'Date': 'Thu, 23 Apr 2020 08:29:36 GMT', 'Content-Length': '347'})
HTTP response body: {
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {

  },
  "status": "Failure",
  "message": "Service in version \"v1\" cannot be handled as a Service: no kind \"Service\" is registered for version \"serving.knative.dev/v1\" in scheme \"k8s.io/kubernetes/pkg/api/legacyscheme/scheme.go:30\"",
  "reason": "BadRequest",
  "code": 400
}

有没有办法用 knative 部署服务?据我了解,knative 服务与普通的 Kubernetes 服务不同。我不知道问题是我试图以错误的方式部署服务,还是 Kubernetes python 客户端库还不支持这种部署。

编辑:

Python Client Library: kubernetes==11.0.0

Kubernetes: 
Client Version: version.Info{Major:"1", Minor:"15", GitVersion:"v1.15.4", GitCommit:"67d2fcf276fcd9cf743ad4be9a9ef5828adc082f", GitTreeState:"clean", BuildDate:"2019-09-18T14:51:13Z", GoVersion:"go1.12.9", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"15+", GitVersion:"v1.15.11-gke.5", GitCommit:"a5bf731ea129336a3cf32c3375317b3a626919d7", GitTreeState:"clean", BuildDate:"2020-03-31T02:49:49Z", GoVersion:"go1.12.17b4", Compiler:"gc", Platform:"linux/amd64"}
4

2 回答 2

3

kubernetes.client.V1Service是对 Kubernetes“服务”概念的引用,它是跨 pod 的选择器,表现为网络端点,而不是 Knative“服务”概念,后者是通过网络提供功能的整个应用程序。

根据repo中的这个示例kubernetes-client/python,您需要执行以下操作来获取和使用 Knative 服务的客户端:

api = kubernetes.client.CustomObjectsApi()
try:
    resource = api.create_namespaced_custom_object(
        group="serving.knative.dev",
        version="v1",
        plural="services",
        namespace="default",
        body=deployment_template)
except (kubernetes.client.rest.ApiException, urllib_exceptions.HTTPError):
    raise TestError

如果您要经常这样做,您可能想要制作一个带有类似于 的参数的帮助create_namespaced_service器,并且可能还需要一个类似于 的包装器对象,kubernetes.client.V1Service以简化创建 Knative 服务。

于 2020-04-30T04:41:26.710 回答
2

尝试使用create_namespaced_custom_object

参考:https ://github.com/kubernetes-client/python/blob/master/kubernetes/docs/CustomObjectsApi.md#create_namespaced_custom_object

这里的 service 是特定于 Knative 的自定义资源。

于 2020-04-23T12:06:29.680 回答