aws terraform_在AWS第3部分上使用Terraform自动伸缩组:弹性负载均衡器和运行状况检查...

壤驷向明
2023-12-01

aws terraform

以前,我们在自动缩放组中设置了一些Apache Ignite服务器。 下一步是在自动扩展组前面添加负载均衡器。

在执行任何步骤之前,我们将一些环境变量添加到variables.tf中。

 variable "autoscalling_group_elb_name" { 
   type = string 
   default = "autoscallinggroupelb"  }  variable "elb_security_group_name" { 
   type = string 
   default = "elb_name"  } 

首先,我们将为负载均衡器添加安全组。

 resource "aws_security_group" "elb_security_group" { 
   name = var.elb_security_group_name 
   egress { 
     from_port = 0 
     to_port = 0 
     protocol = "-1" 
     cidr_blocks = [ "0.0.0.0/0" ] 
   } 
   ingress { 
     from_port = 80 
     to_port = 8080 
     protocol = "tcp" 
     cidr_blocks = [ "0.0.0.0/0" ] 
   }  } 

然后,我们需要检索负载均衡器的可用区域。

 data "aws_availability_zones" "available" { 
   state = "available"  } 

然后让我们添加负载均衡器。

 resource "aws_elb" "autoscalling_group_elb" { 
   name = var.autoscalling_group_elb_name 
   security_groups = [ "${aws_security_group.elb_security_group.id}" ] 
   availability_zones = data.aws_availability_zones.available.names 
   health_check { 
     healthy_threshold = 2 
     unhealthy_threshold = 2 
     timeout = 3 
     interval = 30 
     target = "HTTP:8080/ignite?cmd=version" 
   } 
   listener { 
     lb_port = 80 
     lb_protocol = "http" 
     instance_port = "8080" 
     instance_protocol = "http" 
   }  } 

然后,让负载均衡器与自动伸缩组匹配,并将运行状况类型设置为ELB。

 resource "aws_autoscaling_group" "autoscalling_group_config" { 
   name = var.auto_scalling_group_name 
   max_size = 3 
   min_size = 2 
   health_check_grace_period = 300 
   health_check_type = "ELB" 
   desired_capacity = 3 
   force_delete = true 
   vpc_zone_identifier = [ for s in data.aws_subnet.subnet_values: s. id ] 
   load_balancers = [ "${aws_elb.autoscalling_group_elb.name}" ] 
   launch_configuration = aws_launch_configuration.launch-configuration.name 
   lifecycle { 
     create_before_destroy = true 
   }  } 

和以前一样,您可以使用Terraform解决方案

 > terraform apply 

翻译自: https://www.javacodegeeks.com/2020/01/autoscaling-groups-with-terraform-on-aws-part-3-elastic-load-balancer-and-health-check.html

aws terraform

 类似资料: