开发者社区> CodeSample> 正文

通过Terraform在专有网络中创建负载均衡实例

简介: 本文介绍如何使用Terraform在专有网络中创建负载均衡实例。

前提条件

在开始之前,请您确保完成以下操作:

操作步骤

  1. 创建专有网络和交换机。
    1. 创建terraform.tf文件,输入以下内容,并保存在当前在当前的执行目录中。
      resource "alicloud_vpc" "main" {
        name       = var.long_name
        cidr_block = var.vpc_cidr
      }
      
      resource "alicloud_vswitch" "main" {
        vpc_id            = alicloud_vpc.main.id
        count             = length(split(",", var.availability_zones))
        cidr_block        = var.cidr_blocks["az${count.index}"]
        availability_zone = split(",", var.availability_zones)[count.index]
      
        depends_on = [alicloud_vpc.main]
      }
    2. 运行terraform apply开始创建。
    3. 运行terraform show查看已创建的专有网络和交换机。
  2. 创建负载均衡实例,并添加监听。
    1. terraform.tf文件中增加以下内容:
      resource "alicloud_slb" "instance" {
        name                 = var.name
        vswitch_id           = alicloud_vswitch.main[0].id
        internet_charge_type = var.internet_charge_type
      }
      
      resource "alicloud_slb_listener" "listener" {
        load_balancer_id = alicloud_slb.instance.id
        backend_port     = "2111"
        frontend_port    = "21"
        protocol         = "tcp"
        bandwidth        = "5"
      }
    2. 运行terraform apply开始创建。出现类似下面的日志,说明创建成功。
      alicloud_slb.instance: Creating...
        address:              "" => "<computed>"
        instance_charge_type: "" => "PostPaid"
        internet:             "" => "false"
        name:                 "" => "slb-alicloud"
        vswitch_id:           "" => "vsw-bp1hk718gruebldq7x54f"
      alicloud_slb.instance: Creation complete after 3s (ID: lb-bp1xuzzb6ectvszc3v189)
      alicloud_slb_listener.listener: Creating...
        acl_status:                "" => "off"
        backend_port:              "" => "2111"
        bandwidth:                 "" => "5"
        established_timeout:       "" => "900"
        frontend_port:             "" => "21"
        health_check_connect_port: "" => "<computed>"
        health_check_interval:     "" => "2"
        health_check_timeout:      "" => "5"
        health_check_type:         "" => "tcp"
        healthy_threshold:         "" => "3"
        load_balancer_id:          "" => "lb-bp1xuzzb6ectvszc3v189"
        persistence_timeout:       "" => "0"
        protocol:                  "" => "tcp"
        scheduler:                 "" => "wrr"
        unhealthy_threshold:       "" => "3"
      alicloud_slb_listener.listener: Creation complete after 1s (ID: lb-bp1xuzzb6ectvszc3v189:21)
      Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
    3. 运行terraform show查看已经创建负载均衡实例。
分享:
若有任何问题或疑惑,请反馈给我们 立即提问
相关文档