Kubernetes 支持 OpenAPI
Open API 让 API 提供者可以定义自己的操作和模型,并让开发者可以自动化的生成喜欢语言 的客户端,用以和 API 服务器通信。Kubernetes 已经支持 Swagger 1.2(OpenAPI 规范的前身)有一段时间了,但是这一标准不够完整和有效,凭借这一支持,非常难生成工具或客户端。
在 Kubernetes 1.4,我们对目前的模型和操作进行了升级,引入了 Open API 规范(在没被捐献给 Open API 之前被称作 Swagger 2.0)支持的 Alpha 版本。从 Kubernetes 1.5 开始,OpenAPI 规范的支持已经完备,能够直接从 Kubernetes 源码生成规范,对于模型和方法的任何变更,都会保障文档和规范的完全同步。
新规范让我们有了更好的 API 文档,甚至还有了一个 Python 客户端。
这一模块化的规范用 GroupVersion 进行分隔,这一做法属于未雨绸缪,我们想要让不同的 API Server 使用不同的 GroupVersion。
规范的结构在 Open API spec definition 中有解释。我们用 operation 标记 来拆分每个 GroupVersion 并尽可能的丰富其中的模型、路径、操作等信息。操作的参数、调用方法以及响应都有文档描述。
例如,获取 Pod 信息的 OpenAPI 规范
{ ... "paths": { "/api/v1/namespaces/{namespace}/pods/{name}": { "get": { "description": "read the specified Pod", "consumes": [ "*/*" ], "produces": [ "application/json", "application/yaml", "application/vnd.kubernetes.protobuf" ], "schemes": [ "https" ], "tags": [ "core_v1" ], "operationId": "readCoreV1NamespacedPod", "parameters": [ { "uniqueItems": true, "type": "boolean", "description": "Should the export be exact. Exact export maintains cluster-specific fields like 'Namespace'.", "name": "exact", "in": "query" }, { "uniqueItems": true, "type": "boolean", "description": "Should this value be exported. Export strips fields that a user can not specify.", "name": "export", "in": "query" } ], "responses": { "200": { "description": "OK", "schema": { "$ref": "#/definitions/v1.Pod" } }, "401": { "description": "Unauthorized" } } }, … } …
有了这些信息,以及 kube-apiserver 的 URL,就可以据此来调用接口了(/api/v1/namespaces/{namespace}/pods/{name}),参数包括 name、exact 以及 export 等,调用结果会返回 Pod 信息。客户库生成器也会使用这些信息来创建一个 API 函数调用来读取 Pod 信息。例如 Python 客户端 能够很简单的进行如下调用:
from kubernetes import client ret = client.CoreV1Api().read_namespaced_pod(name="pods_name", namespace="default") https://gist.github.com/mbohlool/d5ec1dace27ef90cf742555c05480146
一个简化版的 read_namespaced_pod。
Python Client:https://github.com/kubernetes-incubator/client-python
还可以使用 Swagger-codegen 文档生成器来根据这些信息生成文档:
GET /api/v1/namespaces/{namespace}/pods/{name} (readCoreV1NamespacedPod) read the specified Pod Path parameters name (required) Path Parameter — name of the Pod namespace (required) Path Parameter — object name and auth scope, such as for teams and projects Consumes This API call consumes the following media types via the Content-Type request header: */* Query parameters pretty (optional) Query Parameter — If 'true', then the output is pretty printed. exact (optional) Query Parameter — Should the export be exact. Exact export maintains cluster-specific fields like 'Namespace'. export (optional) Query Parameter — Should this value be exported. Export strips fields that a user can not specify. Return type v1.Pod Produces This API call produces the following media types according to the Accept request header; the media type will be conveyed by the Content-Type response header. application/json application/yaml application/vnd.kubernetes.protobuf Responses 200 OK v1.Pod 401 Unauthorized
有两种方式访问 OpenAPI :
- 从 kube-apiserver/swagger.json。这个文件会包含所有启用的 GroupVersion 方法和模型,其中的内容会是该 API Server 所对应的最新版本。
- Kubernetes 的 Github 仓库,可以访问 master 或者其他指定的 Release。
有很多工具 能和这些 API 协同工作,例如可以用 swagger editor 来打开规范文件并渲染文档,或者生成客户端;还可以直接利用 swagger codegen 来生成文档和客户端。自动生成的客户端多数时候是开箱即用的。不过可能需要做一些登录和 Kubernetes 相关的设置。可以使用 Python 客户端 作为模板来开发自己的客户端
本文转自中文社区-Kubernetes 支持 OpenAPI
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
K8S-网络模型、POD/RC/SVC YAML 语法官方文档
K8S-网络模型、POD/RC/SVC YAML 语法官方文档 Kubernetes - Production-Grade Container Orchestration kubernetes/kubernetes: Production-Grade Container Scheduling and Management Posts containing 'yaml' - Stack Overflow how to pass a configuration file thought yaml on kubernetes to create new replication controller - Stack Overflow How to expose a Kubernetes service externally using NodePort - Stack Overflow Yaml templates in Kubernetes - Stack Overflow Kubernetes - Production-Grade Container Orchestration kuber...
- 下一篇
Kubernetes从网络策略到安全策略
编者注:今天的文章由 Bernard Van De Walle( Aporeto 公司 Kubernetes 项目带头人)撰写,文章描述 如何使用新的方法来实现Kubernetes 网络策略。 Kubernetes 网络策略 Kubernetes 支持网络策略的新 API,为隔离应用和减少攻击层面提供复杂的模型。这个功能由 SIG-Network group 演化而来,可以通过内置标签和 Kubernetes 创建的选择器,令定义网络策略变得轻松、优雅。 Kubernetes 将这个功能留给到第三方来实施这些网络策略,同时不需要提供默认实现。 我们希望引入一种新的方法来思考“安全”、“网络策略”。要说明的是,安全和可达性是两个不同的问题:将 Pod 识别和策略定义转换成网络约束,比如 IP 地址、子网如此之类的。 然而,我们也从过去的经验中知道,使用外部控制面板也可以引入新的挑战:ACL 的发行版本要求 Kubernetes workers 之间较高的同步性; 每当创建一个新的 Pod,其余 Pod 上的 ACL 都需要进行更新,因为旧的 Pod 上面更新的 Pod 上的一些策略是相关...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- SpringBoot2更换Tomcat为Jetty,小型站点的福音
- CentOS8安装Docker,最新的服务器搭配容器使用
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- 设置Eclipse缩进为4个空格,增强代码规范
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- CentOS8编译安装MySQL8.0.19
- SpringBoot2整合Redis,开启缓存,提高访问速度
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- Hadoop3单机部署,实现最简伪集群