brunch

You can make anything
by writing

C.S.Lewis

by Master Seo Oct 24. 2022

19탄-5. 테라폼-AWS-오토스케일링

다음은 주말 CloudNet 테라폼 스터디 내용 참고하여  정리한 부분입니다.

https://gasidaseo.notion.site/gasidaseo/CloudNet-Blog-c9dfa44a27ff431dafdd2edacc8a1863



1

오토스케일링 만들어보자.

우선 ELB를 콘솔로 만들어보자. 

실제 만들어 보지 않고는 코드로 만들어지는것을 이해하기 힘들다.


2

오토스케일링은 2가지를 만들어야 한다.

// aws_launch_configuration  을 처음으로 만든다.

// resource "aws_autoscaling_group" "myasg"  오토스케일링 그룹을 만든다.



3

콘솔로 로드밸린서 만들어보기

https://catalog.us-east-1.prod.workshops.aws/workshops/600420b7-5c4c-498f-9b80-bc7798963ba3/ko-KR/ec2/50-elb/100-create-elb



4

리소스를 교체할 때 테라폼은 이전 리소스를 먼저 삭제한 다음 대체 리소스를 생성합니다.

 그러나 ASG에 이전 리소스에 대한 참조가 있기 때문에 테라폼은 해당 리소스를 삭제 할 수 없습니다.

해결을 위해 수명 주기 설정(lifecycle)를 할 수 있습니다.


5

create_before_destroy 를 true 로 설정하면 테라폼은 리소스를 교체하는 순서를 반대로 하여 

교체 리소스를 먼저 생성하고(이전 리소스가 가리키고 있던 참조를 업데이트하여 교체한 리소스를 가리킴) 기존 리소스를 삭제합니다.


6

샘플


// aws_launch_configuration  을 처음으로 만든다.

참고


resource "aws_launch_configuration" "example" {

  image_id        = "ami-0e9bfdb247cc8de84"

  instance_type   = "t2.micro"

  security_groups = [aws_security_group.instance.id]


  user_data = <<-EOF

              #!/bin/bash

              echo "Hello, World" > index.html

              nohup busybox httpd -f -p ${var.server_port} &

              EOF


  # Required when using a launch configuration with an auto scaling group.

  lifecycle {

    create_before_destroy = true

  }

}



7

만들어보자~


ASG.TF


// aws_launch_configuration  을 처음으로 만든다.


cat <<EOT > asg.tf

data "aws_ami" "my_amazonlinux2" {

  most_recent = true

  filter {

    name   = "owner-alias"

    values = ["amazon"]

  }


  filter {

    name   = "name"

    values = ["amzn2-ami-hvm-*-x86_64-ebs"]

  }


  owners = ["amazon"]

}


resource "aws_launch_configuration" "mylauchconfig" {

  name_prefix     = "t101-lauchconfig-"

  image_id        = data.aws_ami.my_amazonlinux2.id

  instance_type   = "t2.micro"

  security_groups = [aws_security_group.mysg.id]

  associate_public_ip_address = true


  user_data = <<-EOF

              #!/bin/bash

              wget https://busybox.net/downloads/binaries/1.31.0-defconfig-multiarch-musl/busybox-x86_64

              mv busybox-x86_64 busybox

              chmod +x busybox

              RZAZ=\$(curl http://169.254.169.254/latest/meta-data/placement/availability-zone-id)

              IID=\$(curl 169.254.169.254/latest/meta-data/instance-id)

              LIP=\$(curl 169.254.169.254/latest/meta-data/local-ipv4)

              echo "<h1>RegionAz(\$RZAZ) : Instance ID(\$IID) : Private IP(\$LIP) : Web Server</h1>" > index.html

              nohup ./busybox httpd -f -p 80 &

              EOF


  # Required when using a launch configuration with an auto scaling group.

  lifecycle {

    create_before_destroy = true

  }

}


//  create_before_destroy = true  신규 리소스를 만들고 기존 리소스를 삭제한다.



// resource "aws_autoscaling_group" "myasg"  오토스케일링 그룹을 만든다.


resource "aws_autoscaling_group" "myasg" {

  name                 = "myasg"

  launch_configuration = aws_launch_configuration.mylauchconfig.name

  vpc_zone_identifier  = [aws_subnet.mysubnet1.id, aws_subnet.mysubnet2.id]

  min_size = 2

  max_size = 10


  tag {

    key                 = "Name"

    value               = "terraform-asg"

    propagate_at_launch = true

  }

}

EOT





8

EC2 생성 모니터링 - 다른 창에서 모니터링


export AWS_PAGER=""


while true; do aws ec2 describe-instances --query "Reservations[*].Instances[*].{PublicIPAdd:PublicIpAddress,InstanceName:Tags[?Key=='Name']|[0].Value,Status:State.Name}" --filters Name=instance-state-name,Values=running --output text ; echo "------------------------------" ; sleep 1; done








9

기존 ec2는 삭제

rm ec2.tf


terraform plan && terraform apply -auto-approve

생성됨

------------------------------

terraform-asg   43.200.169.147  running

terraform-asg   43.201.5.88     running



terraform state list



10

CURL로 접속 확인  - ec2 2대 확인

IP1=<EC2 1번 Public IP>

IP2=<EC2 2번 Public IP>

echo "$IP1 - WebSrv" ; curl -s $IP1 ; echo ; echo "$IP2 - WebSrv" ; curl -s $IP2 ; echo


[root@ip-172-31-61-209 vpc]# echo "$IP1 - WebSrv" ; curl -s $IP1 ; echo ; echo "$IP2 - 

WebSrv" ; curl -s $IP2 ; echo


43.200.169.147 - WebSrv

<h1>RegionAz(apne2-az3) : Instance ID(i-0fa1d7e022b755d82) : Private IP(10.10.2.18) : Web Server</h1>

43.201.5.88 - WebSrv

<h1>RegionAz(apne2-az1) : Instance ID(i-0e644930def8bf5ad) : Private IP(10.10.1.93) : Web Server</h1>




11

# ASG 시작 구성, AS 그룹 정보 확인 

aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names | jq

aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names --output table

aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names myasg| jq

aws autoscaling describe-scaling-activities --auto-scaling-group-name myasg | jq




다음 과정

https://brunch.co.kr/@topasvga/2764




https://brunch.co.kr/@topasvga/2421


감사합니다.

매거진의 이전글 19탄-4. 테라폼-AWS-VPC, data사용
작품 선택
키워드 선택 0 / 3 0
댓글여부
afliean
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari