{Alibaba ALB} Use an ALB instance to provide IPv4 services

 

 

https://www.alibabacloud.com/help/en/slb/application-load-balancer/product-overview/what-is-alb

https://www.alibabacloud.com/help/en/slb/application-load-balancer/getting-started/use-an-alb-instance-to-provide-ipv4-services

 

 

-- 1. ECSインスタンス作成


cat <<-'EOF' > variables.tf

locals {
  availability_zone_a = "ap-northeast-1a"
  availability_zone_b = "ap-northeast-1b"
}


variable "instance_type" {
  description = "instance_type"
  type = string
  default = "ecs.t5-lc2m1.nano"
}

variable "image_id" {
  description = "image_id"
  type = string
  default = "aliyun_2_1903_x64_20G_alibase_20231221.vhd"
}

EOF

 

cat <<-'EOF' > main.tf

terraform {
  required_version = ">= 1.0.0, < 2.0.0"
  required_providers {
    alicloud = {
       source  = "aliyun/alicloud"
       version = "= 1.217.0"
    }
  }
}

provider "alicloud" {
    region  = "ap-northeast-1"
}


resource "alicloud_vpc" "vpc01" {
  vpc_name          = "vpc01"
  description       = "vpc01"
  cidr_block        = "10.2.0.0/16"
}

 

resource "alicloud_vswitch" "sw01" {
  vswitch_name      = "sw01"
  description       = "sw01"
  vpc_id            = alicloud_vpc.vpc01.id
  cidr_block        = "10.2.1.0/24"
  zone_id           = local.availability_zone_a
}

resource "alicloud_vswitch" "sw02" {
  vswitch_name      = "sw02"
  description       = "sw02"
  vpc_id            = alicloud_vpc.vpc01.id
  cidr_block        = "10.2.2.0/24"
  zone_id           = local.availability_zone_b
}


 resource "alicloud_security_group" "sg01" {
   name                = "sg01"
   description         = "sg01"
   vpc_id              = alicloud_vpc.vpc01.id
   security_group_type = "normal"
 }


resource "alicloud_security_group_rule" "sg0101" {
  type              = "ingress"
  ip_protocol       = "tcp"
  port_range        = "80/80"
  security_group_id = alicloud_security_group.sg01.id
  nic_type          = "intranet"
  policy            = "accept"
  priority          = 10
  cidr_ip           = "0.0.0.0/0"
  description       = "sg0101"
}

resource "alicloud_security_group_rule" "sg0102" {
  type              = "ingress"
  ip_protocol       = "tcp"
  port_range        = "22/22"
  security_group_id = alicloud_security_group.sg01.id
  nic_type          = "intranet"
  policy            = "accept"
  priority          = 10
  cidr_ip           = "0.0.0.0/0"
  description       = "sg0102"
}


resource "alicloud_vpc_ipv4_gateway" "gw01" {
  ipv4_gateway_name        = "gw01"
  ipv4_gateway_description = "gw01"
  vpc_id                   = alicloud_vpc.vpc01.id
  enabled                  = true
}


# ルートテーブルへvSwitchの関連付け
resource "alicloud_route_table_attachment" "sw01_rt01" {
  vswitch_id     = alicloud_vswitch.sw01.id
  route_table_id = alicloud_vpc.vpc01.route_table_id
}

resource "alicloud_route_table_attachment" "sw02_rt01" {
  vswitch_id     = alicloud_vswitch.sw02.id
  route_table_id = alicloud_vpc.vpc01.route_table_id
}

# ルートテーブルへカスタムルート追加
resource "alicloud_route_entry" "rt0101" {
  name                  = "rt0101"
  route_table_id        = alicloud_vpc.vpc01.route_table_id
  destination_cidrblock = "0.0.0.0/0"
  nexthop_type          = "Ipv4Gateway"
  nexthop_id            = alicloud_vpc_ipv4_gateway.gw01.id
}

 

resource "alicloud_instance" "ecs01" {
  image_id                   = var.image_id
  instance_type              = var.instance_type
  security_groups            = [alicloud_security_group.sg01.id]
  instance_name              = "ecs01"
  system_disk_category       = "cloud_ssd"
  system_disk_name           = "ecs01"
  system_disk_size           = 20
  description                = "ecs01"
  internet_charge_type       = "PayByBandwidth"
  internet_max_bandwidth_out = 0
  host_name                  = "ecs01"
  vswitch_id                 = alicloud_vswitch.sw01.id
  instance_charge_type       = "PostPaid"
  key_name                   = "alibabakey01"
  deletion_protection        = false
  credit_specification       = "Standard"
}


resource "alicloud_eip_address" "eip01" {
  address_name              = "eip01"
  bandwidth                 = 1
  deletion_protection       = false
  description               = "eip01"
  internet_charge_type      = "PayByTraffic"
  isp                       = "BGP"
  payment_type              = "PayAsYouGo"
  netmode                   = "public"
}

# インスタンスとEIPの関連付け
resource "alicloud_eip_association" "ecs01_eip01" {
  instance_id              = alicloud_instance.ecs01.id
  allocation_id            = alicloud_eip_address.eip01.id
}


resource "alicloud_instance" "ecs02" {
  image_id                   = var.image_id
  instance_type              = var.instance_type
  security_groups            = [alicloud_security_group.sg01.id]
  instance_name              = "ecs02"
  system_disk_category       = "cloud_ssd"
  system_disk_name           = "ecs02"
  system_disk_size           = 20
  description                = "ecs02"
  internet_charge_type       = "PayByBandwidth"
  internet_max_bandwidth_out = 0
  host_name                  = "ecs02"
  vswitch_id                 = alicloud_vswitch.sw02.id
  instance_charge_type       = "PostPaid"
  key_name                   = "alibabakey01"
  deletion_protection        = false
  credit_specification       = "Standard"
}


resource "alicloud_eip_address" "eip02" {
  address_name              = "eip02"
  bandwidth                 = 1
  deletion_protection       = false
  description               = "eip02"
  internet_charge_type      = "PayByTraffic"
  isp                       = "BGP"
  payment_type              = "PayAsYouGo"
  netmode                   = "public"
}

# インスタンスとEIPの関連付け
resource "alicloud_eip_association" "ecs02_eip02" {
  instance_id              = alicloud_instance.ecs02.id
  allocation_id            = alicloud_eip_address.eip02.id
}

EOF

 

cat <<-'EOF' > outputs.tf

 

output "vpc01_id" {
  value = alicloud_vpc.vpc01.id
  description = "vpc01.id"
}


output "sw01_id" {
  value = alicloud_vswitch.sw01.id
  description = "sw01.id"
}

output "sw02_id" {
  value = alicloud_vswitch.sw02.id
  description = "sw02.id"
}

output "sg01_id" {
  value = alicloud_security_group.sg01.id
  description = "sg01.id"
}

 

output "ecs01_id" {
  value = alicloud_instance.ecs01.id
  description = "ecs01.id"
}

output "ecs02_id" {
  value = alicloud_instance.ecs02.id
  description = "ecs02.id"
}

EOF

 


terraform init
terraform fmt
terraform -version

 

terraform plan

terraform apply -auto-approve

 

yum install -y httpd
systemctl start httpd
systemctl enable httpd
echo $(uname -n) > /var/www/html/index.html


-- 2. ALB作成

 


aliyun alb ListLoadBalancers 

 

aliyun alb CreateLoadBalancer \
--AddressType internet \
--LoadBalancerBillingConfig.PayType PostPay \
--LoadBalancerEdition Basic \
--VpcId vpc-111111111111111111111 \
--ZoneMappings.1.VSwitchId 'vsw-111111111111111111111' \
--ZoneMappings.1.ZoneId 'ap-northeast-1a' \
--ZoneMappings.2.VSwitchId 'vsw-111111111111111111111' \
--ZoneMappings.2.ZoneId 'ap-northeast-1b' \
--AddressAllocatedMode Dynamic \
--AddressIpVersion IPv4 \
--DeletionProtectionEnabled false \
--LoadBalancerName alb01 \
--force

 

 


--3. サーバーグループ作成

aliyun alb ListServerGroups 

 


aliyun alb CreateServerGroup \
--ServerGroupName svg01 \
--ServerGroupType Instance \
--VpcId vpc-111111111111111111111 \
--Protocol HTTP \
--HealthCheckConfig.HealthCheckConnectPort 80 \
--HealthCheckConfig.HealthCheckEnabled true \
--HealthCheckConfig.HealthCheckHost '$SERVER_IP' \
--HealthCheckConfig.HealthCheckCodes.1 http_2xx \
--HealthCheckConfig.HealthCheckCodes.2 http_3xx \
--HealthCheckConfig.HealthCheckInterval 2 \
--HealthCheckConfig.HealthCheckMethod HEAD \
--HealthCheckConfig.HealthCheckPath '/' \
--HealthCheckConfig.HealthCheckProtocol HTTP \
--HealthCheckConfig.HealthCheckTimeout 5 \
--HealthCheckConfig.HealthyThreshold 3 \
--HealthCheckConfig.UnhealthyThreshold 3 \
--StickySessionConfig.StickySessionEnabled false \
--force 

 

 

aliyun alb ListServerGroupServers 


aliyun alb AddServersToServerGroup \
--ServerGroupId sgp-111111111111111111 \
--Servers.1.Description ecs01 \
--Servers.1.Port 80 \
--Servers.1.ServerId i-11111111111111111111 \
--Servers.1.ServerType Ecs \
--Servers.1.Weight 100 \
--Servers.2.Description ecs02 \
--Servers.2.Port 80 \
--Servers.2.ServerId i-11111111111111111111 \
--Servers.2.ServerType Ecs \
--Servers.2.Weight 100 \
--force

 

 

 

-- 4. リスナー作成

aliyun alb ListListeners 

 


aliyun alb CreateListener \
--LoadBalancerId alb-111111111111111111 \
--ListenerProtocol HTTP \
--ListenerPort 80 \
--ListenerDescription lis01 \
--DefaultActions.1.ForwardGroupConfig.ServerGroupTuples.1.ServerGroupId sgp-111111111111111111 \
--DefaultActions.1.Type ForwardGroup \
--force


--5. 動確

curl http://alb-111111111111111111.ap-northeast-1.alb.aliyuncs.com 

 


--6. クリーンアップ


aliyun alb DeleteListener \
--ListenerId lsn-111111111111111111 

aliyun alb ListListeners  


aliyun alb RemoveServersFromServerGroup \
--ServerGroupId sgp-111111111111111111 \
--Servers.1.Description ecs01 \
--Servers.1.Port 80 \
--Servers.1.ServerId i-11111111111111111111 \
--Servers.1.ServerType Ecs \
--Servers.1.Weight 100 \
--Servers.2.Description ecs02 \
--Servers.2.Port 80 \
--Servers.2.ServerId i-11111111111111111111 \
--Servers.2.ServerType Ecs \
--Servers.2.Weight 100 \
--force


aliyun alb ListServerGroupServers 


aliyun alb DeleteServerGroup \
--ServerGroupId sgp-111111111111111111


aliyun alb ListServerGroups 

 


aliyun alb DeleteLoadBalancer \
--LoadBalancerId alb-111111111111111111 

aliyun alb ListLoadBalancers 

 


terraform destroy -auto-approve