建立執行個體

Bigtable 執行個體是 Bigtable 叢集的容器。如果執行個體的叢集不只一個,這表示系統使用複製程序。您最多可以在 8 個區域中建立叢集,每個區域的叢集數量上限為區域數。

本頁說明如何建立執行個體。閱讀本頁之前,請先熟悉 Bigtable 總覽。並詳閱執行個體、叢集和節點的簡介文章

事前準備

準備環境:

  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Cloud Bigtable API, Cloud Bigtable Admin API APIs.

    Enable the APIs

  5. Install the Google Cloud CLI.

  6. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  7. To initialize the gcloud CLI, run the following command:

    gcloud init
  8. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  9. Make sure that billing is enabled for your Google Cloud project.

  10. Enable the Cloud Bigtable API, Cloud Bigtable Admin API APIs.

    Enable the APIs

  11. Install the Google Cloud CLI.

  12. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  13. To initialize the gcloud CLI, run the following command:

    gcloud init
  14. 執行下列指令,安裝 cbt CLI :
    gcloud components install cbt
  15. 規劃設定:

    1. 選用:如要啟用複製功能,請執行下列操作:

    2. 選用:如要使用客戶自行管理的加密金鑰 (CMEK),而非預設的 Google 代管加密,請完成「建立啟用 CMEK 的執行個體」一節中的工作,並在建立新執行個體準備好 CMEK 金鑰 ID。執行個體建立後,您無法為其新增 CMEK 保護機制,也無法修改或替換 CMEK 金鑰。

    建立執行個體

    如要建立 Bigtable 執行個體,請按照下列步驟操作:

    主控台

    1. 前往 Google Cloud 控制台的「建立執行個體」頁面。

      前往「建立執行個體」

    2. 輸入執行個體的名稱。

      Google Cloud 控制台會顯示這個名稱,以識別執行個體。

    3. 輸入執行個體 ID。

      執行個體 ID 是執行個體的永久性識別碼。

    4. 按一下「繼續」

    5. 請選擇您的叢集要使用 SSD 或是 HDD 磁碟。在大多數情況下,SSD 是最好的選擇。決定後即無法更改。瞭解詳情

    6. 按一下「繼續」

    7. 對第一個叢集輸入叢集 ID。

      叢集 ID 是叢集的永久識別碼。

    8. 請選擇第一個叢集執行的地區及區域

    9. 選用:如要將叢集設定為一律以兩個節點為增量進行資源調度,請選取「啟用 2 倍節點資源調度功能」。2 倍節點資源調度功能僅適用於部分可用區。詳情請參閱「節點資源調度係數」。

    10. 為叢集選擇節點資源調度模式。在大多數情況下,建議您選擇自動調整大小。如需資源調度指南,請參閱「自動調度資源」。

      1. 在「手動節點分配」中,輸入第一個叢集的 Bigtable 節點數量。 如果您不確定需要多少個節點,請使用預設值。您之後可以新增更多節點。
      2. 在「Autoscaling」(自動調度資源) 中,輸入下列值:
        • 節點數量下限
        • 節點數量上限
        • CPU 使用率目標
        • 儲存空間使用率目標
    11. 選用:如要使用 CMEK 保護執行個體,而非預設的 Google 代管加密機制,請完成下列步驟:

      1. 按一下「顯示加密選項」
      2. 選取「客戶管理的加密金鑰 (CMEK)」旁邊的圓形按鈕。
      3. 選取或輸入要用於叢集的 CMEK 金鑰資源名稱。選定後即無法變更。
      4. 如果系統提示您授予 CMEK 金鑰服務帳戶權限,請按一下「授予」。您的使用者帳戶必須獲派 Cloud KMS 管理員角色,才能完成這項工作。
      5. 按一下 [儲存]
    12. 選用:如要立即啟用複製功能,請完成下列額外步驟:

      1. 按一下 [Show advanced options] (顯示進階選項)
      2. 按一下「新增叢集」,輸入叢集設定,然後按一下「新增」。重複這個步驟,在執行個體中建立其他叢集。您也可於稍後再透過新增叢集的方式來啟用複製功能。

      地區中的每個區域只能包含一個叢集。 如果「新增叢集」按鈕已停用,請變更第一個叢集的區域。

      如要建立超過六個叢集的執行個體,請先建立六個叢集的執行個體,然後在該執行個體中新增更多叢集。

    13. 按一下「建立」,建立執行個體。

    14. 查看預設應用程式設定檔中的複製功能設定,確認這些設定是否適用於您的複製功能用途。您可能需要更新預設的應用程式設定檔,或是建立自訂應用程式設定檔

    gcloud

    1. 使用 bigtable instances create 指令建立執行個體:

      gcloud bigtable instances create INSTANCE_ID \
          --display-name=DISPLAY_NAME \
          [--cluster-storage-type=CLUSTER_STORAGE_TYPE] \
          [--cluster-config=id=CLUSTER_ID,zone=CLUSTER_ZONE, \
          nodes=NODES] \
          [--cluster-config=id=CLUSTER_ID,zone=CLUSTER_ZONE, \
          autoscaling-min-nodes=AUTOSCALING_MIN_NODES, \
          autoscaling-max-nodes=AUTOSCALING_MAX_NODES, \
          autoscaling-cpu-target=AUTOSCALING_CPU_TARGET, \
          autoscaling-storage-target=AUTOSCALING_STORAGE_TARGET, \
          kms-key=KMS_KEY], \
          [node-scaling-factor=NODE_SCALING_FACTOR]
      

      更改下列內容:

      • INSTANCE_ID: 執行個體的永久 ID。
      • DISPLAY_NAME:使用者可辨識的名稱,能夠用來識別 Google Cloud 控制台中的執行個體。
      • CLUSTER_ID: 叢集的永久 ID。
      • CLUSTER_ZONE: 執行叢集的區域

      您必須使用 --cluster-config 標記,為執行個體設定至少一個叢集。如要建立具有多個叢集的執行個體,請為每個叢集重複使用 --cluster-config 旗標

      如要手動分配節點,可以視需要設定 --cluster-config 旗標中的 nodes。如未設定任何值,Bigtable 會根據資料足跡自動為叢集分配節點,並將儲存空間使用率最佳化為 50%。自動分配節點會影響價格。如要控管叢集中的節點數量,請將 NODES 替換為叢集中所需的節點數量。進一步瞭解節點

      在大多數情況下,請選擇自動調度資源,而非手動節點分配。 如果是自動調度資源,請在 --cluster-config 標記中提供 autoscaling- 選項 (autoscaling-storage-target 為選用),且不要使用 nodes。如需如何為自動調度資源設定選擇值的指引,請參閱自動調度資源。請替換 cluster-config 選項鍵的下列項目:

      • AUTOSCALING_MIN_NODES:叢集的節點數量下限。
      • AUTOSCALING_MAX_NODES:叢集的節點數量上限。
      • AUTOSCALING_CPU_TARGET:叢集的目標 CPU 使用率百分比。這個值必須介於 10 至 80 之間。
      • AUTOSCALING_STORAGE_TARGET:(選用) Bigtable 透過新增或移除節點維持的儲存空間使用率目標 (以 GiB 為單位)。
      • KMS_KEY:叢集的 CMEK 金鑰。

        KMS_KEY 值必須採用下列格式:

        projects/PROJECT/locations/LOCATION/keyRings/KEYRING/cryptoKeys/KEY
        
        

        更改下列內容:

        • PROJECT:專案的永久 ID
        • LOCATION:叢集位置
        • KEYRING:金鑰所屬金鑰環的名稱
        • KEY:金鑰名稱

        範例如下:

        projects/examplestore.com:dev/locations/us-east1/keyRings/devt-cmek-2/cryptoKeys/key2

        如果執行個體受到 CMEK 保護,每個叢集都必須與其 CMEK 金鑰位於相同區域。您只能將 CMEK 叢集新增至已受 CMEK 保護的執行個體。瞭解詳情

      該指令接受下列選用標記:

      • --cluster-storage-type=CLUSTER_STORAGE_TYPE:執行個體使用的儲存空間類型。預設值為 SSD。 在大多數情況下,預設值是最佳選擇。選定後即無法變更。 瞭解詳情

      • --project=PROJECT:如果與目前的專案不同,請指定要建立叢集的專案。

      • node-scaling-factor=NODE_SCALING_FACTOR:啟用節點擴展 2 倍的標記。無論是手動或自動調度資源,都可以啟用這項功能。 可接受的值為 node-scaling-factor-2xnode-scaling-factor-1x

      如要查看不適用於 2 倍節點資源調度的 Bigtable 區域清單,請參閱節點資源調度係數限制

    2. 查看預設應用程式設定檔中的複製功能設定,確認這些設定是否適用於您的複製功能用途。您可能需要更新預設的應用程式設定檔,或是建立自訂應用程式設定檔

    cbt

    1. 首先建立一個單一叢集的執行個體。使用 createinstance 指令建立執行個體:

      cbt createinstance INSTANCE_ID \
          DISPLAY_NAME \
          CLUSTER_ID \
          CLUSTER_ZONE \
          CLUSTER_NUM_NODES \
          CLUSTER_STORAGE_TYPE
      

      請提供下列資訊:

      • INSTANCE_ID: 執行個體的永久 ID。
      • DISPLAY_NAME:使用者可辨識的名稱,能夠用來識別 Google Cloud 控制台中的執行個體。
      • CLUSTER_ID: 叢集的永久 ID。
      • CLUSTER_ZONE: 執行叢集的區域
      • CLUSTER_NUM_NODES:這是選填欄位。如未設定任何值,Bigtable 會根據資料足跡自動分配節點,並將儲存空間使用率最佳化為 50%。如要控管叢集中的節點數,請更新 CLUSTER_NUM_NODES 值。確認節點數量已設為非零值。 進一步瞭解節點
      • CLUSTER_STORAGE_TYPE: 叢集所使用的儲存類型。執行個體中的每一個叢集必須使用相同的儲存空間類型。可接受的值為 SSDHDD。 在大多數情況下,SSD 是最佳選擇。決定後即無法更改。瞭解詳情
    2. 如要啟用複製功能,請使用 createcluster 指令新增叢集:

      
      cbt -instance=INSTANCE_ID \
      createcluster CLUSTER_ID \
      ZONE \
      NUM_NODES \
      STORAGE_TYPE
      
      

      請提供下列資訊:

      • INSTANCE_ID:您剛剛建立的執行個體永久 ID。
      • CLUSTER_ID: 叢集的永久 ID。
      • ZONE: 執行叢集的區域

        地區中的每個區域只能包含一個叢集。 舉例來說,如果執行個體在 us-east1-b 中有叢集,您可以在同一個地區的不同區域 (例如 us-east1-c) 中新增叢集,也可以在不同地區的區域 (例如 europe-west2-a) 中新增叢集。

      • NUM_NODES: 這是選填欄位。如未設定任何值,Bigtable 會根據資料足跡自動分配節點,並將儲存空間使用率最佳化為 50%。如要控管叢集中的節點數,請更新 NUM_NODES 值。確認節點數量已設為非零值。

        在許多情況下,執行個體中的每個叢集應有相同數量的節點,但也有例外情形。瞭解節點及複製功能

      • STORAGE_TYPE: 叢集所使用的儲存類型。執行個體中的每一個叢集必須使用相同的儲存空間類型。可接受的值為 SSDHDD

    3. (選用) 查看預設應用程式設定檔中的複製功能設定,確認這些設定是否適用於您的複製功能用途。您可能需要更新預設的應用程式設定檔,或是建立自訂應用程式設定檔

    C++

    如要瞭解如何安裝及使用 Bigtable 的用戶端程式庫,請參閱這篇文章

    如要向 Bigtable 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。

    namespace cbta = ::google::cloud::bigtable_admin;
    using ::google::cloud::future;
    using ::google::cloud::Location;
    using ::google::cloud::Project;
    using ::google::cloud::StatusOr;
    [](cbta::BigtableInstanceAdminClient instance_admin,
       std::string const& project_id, std::string const& instance_id,
       std::string const& zone) {
      auto const project = Project(project_id);
      std::string project_name = project.FullName();
      std::string cluster_id = instance_id + "-c1";
    
      google::bigtable::admin::v2::Instance in;
      in.set_type(google::bigtable::admin::v2::Instance::PRODUCTION);
      in.set_display_name("Put description here");
    
      google::bigtable::admin::v2::Cluster cluster;
      cluster.set_location(Location(project, zone).FullName());
      cluster.set_serve_nodes(3);
      cluster.set_default_storage_type(google::bigtable::admin::v2::HDD);
    
      std::map<std::string, google::bigtable::admin::v2::Cluster> cluster_map = {
          {cluster_id, std::move(cluster)}};
    
      future<StatusOr<google::bigtable::admin::v2::Instance>> instance_future =
          instance_admin.CreateInstance(project_name, instance_id, std::move(in),
                                        std::move(cluster_map));
      // Show how to perform additional work while the long running operation
      // completes. The application could use future.then() instead.
      std::cout << "Waiting for instance creation to complete " << std::flush;
      instance_future.wait_for(std::chrono::seconds(1));
      std::cout << '.' << std::flush;
      auto instance = instance_future.get();
      if (!instance) throw std::move(instance).status();
      std::cout << "DONE, details=" << instance->DebugString() << "\n";
    }

    C#

    如要瞭解如何安裝及使用 Bigtable 的用戶端程式庫,請參閱這篇文章

    如要向 Bigtable 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。

    // Creates a production instance with "<intanceId>-prod" instance ID,
    // with cluster ID "ssd-cluster1", 3 nodes and location us-east1-b.
    displayName += " Prod"; // Display name is for display purposes only. It doesn't have to equal instanceId and can be amended after instance is created.
    string instanceId = Regex.Replace(displayName, @"[^A-Za-z0-9_\.~]+", "-").ToLower();
    
    // Please refer to the link below for the full list of available locations:
    // https://cloud.google.com/bigtable/docs/locations
    string zone1 = "us-east1-b";
    
    // The instance to create.
    Instance myInstance = new Instance
    {
        DisplayName = displayName,
        // You can choose DEVELOPMENT or PRODUCTION type here.
        // If not set, will default to PRODUCTION type.
        // Instance type can be upgraded from DEVELOPMENT to PRODUCTION but cannot be dowgraded after the instance is created.
        Type = Instance.Types.Type.Production,
        Labels = { { "prod-label", "prod-label" } }
    };
    
    // The first cluster to be created within the instance.
    Cluster myCluster1 = new Cluster
    {
        // You can choose SSD or HDD storage type here: StorageType.Ssd or StorageType.Hdd.
        // Cluster storage type can not be changed after the instance is created.
        // If not set will default to SSD type.
        DefaultStorageType = StorageType.Ssd,
        LocationAsLocationName = new LocationName(projectId, zone1),
        // Serve Nodes count can only be set if PRODUCTION type instance is being created.
        // Minimum count of 3 serve nodes must be specified.
        // Serve Nodes count can be increased and decreased after an instance is created.
        ServeNodes = 3
    };
    
    // Initialize request argument(s).
    CreateInstanceRequest request = new CreateInstanceRequest
    {
        ParentAsProjectName = new ProjectName(projectId),
        Instance = myInstance,
        InstanceId = instanceId,
        // Must specify at lease one cluster.
        // Only PRODUCTION type instance can be created with more than one cluster.
        // Currently all clusters must have the same storage type.
        // Clusters must be set to different locations.
        Clusters = { { "ssd-cluster1", myCluster1 } }
    };
    
    try
    {
        // Make a request.
        Operation<Instance, CreateInstanceMetadata> createInstanceResponse =
            bigtableInstanceAdminClient.CreateInstance(request);
        Console.WriteLine("Waiting for operation to complete...");
    
        // Poll until the returned long-running operation is complete
        Operation<Instance, CreateInstanceMetadata> completedResponse =
            createInstanceResponse.PollUntilCompleted();
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Exception while creating {displayName} instance");
        Console.WriteLine(ex.Message);
    }

    Java

    如要瞭解如何安裝及使用 Bigtable 的用戶端程式庫,請參閱這篇文章

    如要向 Bigtable 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。

    // Creates a Production Instance with the ID "ssd-instance",
    // cluster id "ssd-cluster", 3 nodes and location "us-central1-f".
    CreateInstanceRequest createInstanceRequest =
        CreateInstanceRequest.of(instanceId)
            .addCluster(clusterId, "us-central1-f", 3, StorageType.SSD)
            .setType(Instance.Type.PRODUCTION)
            .addLabel("department", "accounting");
    // Creates a production instance with the given request.
    try {
      Instance instance = adminClient.createInstance(createInstanceRequest);
      System.out.printf("PRODUCTION type instance %s created successfully%n", instance.getId());
    } catch (Exception e) {
      System.err.println("Failed to create instance: " + e.getMessage());
      throw e;
    }

    Node.js

    如要瞭解如何安裝及使用 Bigtable 的用戶端程式庫,請參閱這篇文章

    如要向 Bigtable 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。

    // Creates a Production Instance with the ID "ssd-instance"
    // with cluster id "ssd-cluster", 3 nodes and location us-central1-f
    
    const instanceOptions = {
      clusters: [
        {
          id: clusterID,
          nodes: 3,
          location: 'us-central1-f',
          storage: 'ssd',
        },
      ],
      type: 'PRODUCTION', // Optional as default type is PRODUCTION
      labels: {'prod-label': 'prod-label'},
    };
    
    // Create production instance with given options
    const [prodInstance, operation] = await instance.create(instanceOptions);
    await operation.promise();
    console.log(`Created Instance: ${prodInstance.id}`);

    PHP

    如要瞭解如何安裝及使用 Bigtable 的用戶端程式庫,請參閱這篇文章

    如要向 Bigtable 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。

    use Exception;
    use Google\ApiCore\ApiException;
    use Google\Cloud\Bigtable\Admin\V2\Client\BigtableInstanceAdminClient;
    use Google\Cloud\Bigtable\Admin\V2\Cluster;
    use Google\Cloud\Bigtable\Admin\V2\CreateInstanceRequest;
    use Google\Cloud\Bigtable\Admin\V2\GetInstanceRequest;
    use Google\Cloud\Bigtable\Admin\V2\Instance;
    use Google\Cloud\Bigtable\Admin\V2\Instance\Type as InstanceType;
    use Google\Cloud\Bigtable\Admin\V2\StorageType;
    
    /**
     * Create a production Bigtable instance
     *
     * @param string $projectId The Google Cloud project ID
     * @param string $instanceId The ID of the Bigtable instance to be generated
     * @param string $clusterId The ID of the cluster to be generated
     * @param string $locationId The Bigtable region ID where you want your instance to reside
     */
    function create_production_instance(
        string $projectId,
        string $instanceId,
        string $clusterId,
        string $locationId = 'us-east1-b'
    ): void {
        $instanceAdminClient = new BigtableInstanceAdminClient();
    
        $projectName = $instanceAdminClient->projectName($projectId);
        $instanceName = $instanceAdminClient->instanceName($projectId, $instanceId);
    
        $serveNodes = 3;
        $storageType = StorageType::SSD;
        $production = InstanceType::PRODUCTION;
        $labels = ['prod-label' => 'prod-label'];
    
        $instance = new Instance();
        $instance->setDisplayName($instanceId);
    
        $instance->setLabels($labels);
        $instance->setType($production);
    
        $cluster = new Cluster();
        $cluster->setDefaultStorageType($storageType);
        $locationName = $instanceAdminClient->locationName($projectId, $locationId);
        $cluster->setLocation($locationName);
        $cluster->setServeNodes($serveNodes);
        $clusters = [
            $clusterId => $cluster
        ];
        try {
            $getInstanceRequest = (new GetInstanceRequest())
                ->setName($instanceName);
            $instanceAdminClient->getInstance($getInstanceRequest);
            printf('Instance %s already exists.' . PHP_EOL, $instanceId);
            throw new Exception(sprintf('Instance %s already exists.' . PHP_EOL, $instanceId));
        } catch (ApiException $e) {
            if ($e->getStatus() === 'NOT_FOUND') {
                printf('Creating an Instance: %s' . PHP_EOL, $instanceId);
                $createInstanceRequest = (new CreateInstanceRequest())
                    ->setParent($projectName)
                    ->setInstanceId($instanceId)
                    ->setInstance($instance)
                    ->setClusters($clusters);
                $operationResponse = $instanceAdminClient->createInstance($createInstanceRequest);
                $operationResponse->pollUntilComplete();
                if (!$operationResponse->operationSucceeded()) {
                    print('Error: ' . $operationResponse->getError()->getMessage());
                } else {
                    printf('Instance %s created.', $instanceId);
                }
            } else {
                throw $e;
            }
        }
    }

    Python

    如要瞭解如何安裝及使用 Bigtable 的用戶端程式庫,請參閱這篇文章

    如要向 Bigtable 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。

    cluster = instance.cluster(
        cluster_id,
        location_id=location_id,
        serve_nodes=serve_nodes,
        default_storage_type=storage_type,
    )
    if not instance.exists():
        print("\nCreating an instance")
        # Create instance with given options
        operation = instance.create(clusters=[cluster])
        # Ensure the operation completes.
        operation.result(timeout=480)
        print("\nCreated instance: {}".format(instance_id))

    Ruby

    如要瞭解如何安裝及使用 Bigtable 的用戶端程式庫,請參閱這篇文章

    如要向 Bigtable 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。

    # instance_id      = "my-instance"
    # cluster_id       = "my-cluster"
    # cluster_location = "us-east1-b"
    puts "Creating a PRODUCTION Instance"
    job = bigtable.create_instance(
      instance_id,
      display_name: "Sample production instance",
      labels:       { "env": "production" },
      type:         :PRODUCTION # Optional as default type is :PRODUCTION
    ) do |clusters|
      clusters.add cluster_id, cluster_location, nodes: 3, storage_type: :SSD
    end
    
    job.wait_until_done!
    instance = job.instance
    puts "Created Instance: #{instance.instance_id}"

    後續步驟