管理高可用性

本指南涵蓋如何啟用並管理 Lakebase 端點的高可用性。 關於高可用性運作原理及次級運算實例與獨立讀取副本的差異,請參見 高可用性。

啟用高可用性

要啟用高可用性,請在介面中設定運算類型與 HA 設定,或透過 API 設定端點 EndpointGroupSpec 。

先決條件

  • 必須停用「縮放至零」。 在介面裡,將 Scale 設為關閉,在 Edit 計算抽屜中。 透過 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,
            no_suspension=True,
            group=EndpointGroupSpec(
                min=2,
                max=2,
                enable_readable_secondaries=True
            )
        )
    ),
    update_mask=FieldMask(field_mask=["spec.group", "spec.suspension"])
).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,spec.suspension" \
  --json '{
    "spec": {
      "no_suspension": true,
      "group": {
        "min": 2,
        "max": 2,
        "enable_readable_secondaries": true
      }
    }
  }'

curl (Unix指令)

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

設定對次要計算實例的唯讀存取

允許存取唯讀運算實例控制次要運算實例是否透過-ro 連接字串來處理讀取流量。

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 (Unix指令)

# 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

警告

僅啟用一個次要運算實例並開啟讀取存取時,-ro 連接字串 上的所有讀取流量在故障轉移期間會暫時中斷,直到加入替代的實例。 為了實現穩定的讀取存取,應配置兩個或以上啟用讀取權限的次要運算實例。

變更次要計算實例的數量

UI

  1. 在 「運算」 分頁,點選 編輯 主要運算。
  2. 在 高可用性中,從下拉選單中選擇新的 計算配置 (總計 2、 3 或 4 個運算實例)。
  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 (Unix指令)

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 小學、中學
狀態 啟動中,活躍
Access 讀寫(主要)、僅讀取(已啟用存取權的次要計算實例)、停用(未啟用讀取權的次要計算實例)

主要計算標頭同時顯示端點 ID、自動縮放範圍及次要計數(例如 8 ↔ 16 CU · 3 secondaries)。

取得連接字串

UI

在主要運算器上點選「 連接 」以開啟連接細節對話框。 計算 下拉 選單列出了你高可用性端點的兩個連線選項。

連接詳細資訊對話框顯示「計算」下拉選單已開啟,包含「主存取權限」和「次要唯讀 (RO)」選項,並顯示唯讀的連接字串

計算選項 連接字串 用途
Primary (name) ● Active {endpoint-id}.database.{region}.databricks.com 所有寫入與讀寫連線
Secondary (name) ● Active RO {endpoint-id}-ro.database.{region}.databricks.com 讀取卸載至次要運算實例

-ro 連接字串僅在啟用允許存取唯讀 [運算實例] 時可用。

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 (Unix指令)

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}'

完整連接字串參考資料請參見 Connection strings。

其他資源