使用声明式自动化捆绑包的典型 Lakebase 项目设置

Important

Lakebase 的声明式自动化捆绑包支持目前处于 测试阶段。

本页展示了适用于生产准备的 Lakebase 项目的完整声明式自动化捆绑包,包含最常用的功能:

  • 受保护的生产分支
  • 具有可读辅助节点的高可用性 (HA) 读写端点
  • 服务主体的内联工作区级 CAN_MANAGE 权限
  • 来自 Unity Catalog 的连续同步表流
  • Lakebase 数据库的 Unity 目录绑定
  • 连接到 Lakebase 项目的 Databricks 应用

有关使用 Lakebase 的声明性自动化捆绑包的分步介绍,请参阅 使用声明性自动化捆绑包管理 Lakebase。

先决条件

在开始之前,需要:

  • Databricks CLI v1.0.0 或更高版本。 若要检查版本,请运行 databricks --version。 若要安装或升级,请参阅 安装或更新 Databricks CLI。
  • 已启用 Lakebase 的 Azure Databricks 工作区。
  • 为 OAuth 计算机到计算机(M2M)身份验证配置的服务主体。 该软件包授予此主体在该项目上的工作区 CAN_MANAGE 权限。 请参阅使用 OAuth 授权服务主体访问 Azure Databricks和管理项目权限。
  • 一个启用了变更数据源 (CDF) 的 Unity Catalog Delta 表,用作同步源。 如果不需要数据同步,请删除 postgres_synced_tables 和 postgres_catalogs 块。

完成捆绑包配置

该包使用变量来表示所有工作区特定的值。 在 .databricks/bundle/<target>/variables.json 文件中设置这些参数,或在部署时通过 --var 传递。

创建项目时,Azure Databricks 会自动创建一个 production 分支、一个 primary 读写终结点、一个与你的身份关联的所有者 Postgres 角色,以及一个 databricks_postgres 数据库。 若要配置这些隐式创建的资源,请使用 replace_existing: true..

bundle:
  name: lakebase-typical-project

variables:
  project_id:
    description: 'Lakebase project ID (lowercase, hyphen-delimited)'
    default: 'my-lakebase-project'
  display_name:
    description: 'Human-readable project name shown in the UI'
    default: 'My Lakebase project'
  pg_version:
    description: 'Postgres major version'
    default: 17
  min_cu:
    description: 'Minimum compute units on the default endpoint'
    default: 0.5
  max_cu:
    description: 'Maximum compute units on the default endpoint'
    default: 4.0
  suspend_timeout:
    description: 'Idle time before the default endpoint suspends. Ignored when no_suspension is true.'
    default: '300s'
  admin_sp_app_id:
    description: 'Application ID of the service principal to grant CAN_MANAGE on the project'
    default: '<your-sp-application-id>'
  source_table:
    description: 'Unity Catalog three-part name of the Delta table to sync (catalog.schema.table)'
    default: '<catalog>.<schema>.<table>'
  primary_key_column:
    description: 'Primary key column of the source Delta table'
    default: '<pk>'
  storage_catalog:
    description: 'Unity Catalog catalog where the sync pipeline stores its metadata'
    default: '<catalog>'
  storage_schema:
    description: 'Unity Catalog schema where the sync pipeline stores its metadata'
    default: '<schema>'
  app_name:
    description: 'Databricks App name (must be unique in the workspace)'
    default: 'my-lakebase-app'
  uc_catalog_id:
    description: 'Name to register the Lakebase database in Unity Catalog'
    default: 'my_lakebase_uc_catalog'
  database_name:
    description: 'Postgres-internal name for the app database'
    default: 'app_database'

targets:
  prod:
    default: true
    workspace:
      host: https://<your-workspace>.cloud.databricks.com

    resources:
      # Project — top-level container for branches, endpoints, and databases.
      # The permissions block grants workspace-level CAN_MANAGE to the service principal.
      postgres_projects:
        lakebase_project:
          project_id: ${var.project_id}
          # purge_on_delete: true  # Uncomment to permanently delete on destroy (default: soft delete, 7-day retention).
          pg_version: ${var.pg_version}
          display_name: ${var.display_name}
          default_endpoint_settings:
            autoscaling_limit_min_cu: ${var.min_cu}
            autoscaling_limit_max_cu: ${var.max_cu}
            suspend_timeout_duration: ${var.suspend_timeout}
          permissions:
            - service_principal_name: ${var.admin_sp_app_id}
              level: CAN_MANAGE

      # Configure the implicitly created production branch as protected.
      postgres_branches:
        production:
          branch_id: production
          parent: ${resources.postgres_projects.lakebase_project.name}
          no_expiry: true
          is_protected: true
          replace_existing: true

      # Configure the implicitly created primary endpoint with HA.
      # HA requires no_suspension: true. group.min: 2 adds a standby for automatic failover.
      postgres_endpoints:
        primary:
          endpoint_id: primary
          parent: ${resources.postgres_branches.production.name}
          endpoint_type: ENDPOINT_TYPE_READ_WRITE
          autoscaling_limit_min_cu: ${var.min_cu}
          autoscaling_limit_max_cu: ${var.max_cu}
          no_suspension: true
          group:
            min: 2
            max: 2
            enable_readable_secondaries: true
          replace_existing: true

      # Postgres role that owns the app database.
      postgres_roles:
        app_role:
          role_id: app-role # Resource ID: lowercase letters, digits, and hyphens.
          parent: ${resources.postgres_branches.production.name}
          postgres_role: app_role # Postgres identifier: lowercase letters, digits, and underscores.

      # Named Postgres database for the app.
      postgres_databases:
        app_db:
          database_id: app-database
          parent: ${resources.postgres_branches.production.name}
          postgres_database: ${var.database_name}
          role: ${resources.postgres_roles.app_role.id}

      # Sync a Unity Catalog Delta table into the project continuously.
      postgres_synced_tables:
        orders_sync:
          synced_table_id: '${var.storage_catalog}.${var.storage_schema}.orders_synced'
          branch: ${resources.postgres_branches.production.name}
          postgres_database: ${var.database_name}
          source_table_full_name: ${var.source_table}
          primary_key_columns:
            - ${var.primary_key_column}
          scheduling_policy: CONTINUOUS
          create_database_objects_if_missing: true
          new_pipeline_spec:
            storage_catalog: ${var.storage_catalog}
            storage_schema: ${var.storage_schema}

      # Bind the Lakebase database into Unity Catalog so it is queryable as UC data.
      postgres_catalogs:
        lakebase_uc_catalog:
          catalog_id: ${var.uc_catalog_id}
          postgres_database: ${var.database_name}
          branch: ${resources.postgres_branches.production.name}
          create_database_if_missing: true

      # Databricks App connected to the project.
      # Update source_code_path to point to your app source directory.
      apps:
        lakebase_app:
          name: ${var.app_name}
          description: 'App backed by Lakebase autoscaling'
          source_code_path: ./app_src
          config:
            command:
              - flask
              - run
              - --host=0.0.0.0
              - --port=8000
          resources:
            - name: lakebase-db
              postgres:
                branch: ${resources.postgres_branches.production.name}
                database: ${resources.postgres_databases.app_db.name}
                permission: CAN_CONNECT_AND_CREATE

Note

每个 Lakebase 项目都会自动创建一个由与你的身份关联的 Postgres 角色拥有的 databricks_postgres 数据库。 此捆绑包会创建一个独立的命名数据库 (${var.database_name}),该数据库归专用应用角色所有,从而实现对应用数据的隔离。 若要直接使用隐式数据库和角色,请删除 postgres_roles 和 postgres_databases 资源块,在 postgres_synced_tables 和 postgres_catalogs 上直接设置 postgres_database: databricks_postgres,并将应用资源更新为 database: ${resources.postgres_branches.production.name}/databases/databricks-postgres。

若要改为将隐式所有者角色和 databricks_postgres 数据库纳入捆绑管理,请使用它们现有的 ID 通过 replace_existing: true 声明它们。 数据库 ID 始终为 databricks-postgres。 角色 ID 是根据您的 Databricks 身份生成的,而不是一个固定名称,因此请先查出该 ID:

databricks postgres list-roles projects/<project-id>/branches/production

然后声明这两个资源,使其每个字段都与该角色中已设置的字段保持一致。 省略 membership_roles 会导致在采用角色时删除 DATABRICKS_SUPERUSER 成员身份,因此请显式声明:

postgres_roles:
  owner:
    role_id: <role-id-from-list-roles>
    parent: ${resources.postgres_branches.production.name}
    postgres_role: user@databricks.com # Or the service principal application ID.
    identity_type: USER # Or SERVICE_PRINCIPAL.
    membership_roles:
      - DATABRICKS_SUPERUSER
    replace_existing: true

postgres_databases:
  databricks_postgres:
    database_id: databricks-postgres
    parent: ${resources.postgres_branches.production.name}
    postgres_database: databricks_postgres
    role: ${resources.postgres_roles.owner.id}
    replace_existing: true

Note

若要拆解此捆绑包创建的资源,请运行 databricks bundle destroy -t prod。 默认情况下,项目会软删除,并在永久删除前保留 7 天,以便在保留期内恢复该项目。 若要仅立即删除该项目,请使用带有 --purge 的 Databricks CLI,或取消注释上述项目资源中的 purge_on_delete: true,以便在每次执行 destroy 时将其硬删除:

databricks postgres delete-project projects/<project-id> --purge

应用该软件包

验证和部署:

databricks bundle validate -t prod
databricks bundle deploy -t prod

如果 databricks bundle deploy 第一次运行未完成,请重新运行它。

部署的内容

捆绑包创建以下资源:

  • 一个带有你指定计算默认值的Lakebase项目。
  • 受保护的 production 分支。
  • 一个具有高可用性 (HA) 且具有可读辅助节点的读写主端点。
  • 一个将 Unity Catalog Delta 表流式传输到项目数据库的持续同步管道。
  • 由 Lakebase 数据库支持的 Unity Catalog 目录,可作为 Unity Catalog 数据进行查询。
  • 连接到项目数据库的 Databricks 应用。
  • 你指定的服务主体的工作区 CAN_MANAGE 权限。

其他资源