服务发现提议 Service Discovery Proposal

文档目标 Goal of this document

要消费一个服务,开发者需要知道 API 的描述以及完整的 URL 。Kubernetes 包含一个服务的主机和端口信息,但如果该服务没有绑定在 root ,它缺少需要的 scheme 和 path 信息。在这篇文档中,我们提出一些标准 kubernetes 服务注解以填补这些空缺。重要的是,这些注释是允许跨 Kubernetes 实现的标准服务发现的标准。请注意,该示例主要涉及使用 WebServices,但同样的概念也适用于其他类型的服务。

To consume a service, a developer needs to know the full URL and a description of the API. Kubernetes contains the host and port information of a service, but it lacks the scheme and the path information needed if the service is not bound at the root. In this document we propose some standard kubernetes service annotations to fix these gaps. It is important that these annotations are a standard to allow for standard service discovery across Kubernetes implementations. Note that the example largely speaks to consuming WebServices but that the same concepts apply to other types of services.

终端URL,服务类型 Endpoint URL, Service Type

URL 能精确地描述一个服务的地址。典型的 URL 的形式如下

A URL can accurately describe the location of a Service. A generic URL is of the following form

  1. scheme:[//[user:password@]host[:port]][/]path[?query][#fragment]

但是,出于服务发现的目的,我们可以将其简化为以下形式

however for the purpose of service discovery we can simplify this to the following form

  1. scheme:[//host[:port]][/]path

如果需要用户 和(或)密码,那这些信息可以通过 Kubernetes Secret 进行传递。Kubernetes 包含每个服务的主机和端口,但是没有 scheme 和 path。

If a user and/or password is required then this information can be passed using Kubernetes Secrets. Kubernetes contains the host and port of each service but it lacks the scheme and path.

Service Path—— 每个服务有一个或多个端点。通常,端点应位于位置URL的根“/”处,例如 http://172.100.1.52/。在某些情况下,这是不可能的,实际的服务端点可能位于http://172.100.1.52/cxfcdi.服务的 Kubernetes 元数据没有捕获 path 部分,因此很难使用该服务。

Service Path - Every Service has one (or more) endpoint. As a rule the endpoint should be located at the root “/“ of the location URL, i.e. http://172.100.1.52/. There are cases where this is not possible and the actual service endpoint could be located at http://172.100.1.52/cxfcdi. The Kubernetes metadata for a service does not capture the path part, making it hard to consume this service.

Service Path—— 服务可以用不同的 scheme 部署,一些流行的 scheme 包括 http、https、file、ftp 以及 jdbc。

Service Scheme - Services can be deployed using different schemes. Some popular schemes include http,https,file,ftp and jdbc.

Service Protocol —— 服务使用不同协议,客户端需要用这些协议来与服务进行通信,一些服务级协议的例子,SOAP,REST(是的,技术上来说,REST 不是协议而是一种架构风格)。对于服务消费者来说,很难确定预期的协议是什么。

Service Protocol - Services use different protocols that clients need to speak in order to communicate with the service, some examples of service level protocols are SOAP, REST (Yes, technically REST isn’t a protocol but an architectural style). For service consumers it can be hard to tell what protocol is expected.

服务描述 Service Description

服务的 API 是与服务消费者交互的点。API 的描述是服务消费者创建时的重要信息。在服务本身的已知位置上发布服务定义文档已变得很常见。这个“众所周知”的地方不是很标准,因此对服务开发人员提供服务描述 path 和使用的定义语言(DL)类型给出建议。

The API of a service is the point of interaction with a service consumer. The description of the API is an essential piece of information at creation time of the service consumer. It has become common to publish a service definition document on a know location on the service itself. This ‘well known’ place it not very standard, so it is proposed the service developer provides the service description path and the type of Definition Language (DL) used.

Service Description Path —— 为了方便客户端使用服务,此文档对服务使用者非常有帮助。在某些情况下,客户端代码可以从这样的文档中生成。这里假设服务描述文档发布在服务端点本身所在的某个位置。

Service Description Path - To facilitate the consumption of the service by client, the location this document would be greatly helpful to the service consumer. In some cases the client side code can be generated from such a document. It is assumed that the service description document is published somewhere on the service endpoint itself.

Service Description Language —— 已经开发了许多定义语言(DL)来描述服务。其中一些例子是WSDL、WADL和Swager。为了使用描述文档,最好知道所使用的 DL 类型。

Service Description Language - A number of Definition Languages (DL) have been developed to describe the service. Some of examples are WSDL, WADL and Swagger. In order to consume a description document it is good to know the type of DL used.

标准服务注解 Standard Service Annotations

Kubernetes 允许创建 Service Annotations。这样我们建议用以下标准注解

  • api.service.kubernetes.io/path —— 服务端点 url 的 path 部分。一个示例值:cxfcdi
  • api.service.kubernetes.io/scheme —— 服务端点 url 的 scheme 部分。示例值:http或 https。
  • api.service.kubernetes.io/protocol —— 服务的协议。 已知值: SOAP、 XML-RPC 和REST
  • api.service.kubernetes.io/description-path —— 服务描述文档的端点的 path 部分。这是一个相当安全的假设,即服务自记录。swagger 2.0 的示例值:cxfcdi/swagger.json
  • api.kubernetes.io/description-language —— 使用描述语言的类型。已知值: WSDL、WADL、 SwaggerJSON、SwaggerYAML。

    Kubernetes allows the creation of Service Annotations. Here we propose the use of the following standard annotations

    • api.service.kubernetes.io/path - the path part of the service endpoint url. An example value could be cxfcdi,
    • api.service.kubernetes.io/scheme - the scheme part of the service endpoint url. Some values could be http or https.
    • api.service.kubernetes.io/protocol - the protocol of the service. Known values are SOAP, XML-RPC and REST,
    • api.service.kubernetes.io/description-path - the path part of the service description document’s endpoint. It is a pretty safe assumption that the service self-documents. An example value for a swagger 2.0 document can be cxfcdi/swagger.json,
    • api.kubernetes.io/description-language - the type of Description Language used. Known values are WSDL, WADL, SwaggerJSON, SwaggerYAML.

下面的片段取自使用了这些注解的 kubernetes.json 服务部分。

The fragment below is taken from the service section of the kubernetes.json were these annotations are used

  1. ...
  2. "objects" : [ {
  3. "apiVersion" : "v1",
  4. "kind" : "Service",
  5. "metadata" : {
  6. "annotations" : {
  7. "api.service.kubernetes.io/protocol" : "REST",
  8. "api.service.kubernetes.io/scheme" "http",
  9. "api.service.kubernetes.io/path" : "cxfcdi",
  10. "api.service.kubernetes.io/description-path" : "cxfcdi/swagger.json",
  11. "api.service.kubernetes.io/description-language" : "SwaggerJSON"
  12. },
  13. ...

总结 Conclusion

建议用五个服务注解作为描述一个服务端点的标准方式。这五个注释被提升为 Kubernetes 标准,这样可以发现服务并构建服务目录,以方便服务使用者。

Five service annotations are proposed as a standard way to describe a service endpoint. These five annotation are promoted as a Kubernetes standard, so that services can be discovered and a service catalog can be build to facilitate service consumers.