使用Terraform管理实例

Terraform的alicloud_alikafka_instance资源提供一系列参数来管理Kafka实例,使用Terraform自动创建、部署实例可以缩短部署时间,保证多个环境的配置一致性,减少人为操作,降低引入错误的概率。

前提条件

  • 安装Terraform。支持Terraform 0.13及以上版本。

  • 配置阿里云账号信息。

    选择一种阿里云认证方式,为Terraform的执行提供认证信息。本文以环境变量认证方式为例:

    export ALICLOUD_ACCESS_KEY="************"
    export ALICLOUD_SECRET_KEY="************"
    export ALICLOUD_REGION="cn-hangzhou"
    说明

    为保障数据安全性,建议您按需为RAM用户授予Kafka资源的操作权限。具体操作,请参见RAM主子账号授权

创建并部署实例

说明

目前不支持通过Terraform删除预付费实例。

本示例以在华东1(杭州)地域下创建流量规格为alikafka.hw.2xlarge的VPC Kafka实例为例,实例名称为alikafkaInstanceName

  1. 创建一个用于存放Terraform资源的项目文件夹,命名为terraform。

  2. 执行以下命令,进入项目目录。

    cd terraform
  3. 执行以下命令,创建名为instance.tf的配置文件。

    variable "instance_name" {
      default = "alikafkaInstanceName"
    }
    # 创建VPC。
    resource "alicloud_vpc" "default" {
      cidr_block = "172.16.0.0/12"
    }
    # 创建vSwitch。
    resource "alicloud_vswitch" "default" {
      vpc_id     = alicloud_vpc.default.id
      cidr_block = "172.16.0.0/24"
      zone_id    = "cn-hangzhou-b"
    }
    # 创建安全组。
    resource "alicloud_security_group" "default" {
      vpc_id = alicloud_vpc.default.id
    }
    # 创建实例,磁盘类型为高效云盘,磁盘容量为500 GB,流量规格为alikafka.hw.2xlarge。
    # 部署实例。
    resource "alicloud_alikafka_instance" "default" {
      name           = var.instance_name
      partition_num  = 50
      disk_type      = 0
      disk_size      = 500
      deploy_type    = 5
      io_max_spec    = "alikafka.hw.2xlarge"
      vswitch_id     = alicloud_vswitch.default.id
      security_group = alicloud_security_group.default.id
    }
    
  4. 执行以下命令,初始化Terraform运行环境。

    terraform init

    预期输出:

    Initializing provider plugins...
    - Reusing previous version of hashicorp/alicloud from the dependency lock file
    - Using previously-installed hashicorp/alicloud v1.211.2
    
    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.
  5. 执行以下命令,创建VPC、VSwitch、安全组、购买实例并部署。

    terraform apply

    预期输出:

    ...
    
    Plan: 4 to add, 0 to change, 0 to destroy.
    
    ...
    
    alicloud_alikafka_instance.default: Destroying... [id=alikafka_post-cn-9lb3gvt0****]
    alicloud_alikafka_instance.default: Still destroying... [id=alikafka_post-cn-9lb3gvt0****, 10s elapsed]
    alicloud_alikafka_instance.default: Destruction complete after 11s
    alicloud_security_group.default: Destroying... [id=sg-uf6dgidvn6lst04v****]
    alicloud_vswitch.vsw: Destroying... [id=vsw-uf69ozfmskryxm60t****]
    alicloud_security_group.default: Destruction complete after 1s
    alicloud_vpc.default: Creating...
    alicloud_vswitch.vsw: Destruction complete after 4s
    alicloud_vpc.vpc: Destroying... [id=vpc-uf6358fviz5jczeo3****]
    alicloud_vpc.vpc: Destruction complete after 1s
    alicloud_vpc.default: Creation complete after 4s [id=vpc-uf6q7z8yvogdaclq0****]
    alicloud_security_group.default: Creating...
    alicloud_vswitch.default: Creating...
    alicloud_security_group.default: Creation complete after 1s [id=sg-uf6gipzruu9zfpay****]
    alicloud_vswitch.default: Creation complete after 4s [id=vsw-uf6m08al38ngv3ozb****]
    alicloud_alikafka_instance.default: Creating...
    alicloud_alikafka_instance.default: Still creating... [10s elapsed]
    ...
    alicloud_alikafka_instance.default: Creation complete after 3m42s [id=alikafka_post-cn-uqm3gvt9****]
    
    Apply complete! Resources: 4 added, 0 changed, 0 destroyed.

    登录控制台查看实例已创建,且状态服务中,说明实例已成功创建。

删除实例

  1. 在目标项目目录内执行以下命令,运行配置文件。

    terraform destroy

    预期输出:

    alicloud_vpc.default: Refreshing state... [id=vpc-uf6q7z8yvogdaclq0****]
    alicloud_security_group.default: Refreshing state... [id=sg-uf6gipzruu9zfpay****]
    alicloud_vswitch.default: Refreshing state... [id=vsw-uf6m08al38ngv3ozb****]
    alicloud_alikafka_instance.default: Refreshing state... [id=alikafka_post-cn-uqm3gvt9****]
    
    Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
      - destroy
    
    Terraform will perform the following actions:
    
      # alicloud_alikafka_instance.default will be destroyed
      - resource "alicloud_alikafka_instance" "default" {
          - config          = jsonencode(
                {
                  - "enable.compact"                  = "true"
                  - "kafka.log.retention.hours"       = "72"
                  - "kafka.message.max.bytes"         = "1048576"
                  - "kafka.offsets.retention.minutes" = "10080"
                  - "kafka.ssl.bit"                   = "1024"
                }
            ) -> null
          - deploy_type     = 5 -> null
          - disk_size       = 500 -> null
          - disk_type       = 0 -> null
          - eip_max         = 0 -> null
          - end_point       = "172.16.XX.XX:9092,172.16.XX.XX:9092,172.16.XX.XX:9092" -> null
          - id              = "alikafka_post-cn-uqm3gvt9****" -> null
          - io_max          = 20 -> null
          - io_max_spec     = "alikafka.hw.2xlarge" -> null
          - name            = "alikafkaInstanceName" -> null
          - paid_type       = "PostPaid" -> null
          - partition_num   = 50 -> null
          - security_group  = "sg-uf6gipzruu9zfpay****" -> null
          - service_version = "0.10.2" -> null
          - spec_type       = "normal" -> null
          - status          = 5 -> null
          - tags            = {} -> null
          - topic_quota     = 1050 -> null
          - vpc_id          = "vpc-uf6q7z8yvogdaclq0****" -> null
          - vswitch_id      = "vsw-uf6m08al38ngv3ozb****" -> null
          - zone_id         = "zoneb" -> null
        }
    
      # alicloud_security_group.default will be destroyed
      - resource "alicloud_security_group" "default" {
          - id                  = "sg-uf6gipzruu9zfpay****" -> null
          - inner_access        = true -> null
          - inner_access_policy = "Accept" -> null
          - security_group_type = "normal" -> null
          - tags                = {} -> null
          - vpc_id              = "vpc-uf6q7z8yvogdaclq0****" -> null
        }
    
      # alicloud_vpc.default will be destroyed
      - resource "alicloud_vpc" "default" {
          - cidr_block            = "172.16.0.0/12" -> null
          - classic_link_enabled  = false -> null
          - create_time           = "2023-11-06T09:43:42Z" -> null
          - id                    = "vpc-uf6q7z8yvogdaclq0****" -> null
          - ipv6_cidr_blocks      = [] -> null
          - resource_group_id     = "rg-acfmwqnwhqo****" -> null
          - route_table_id        = "vtb-uf6tcjcmu61ggyme9****" -> null
          - router_id             = "vrt-uf6uns13qridfnlr7****" -> null
          - router_table_id       = "vtb-uf6tcjcmu61ggyme9****" -> null
          - secondary_cidr_blocks = [] -> null
          - status                = "Available" -> null
          - tags                  = {} -> null
          - user_cidrs            = [] -> null
        }
    
      # alicloud_vswitch.default will be destroyed
      - resource "alicloud_vswitch" "default" {
          - availability_zone = "cn-hangzhou-b" -> null
          - cidr_block        = "172.16.0.0/24" -> null
          - create_time       = "2023-11-06T09:43:46Z" -> null
          - id                = "vsw-uf6m08al38ngv3ozb****" -> null
          - status            = "Available" -> null
          - tags              = {} -> null
          - vpc_id            = "vpc-uf6q7z8yvogdaclq0****" -> null
          - zone_id           = "cn-hangzhou-b" -> null
        }
    
    Plan: 0 to add, 0 to change, 4 to destroy.
    
    ...
    
    alicloud_alikafka_instance.default: Destroying... [id=alikafka_post-cn-uqm3gvt9****]
    alicloud_alikafka_instance.default: Still destroying... [id=alikafka_post-cn-uqm3gvt9****, 10s elapsed]
    alicloud_alikafka_instance.default: Destruction complete after 3m34s
    alicloud_security_group.default: Destroying... [id=sg-uf6gipzruu9zfpay****]
    alicloud_vswitch.default: Destroying... [id=vsw-uf6m08al38ngv3ozb****]
    alicloud_security_group.default: Still destroying... [id=sg-uf6gipzruu9zfpay****, 10s elapsed]
    alicloud_vswitch.default: Still destroying... [id=vsw-uf6m08al38ngv3ozb****, 10s elapsed]
    alicloud_vswitch.default: Still destroying... [id=vsw-uf6m08al38ngv3ozb****, 20s elapsed]
    alicloud_security_group.default: Still destroying... [id=sg-uf6gipzruu9zfpay****, 20s elapsed]
    alicloud_vswitch.default: Still destroying... [id=vsw-uf6m08al38ngv3ozb****, 30s elapsed]
    alicloud_security_group.default: Still destroying... [id=sg-uf6gipzruu9zfpay****, 30s elapsed]
    alicloud_security_group.default: Still destroying... [id=sg-uf6gipzruu9zfpay****, 40s elapsed]
    alicloud_vswitch.default: Still destroying... [id=vsw-uf6m08al38ngv3ozb****, 40s elapsed]
    alicloud_security_group.default: Still destroying... [id=sg-uf6gipzruu9zfpay****, 50s elapsed]
    alicloud_vswitch.default: Still destroying... [id=vsw-uf6m08al38ngv3ozb****, 50s elapsed]
    alicloud_security_group.default: Still destroying... [id=sg-uf6gipzruu9zfpay****, 1m0s elapsed]
    alicloud_vswitch.default: Still destroying... [id=vsw-uf6m08al38ngv3ozb****, 1m0s elapsed]
    alicloud_security_group.default: Still destroying... [id=sg-uf6gipzruu9zfpay****, 1m10s elapsed]
    alicloud_vswitch.default: Still destroying... [id=vsw-uf6m08al38ngv3ozb****, 1m10s elapsed]
    alicloud_security_group.default: Still destroying... [id=sg-uf6gipzruu9zfpay****, 1m20s elapsed]
    alicloud_vswitch.default: Still destroying... [id=vsw-uf6m08al38ngv3ozb****, 1m20s elapsed]
    alicloud_security_group.default: Still destroying... [id=sg-uf6gipzruu9zfpay****, 1m30s elapsed]
    alicloud_vswitch.default: Still destroying... [id=vsw-uf6m08al38ngv3ozb****, 1m30s elapsed]
    alicloud_security_group.default: Still destroying... [id=sg-uf6gipzruu9zfpay****, 1m40s elapsed]
    alicloud_vswitch.default: Still destroying... [id=vsw-uf6m08al38ngv3ozb****, 1m40s elapsed]
    alicloud_security_group.default: Still destroying... [id=sg-uf6gipzruu9zfpay****, 1m50s elapsed]
    alicloud_vswitch.default: Still destroying... [id=vsw-uf6m08al38ngv3ozb****, 1m50s elapsed]
    alicloud_security_group.default: Destruction complete after 1m59s
    alicloud_vswitch.default: Destruction complete after 2m0s
    alicloud_vpc.default: Destroying... [id=vpc-uf6q7z8yvogdaclq0****]
    alicloud_vpc.default: Destruction complete after 0s
    
    Destroy complete! Resources: 4 destroyed.

    实例已成功删除。

查询实例

  1. 创建一个用于存放Terraform资源的项目文件夹,命名为terraform。

  2. 执行以下命令,进入项目目录。

    cd terraform
  3. 执行以下命令,创建名为main.tf的配置文件。

    data "alicloud_alikafka_instances" "instances_ds" {
      output_file = "instances.txt"
    }
    
    output "first_instance_name" {
      # 索引[0]在这里表示列表中的第一项。
      value = data.alicloud_alikafka_instances.instances_ds.instances.0.name
    }
  4. 执行以下命令,初始化Terraform运行环境。

    terraform init

    预期输出:

    Initializing provider plugins...
    - Reusing previous version of hashicorp/alicloud from the dependency lock file
    - Using previously-installed hashicorp/alicloud v1.211.2
    
    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.
  5. 执行以下命令,查询云账号下的第一个实例。

    terraform apply

    预期输出:

    Changes to Outputs:
      + first_instance_name = "alikafka_post-cn-uax3go6z****"
      - first_topic_name    = "testtopic" -> null
    
    You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure.
    
    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
    
    
    Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
    
    Outputs:
    
    first_instance_name = "alikafka_post-cn-uax3go6z****"

    实例的其他更多信息,可在instances.txt文件中查看。

    [
    	{
    		"config": "{\"enable.vpc_sasl_ssl\":\"false\",\"kafka.log.retention.hours\":\"72\",\"kafka.offsets.retention.minutes\":\"10080\",\"enable.tiered\":\"false\",\"cloud.maxTieredStoreSpace\":\"0\",\"enable.acl\":\"true\",\"kafka.ssl.bit\":\"1024\",\"enable.compact\":\"true\",\"kafka.message.max.bytes\":\"1048576\"}",
    		"create_time": 1698908400000,
    		"deploy_type": 5,
    		"disk_size": 900,
    		"disk_type": 0,
    		"domain_endpoint": "alikafka-post-cn-uax3go6z****-1-vpc.alikafka.aliyuncs.com:9092,alikafka-post-cn-uax3go6z****-2-vpc.alikafka.aliyuncs.com:9092,alikafka-post-cn-uax3go6z****-3-vpc.alikafka.aliyuncs.com:9092",
    		"eip_max": 0,
    		"end_point": "192.168.XX.XX:9092,192.168.XX.XX:9092,192.168.XX.XX:9092",
    		"expired_time": 2014527600000,
    		"id": "alikafka_post-cn-uax3go6z****",
    		"io_max": 20,
    		"msg_retain": 72,
    		"name": "alikafka_post-cn-uax3go6z****",
    		"paid_type": "PostPaid",
    		"sasl_domain_endpoint": "alikafka-post-cn-uax3go6z****-1-vpc.alikafka.aliyuncs.com:9094,alikafka-post-cn-uax3go6z****-2-vpc.alikafka.aliyuncs.com:9094,alikafka-post-cn-uax3go6z****-3-vpc.alikafka.aliyuncs.com:9094",
    		"security_group": "sg-bp1fy2u84fc8go8x****",
    		"service_status": 5,
    		"service_version": "2.2.0",
    		"spec_type": "professional",
    		"ssl_domain_endpoint": null,
    		"ssl_end_point": "",
    		"tags": {},
    		"upgrade_service_detail_info": [
    			{
    				"current2_open_source_version": "2.2.0"
    			}
    		],
    		"vpc_id": "vpc-bp1pw80xofpqcujv6****",
    		"vswitch_id": "vsw-bp11km9bigujk4y52****",
    		"zone_id": "zonek"
    	},
    ]

相关文档