Create an HBase cluster with Terraform

更新时间:
复制 MD 格式

Use Terraform as a RAM user to create an ApsaraDB for HBase cluster.

Note

The sample code is ready to run. Run Now

Prerequisites

  • An Alibaba Cloud account has full permissions for all resources, so compromised credentials pose a significant security risk. Create a RAM user and an AccessKey pair. Create a RAM user and Create an AccessKey.

  • Grant the RAM user the AliyunHBaseFullAccess (to manage ApsaraDB for HBase) and AliyunVPCFullAccess (to manage Virtual Private Cloud) permissions. The following is a sample policy. Manage RAM user permissions.

    {
        "Version": "1",
        "Statement": [
            {
                "Action": [
                    "vpc:CreateVpc",
                    "vpc:DescribeVpc",
                    "vpc:ModifyVpcAttribute",
                    "vpc:DeleteVpc",
                    "vpc:CreateVSwitch",
                    "vpc:DescribeVSwitch",
                    "vpc:ModifyVSwitchAttribute",
                    "vpc:DeleteVSwitch"
                ],
                "Resource": "*",
                "Effect": "Allow"
            },
            {
                "Action": "hbase:*",
                "Resource": "*",
                "Effect": "Allow"
            }
        ]
    }
    
  • Prepare a Terraform runtime environment using one of the following methods:

Important

Terraform v0.12.28 or later is required. Run terraform --version to verify.

Note

Some resources in this tutorial incur charges. Release them promptly when no longer needed.

Resources

Create an HBase cluster with Terraform

  1. Create a working directory and a configuration file named main.tf. main.tf is the main Terraform file that defines the resources to deploy.

    variable "region" {
      default = "cn-qingdao"
    }
    provider "alicloud" {
      region = var.region
    }
    data "alicloud_hbase_zones" "default" {}
    variable "instance_type" {
      default = "hbase.sn2.large"
    }
    variable "name" {
      default = "hbasetest"
    }
    # Create an alicloud_vpc resource.
    resource "alicloud_vpc" "vpc1" {
      vpc_name   = var.name
      cidr_block = "172.16.0.0/12"
    }
    # Create an alicloud_vswitch resource within the VPC and specify a zone.
    resource "alicloud_vswitch" "default" {
      vswitch_name = var.name
      cidr_block   = "172.16.20.0/24"
      vpc_id       = alicloud_vpc.vpc1.id
      zone_id      = data.alicloud_hbase_zones.default.zones[0].id
    }
    resource "alicloud_hbase_instance" "default" {
      # (Required) The name of the HBase instance. The name must be 2 to 128 characters in length and can contain Chinese characters, letters, digits, periods (.), underscores (_), and hyphens (-).
      name        = var.name
      #  (Optional, ForceNew) The zone in which to launch the HBase instance.
      zone_id                = data.alicloud_hbase_zones.default.zones[0].id
      # (Optional, ForceNew) The ID of the VSwitch. 
      vswitch_id             = alicloud_vswitch.default.id
      #  (Optional, ForceNew, Available from v1.185.0) The ID of the VPC.
      vpc_id                 = alicloud_vpc.vpc1.id
      #  (Optional, ForceNew) The engine of the instance. Valid values: "hbase", "hbaseue", "bds". "hbaseue" and "bds" are supported from v1.73.0. A single-node HBase instance requires `engine="hbase"` and `core_instance_quantity=1`.
      engine                 = "hbaseue"
      # (Required, ForceNew) The major engine version. Valid values for different engines: hbase (1.1, 2.0), hbaseue (2.0), bds (1.0).
      engine_version         = "2.0"
      # (Required) The instance type of the master nodes.
      master_instance_type   = var.instance_type
      # (Required) The instance type of the core nodes.
      core_instance_type     = var.instance_type
      # (Optional) The number of core nodes. Default value: 2. Valid values: 1 to 200. An instance with more than one core node is a cluster. An instance with one core node is a single-node instance.
      core_instance_quantity = "2"
      #  (Optional, ForceNew) The disk type of the core nodes. Valid values: cloud_ssd, cloud_essd_pl1, cloud_efficiency, local_hdd_pro, and local_ssd_pro. The `local_disk` has a fixed size. If `engine` is set to `bds`, this parameter is not required.
      core_disk_type         = "cloud_ssd"
      # (Optional) The storage capacity of each core node, in GB. This parameter is valid only when `engine` is "hbase" or "hbaseue". For the `bds` engine, `core_disk_size` is not required. Value ranges: cluster [400, 64000] in 40 GB increments; single-node [20, 500] in 1 GB increments.
      core_disk_size         = 400
      #  (Optional) The billing method. Valid values: PrePaid and PostPaid. Default value: PostPaid. You can change the billing method from PostPaid to PrePaid. From v1.115.0+, you can also change from PrePaid to PostPaid.
      pay_type               = "PostPaid"
      # (Optional) The cold storage capacity in GB. Valid values: 0 or 800 to 1,000,000 in 10 GB increments. A value of 0 indicates that cold storage is disabled. A value greater than 0 indicates that cold storage is enabled.
      cold_storage_size      = 0
      # (Optional, Available from v1.109.0) Specifies whether to delete the instance immediately. If true, the instance is deleted immediately. If false, it is deleted after a retention period.
      immediate_delete_flag  =  true
      # (Optional) The subscription duration in months. This parameter is valid only when `pay_type` is PrePaid. Valid values: 1 to 9, 12, 24, and 36. Values of 12, 24, and 36 correspond to 1, 2, and 3 years, respectively.
      duration               = 1
      # (Optional, Available from v1.73.0) The deletion protection switch. A value of true enables deletion protection. A value of false disables deletion protection. You must set this parameter to false to delete the instance.
      deletion_protection    = false
    }
  2. Initialize Terraform:

    terraform init

    If the output shows successful initialization, proceed to the next step.

    Initializing the backend...
    Initializing provider plugins...
    - Finding latest version of hashicorp/alicloud...
    - Installing hashicorp/alicloud v1.234.0...
    - Installed hashicorp/alicloud v1.234.0 (signed by HashiCorp)
    Terraform has created a lock file .terraform.lock.hcl to record the provider
    selections it made above. Include this file in your version control repository
    so that Terraform can guarantee to make the same selections by default when
    you run "terraform init" in the future.
    Terraform has been successfully initialized!
    You may now begin working with Terraform. Try running "terraform plan" to see
    any changes that are required for your infrastructure. All Terraform commands
    should now work.
    If you ever set or change modules or backend configuration for Terraform,
    rerun this command to reinitialize your working directory. If you forget, other
    commands will detect it and remind you to do so if necessary.
  3. Create an execution plan and preview changes:

    terraform plan
  4. Create the HBase cluster:

    terraform apply

    Enter yes when prompted and press Enter. The 'Apply complete!' message confirms successful cluster creation.

    Plan: 3 to add, 0 to change, 0 to destroy.
    Do you want to perform these actions?
      Terraform will perform the actions described above.
      Only 'yes' will be accepted to approve.
      Enter a value: yes
    alicloud_vpc.vpc1: Creating...
    alicloud_vpc.vpc1: Creation complete after 6s [id=vpc-m5ecf5pojlcjkolx7u***]
    alicloud_vswitch.default: Creating...
    alicloud_vswitch.default: Creation complete after 4s [id=vsw-m5ehryjeb8rs3bm56j***]
    alicloud_hbase_instance.default: Creating...
    alicloud_hbase_instance.default: Still creating... [10s elapsed]
    alicloud_hbase_instance.default: Still creating... [20s elapsed]
    alicloud_hbase_instance.default: Still creating... [30s elapsed]
    alicloud_hbase_instance.default: Still creating... [40s elapsed]
    alicloud_hbase_instance.default: Still creating... [50s elapsed]
    alicloud_hbase_instance.default: Still creating... [1m0s elapsed]
    alicloud_hbase_instance.default: Still creating... [1m10s elapsed]
    alicloud_hbase_instance.default: Still creating... [1m20s elapsed]
    ...
    alicloud_hbase_instance.default: Still creating... [16m30s elapsed]
    alicloud_hbase_instance.default: Still creating... [16m40s elapsed]
    alicloud_hbase_instance.default: Still creating... [16m50s elapsed]
    alicloud_hbase_instance.default: Creation complete after 16m57s [id=ld-m5ezvi1qi8tkbu***]
    Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
  5. Verify the result.

    Terraform show

    View the details of resources created by Terraform:

    terraform show
    shell@Alicloud:~/ens/Hbase$ terraform show
    # alicloud_hbase_instance.default:
    resource "alicloud_hbase_instance" "default" {
        auto_renew              = false
        cold_storage_size       = 0
        core_disk_size          = 400
        core_disk_type          = "cloud_ssd"
        core_instance_quantity  = 2
        core_instance_type      = "hbase.sn2.large"
        deletion_protection     = false
        engine                  = "hbaseue"
        engine_version          = "2.0"
        id                      = "xxx"
        immediate_delete_flag   = true
        ip_white                = "127.0.0.1"
        maintain_end_time       = "22:00Z"
        maintain_start_time     = "18:00Z"
        master_instance_quantity = 2
        master_instance_type    = "hbase.sn2.large"
        name                    = "hbasetest"
        pay_type                = "PostPaid"
        security_groups         = []
        slb_conn_addrs          = []
    }

    Log on to the console

    Log on to the ApsaraDB for HBase console to verify. The cluster list shows the hbasetest cluster with service/version HBase enhanced edition (Lindorm)/2.0, status running, billing method pay-as-you-go, and network type Virtual Private Cloud.

Clean up resources

Run the following command to destroy unneeded resources. Common commands.

terraform destroy

Complete example

Note

The sample code is ready to run. Run Now

Sample code

variable "region" {
  default = "cn-qingdao"
}
provider "alicloud" {
  region = var.region
}
data "alicloud_hbase_zones" "default" {}
variable "instance_type" {
  default = "hbase.sn2.large"
}
variable "name" {
  default = "hbasetest"
}
# Create an alicloud_vpc resource.
resource "alicloud_vpc" "vpc1" {
  vpc_name   = var.name
  cidr_block = "172.16.0.0/12"
}
# Create an alicloud_vswitch resource within the VPC and specify a zone.
resource "alicloud_vswitch" "default" {
  vswitch_name = var.name
  cidr_block   = "172.16.20.0/24"
  vpc_id       = alicloud_vpc.vpc1.id
  zone_id      = data.alicloud_hbase_zones.default.zones[0].id
}
resource "alicloud_hbase_instance" "default" {
  timeouts {
    create = "100m" # Set a timeout for the creation.
  }
  # (Required) The name of the HBase instance. The name must be 2 to 128 characters in length and can contain Chinese characters, letters, digits, periods (.), underscores (_), and hyphens (-).
  name        = var.name
  #  (Optional, ForceNew) The zone in which to launch the HBase instance.
  zone_id                = data.alicloud_hbase_zones.default.zones[0].id
  # (Optional, ForceNew) The ID of the VSwitch. 
  vswitch_id             = alicloud_vswitch.default.id
  #  (Optional, ForceNew, Available from v1.185.0) The ID of the VPC.
  vpc_id                 = alicloud_vpc.vpc1.id
  #  (Optional, ForceNew) The engine of the instance. Valid values: "hbase", "hbaseue", "bds". "hbaseue" and "bds" are supported from v1.73.0. A single-node HBase instance requires `engine="hbase"` and `core_instance_quantity=1`.
  engine                 = "hbaseue"
  # (Required, ForceNew) The major engine version. Valid values for different engines: hbase (1.1, 2.0), hbaseue (2.0), bds (1.0).
  engine_version         = "2.0"
  # (Required) The instance type of the master nodes.
  master_instance_type   = var.instance_type
  # (Required) The instance type of the core nodes.
  core_instance_type     = var.instance_type
  # (Optional) The number of core nodes. Default value: 2. Valid values: 1 to 200. An instance with more than one core node is a cluster. An instance with one core node is a single-node instance.
  core_instance_quantity = "2"
  #  (Optional, ForceNew) The disk type of the core nodes. Valid values: cloud_ssd, cloud_essd_pl1, cloud_efficiency, local_hdd_pro, and local_ssd_pro. The `local_disk` has a fixed size. If `engine` is set to `bds`, this parameter is not required.
  core_disk_type         = "cloud_ssd"
  # (Optional) The storage capacity of each core node, in GB. This parameter is valid only when `engine` is "hbase" or "hbaseue". For the `bds` engine, `core_disk_size` is not required. Value ranges: cluster [400, 64000] in 40 GB increments; single-node [20, 500] in 1 GB increments.
  core_disk_size         = 400
  #  (Optional) The billing method. Valid values: PrePaid and PostPaid. Default value: PostPaid. You can change the billing method from PostPaid to PrePaid. From v1.115.0+, you can also change from PrePaid to PostPaid.
  pay_type               = "PostPaid"
  # (Optional) The cold storage capacity in GB. Valid values: 0 or 800 to 1,000,000 in 10 GB increments. A value of 0 indicates that cold storage is disabled. A value greater than 0 indicates that cold storage is enabled.
  cold_storage_size      = 0
  # (Optional, Available from v1.109.0) Specifies whether to delete the instance immediately. If true, the instance is deleted immediately. If false, it is deleted after a retention period.
  immediate_delete_flag  =  true
  # (Optional) The subscription duration in months. This parameter is valid only when `pay_type` is PrePaid. Valid values: 1 to 9, 12, 24, and 36. Values of 12, 24, and 36 correspond to 1, 2, and 3 years, respectively.
  duration               = 1
  # (Optional, Available from v1.73.0) The deletion protection switch. A value of true enables deletion protection. A value of false disables deletion protection. You must set this parameter to false to delete the instance.
  deletion_protection    = false
}

Product-specific examples are available in More complete examples.