跳转到内容
AggregaAggrega
选择语言: 简体中文
本页面由机器翻译。阅读英文原文。

一个 Aggregate,两种规模的 MySQL 集群

一个 MySQL InnoDB Cluster 及对其进行协调的控制平面,由同一个未改动的 Aggregate 构建两次:一次请求单个服务器实例,一次 请求三个。

要点在于规模存放在哪里。 数据库背后有多少台服务器,既不是开发者写的,也不是 Recipe 决定的——它是 Release 中的一行运维规模配置,也是两棵渲染树之间的 全部差异。

编译器写出什么,不写出什么

此实现输出一个 InnoDBCluster 自定义资源。它不输出 服务器、Service 或路由器:这些由 MySQL Operator for Kubernetes 在自定义资源应用之后 于目标集群内创建,这项工作发生在编译器完成很久之后。

因此,下文描述的都是渲染出的自定义资源请求了什么。 运行中的集群最终包含什么,是控制器对该请求的回应,发生在编译器从未看到的时刻——编译器对此不作任何断言,因为它无从查看。

唯一变化的部分

.-- release-single.yaml ----- spec.instances: 1
/
one Aggregate -- one Manifest -+
one Component \
one Recipe '- release-replicated.yaml -- spec.instances: 3

两个 Releases 选择同一个 Component 和同一个 Recipe,因此只有一个 Manifest,而不是每个 Release 一个。当两个 Releases 仅在规模上不同时, Ops 映射对这一差异无话可说,再复制一份 只会多出一样需要保持同步的东西,毫无收益。

文件

  • aggregate.yaml —— 开发者意图,且两个 Releases 使用同一份文档:一个用于控制平面的 mysql-operator 组件,一个类型为 core/datastore/mysql-innodb-cluster 的 db 组件,以及开发者希望服务器使用的字符集。这里没有任何内容说明服务器有多少台。
  • manifest.yaml —— Ops 映射,由两个 Releases 共用。共两行:哪个 Recipe 实现控制平面,哪个实现数据库。
  • release-single.yaml、release-replicated.yaml —— 运维人员的规模配置。两者在 metadata.name 和 server_instances 上不同,其余每个字段都有意保持一致,使这一行成为全部差异。
  • .aggrega-config.yaml —— 指定标准库目录从中解析的 hub 注册表。本示例不编写任何自有实体。

生成示例

你可以用下面的命令构建这个示例,也可以让你的 AI 助手替你构建。

将示例写入一个以其命名的新文件夹:

Terminal window
aggrega scaffold --example mysql-innodb-cluster

然后它会打印接下来要运行的命令。第一条命令进入新文件夹:

Terminal window
cd mysql-innodb-cluster

构建

Terminal window
aggrega build --release release-single.yaml --auto-pull
aggrega build --release release-replicated.yaml --auto-pull

--auto-pull 会在构建期间写入全新克隆所缺少的 lockfile。每次构建都写入 dist/,且构建会添加其文件,而不会清除先前构建留下的内容。若要单独查看某个 Release 的输出,请在两次运行之间删除 dist/:

Terminal window
rm -rf dist && aggrega build --release release-replicated.yaml --auto-pull

每个 Release 渲染 5 个文件:数据库的自定义资源,以及用于搭建控制平面的 4 个文件。

dist/stack/helm/db/innodbcluster.yaml
dist/stack/helm/mysql-operator/clusterrole.yaml
dist/stack/helm/mysql-operator/clusterrolebinding.yaml
dist/stack/helm/mysql-operator/deployment.yaml
dist/stack/helm/mysql-operator/serviceaccount.yaml

借助 AI 助手

已配置 Aggrega 工具的 AI 助手也能完成同样的操作:把下面这条提示粘贴给它。

只使用 Aggrega 工具,不要自己创建任何文件夹。如果工作区文件夹中还没有 `mysql-innodb-cluster` 文件夹,调用一次 `scaffold` 工具,将 `example` 设为 `mysql-innodb-cluster`,不设 `dir`:它会把示例写入一个新的 `mysql-innodb-cluster` 文件夹。然后按以下顺序为每个 Release 各调用一次 `build` 工具——`release-single.yaml` 和 `release-replicated.yaml`——每次都将 `workspace_root` 设为 `mysql-innodb-cluster`、`release` 设为该文件、`auto_pull` 设为 true、`write` 设为 true。告诉我每次构建写入了哪些文件;它们位于 `mysql-innodb-cluster/dist/` 下。

数据库渲染成什么

# dist/stack/helm/db/innodbcluster.yaml — aggrega build --release release-single.yaml
apiVersion: mysql.oracle.com/v2
kind: InnoDBCluster
metadata:
[… omitted: the labels and annotations, described below]
name: db
spec:
instances: 1
router:
instances: 1
version: "8.4.0"
secretName: "mysql-root"
mycnf: |
[mysqld]
character-set-server=utf8mb4
collation-server=utf8mb4_0900_ai_ci

渲染出的文件还带有 Aggrega 为其渲染的每个 制品添加的标签和注解——来自哪个 Aggregate、哪个组件、哪个 Recipe、哪个边界——此处省略,以便聚焦于请求本身。

这 5 个 spec 条目中有 4 个可直接追溯到编写的内容:instances 对应 Release 的 server_instances,version 对应 Component 的 engine_version 默认值,secretName 对应 Release 的 credentials_secret,mycnf 块对应开发者的 charset 和 collation。第五个 router.instances 将在下文讨论。

关键在于这一对比

两棵渲染树之间的全部差异:

--- dist/stack/helm/db/innodbcluster.yaml (single)
+++ dist/stack/helm/db/innodbcluster.yaml (replicated)
spec:
- instances: 1
+ instances: 3
router:
instances: 1

5 个文件之一中的一行。 4 个控制平面文件在两个 Releases 之间 逐字节相同:协调单实例请求的控制器与协调三实例请求的控制器 是同一个控制器,因此它没有任何变化。

路由器数量同样不变,而且它不是该组件可随意调节的旋钮。 组件发布的地址是路由器 应答的 Service,因此数量以自定义资源自身的默认值暴露,且绝不 低于该值。这是对 Release 可请求内容的约束,而非本示例要变化的规模,这也是为什么该行出现在上面的差异中却没有改变。

控制平面

dist/stack/helm/mysql-operator/deployment.yaml
[… omitted: the Deployment's metadata, selector and pod template up to the containers]
containers:
- name: controller
image: container-registry.oracle.com/mysql/community-operator:26.7.0-2.3.0
[… omitted: the controller's readiness probe]

两个 Release 都没有指定镜像。 镜像引用来自 Component 自身的 infra.image 默认值,因此即使 Release 什么都不设置,也会渲染出一个带有确定版本的 Deployment, 而不是一个必须先填写才能应用的 Deployment。从自有镜像仓库镜像拉取的目标可在 Release 中设置 infra.image,从而改用该引用。

控制平面在 Aggregate 中与数据库并列声明,因为 自定义资源本身是惰性的:只有在控制器监听它时,它才会变成运行中的服务器。将两者一起声明,才使二者能够作为一个单元应用。

前提条件由声明断言,而非探测发现

编译器提前运行,从不连接集群,因此无法检查 控制器是否已安装。它改为询问。若不设置 control_plane_present,构建将停止:

[… omitted: the pull's lines, which carry the same halt]
[CATALOG_ERROR E0033]
Recipe/FeatureRecipe assertions
caused by: Recipe "mysql-innodb-cluster" instance "db" assertion "mysql_operator_prerequisite": assertion "Recipe/mysql-innodb-cluster/db/mysql_operator_prerequisite": template: Recipe/mysql-innodb-cluster/db/mysql_operator_prerequisite:1:48: executing "Recipe/mysql-innodb-cluster/db/mysql_operator_prerequisite" at <fail "the MySQL Operator control plane must be installed in the target cluster before this realization emits its InnoDBCluster custom resource; once the control plane is present, affirm the prerequisite by setting infra.control_plane_present to true in the Release">: error calling fail: assertion failed: the MySQL Operator control plane must be installed in the target cluster before this realization emits its InnoDBCluster custom resource; once the control plane is present, affirm the prerequisite by setting infra.control_plane_present to true in the Release

不会写出任何内容。一个无人协调的自定义资源并不是比可用资源小一点的 成功——它是一个能顺利应用、随后就一直闲置的制品,因此构建拒绝生成它,并指出可以表明相反情况的键。

root 账户

这里的两个 Releases 都让集群指向目标集群中已存在的 Secret:

db:
infra:
credentials_secret: mysql-root

不会为该 Secret 渲染任何内容,也没有密码进入输出—— 自定义资源携带其名称,控制器从中读取账户。 若将 infra.credentials 设置为 generated,则会根据附加到组件 root_password 槽位的凭据,在自定义资源旁渲染该 Secret,这会把密码放入渲染输出;当输出本身就是用来供应集群的内容时,这是正确的选择。

此实现不会创建逻辑数据库或应用 用户。控制器只建立集群的 root 账户,其下的一概不管, 因此应用所需的 schema 和账户由持有 root 的人创建。

应选用哪种实现

当不应需要预先安装任何东西时,选用 core/datastore/mysql 及其合成的 Recipe——它不需要集群中有控制器,也不需要访问 chart 仓库,因此无需获取任何内容即可构建,并且会创建应用所请求的逻辑数据库和登录角色。当目标已运行 MySQL Operator 控制平面时,选用 core/datastore/mysql-innodb-cluster,或者像本示例一样安装它,且应用需要的是对位于集群内地址的集群的 root 访问权限。

合成实现的单服务器形态在 ../mysql-standalone/ 中有完整演示,此处不再重复。

分界线在哪里

在一个实例与三个实例之间的选择没有触及开发者文档, Ops 映射也没有。这正是本示例要具体展示的分工:

  • 开发者说明应用需要数据库提供什么;
  • Ops 说明数据库由什么构成——由哪个 Recipe 实现;
  • 运维人员说明这一个有多大。

将 server_instances 改为自定义资源接受的任意值,本目录中的 aggregate.yaml 都不会改变。