新功能-应用负载均衡器的加权目标组配置
我们知道海外的AWS Route53服务,有多种路由策略,其中有一项为加权路由。可将极少的一部分流量发送到一个资源,并将其余流量发送到另一个资源,则可以指定权重 1 和 255。权重为 1 的资源将获得 1/256 (1/1+255) 的流量,另一个资源将获得 255/256 (255/1+255) 的流量。可以通过更改权重来逐渐改变平衡。
使用这种加权路由技术,开发人员可以将新版本应用程序的基础架构,与旧版本并行运行一段时间,将流量逐步切换至新版本中,若新版本没有问题之后再将旧版本删除。这种技术手段称为蓝绿部署。
虽然AWS中国并没有Route53服务,但是AWS 推出了一项新功能,即应用负载均衡器支持加权目标组,允许开发人员将流量分配至应用程序的多个版本中,下面我们来实验一下这个功能。
参考架构图:
首先,我们启动两个ec2实例。使用用户数据,安装并启动http、php服务,下载web程序包。
安全组设置为:http,来源:任何地方
第二步,创建应用负载均衡器
第三步,创建2个目标组,每个目标组各自加入一台ec2实例
第四步,配置负载均衡器权重分配,设置监听器规则。
将第一个目标组分配80%的流量,第二个目标组分配20%的流量,点击Save。
第五步,等待目标组elb健康检查状态为healthy后,即可访问负载均衡器的DNS名称。
测试了10次访问结果,其中第一台ec2被访问了8次,第二台ec2被访问了2次。
EC2 Instance ID: i-0765a53082a78c2c5 1 2 3 5 6 8 9 10 共计8次
EC2 Instance ID: i-013badc4196cfdb2b 4 7 共计2次
New Feature - Weighted Target Group Configuration for Application Load Balancer
We know that AWS Route53 service overseas has multiple routing policies, one of which is weighted routing. You can send a small portion of traffic to one resource and the rest to another by specifying weights 1 and 255. The resource with weight 1 will receive 1/256 (1/1+255) of the traffic, and the other resource will receive 255/256 (255/1+255) of the traffic. You can gradually change the balance by changing the weights.
Using this weighted routing technique, developers can run the infrastructure of a new version of the application in parallel with the old version for a period of time, gradually switching traffic to the new version, and then removing the old version after confirming the new version has no issues. This technique is called blue-green deployment.
Although AWS China does not have Route53 service, AWS has launched a new feature - Application Load Balancer supports weighted target groups, allowing developers to distribute traffic to multiple versions of an application. Let's experiment with this feature.
Reference Architecture Diagram:
First, we launch two EC2 instances. Using user data, we install and start http and php services, and download the web application package.
Security Group Settings: HTTP, Source: Anywhere
Step 2: Create an Application Load Balancer
Step 3: Create 2 Target Groups, each with one EC2 instance
Step 4: Configure Load Balancer Weight Distribution, Set Listener Rules
Assign 80% of traffic to the first target group and 20% to the second target group, then click Save.
Step 5: Wait for the target group ELB health check status to become healthy, then access the load balancer's DNS name.
Tested 10 access results, where the first EC2 was accessed 8 times and the second EC2 was accessed 2 times.
EC2 Instance ID: i-0765a53082a78c2c5 1 2 3 5 6 8 9 10 Total 8 times
EC2 Instance ID: i-013badc4196cfdb2b 4 7 Total 2 times