创建实例

Bigtable 实例是 Bigtable 集群的容器。具有多个集群的实例使用复制功能。您最多可以在 8 个区域中创建集群,每个区域中的集群数量与可用区数量相同。

本页面介绍如何创建实例。 在阅读本页内容之前,您应先熟悉 Bigtable 概览。此外,还应阅读实例、集群和节点概览

准备工作

准备环境:

  1. 登录您的 Google 账号。

    如果您还没有 Google 账号,请注册新账号

  2. 在 Google Cloud Console 中的项目选择器页面上,选择或创建一个 Google Cloud 项目

    转到“项目选择器”

  3. 确保您的 Google Cloud 项目已启用结算功能

  4. 启用 Cloud Bigtable API, Cloud Bigtable Admin API API。

    启用 API

  5. 安装 Google Cloud CLI。
  6. 如需初始化 gcloud CLI,请运行以下命令:

    gcloud init
  7. 在 Google Cloud Console 中的项目选择器页面上,选择或创建一个 Google Cloud 项目

    转到“项目选择器”

  8. 确保您的 Google Cloud 项目已启用结算功能

  9. 启用 Cloud Bigtable API, Cloud Bigtable Admin API API。

    启用 API

  10. 安装 Google Cloud CLI。
  11. 如需初始化 gcloud CLI,请运行以下命令:

    gcloud init
  12. 运行以下命令以安装 cbt CLI:
    gcloud components install cbt

规划配置:

  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. 为集群选择节点扩缩模式。如需获取扩缩指南,请参阅自动扩缩

    1. 手动节点分配部分,输入第一个集群的 Bigtable 节点数量。 如果您不确定需要多少个节点,请使用默认值。您可以稍后添加更多节点。
    2. 对于自动扩缩,请输入以下内容的值:
      • 节点数下限
      • 节点数上限
      • CPU 利用率目标
      • 存储空间利用率目标
  10. (可选)如需使用 CMEK 而非默认的 Google 管理的加密来保护您的实例,请完成以下操作:

    1. 点击显示加密选项
    2. 选择客户管理的加密密钥 (CMEK) 旁边的单选按钮。
    3. 选择或输入要用于集群的 CMEK 密钥的资源名称。您无法在之后添加。
    4. 如果系统提示您向 CMEK 密钥的服务账号授予权限,请点击授予。您的用户账号必须获得 Cloud KMS Admin 角色才能完成此任务。
    5. 点击保存
  11. (可选)如需立即启用复制功能,请完成以下额外步骤:

    1. 点击显示高级选项
    2. 点击添加集群,输入集群的设置,然后点击添加。重复此步骤,在实例中创建其他集群。您也可以稍后通过添加集群来启用复制功能。

    一个区域中的每个地区只能包含一个集群。 如果添加集群按钮不可用,请更改第一个集群所在的地区。

    要创建包含六个以上集群的实例,请先创建包含六个集群的实例,然后向该实例添加更多集群。

  12. 点击创建以创建实例。

  13. 查看默认应用配置文件中的复制功能设置,确认这些设置是否适用于您的复制功能使用场景。您可能需要更新默认应用配置文件创建自定义应用配置文件

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]
    

    替换以下内容:

    • INSTANCE_ID:实例的永久性标识符。
    • DISPLAY_NAME:在 Google Cloud 控制台中用于识别实例的简单易懂的名称。
    • CLUSTER_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:项目的永久标识符
      • 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:要在其中创建集群的项目(如果与当前项目不同)。

  2. 查看默认应用配置文件中的复制功能设置,确认这些设置是否适用于您的复制功能使用场景。您可能需要更新默认应用配置文件创建自定义应用配置文件

cbt

  1. 首先创建一个单集群实例。使用 createinstance 命令创建一个实例:

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

    提供以下信息:

    • INSTANCE_ID:实例的永久性标识符。
    • DISPLAY_NAME:在 Google Cloud 控制台中用于识别实例的简单易懂的名称。
    • CLUSTER_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:您刚刚创建的实例的永久性标识符。
    • CLUSTER_ID:集群的永久性标识符。
    • ZONE:运行集群的地区

      一个区域中的每个地区只能包含一个集群。例如,如果实例在 us-east1-b 中有一个集群,则您可以在同一区域中的不同可用区(例如 us-east1-c)或一个单独区域中的一个可用区(例如 europe-west2-a)中添加一个集群。

    • NUM_NODES:此字段是可选字段。如果未设置值,则 Bigtable 会根据您的数据足迹自动分配节点,并针对 50% 的存储空间利用率进行优化。如果要控制集群中的节点数量,请更新 NUM_NODES 值。确保将节点数设置为非零值。

      在许多情况下,实例中每个集群的节点数量应当相同,但也存在例外情况。了解节点和复制功能

    • STORAGE_TYPE:要供集群使用的存储空间类型。实例中的每个集群都必须使用相同类型的存储空间。接受的值为 SSDHDD

  3. (可选)查看默认应用配置文件中的复制功能设置,确认这些设置是否适用于您的复制功能使用场景。您可能需要更新默认应用配置文件创建自定义应用配置文件

C++

如需了解如何安装和使用 Bigtable 的客户端库,请参阅 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 客户端库

如需向 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 客户端库

如需向 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 客户端库

如需向 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 客户端库

如需向 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 客户端库

如需向 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 客户端库

如需向 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}"

后续步骤