Auto Scaling on AWS via AWS CLI
前言
以下的文章將帶著大家一步一步的在AWS雲上建立Auto Scaling Group做為可以自動水平擴展的EC2群集, 若文章有有什麼不清楚的地方, 歡迎各位在下方留言, 希望這篇教學能幫助到正在學習AWS的朋友們大致上需要部屬在雲上的資源有
- Security Group
- Launch Configuration
- Load Balancer
- Auto Scaling Group
Security Group
Security Group可以決定要開那些Port或是限制那些IP才可以訪問因為建立Security Group時需要明確指定要建在哪個VPC上, 所以在開始建立Security Group前, 先執行以下的指令把目前可用的VPC Id輸出
vpcId=$(aws ec2 describe-vpcs | jq -r ".Vpcs[0].VpcId")
有了VPC Id後, 接下來才能開始建立Security Group__sgName='CliGroup123'
sgGroupId=$(aws ec2 create-security-group \
--group-name $__sgName \
--description "My security group" \
--vpc-id $vpcId | jq -r ".GroupId")
最後做ingress來宣告網路的開口aws ec2 authorize-security-group-ingress \
--group-id ${sgGroupId} \
--protocol tcp \
--port 5000 \
--cidr "0.0.0.0/0"
Launch Configuration
Launch Configuration是用來定義VM instance的種類, OS Image的種類以及套哪個Security Group, 使用哪支金鑰對, 網路要不要對外等等的配置資訊__imgId='ami-0986c2ac728528ac2' __lcfgName='my-launch-config10' __keyName='app123' __vmType='t2.micro'
#required public ip aws autoscaling create-launch-configuration \ --launch-configuration-name ${lcfgName} \ --key-name $__keyName \ --security-groups $sgGroupId \ --instance-type $__vmType \ --image-id $__imgId \ --instance-monitoring Enabled=true \ --no-ebs-optimized \ --associate-public-ip-address \ --user-data "#include https://raw.githubusercontent.com/andy51002000/cloud-formation-example/master/init.sh"
Load Balancer
Load Balancer需要明確定義使用哪個subnet, 為了簡化說明, 這邊拿query到的第一組subnet來用#https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-subnets.html
subnnetsString = [system.String]::Join(",", $( aws ec2 describe-subnets | jq -r ".Subnets[].SubnetId"))
subnnetsStringList = $( aws ec2 describe-subnets | jq -r ".Subnets[].SubnetId")
建立Load Balancer
#https://docs.aws.amazon.com/zh_tw/elasticloadbalancing/latest/classic/elb-create-internal-load-balancer.html __lbname="my-internal-loadbalancer4" aws elb create-load-balancer ` --load-balancer-name $__lbname ` --listeners Protocol=HTTP,LoadBalancerPort=80,InstanceProtocol=HTTP,InstancePort=80 ` --subnets $subnnetsStringList[0] $subnnetsStringList[1] $subnnetsStringList[2]` --scheme internet-facing ` --security-groups ${sgGroupId}
Load Balancer建立完成後, 還需要新增Listener去監聽指定的port, 以及設定Health Check的規則
aws elb create-load-balancer-listeners \
--load-balancer-name $__lbname \
--listeners Protocol=HTTP,LoadBalancerPort=5000,InstanceProtocol=HTTP,InstancePort=5000
aws elb configure-health-check \
--load-balancer-name $__lbname \
--health-check Target=HTTP:5000/,Interval=30,UnhealthyThreshold=2,HealthyThreshold=2,Timeout=3
Auto Scaling Group
最後建立Auto Scaling Group來部屬以及管理Instance, 我們可以在這邊配置初始的VM數量__asgname='asg123new222'
aws autoscaling create-auto-scaling-group `
--auto-scaling-group-name $__asgname `
--launch-configuration-name $__lcfgName `
--min-size 1 `
--max-size 5 `
--desired-capacity 1 `
--vpc-zone-identifier "$subnnetsString"
上面的範例只有配置一個Instance, 若想要增加數量, 可以去調整desired-capacity的參數大小Ref: https://docs.aws.amazon.com/cli/latest/reference/autoscaling/create-launch-configuration.html https://docs.aws.amazon.com/cli/latest/reference/ec2/create-security-group.html https://docs.aws.amazon.com/cli/latest/userguide/cli-services-ec2-sg.html https://docs.aws.amazon.com/cli/latest/reference/autoscaling/create-auto-scaling-group.html https://docs.aws.amazon.com/zh_tw/elasticloadbalancing/latest/classic/elb-create-internal-load-balancer.html
留言
張貼留言