ARTICLE DETAIL

资讯详情

深耕网站视觉设计与运营推广的一线实战洞察。

使用 KubeDB 托管 PostgreSQL 在 Kubernetes 上部署 FerretDB 实战指南

使用 KubeDB 托管 PostgreSQL 在 Kubernetes 上部署 FerretDB 实战指南 后端数据库文档数据库【免费下载链接】FerretDBA truly Open Source MongoDB alternative项目地址https://gitcode.com/gh_mirrors/fe/FerretDB点击查看免费下载FerretDB 是一款开源文档数据库它在 PostgreSQL 之上提供 MongoDB 兼容能力让开发者可以继续使用熟悉的 MongoDB 语法与命令而数据实际存储在 PostgreSQL 中。本指南以 KubeDBKubernetes 生态中流行的数据库生命周期管理工具为入口完整演示如何在 Kubernetes 集群中通过 KubeDB 托管 PostgreSQL 作为后端一键部署 FerretDB 实例并完成从获取凭据、mongosh连接、CRUD 操作到 PostgreSQL 侧数据验证的端到端流程。读完本文你将掌握两种部署形态由 KubeDB 全托管 PostgreSQL 的集成部署以及接入集群内外部自管 PostgreSQL 的解耦部署。为什么在 Kubernetes 上用 KubeDB 跑 FerretDB过去几年Kubernetes 已成为生产级数据库的主流部署平台。像 KubeDB 这类工具将数据库的供给provisioning、监控、升级、自动扩缩容、备份以及故障检测等运维任务自动化显著降低了在 Kubernetes 上运行有状态服务的门槛。FerretDB 的架构决定了它天然适合这种模式它是一个代理 翻译层式的服务对外暴露 MongoDB Wire Protocol对内通过 PostgreSQL 连接串将文档操作映射为 SQL 执行。因此一个可用的 FerretDB 实例至少需要两个组件FerretDB 服务本身提供27017端口的 MongoDB 协议监听PostgreSQL 后端存储全部文档数据。在 cmd/ferretdb/main.go 中可以看到FerretDB 服务端通过--postgresql-url对应环境变量FERRETDB_POSTGRESQL_URL指定后端 PostgreSQL 连接串默认值为postgres://127.0.0.1:5432/postgres而 MongoDB 协议监听地址默认为127.0.0.1:27017。KubeDB 正是把这两者的供给、编排与生命周期管理统一收编进了 Kubernetes 的声明式资源体系——通过一个FerretDB类型的 Custom ResourceKubeDB 会自动创建 FerretDB 的 StatefulSet 以及配套的 PostgreSQL 集群。与直接编写原生DeploymentService参考 website/docs/installation/ferretdb/kubernetes.md 中的基础示例相比KubeDB 方案额外获得了数据库级别的运维能力持久化存储编排、高可用副本、备份与恢复、监控集成等因此更适合对数据库 SLA 有要求的场景。前置条件开始之前请确认以下环境已就绪Kubernetes 集群可以是本地 Minikube、Docker Desktop 自带的 Kubernetes或任意云厂商托管集群AppsCode License集群 IDKubeDB 需要企业版许可先向 AppsCode 申请申请时会要求提供集群 IDHelm用于安装 KubeDB OperatorkubectlKubernetes 命令行工具用于应用资源与排查状态mongoshMongoDB Shell用于连接 FerretDB 执行数据操作。第一步获取集群 ID申请 AppsCode License 需要先拿到集群 ID。执行下面的命令kube-system命名空间的 UID 即为集群 IDkubectl get ns kube-system -o jsonpath{.metadata.uid}拿到该 ID 后到 AppsCode 的许可签发页面完成 License 申请并将 License 文件保存到本地后续 Helm 安装命令会用到它的路径。第二步通过 Helm 安装 KubeDB使用 Helm 以 OCI 仓库方式安装 KubeDB关键点有两个一是通过--set-file global.license传入 License 文件二是通过--set global.featureGates.FerretDBtrue显式开启 FerretDB 特性门控helm install kubedb oci://ghcr.io/appscode-charts/kubedb \ --version v2024.2.14 \ --namespace kubedb --create-namespace \ --set-file global.license/path/to/the/license.txt \ --set global.featureGates.FerretDBtrue \ --wait --burst-limit10000 --debug注意请务必将/path/to/the/license.txt替换为第一步申请到的 License 文件实际路径否则安装会失败。安装完成后验证 KubeDB 各组件 Pod 是否正常运行$ kubectl get pods --all-namespaces -l app.kubernetes.io/instancekubedb NAMESPACE NAME READY STATUS RESTARTS AGE kubedb kubedb-kubedb-autoscaler-5c97c8c7f9-lw64s 1/1 Running 0 11m kubedb kubedb-kubedb-ops-manager-7b8fc4d7bf-28qk4 1/1 Running 0 11m kubedb kubedb-kubedb-provisioner-6c89ddd5d8-fw24w 1/1 Running 0 11m kubedb kubedb-kubedb-webhook-server-6fc6c8b44f-pwdvr 1/1 Running 0 11m kubedb kubedb-sidekick-86c64c8f59-gvzd8 1/1 Running 0 11mKubeDB 会随安装注册多组 CRDCustom Resource Definition其中就包含FerretDB。可用下面的命令列出全部 CRD Group 进行确认kubectl get crd -l app.kubernetes.io/namekubedb第三种使用 KubeDB 托管 PostgreSQL 部署 FerretDB创建独立命名空间为 FerretDB 及其全部关联对象创建独立命名空间便于资源隔离与统一管理kubectl create namespace ferretdemo编写 FerretDB 自定义资源接下来创建 FerretDB 的 Custom Resource。将以下内容保存为ferret.yamlapiVersion: kubedb.com/v1alpha2 kind: FerretDB metadata: name: ferret namespace: ferretdemo spec: version: 1.18.0 storageType: Durable storage: accessModes: - ReadWriteOnce resources: requests: storage: 1Gi backend: externallyManaged: false terminationPolicy: WipeOut各字段含义如下spec.versionFerretDB 镜像版本。KubeDB 在本文写作时对应 FerretDB v1.18.0仅支持该版本升级版本前请确认 KubeDB 的版本支持矩阵spec.storageType: Durable使用持久化存储而非内存型保证数据落盘spec.storage定义持久卷的访问模式ReadWriteOnce与容量1Gi实际大小可按需调整spec.backend.externallyManaged: false声明 PostgreSQL 后端由 KubeDB 一并托管创建这是本节的默认形态spec.terminationPolicy: WipeOut删除该 CR 时同时清理后端数据测试环境常用生产环境建议评估Retain等更保守的策略。应用该资源kubectl apply -f ferret.yamlKubeDB 会据此自动创建 FerretDB 及其全部附属对象StatefulSet、Service、PostgreSQL 集群等。验证部署结果查看ferretdemo命名空间下的全部资源$ kubectl get all -n ferretdemo NAME READY STATUS RESTARTS AGE pod/ferret-0 1/1 Running 0 4m42s pod/ferret-pg-backend-0 2/2 Running 0 5m15s pod/ferret-pg-backend-1 2/2 Running 0 5m10s pod/ferret-pg-backend-arbiter-0 1/1 Running 0 5m1s NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/ferret ClusterIP 10.111.78.151 none 27017/TCP 5m18s service/ferret-pg-backend ClusterIP 10.99.57.62 none 5432/TCP,2379/TCP 5m18s service/ferret-pg-backend-pods ClusterIP None none 5432/TCP,2380/TCP,2379/TCP 5m18s service/ferret-pg-backend-standby ClusterIP 10.108.28.132 none 5432/TCP 5m18s NAME READY AGE statefulset.apps/ferret 1/1 4m42s statefulset.apps/ferret-pg-backend 2/2 5m15s statefulset.apps/ferret-pg-backend-arbiter 1/1 5m1s NAME TYPE VERSION AGE appbinding.appcatalog.appscode.com/ferret kubedb.com/ferretdb 1.18.0 4m42s appbinding.appcatalog.appscode.com/ferret-pg-backend kubedb.com/postgres 13.13 5m1s NAME VERSION STATUS AGE postgres.kubedb.com/ferret-pg-backend 13.13 Ready 5m18s从输出中可以清晰看到 KubeDB 的编排结果statefulset.apps/ferretFerretDB 本体1 副本通过service/ferret27017/TCP对外提供 MongoDB 协议statefulset.apps/ferret-pg-backend与ferret-pg-backend-arbiterKubeDB 托管的 PostgreSQL 高可用集群主从 仲裁者版本 13.13通过5432/TCP对外提供 SQL 服务appbinding与postgres.kubedb.com/ferret-pg-backendKubeDB 的应用绑定与 PostgreSQL 资源记录状态均为Ready。再单独确认 FerretDB 资源本身的状态$ kubectl get ferretdb -n ferretdemo ferret NAME NAMESPACE VERSION STATUS AGE ferret ferretdemo 1.18.0 Ready 9m6sSTATUS为Ready即表示部署成功。通过port-forward暴露本地访问集群内service/ferret是ClusterIP类型本地无法直接访问。先列出 KubeDB 创建的相关 Service$ kubectl get service -n ferretdemo | grep ferret ferret ClusterIP 10.111.78.151 none 27017/TCP 11m ferret-pg-backend ClusterIP 10.99.57.62 none 5432/TCP,2379/TCP 11m ferret-pg-backend-pods ClusterIP None none 5432/TCP,2380/TCP,2379/TCP 11m ferret-pg-backend-standby ClusterIP 10.108.28.132 none 5432/TCP 11m将ferretService 的27017端口转发到本地kubectl port-forward -n ferretdemo svc/ferret 27017从 Secret 中获取连接凭据通过mongosh连接前需要拿到数据库凭据。KubeDB 会自动将ferret服务使用的凭据以 KubernetesSecret的形式保存。先确认相关 Secretkubectl get secret -n ferretdemo | grep ferret其中ferret-pg-backend-auth保存了 PostgreSQL 后端的用户名与密码用下面两条命令解码echo $(kubectl get secret -n ferretdemo ferret-pg-backend-auth -o jsonpath{.data.username} | base64 -d) echo $(kubectl get secret -n ferretdemo ferret-pg-backend-auth -o jsonpath{.data.password} | base64 -d)输出即为连接 FerretDB 时使用的username与password。使用mongosh连接 FerretDB连接串格式如下其中authMechanismPLAIN是 FerretDB 对接 PostgreSQL 用户体系时的认证机制mongosh mongodb://username:passwordhost:27017/ferretdb?authMechanismPLAIN实际连接效果mongosh mongodb://postgres:p.i~glw7q9mdbpQ2localhost:27017/ferretdb?authMechanismPLAIN Current Mongosh Log ID: 662699b8fa65a75337cb3ec7 Connecting to: mongodb://credentialslocalhost:27017/ferretdb?authMechanismPLAINdirectConnectiontrueserverSelectionTimeoutMS2000appNamemongosh2.2.2 Using MongoDB: 7.0.42 Using Mongosh: 2.2.2 ------ The server generated these startup warnings when booting 2024-04-22T17:09:13.234Z: Powered by FerretDB v1.18.0 and PostgreSQL 13.13 on aarch64-unknown-linux-musl, compiled by gcc. 2024-04-22T17:09:13.235Z: Please star us on GitHub: https://github.com/FerretDB/FerretDB. 2024-04-22T17:09:13.235Z: The telemetry state is undecided. 2024-04-22T17:09:13.235Z: Read more about FerretDB telemetry and how to opt out at https://beacon.ferretdb.io. ------ ferretdb几点值得注意连接信息显示Powered by FerretDB v1.18.0 and PostgreSQL 13.13说明客户端实际连接的是 FerretDB而非真正的 MongoDB 服务启动警告中的 telemetry 状态默认undecidedFerretDB 会在运行一段时间后才决定是否上报匿名使用数据。仓库源码中通过--telemetry参数枚举undecided/enabled/disabled控制参见 cmd/ferretdb/main.go 与 ferretdb/ferretdb.go 中可嵌入模式的Telemetry配置项提示符进入ferretdb即表示连接成功可以开始执行 MongoDB 风格的数据操作。版本说明本文对应的 KubeDB 集成版本为 FerretDB v1.18.0v1.x 时代后端基于 PostgreSQL 的 pjson 存储方案数据库名称为ferretdb。当前 FerretDB 主线仓库已演进至 v2.x后端改用 PostgreSQL 的 DocumentDB 扩展相关 schema 约定见 internal/documentdb/documentdb.go服务端入口与FERRETDB_POSTGRESQL_URL等配置项保持一致。实操时请以所用 KubeDB 版本支持的 FerretDB 版本为准并参阅仓库根目录 CHANGELOG.md 了解版本演进。数据操作演练插入、更新与查询连接成功后先向weather集合插入一条天气记录验证写路径db.weather.insertMany([ { date: new Date(2024-04-22), location: { city: New York, country: USA, coordinates: { lat: 40.7128, lon: -74.006 } }, weather: { temperature: 18, conditions: Cloudy, wind_speed: 12, humidity: 80 }, remarks: Possible light rain in the evening. } ])接着演示条件更新更新纽约地区风速大于 10 km/h 记录的湿度字段。这里用到了嵌套字段路径location.city、weather.wind_speed与$gt比较运算符、$set更新运算符db.weather.updateMany( { location.city: New York, weather.wind_speed: { $gt: 10 } }, { $set: { weather.humidity: 85 } } )执行结果返回标准的 MongoDB 风格写入确认对象{ acknowledged: true, insertedId: null, matchedCount: 1, modifiedCount: 1, upsertedCount: 0 }最后用db.weather.find()读取数据确认更新已生效humidity已从 80 变为 85_id为 FerretDB 自动生成的 ObjectIdresponse [ { _id: ObjectId(66278976fba61a5fec8bad82), date: ISODate(2024-04-22T00:00:00.000Z), location: { city: New York, country: USA, coordinates: { lat: 40.7128, lon: -74.006 } }, weather: { temperature: 18, conditions: Cloudy, wind_speed: 12, humidity: 85 }, remarks: Possible light rain in the evening. } ]深入验证数据真实落在 PostgreSQLFerretDB 的承诺是文档操作透明落库。为验证这一点直接进入后端 PostgreSQL 容器查看数据。先 exec 进ferret-pg-backend-0并连接ferretdb数据库% kubectl exec -it -n ferretdemo ferret-pg-backend-0 -- bash -c psql -d ferretdb在 PostgreSQL 中将SEARCH_PATH设为ferretdb模式列出全部表ferretdb# set SEARCH_PATH to ferretdb; SET ferretdb# \dt List of relations Schema | Name | Type | Owner -------------------------------------------------------- ferretdb | _ferretdb_database_metadata | table | postgres ferretdb | weather_36404793 | table | postgres (2 rows)可以看到两张表_ferretdb_database_metadata元数据表与weather_36404793对应weather集合的数据表。查询该表内容ferretdb# SELECT * FROM weather_36404793;返回的结果是一条 JSONB 记录其中不仅包含原始文档字段还带有$s模式描述信息完整记录了每个字段的 BSON 类型如objectId、date、string、int、double这就是 FerretDB 在 PostgreSQL 中无损保存 MongoDB 文档类型语义的实现方式。humidity字段值已更新为 85与mongosh侧看到的完全一致。至此一条数据从mongosh写入、经 FerretDB 翻译、最终持久化到 PostgreSQL 的完整链路得到验证。第四种接入外部托管的 PostgreSQL上面演示了由 KubeDB 全托管 PostgreSQL 的集成形态。如果你的团队已经维护了一套 PostgreSQL 集群例如跨集群或由其他 Operator 管理FerretDB 也可以直接对接只需将backend.externallyManaged置为true并指向外部服务。YAML 配置如下同样部署到ferretdemo命名空间apiVersion: kubedb.com/v1alpha2 kind: FerretDB metadata: name: ferretdb-external namespace: ferretdemo spec: version: 1.18.0 authSecret: externallyManaged: true name: ha-postgres-auth storageType: Durable storage: accessModes: - ReadWriteOnce resources: requests: storage: 1Gi backend: externallyManaged: true postgres: service: name: ha-postgres namespace: ferretdemo pgPort: 5432 terminationPolicy: WipeOut与托管模式的差异点集中在两个字段spec.authSecret声明认证 Secret 由外部管理。authSecret.name指向访问外部 PostgreSQL 时使用的认证 Secret 名称本例为ha-postgres-auth需要你自行在集群中预先创建好该 Secretspec.backend.externallyManaged: truespec.backend.postgres.service声明后端 PostgreSQL 不归 KubeDB 创建而是指向集群内已存在的服务。spec.backend.postgres.service.name、namespace、pgPort分别指定外部 PostgreSQL 的 Service 名称、所在命名空间与服务端口默认 PostgreSQL 端口为 5432。在这种形态下KubeDB 只负责编排 FerretDB 本体及其持久化存储PostgreSQL 的供给、备份、高可用等职责仍由外部系统承担。该模式适合已有成熟 PostgreSQL 运维体系、希望把 FerretDB 作为接入层叠加到现有数据库之上的场景。总结与建议本文完整走通了两种在 Kubernetes 上运行 FerretDB 的路径KubeDB 托管 PostgreSQL一个FerretDBCR 即可让 KubeDB 同时编排 FerretDB 与高可用 PostgreSQL 集群获得存储编排、凭据管理自动生成 Secret、健康检查等开箱能力适合快速起步外部 PostgreSQL通过backend.externallyManaged与authSecret.externallyManaged对接既有 PostgreSQL 服务适合已有数据库资产、需要统一接入 MongoDB 协议的场景。无论哪种方式客户端视角下 FerretDB 都表现为一个标准的 MongoDB 兼容服务27017端口、mongosh连接、insertMany/updateMany/find等 API 全部可用而数据最终沉淀在 PostgreSQL 中。在实际投入生产前建议关注三点确认所部署 FerretDB 版本与 KubeDB 的兼容矩阵版本更新记录可参阅仓库 CHANGELOG.md按数据安全要求设置合理的terminationPolicy在生产环境显式配置 telemetry 策略仓库源码中可通过--telemetry参数关闭匿名上报。赞分享后端数据库文档数据库【免费下载链接】FerretDBA truly Open Source MongoDB alternative项目地址https://gitcode.com/gh_mirrors/fe/FerretDB点击查看免费下载相关推荐JDK HotSpot 反汇编插件 hsdisCapstone、LLVM、binutils 三大后端的构建与使用实战指南JDK HotSpot 反汇编插件 hsdisCapstone、LLVM、binutils 三大后端的构建与使用实战指南 本文围绕 JDK 仓库中的 hsdi后端数据库文档数据库掌握 GitHub Copilot CLI 技能系统用 SKILL.md 打造可自动匹配的领域专家掌握 GitHub Copilot CLI 技能系统用 SKILL.md 打造可自动匹配的领域专家 本篇技术指南以 awesome copilot 仓库中 c后端数据库文档数据库OpenReplay 自托管 PostgreSQL 18 容器实战非 root 用户、fsGroup 与 Kubernetes 部署全指南OpenReplay 自托管 PostgreSQL 18 容器实战非 root 用户、fsGroup 与 Kubernetes 部署全指南 本指南以 Open可观测性开发工具前端后端创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表