如何利用 Zeabur 实现 OceanBase 的一键部署
引言
 
- Zeabur 是一个功能强大且即开即用的自动化部署平台,它不仅能迅速部署多种应用,还支持一键安装 MySQL、PostgreSQL 等数据库服务。
 -  
Zeabur 拥有众多国内外用户,如 AFFiNE、Bytebase 等企业客户,以及大量全栈和独立开发者。将 OceanBase 集成进 Zeabur 的 第三方服务市场,将能显著降低 OceanBase 的应用门槛,并增强与开发者当前技术栈及开发流程的集成效率。
 
Zeabur 模板
- 简单来说,Zeabur 模板 通过 YAML 配置来声明和编排服务,类似于 Docker Compose 和 Kubernetes Object 的能力。并且 Zeabur 还提供 CLI 工具来快速部署、创建和管理模板。
 - 目前 Zeabur 支持两种类型的服务: Docker 镜像和 Git 代码。因此,只需要按照 Zeabur 的 模板语法,声明 OceanBase 服务的部署逻辑即可。
 
OceanBase 部署模板
- OceanBase 服务的部署模板如下:
 
# yaml-language-server: $schema=https://schema.zeabur.app/template.json
apiVersion: zeabur.com/v1
kind: Template
metadata:
    name: OceanBase
spec:
    description: An enterprise distributed relational database with high availability, high performance, horizontal scalability, and compatibility with SQL standards.
    coverImage: https://mdn.alipayobjects.com/huamei_fhnyvh/afts/img/A*qI5lT4lL1PEAAAAAAAAAAAAAemfOAQ/original
    icon: https://avatars.githubusercontent.com/u/82347605
    tags:
        - Database
    readme: |
        An enterprise distributed relational database with high availability, high performance, horizontal scalability, and compatibility with SQL standards.
        ## Environment Variables
        When you deploy the OceanBase service, Zeabur will automatically inject the relevant environment variables into other services.
        - `OB_HOST`: The hostname of the OceanBase service.
        - `OB_PORT`: The port of the OceanBase service.
        - `OB_TENANT`: The tenant of the OceanBase service.
        - `OB_USERNAME`: The username for the OceanBase service.
        - `OB_PASSWORD`: The password for the OceanBase service.
        - `OB_URI` or `OB_CONNECTION_STRING`: The connection string for the OceanBase service.
    services:
        - name: oceanbase
          icon: https://avatars.githubusercontent.com/u/82347605
          template: PREBUILT
          spec:
            source:
                image: oceanbase/oceanbase-ce:4.3.5.0-100000202024123117
            ports:
                - id: database
                  port: 2881
                  type: TCP
            volumes:
                - id: data
                  dir: /data/1
                - id: log
                  dir: /data/log1
            instructions:
                - type: TEXT
                  title: Command to connect to your OceanBase
                  content: mysql -h${PORT_FORWARDED_HOSTNAME} -P${DATABASE_PORT_FORWARDED_PORT} -u${OB_USERNAME}@${OB_TENANT} -p${OB_PASSWORD}
                - type: TEXT
                  title: OceanBase host
                  content: ${PORT_FORWARDED_HOSTNAME}
                  category: Hostname & Port
                - type: TEXT
                  title: OceanBase port
                  content: ${DATABASE_PORT_FORWARDED_PORT}
                  category: Hostname & Port
                - type: TEXT
                  title: OceanBase tenant
                  content: ${OB_TENANT}
                  category: Credentials
                - type: TEXT
                  title: OceanBase username
                  content: ${OB_USERNAME}
                  category: Credentials
                - type: PASSWORD
                  title: OceanBase password
                  content: ${OB_PASSWORD}
                  category: Credentials
            env:
                # custom env in zeabur
                OB_HOST:
                    default: ${CONTAINER_HOSTNAME}
                    expose: true
                    readonly: true
                OB_PORT:
                    default: ${DATABASE_PORT}
                    expose: true
                    readonly: true
                OB_TENANT:
                    default: ${OB_TENANT_NAME}
                    expose: true
                    readonly: true
                OB_USERNAME:
                    default: root
                    expose: true
                    readonly: true
                OB_PASSWORD:
                    default: ${OB_TENANT_PASSWORD}
                    expose: true
                    readonly: true
                # oceanbase-ce docker built-in env
                OB_SYS_PASSWORD:
                    default: ${PASSWORD}
                    expose: true
                    readonly: true
                OB_TENANT_PASSWORD:
                    default: ${PASSWORD}
                    expose: true
                    readonly: true
                OB_TENANT_NAME:
                    default: test
                    expose: true
                    readonly: true
localization:
    zh-CN:
        description: 为现代数据架构打造的分布式数据库。
        readme: |
            为现代数据架构打造的分布式数据库。
            ## 环境变量
            当您部署 OceanBase 服务时,Zeabur 会自动将相关的环境变量注入到其他服务中。
            - `OB_HOST`:OceanBase 服务的主机名。
            - `OB_PORT`:OceanBase 服务的端口。
            - `OB_TENANT`:OceanBase 服务的租户。
            - `OB_USERNAME`:OceanBase 服务的用户名。
            - `OB_PASSWORD`:OceanBase 服务的密码。
            - `OB_URI` 或 `OB_CONNECTION_STRING`:OceanBase 服务的连接字符串。
    zh-TW:
        description: 為現代數據架構打造的分散式資料庫。
        readme: |
            為現代數據架構打造的分散式資料庫。
            ## 環境變數
            當您部署 OceanBase 服務時,Zeabur 會自動將相關的環境變數注入到其他服務中。
            - `OB_HOST`:OceanBase 服務的主機名稱。
            - `OB_PORT`:OceanBase 服務的連接埠。
            - `OB_TENANT`:OceanBase 服務的租戶。
            - `OB_USERNAME`:OceanBase 服務的使用者名稱。
            - `OB_PASSWORD`:OceanBase 服務的密碼。
            - `OB_URI` 或 `OB_CONNECTION_STRING`:OceanBase 服務的連線字串。
 
- 该模板已发布到 Zeabur 的服务市场 OceanBase Deploy Guide。

 
一键部署 OceanBase
添加独立服务器
- OceanBase 部署规格 如下:

 
📢️ 注意: 服务器的可用内存 >= 6G,否则 OceanBase 会启动失败,可通过free -h命令查看。
- 在 Zeabur 上 添加独立服务器 (如果没有服务器,也可以在 Zeabur 上直接购买,这里我选择阿里云的 ECS):

 
📢 部分用户可能对于 ECS 的端口访问有安全要求,如果不想全网开放、只针对 Zeabur 开放访问,需要针对 Zeabur 所在网段进行配置。可以参考 这个案例 在 Discord 上提单咨询,Zeabur 的技术支持人员会提供解决方案。
- 在添加的服务器上「新建项目」。
 

部署 OceanBase 服务
- 在新建的项目上「添加服务 -> 从模板部署 -> 搜索并选择 OceanBase」:
 

- 如果是国内的服务器,则可能会遇到镜像拉取失败的问题,可以选择「更换国内镜像源」:
 

- 然后选择搜索结果的第一个:
 

- 镜像地址回填之后,点击「更新」会重新发起部署:
 

- 等待几分钟之后,即可部署成功:
 

- 部署日志如下:
 

连接 OceanBase
- 复制数据库连接串:
 

- 使用 MySQL 命令行连接 OceanBase:
 

- 更多用法可参考 OceanBase 官方文档 https://www.oceanbase.com/docs/common-oceanbase-database-cn-1000000002059286。
 
环境变量
- 目前仅透出以下环境变量 (如有更多诉求,👏🏻 欢迎留言评论):
 

- 完整的环境变量可参考 https://hub.docker.com/r/oceanbase/oceanbase-ce。
 
