1. 需求分析

1.1 需求背景

  • 目前 K8S 资源操作提供的是命令行的方式。学习成本高,需要学习和记住大量的命令行;
  • 命令行操作的体验性不佳,特别是对于不熟悉的人来说,容易用错或者误用命令行,导致出现事故;
  • 命令行的形式下无法直观的呈现K8S的所有资源,对管理资源带来一定的不便性;

    1.2 需求

    AB21-TopN-需求分析
    JIRA : ADRE-1048 K8S 资源管理平台

    2. 服务定义

    2.1 名称

  • 中文名称:K8S资源管理平台

  • 英文名称:K8S Resource management service
  • 标识名称:K8S-Resource-management-service

    2.2 职责

  • 实现对k8s集群资源的管理

    2.3 设计目标

    2.3.1 功能性目标

  • 支持K8S用户权限的登入功能

  • 支持K8S节点资源使用呈现的功能
  • 支持K8S命名空间信息呈现的功能
  • 支持K8S 工作负载的增、删、改、查的功能,如Deployment、ReplicaSet、StatefulSet、DaemonSet
  • 支持K8S service 的增、删、改、查的功能

    2.3.2 非功能性目标

  • 数据校验:对用户的请求参数进行校验。

  • 日志记录:有完整的日志记录,便于系统出错后进行排查。
  • 请求超时处理:对用户的请求超时进行处理,避免响应时间过长。(M2实现)

    2.4 客户价值

    满足客户以web页面的形式操作Kubernetes平台。

    3. 核心技术预研

    AB21- TopN K8S 资源管理预研报告

    4. 整体设计方案

    4.1 框架

    image.png

  • api:提供资源管理接口供外部调用。

  • 资源操作模块:实现资源的管理,包括创建、修改、删除、查询等。
  • 权限验证:获取K8S集群的访问权限。
  • K8s的client-go: client-go是一个调用kubernetes集群资源对象API的客户端。
  • k8s的ETCD:etcd是k8s自带的一个高可用的分布式键值(key-value)数据库。

4.2 逻辑设计

4.2.1 登录

系统通过config文件和token验证的两种方式来登录集群:登录集群
用户使用方式:

  • 在前端上传config文件
  • 获取token后粘贴到文本框

即在登陆页面,用户可以任选一种方式进行登录!
逻辑设计 - 图2

4.2.1.1 用konfig文件登录

首先获取kube.conf文件,之后创建一个clientset对象,验证登录!

  1. func Configlogin(kubeconfig string) bool {
  2. config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
  3. if err != nil {
  4. fmt.Println("configlogin fail!\n")
  5. return false
  6. }
  7. clientset, err := kubernetes.NewForConfig(config)
  8. if err != nil {
  9. fmt.Println("configlogin fail!\n")
  10. return false
  11. }
  12. _, err2 := clientset.CoreV1().Pods("").List(context.TODO(), metav1.ListOptions{})
  13. if err2 != nil {
  14. fmt.Println("get fail!")
  15. return false
  16. } else {
  17. fmt.Println("get success!")
  18. return true
  19. }
  20. }

4.2.1.2 用token字符串登录

获取到token之后传入方法中,之后创建一个dynamicClient对象,实现登录!

  1. //token方式实现登录
  2. func Tokenlogin(token string) bool {
  3. config := getconfig(token)
  4. dynamicClient, _ := dynamic.NewForConfig(config)
  5. gvr := schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "deployments"}
  6. _, err := dynamicClient.Resource(gvr).Namespace("default").Get(context.TODO(), "mysql", metav1.GetOptions{})
  7. if err != nil {
  8. fmt.Printf("tokenlogin fail!\n")
  9. return false
  10. } else {
  11. fmt.Printf("tokenlogin success!\n")
  12. return true
  13. }
  14. }

4.2.2 K8S资源管理

结构化程序流程图.png

4.2.2.1 Workload(工作负载)管理

管理操作支持说明:

管理操作
说明
获取 获取所有命名空间下所有工作负载 命名空间:获取集群下所有命名空间
工作负载指:Deployment、ReplicaSet、StatefulSet、DaemonSet
获取某一命名空间下所有工作负载 命名空间作为参数
工作负载指:Deployment、ReplicaSet、StatefulSet、DaemonSet
获取某一命名空间下某一类型的工作负载 命名空间名称和资源类型作为参数
资源类型有:Deployment、ReplicaSet、StatefulSet、DaemonSet
创建 某一命名空间下资源的创建
- 命名空间名称作为参数
- 用户上传yaml文件
- 后端解析yaml文件后创建相应资源
更新 某一命名空间下某一资源的创建
- 展示给用户该资源的yaml文件
- 用户通过编辑yaml文件来修改资源
删除 某一命名空间下某一资源的删除 通过指定Namespace(命名空间)和工作负载的名称删除某个资源

4.2.2.2 Service(服务)管理

管理操作支持说明:

管理操作 说明
创建
- 用户上传yaml文件
- 后端解析yaml文件后创建相应资源
更新
- 首先需要获取到服务的版本号
- 用户编辑服务的yaml文件来修改服务
删除 通过指定Namespace(命名空间)和服务的名称删除服务

4.3 非功能逻辑设计

4.3.1 日志记录

日志汇总:日志分为普通日志和异常日志两种,两种日志合到一起统一成一个日志文件。
导包: “github.com/sirupsen/logrus”
功能:

  1. 日志文件的分割(每天自动生成一个log文件)
  2. 日志的级别划分(目前使用info和error两种)
  3. 记录普通日志
  4. 记录运行时异常
  5. 记录代码抛出的错误

方式:

  1. 以中间件的方式记录普通日志
  2. 以中间件的方式记录运行时抛出的异常
  3. 记录代码中定义的error

展示:

1、普通日志

字段 说明 举例
访问时间 客户端访问服务器的时刻 “2021-08-19 12:21:42”
日志级别 普通信息 info
http返回码 返回给客户端的http码 200
访问用时 处理访问的时长 9.2133ms
访问者IP 访问者的IP和端口 ::1(用内网查看,开发桌面没有IP)
访问方式 http的请求方式 GET/POST/DELETE/PUT
访问路径 精确到后端的某个功能入口 /api/v1/login/config

整体举例:
逻辑设计 - 图4

2、异常(和错误)日志

字段 说明 举例
异常时间 发生异常的时刻 “2021-08-19 12:21:42”
日志级别 异常信息 error
异常信息 打印出异常信息 “namespace ‘default1’ not found”

整体举例:
逻辑设计 - 图5

3、日志文件展示

逻辑设计 - 图6

4.3.2 数据校验

对以下字段进行判空校验

4.3.2.1 deployment

1. 创建deployment!
url : POST /apis/apps/v1/namespaces/{namespace}/deployments (适用于1.15以上版本的所有集群)
传参:

参数 是否必选 描述
namespace Yes object name and auth scope, such as for teams and projects

yaml文件请求参数校验:

参数 子结构 是否必选 参数类型 描述
apiVersion
Yes String APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values.
kind
Yes String Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase.
metadata
Yes metadata object Standard object metadata.
spec
Yes spec object Specification of the desired behavior of the Deployment.
template Yes template object Template describes the pods that will be created.
status
No status object Most recently observed status of the Deployment.

2. 删除deployment
URI:DELETE /apis/apps/v1/namespaces/{namespace}/deployments/{name} (适用于1.9及以上版本的所有集群)
描述该API的参数。

参数 是否必选 描述
name Yes name of the Deployment
namespace Yes object name and auth scope, such as for teams and projects

3. 查询deployment
URI:GET /apis/apps/v1/namespaces/{namespace}/deployments/{name} (适用于1.9及以上版本的所有集群)
描述该API的参数。

参数 是否必选 描述
name Yes name of the Deployment
namespace Yes object name and auth scope, such as for teams and projects

4. 修改deployment
url:PUT /apis/apps/v1/namespaces/{namespace}/deployments/{name} (适用于1.9及以上版本的所有集群)
传参:

参数 是否必选 描述
namespace Yes object name and auth scope, such as for teams and projects
name Yes name of the Deployment

yaml文件可修改字段(鸡肋):

参数 子结构 子结构 是否可修改
metadata selfLink
Yes
resourceVersion
labels
clusterName
generateName
annotations
spec replicas
restartPolicy
activeDeadlineSeconds
template containers

4.3.2.2 StatefulSet

1. 创建StatefulSet!
url : POST /apis/apps/v1/namespaces/{namespace}/statefulsets (适用于1.9及以上版本的所有集群)
POST /apis/apps/v1beta1/namespaces/{namespace}/statefulsets (仅适用于1.15及以下版本的集群)
传参:

参数 是否必选 描述
namespace Yes object name and auth scope, such as for teams and projects

yaml文件请求参数校验:

参数 子结构

是否必选 参数类型 描述
apiVersion


Yes String APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values.
kind


Yes String Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase.
metadata


Yes metadata object Standard object metadata.
spec


Yes spec object Specification of the desired behavior of the Deployment.
serviceName

Yes String ServiceName is the name of the service that governs this StatefulSet. This service must exist before the StatefulSet, and is responsible for the network identity of the set. Pods get DNS/hostnames that follow the pattern: pod-specific-string.serviceName.default.svc.cluster.local where “pod-specific-string” is managed by the StatefulSet controller.
selector

Yes selector object Selector is a label query over pods that should match the replica count. If empty, defaulted to labels on the pod template.
template

Yes template object Template describes the pods that will be created.

PodTemplateSpec





metadata Yes metadata object Standard object’s metadata.


spec Yes spec object Specification of the desired behavior of the pod.
status


No status object Most recently observed status of the Deployment.

2. 删除StatefulSet
URI:DELETE /apis/apps/v1/namespaces/{namespace}/statefulsets/{name} (适用于1.9及以上版本的所有集群)
DELETE /apis/apps/v1beta1/namespaces/{namespace}/statefulsets/{name} (仅适用于1.15及以下版本的集群)
描述该API的参数。

参数 是否必选 描述
name Yes name of the Deployment
namespace Yes object name and auth scope, such as for teams and projects

3. 查询deployment
URI:GET /apis/apps/v1/namespaces/{namespace}/statefulsets/{name} (适用于1.9及以上版本的所有集群)
GET /apis/apps/v1beta1/namespaces/{namespace}/statefulsets/{name} (仅适用于1.15及以下版本的集群)
描述该API的参数。

参数 是否必选 描述
name Yes name of the Deployment
namespace Yes object name and auth scope, such as for teams and projects

4. 修改deployment
url:PUT /apis/apps/v1/namespaces/{namespace}/statefulsets/{name} (适用于1.9及以上版本的所有集群)
PUT /apis/apps/v1beta1/namespaces/{namespace}/statefulsets/{name} (仅适用于1.15及以下版本的集群)
传参:

参数 是否必选 描述
namespace Yes object name and auth scope, such as for teams and projects
name Yes name of the Deployment

yaml文件可修改字段(鸡肋):
This API is used to replace a StatefulSet object under a specified Namespace.
The following fields can be updated:

  • metadata.selfLink
  • metadata.resourceVersion
  • metadata.labels
  • metadata.clusterName
  • metadata.generateName
  • metadata.annotations
  • spec.replicas
  • template.containers
  • spec.restartPolicy
  • spec.activeDeadlineSeconds

The other fields cannot be updated.

4.3.2.3 ReplicaSet

1. 创建ReplicaSet!
url : POST /apis/apps/v1/namespaces/{namespace}/deployments (适用于1.15以上版本的所有集群)
传参:

参数 是否必选 描述
namespace Yes object name and auth scope, such as for teams and projects

yaml文件请求参数校验:

参数 子结构 是否必选 参数类型 描述
apiVersion
Yes String APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values.
kind
Yes String Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase.
metadata
Yes metadata object Standard object metadata.
spec
Yes spec object Specification of the desired behavior of the Deployment.
template Yes template object Template describes the pods that will be created.
status
No status object Most recently observed status of the Deployment.

2. 删除ReplicaSet
URI:DELETE /apis/apps/v1/namespaces/{namespace}/deployments/{name} (适用于1.9及以上版本的所有集群)
描述该API的参数。

参数 是否必选 描述
name Yes name of the Deployment
namespace Yes object name and auth scope, such as for teams and projects

3. 查询ReplicaSet
URI:GET /apis/apps/v1/namespaces/{namespace}/replicasets/{name} (适用于1.9及以上版本的所有集群)
GET /apis/extensions/v1beta1/namespaces/{namespace}/replicasets/{name}(仅适用于1.15及以下版本的集群)
描述该API的参数。

参数 是否必选 描述
name Yes name of the Deployment
namespace Yes object name and auth scope, such as for teams and projects

4. 修改ReplicaSet
url:PUT /apis/apps/v1/namespaces/{namespace}/deployments/{name} (适用于1.9及以上版本的所有集群)
传参:

参数 是否必选 描述
namespace Yes object name and auth scope, such as for teams and projects
name Yes name of the Deployment

yaml文件可修改字段(鸡肋):

参数 子结构 子结构 是否可修改
metadata selfLink
Yes
resourceVersion
labels
clusterName
generateName
annotations
spec replicas
restartPolicy
activeDeadlineSeconds
template containers

4.3.2.4 DaemonSet

1. 创建DaemonSet!
url : POST /apis/apps/v1/namespaces/{namespace}/deployments (适用于1.15以上版本的所有集群)
传参:

参数 是否必选 描述
namespace Yes object name and auth scope, such as for teams and projects

yaml文件请求参数校验:

参数 子结构
是否必选 参数类型 描述
apiVersion

Yes String APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values.
kind

Yes String Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase.
metadata

Yes metadata object Standard object metadata.
spec

Yes spec object Specification of the desired behavior of the Deployment.
template
Yes template object Template describes the pods that will be created.


metadata Yes metadata object Standard object’s metadata.


spec Yes Object Specification of the desired behavior of the pod.
status

No status object Most recently observed status of the Deployment.

2. 删除DaemonSet
URI:DELETE /apis/apps/v1/namespaces/{namespace}/daemonsets/{name} (适用于1.9及以上版本的所有集群)
DELETE /apis/extensions/v1beta1/namespaces/{namespace}/daemonsets/{name} (仅适用于1.15及以下版本的集群)
描述该API的参数。

参数 是否必选 描述
name Yes name of the Deployment
namespace Yes object name and auth scope, such as for teams and projects

3. 查询DaemonSet
URI:GET /apis/apps/v1/namespaces/{namespace}/daemonsets/{name} (适用于1.9及以上版本的所有集群)
GET /apis/extensions/v1beta1/namespaces/{namespace}/daemonsets/{name} (仅适用于1.15及以下版本的集群)
描述该API的参数。

参数 是否必选 描述
name Yes name of the Deployment
namespace Yes object name and auth scope, such as for teams and projects

4. 修改DaemonSet
url:PATCH /apis/apps/v1/namespaces/{namespace}/daemonsets/{name} (适用于1.9及以上版本的所有集群)
PATCH /apis/extensions/v1beta1/namespaces/{namespace}/daemonsets/{name} (仅适用于1.15及以下版本的集群)
传参:

参数 是否必选 描述
namespace Yes object name and auth scope, such as for teams and projects
name Yes name of the Deployment

yaml文件可修改字段(鸡肋):
This API is used to update a DaemonSet object under a specific Namespace.
The following fields can be updated:

  • metadata.selfLink
  • metadata.resourceVersion
  • metadata.labels
  • spec.selector
  • spec.template.spec.containers
  • spec.templateGeneration
  • spec.revisionHistoryLimit

The other fields cannot be updated.

4.3.2.5 service

1. 创建deployment!
url: POST /api/v1/namespaces/{namespace}/services

参数 是否必选 描述
pretty No If ‘true’, then the output is pretty printed.
namespace Yes Name and auth scope, such as for teams and projects.

校验参数:
只校验最外层非空即可!

参数 子结构 子结构 是否必选 参数类型 描述
kind

Yes String Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase.
The value of this parameter is Service.
apiVersion

Yes String APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values.
The value of this parameter is v1.
metadata

Yes metadata object -
name
Yes String Name must be unique within a namespace. Is required when creating resources, although some resources may allow a client to request the generation of an appropriate name automatically. Name is primarily intended for creation idempotence and configuration definition. Cannot be updated.
Value length: 4 character ≤ String length ≤ 63 characters
The string must comply with regular expression: ^a-z?$.

labels
No Object Map of string keys and values that can be used to organize and categorize (scope and select) objects. May match selectors of replication controllers and services.
spec

Yes spec object -
ports
Yes ports object The list of ports that are exposed by this service.

port Yes Integer The port that will be exposed by this service.
Value range: (0,65535].
status

No status object -

2. 删除service
URI:DELETE /api/v1/namespaces/{namespace}/services/{name}
表1 描述该API的参数。

参数 是否必选 描述
namespace Yes Object name and auth scope, such as for teams and projects.
name Yes Name of the Service.

3. 查询service
URI: GET /api/v1/namespaces/{namespace}/services/{name}
表1 描述该API的参数。

参数 是否必选 描述
namespace Yes Object name and auth scope, such as for teams and projects.
name Yes Name of the Service.

4. 修改service
url:PUT /api/v1/namespaces/{namespace}/services/{name}
传参:

参数 是否必选 描述
pretty No If ‘true’, then the output is pretty printed.
namespace Yes Object name and auth scope, such as for teams and projects.

yaml文件可修改字段(鸡肋):

  • metadata.selfLink
  • metadata.resourceVersion
  • metadata.labels
  • metadata.clusterName
  • metadata.generateName
  • metadata.annotations
  • spec.externalTrafficPolicy
  • spec.sessionAffinity
  • spec.type
  • image.png说明:spec.type的值,可更新的优先级为LoadBalancer>NodePort>ClusterIP,即当spec.type的值为ClusterIP时,可更新为LoadBalancer或NodePort;当spec.type的值为NodePort时,只能更新为LoadBalancer;当spec.type的值为LoadBalancer时,spec.type不可更新。
  • spec.port
  • spec.selector
  • spec.externalIPs
  • spec.loadBalancerIP
  • spec.loadBalancerSourceRanges

    4. 质量目标

    | 特性 | 描述 | | —- | —- | | 功能 | 1. 实现正常登录功能(token和config)、查看节点信息功能、查看命名空间功能、对workload进行增删改查、对service进行增删改查
    2.所实现功能均能给出达到预期目标的合理可接受的结果 | | 性能 | 1.响应时间:最大响应时间<5s
    90%响应时间<1s | | 易用性 | 1.用户指导完整(在用户文档或帮助机制,充分描述并能使用户使用这些功能 )
    2.差错信息能够给出差错发生原因及解决方法
    3.能在未进行学习情况完成常规任务操作
    4.能够给用户传达操作正确结果或提示
    5.相似交互任务具有一致的行为和外观 | | 可靠性 | 1.系统在使用过程中稳定流畅。 | | 可维护性 | 1.日志记录完整
    2.文档详细 | | 健壮性 | 1. 整个系统的运行结果符合设计开发者的预期;
    2. 可以在一定限度内容忍,在最大程度内避免操作人员的误操作;
    3. 系统防止越权行为和其他的用户不合法操作 | | 兼容性 | 兼容IE(10以上版本)、chrome 、火狐 | | 内建质量 | UT 覆盖率 > 60%
    AT 覆盖率 > 60% |

5 API设计要求

满足 RESTful API 设计规范 要求。
【该环节的主要目标是,列出该需求所关联的下游环节或下游需求的设计要求,便于跨团队协作对齐】
以deployment为例,以下是api设计范例 :

  1. func DeploymentRouters(e *gin.Engine) {
  2. mainGroup := e.Group("/apis/apps/v1/namespaces/deployments")
  3. {
  4. mainGroup.PUT("", UpdateDeploymentController)
  5. mainGroup.DELETE("", DeleteDeploymentController)
  6. mainGroup.GET("/yaml", QueryDeploymentJsonController)
  7. }
  8. }

6. 部署要求

  • 第一阶段主机部署,第二阶段容器化部署。

    7. 其他资料

    无