Custom Applications

理解自定义应用

自定义应用是一种基于原生 Kubernetes 资源(如 Deployment、Service、ConfigMap)构建的应用范式,严格遵循 Kubernetes 声明式 API 设计原则。用户可以通过标准 YAML 文件或直接调用 Kubernetes API 来定义和部署应用,实现对应用生命周期的细粒度控制。由镜像、代码和 YAML 等来源创建的应用在 Alauda Container Platform 中被归类为自定义应用。其设计核心在于平衡灵活性与标准化,适用于需要深度定制管理的场景。

核心能力

  1. 声明式 API 驱动管理
  • 通过 Application CRD 将分布式资源(如 Deployment、Service、Ingress)聚合为逻辑应用单元,实现原子操作。
  1. 应用级抽象与状态聚合
  • 屏蔽底层资源细节(如 Pod 副本状态),开发者可直接通过 Application 资源监控整体应用健康状况(如就绪端点比例、版本一致性)。

  • 支持跨组件依赖声明(如数据库服务必须先于应用服务启动),确保资源初始化顺序与协调。

  1. 全生命周期治理
  • 版本控制:跟踪历史配置,实现一键回滚至任意稳定状态。

  • 依赖解析:自动识别并管理组件间版本兼容性(如 Service API 版本与 Ingress 控制器匹配)。

  1. 增强的可观测性
  • 聚合所有关联资源的状态指标(如 Deployment 可用副本数、Service 流量负载),通过统一监控面板提供全局视图。

设计价值

维度价值主张
复杂度管理将分散的资源(如 Deployment、Service)封装为单一逻辑实体,降低认知和运维负担。
标准化通过 Application CRD 统一应用描述标准,消除因 YAML 分散带来的管理混乱。
生态兼容性无缝集成原生工具链(如 kubectl、Kubernetes Dashboard),并支持 Helm Chart 扩展。
DevOps 效率通过 GitOps 流水线(如 Argo CD)实现声明式交付,加速 CI/CD 自动化。

自定义应用 CRD 架构设计

自定义应用模块定义了两个核心 CRD 资源,构成应用管理的原子抽象单元:

维度价值主张
Application描述逻辑应用单元的元数据和组件拓扑,将 Deployment/Service 等资源聚合为单一实体。
ApplicationHistory记录所有应用生命周期操作(创建/更新/回滚/删除)为版本化快照,与 Application CRD 紧密耦合,实现端到端变更可追溯。

Application CRD 定义

Application CRD 使用 spec.componentKinds 字段声明 Kubernetes 资源类型(如 Deployment、Service),支持跨资源生命周期管理。

apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  name: applications.app.k8s.io
spec:
  group: app.k8s.io
  names:
    kind: Application
    listKind: ApplicationList
    plural: applications
    singular: application
  scope: Namespaced
  subresources:
    status: {}
  validation:
    openAPIV3Schema:
      properties:
        apiVersion:
          description: 'APIVersion 定义该对象表示的版本化模式。服务器应将识别的模式转换为最新的内部值,并可能拒绝未识别的值。更多信息见:https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#resources'
          type: string
        kind:
          description: 'Kind 是表示该对象所代表的 REST 资源的字符串值。服务器可根据客户端请求的端点推断该值。不可更新。采用 CamelCase 格式。更多信息见:https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
          type: string
        metadata:
          description: 'Metadata 是表示 Kubernetes 资源元数据的对象。更多信息见:https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#metadata'
          type: object
        spec:
          properties:
            assemblyPhase:
              description: |
                安装器可设置此字段以指示应用组件是否仍在部署中("Pending")或全部已部署完成("Succeeded")。当应用无法成功组装时,安装器可将此字段设置为 "Failed"。
              type: string
            componentKinds:
              description: |
                该 GroupKinds 数组用于指示应用所组成的资源类型。例如,一个包含 Service 和 Deployment 的 Application 会将此字段设置为 [{"group":"core","kind": "Service"},{"group":"apps","kind":"Deployment"}]
              items:
                description: 'GroupKinds 的项,结构如 "{\"group\":\"core\",\"kind\": \"Service\"}"'
                type: object
              type: array
            descriptor:
              properties:
                description:
                  description: '应用的简短、可读文本描述。'
                  type: string
                icons:
                  description: '应用图标列表。图标信息包括来源、大小和 MIME 类型。'
                  items:
                    properties:
                      size:
                        description: '图标大小。'
                        type: string
                      src:
                        description: '图标来源。'
                        type: string
                      type:
                        description: '图标的 MIME 类型。'
                        type: string
                    required:
                    - src
                    type: object
                  type: array
                keywords:
                  description: '标识应用的关键词列表。'
                  items:
                    type: string
                  type: array
                links:
                  description: '链接列表,描述性 URL 用于展示额外文档、监控面板等。'
                  items:
                    properties:
                      description:
                        description: '链接描述。'
                        type: string
                      url:
                        description: '链接 URL。'
                        type: string
                    type: object
                  type: array
                maintainers:
                  description: '应用维护者列表。每个维护者包含姓名、邮箱和 URL。该字段用于应用分发者标识身份和联系方式。'
                  items:
                    properties:
                      email:
                        description: '维护者邮箱。'
                        type: string
                      name:
                        description: '维护者姓名。'
                        type: string
                      url:
                        description: '维护者联系方式 URL。'
                        type: string
                    type: object
                  type: array
                notes:
                  description: '包含面向应用用户的快速入门人类可读片段。可为纯文本或 CommonMark markdown。'
                  type: string
                owners:
                  items:
                    properties:
                      email:
                        description: '所有者邮箱。'
                        type: string
                      name:
                        description: '所有者姓名。'
                        type: string
                      url:
                        description: '所有者联系方式 URL。'
                        type: string
                    type: object
                  type: array
                type:
                  description: '应用类型(如 WordPress、MySQL、Cassandra)。同一命名空间内可存在多个不同名称的应用。type 字段用于指示它们属于同一类型应用。'
                  type: string
                version:
                  description: '应用版本标识(如 MySQL 5.7 的版本号为 5.7)。'
                  type: string
              type: object
            info:
              description: '应用的人类可读键值对信息。'
              items:
                properties:
                  name:
                    description: '信息名称。'
                    type: string
                  type:
                    description: '信息类型。'
                    type: string
                  value:
                    description: '信息值。'
                    type: string
                  valueFrom:
                    description: '信息值引用自其他资源。'
                    properties:
                      configMapKeyRef:
                        description: 'ConfigMap 键引用。'
                        properties:
                          key:
                            type: string
                        type: object
                      ingressRef:
                        description: 'Ingress 引用。'
                        properties:
                          host:
                            description: 'Ingress 引用的主机。'
                            type: string
                          path:
                            description: 'Ingress 引用的路径。'
                            type: string
                        type: object
                      secretKeyRef:
                        description: 'Secret 键引用。'
                        properties:
                          key:
                            type: string
                        type: object
                      serviceRef:
                        description: 'Service 引用。'
                        properties:
                          path:
                            description: 'Service 引用的路径。'
                            type: string
                          port:
                            description: 'Service 引用的端口。'
                            format: int32
                            type: integer
                        type: object
                      type:
                        type: string
                    type: object
                type: object
              type: array
            selector:
              description: '选择器用于匹配属于该 Application 的资源。所有应用资源应具有标签以匹配此选择器。用户应在所有组件上使用 app.kubernetes.io/name 标签,并将选择器设置为匹配该标签。例如,名为 "my-cool-app" 的 Application 应使用 {"matchLabels": [{"app.kubernetes.io/name": "my-cool-app"}]} 作为选择器,且每个组件应包含匹配该标签的标签。'
              type: object
          type: object
        status:
          description: '状态总结对象的当前状态。'
          properties:
            observedGeneration:
              description: 'observedGeneration 是组件最近观察到的资源期望状态变更的代数。'
              format: int64
              type: integer
          type: object
  version: v1beta1
  versions:
  - name: v1beta1
    served: true
    storage: true

ApplicationHistory 定义

ApplicationHistory CRD 捕获所有生命周期操作(如创建、更新、回滚)为版本控制快照,并与 Application CRD 紧密集成,实现端到端审计追踪。

apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  name: applicationhistories.app.k8s.io
spec:
  group: app.k8s.io
  names:
    kind: ApplicationHistory
    listKind: ApplicationHistoryList
    plural: applicationhistories
    singular: applicationhistory
  scope: Namespaced
  validation:
    openAPIV3Schema:
      properties:
        apiVersion:
          description: 'APIVersion 定义该对象表示的版本化模式。服务器应将识别的模式转换为最新的内部值,并可能拒绝未识别的值。更多信息见:https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#resources'
          type: string
        kind:
          description: 'Kind 是表示该对象所代表的 REST 资源的字符串值。服务器可根据客户端请求的端点推断该值。不可更新。采用 CamelCase 格式。更多信息见:https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
          type: string
        metadata:
          description: 'Metadata 是表示 Kubernetes 资源元数据的对象。更多信息见:https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#metadata'
          type: object
        spec:
          properties:
            changeCause:
              description: '生成 ApplicationHistory 的应用变更原因。'
              type: string
            creationTimestamp:
              description: '应用历史的创建时间戳。'
              format: date-time
              type: string
            resourceDiffs:
              description: '当前版本与上一个版本应用资源的差异,包含三种类型的差异:`create`、`delete` 和 `update`。差异项由差异资源对象的 kind 和 name 组成。'
              properties:
                create:
                  items:
                    properties:
                      kind:
                        description: '创建资源的类型。'
                        type: string
                      name:
                        description: '创建资源的名称。'
                        type: string
                    type: object
                  type: array
                delete:
                  items:
                    properties:
                      kind:
                        description: '删除资源的类型。'
                        type: string
                      name:
                        description: '删除资源的名称。'
                        type: string
                    type: object
                  type: array
                update:
                  items:
                    properties:
                      kind:
                        description: '更新资源的类型。'
                        type: string
                      name:
                        description: '更新资源的名称。'
                        type: string
                    type: object
                  type: array
              type: object
            revision:
              description: |
                应用历史的修订号。为整数,每次应用变更时递增。
              type: integer
            user:
              description: '触发应用变更的用户名称。'
              type: string
            yaml:
              description: |
                应用及其组件快照的 YAML 字符串。
              type: string
          type: object
        status:
          description: '状态总结对象的当前状态。'
          properties:
            observedGeneration:
              description: 'observedGeneration 是组件最近观察到的资源期望状态变更的代数。'
              format: int64
              type: integer
          type: object
      type: object
  version: v1beta1
  versions:
  - name: v1beta1
    served: true
    storage: true