管理高可用性

重要

Lakebase Autoscaling 是 Lakebase 的最新版本更新,具有自动缩放计算、缩放到零、分支和即时还原功能。 有关支持的区域,请参阅 区域可用性。 如果你是 Lakebase 预配的用户,请参阅 Lakebase 预配

本指南介绍如何为 Lakebase 终结点启用和管理高可用性。 有关高可用性的工作原理以及辅助计算实例与独立只读副本有何不同的背景信息,请参阅 高可用性

启用高可用性

若要启用高可用性,请在 UI 中设置计算类型和 HA 配置,或通过 API 配置终结点 EndpointGroupSpec

先决条件

  • 缩放到零功能必须禁用。 在 UI 中,在编辑计算设置中将 Scale 设置为 Off。 通过 API,在终结点规格中设置 no_suspension: true(将 spec.suspension 用作更新掩码)。

UI

创建项目后,单击项目仪表板上的主要计算链接,以打开编辑计算面板。

项目仪表板,其中显示了生产分支及其主要计算链接

计算类型设置为高可用性,然后在“高可用性”下选择配置

  • 2 (1 个主要,1 个次要),
  • 3 (1 个主要,2 个辅助数据库),
  • 4 (1 个主数据库,3 个辅助数据库)的总计算实例。

编辑计算抽屉,其中显示“计算类型”切换设置为“高可用性”和“配置”下拉列表,其中包含 2、3 或 4 个计算实例总数的选项

Lakebase 在不同的可用性区域中预配辅助计算实例。 当所有计算实例都处于活动状态时,端点将自动进行故障转移。

Python SDK

from databricks.sdk import WorkspaceClient
from databricks.sdk.service.postgres import (
    Endpoint, EndpointSpec, EndpointType, EndpointGroupSpec, FieldMask
)

w = WorkspaceClient()

endpoint_name = "projects/my-project/branches/production/endpoints/my-endpoint"

result = w.postgres.update_endpoint(
    name=endpoint_name,
    endpoint=Endpoint(
        name=endpoint_name,
        spec=EndpointSpec(
            endpoint_type=EndpointType.ENDPOINT_TYPE_READ_WRITE,
            group=EndpointGroupSpec(
                min=2,
                max=2,
                enable_readable_secondaries=True
            )
        )
    ),
    update_mask=FieldMask(field_mask=["spec.group"])
).wait()

print(f"Group size: {result.status.group.max}")
print(f"Host: {result.status.hosts.host}")
print(f"Read-only host: {result.status.hosts.read_only_host}")

CLI

databricks postgres update-endpoint \
  projects/my-project/branches/production/endpoints/my-endpoint \
  "spec.group" \
  --json '{
    "spec": {
      "group": {
        "min": 2,
        "max": 2,
        "enable_readable_secondaries": true
      }
    }
  }'

curl

curl -X PATCH "$DATABRICKS_HOST/api/2.0/postgres/projects/my-project/branches/production/endpoints/my-endpoint?update_mask=spec.group" \
  -H "Authorization: Bearer $DATABRICKS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "projects/my-project/branches/production/endpoints/my-endpoint",
    "spec": {
      "group": {
        "min": 2,
        "max": 2,
        "enable_readable_secondaries": true
      }
    }
  }' | jq

配置对辅助计算实例的只读访问权限

是否允许对只读计算实例的访问控制辅助计算实例是否通过 -ro connection string 来提供读取流量。

UI

  1. 在“ 计算 ”选项卡上,单击主计算上的 “编辑 ”。
  2. “高可用性”下,选中或取消选中 “允许访问只读计算实例”。
  3. 单击“ 保存”。

Python SDK

from databricks.sdk import WorkspaceClient
from databricks.sdk.service.postgres import (
    Endpoint, EndpointSpec, EndpointType, EndpointGroupSpec, FieldMask
)

w = WorkspaceClient()

endpoint_name = "projects/my-project/branches/production/endpoints/my-endpoint"

# Get current group size first
current = w.postgres.get_endpoint(name=endpoint_name)
current_size = current.status.group.max

w.postgres.update_endpoint(
    name=endpoint_name,
    endpoint=Endpoint(
        name=endpoint_name,
        spec=EndpointSpec(
            endpoint_type=EndpointType.ENDPOINT_TYPE_READ_WRITE,
            group=EndpointGroupSpec(
                min=current_size,
                max=current_size,
                enable_readable_secondaries=True  # set False to disable
            )
        )
    ),
    update_mask=FieldMask(field_mask=["spec.group.enable_readable_secondaries"])
).wait()

CLI

# Replace 2 with your current group size
databricks postgres update-endpoint \
  projects/my-project/branches/production/endpoints/my-endpoint \
  "spec.group.enable_readable_secondaries" \
  --json '{
    "spec": {
      "group": {
        "min": 2,
        "max": 2,
        "enable_readable_secondaries": true
      }
    }
  }'

curl

# Replace 2 with your current group size
curl -X PATCH "$DATABRICKS_HOST/api/2.0/postgres/projects/my-project/branches/production/endpoints/my-endpoint?update_mask=spec.group.enable_readable_secondaries" \
  -H "Authorization: Bearer $DATABRICKS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "projects/my-project/branches/production/endpoints/my-endpoint",
    "spec": {
      "group": {
        "min": 2,
        "max": 2,
        "enable_readable_secondaries": true
      }
    }
  }' | jq

警告

只有one个辅助计算实例,并且读取访问已启用的情况下,在故障转移期间,连接字符串-ro上的所有读取流量都会中断,直到添加替代实例。 对于可靠的读取访问,请配置两个或多个启用了读取访问权限的从属计算实例。

更改辅助计算实例数

UI

  1. 在“ 计算 ”选项卡上,单击主计算上的 “编辑 ”。
  2. “高可用性”下,从下拉列表中选择新的 计算配置234 个计算实例总数)。
  3. 单击“ 保存”。

注释

若要禁用高可用性,请将 计算类型 设置回 单个计算。 这会删除所有辅助计算实例,终结点将返回到单计算配置。

Python SDK

from databricks.sdk import WorkspaceClient
from databricks.sdk.service.postgres import (
    Endpoint, EndpointSpec, EndpointType, EndpointGroupSpec, FieldMask
)

w = WorkspaceClient()

endpoint_name = "projects/my-project/branches/production/endpoints/my-endpoint"

# Scale to 3 compute instances (1 primary + 2 secondaries)
w.postgres.update_endpoint(
    name=endpoint_name,
    endpoint=Endpoint(
        name=endpoint_name,
        spec=EndpointSpec(
            endpoint_type=EndpointType.ENDPOINT_TYPE_READ_WRITE,
            group=EndpointGroupSpec(min=3, max=3)
        )
    ),
    update_mask=FieldMask(field_mask=["spec.group.min", "spec.group.max"])
).wait()

CLI

# Scale to 3 compute instances (1 primary + 2 secondaries)
databricks postgres update-endpoint \
  projects/my-project/branches/production/endpoints/my-endpoint \
  "spec.group.min,spec.group.max" \
  --json '{
    "spec": {
      "group": { "min": 3, "max": 3 }
    }
  }'

curl

curl -X PATCH "$DATABRICKS_HOST/api/2.0/postgres/projects/my-project/branches/production/endpoints/my-endpoint?update_mask=spec.group.min,spec.group.max" \
  -H "Authorization: Bearer $DATABRICKS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "projects/my-project/branches/production/endpoints/my-endpoint",
    "spec": {
      "group": { "min": 3, "max": 3 }
    }
  }' | jq

查看高可用性状态和角色

计算 ”选项卡显示高可用性配置中的每个计算实例及其当前角色、状态和访问级别。

价值观
Role 主数据库、辅助数据库
状态 Starting、Active
Access 读/写(主)、只读(启用了访问权限的辅助计算实例)、禁用(没有读取访问权限的辅助计算实例)

主计算标头还显示终结点 ID、自动缩放范围和辅助计数(例如 8 ↔ 16 CU · 3 secondaries)。

获取连接字符串

UI

单击主计算上的 “连接 ”以打开“连接详细信息”对话框。 “ 计算 ”下拉列表列出了高可用性终结点的两个连接选项。

连接详细信息对话框,其中显示“计算功能”下拉列表,包含“主要”和“次要只读”选项,并显示只读连接字符串

计算选项 连接字符串 用于
Primary (name) ● Active {endpoint-id}.database.{region}.databricks.com 所有写入操作和读/写连接
Secondary (name) ● Active RO {endpoint-id}-ro.database.{region}.databricks.com 将读取任务卸载到次级计算实例

仅当启用了-ro时, connection string才可用。

Python SDK

from databricks.sdk import WorkspaceClient

w = WorkspaceClient()

endpoint = w.postgres.get_endpoint(
    name="projects/my-project/branches/production/endpoints/my-endpoint"
)

print(f"Read/write host: {endpoint.status.hosts.host}")
print(f"Read-only host:  {endpoint.status.hosts.read_only_host}")

CLI

databricks postgres get-endpoint \
  projects/my-project/branches/production/endpoints/my-endpoint \
  -o json | jq '{rw_host: .status.hosts.host, ro_host: .status.hosts.read_only_host}'

curl

curl -X GET "$DATABRICKS_HOST/api/2.0/postgres/projects/my-project/branches/production/endpoints/my-endpoint" \
  -H "Authorization: Bearer $DATABRICKS_TOKEN" \
  | jq '{rw_host: .status.hosts.host, ro_host: .status.hosts.read_only_host}'

有关完整的连接字符串参考,请参阅 连接字符串

后续步骤