亚马逊AWS官方博客

手把手教你快速部署流量压测工具 – Bees with Machine Guns

(可用于测试AWS ELB、EC2、Auto Scaling、HA)

一群勤劳的小蜜蜂

很多时候我们需要进行负载均衡、Web服务器的并发式压力测试,但像Siege, JMeter等工具都是从一个源IP地址发送流量,这不能很好的模拟出对负载均衡的实际压测效果。这里将详细介绍如何快速部署一个分布式压测工具Bees with Machine Guns,模拟一组不同的IP(可自定义)地址进行压测,这可更加准确的模式实际生产场景。

(注:请合理、正确使用此工具,核对你要压测的目标,避免造成不必要的“攻击”行为。)

接下去将手把手教你如何快速搭建一组分布式的“勤劳的小蜜蜂”

1. 启动Ubuntu EC2(Amazon Linux机器也支持)

启动成功

2. 连接启动完成的实例,例如

ssh -i “ubuntu-instance.pem” ubuntu@ec2-54-201-96-220.us-west-2.compute.amazonaws.com

3. 运行sudo apt-get install python-paramiko git

4. 进入到/tmp目录,然后下载bees源码,进入到bees目录,通过python安装。

ubuntu@ip-10-200-1-230:~$ cd /tmp

ubuntu@ip-10-200-1-230:/tmp$ git clone git://github.com/newsapps/beeswithmachineguns.git

ubuntu@ip-10-200-1-230:/tmp$ cd beeswithmachineguns

ubuntu@ip-10-200-1-230:/tmp/beeswithmachineguns$ sudo python setup.py install
5. 进入到/home/ubuntu/目录,创建.boto文件

/home/ubuntu/

vim .boto

接着,然后输入credentials相关内容

[Credentials]

aws_access_key_id=AKIAJOSWXXXXXXXXXX

aws_secret_access_key=RI2h19QXXXXXXXXXXXXXXXXXXXXXXXX

[Boto]

ec2_region_name=us-west-2

ec2_region_endpoint=us-west-2.ec2.amazonaws.com

#elb_region_name=us-west-2

#elb_region_endpoint=elasticloadbalancing.us-west-2.amazonaws.com
6. 进入/home/ubuntu/.ssh/目录,上传EC2实例的pem文件。

cd /home/ubuntu/.ssh/ (注意,这个地方一定要确保进入.ssh目录下。如果没有成功,请再次确认自己所在路径)

7. 执行启动“压测”的EC2机器

(1)简单执行:-s代表启动几台a bee!机器,-k代表秘钥的名称(注意,代码中已经带了后缀,所以这里只需要输入名称)

bees up -s 4 -k Internal-Amazon-Linux

(2)带参数执行(推荐使用)

bees up -s 4 -k Internal-Amazon-Linux -z us-west-2a -g HTTP -l ubuntu -i ami-113af271 -t t2.micro
执行结果:会发现启动了4台名称为a bee!的EC2机器。

ubuntu@ip-10-200-1-230:~/.ssh$ bees up -s 4 -k Internal-Amazon-Linux -z us-west-2a -g HTTP -l ubuntu -i ami-113af271 -t t2.microConnecting to the hive.

GroupId found: HTTP

Placement: us-west-2a

Attempting to call up 4 bees.

Waiting for bees to load their machine guns…

.

Bee i-0c8ddc14 is ready for the attack.

Bee i-0d8ddc15 is ready for the attack.

Bee i-0e8ddc16 is ready for the attack.

Bee i-0f8ddc17 is ready for the attack.

The swarm has assembled 4 bees.

看到这里说明4台EC2实例都已经正常启动了!

8. 可以看一下目前启动的report

ubuntu@ip-10-200-1-230:~$ bees report

9. 开始“压测”,这里假设压测ELB

bees attack -n 100 -c 4 -k Internal-Amazon-Linux -u http://mylabelb-XXXXXX.us-west-x.elb.amazonaws.com/

这个时候发现报错!

这里注意,需登到bee机器,然后在a bee!机器上安装apache2-utils

ubuntu@ip-172-31-29-100:~$ sudo apt-get install apache2-utils

Reading package lists… Done

Building dependency tree

Reading state information… Done

The following extra packages will be installed:

libapr1 libaprutil1

The following NEW packages will be installed:

apache2-utils libapr1 libaprutil1

0 upgraded, 3 newly installed, 0 to remove and 29 not upgraded.

Need to get 244 kB of archives.

After this operation, 877 kB of additional disk space will be used.

Do you want to continue? [Y/n] y

等到所有的a bee!机器都安装完apache2-utials之后,回到bee control机器,再次运行。成功!

此时再通过CloudWatch监控查看ELB请求总数的情况,发现变了。

说明:这里进行了两次压测,所以会看到监控中两段不同的曲线。

10. 停止“压测” 命令

bees down

此时所有的EC2都会马上处于Terminated状态

附: 源码参考https://github.com/newsapps/beeswithmachineguns

作者介绍:

毛郸榕

亚马逊AWS中国助理解决方案架构师,负责基于AWS的云计算方案架构的咨询和设计,同时致力于AWS云服务在国内的应用和推广,毕业于北京航空航天大学云 计算专业,硕士,毕业后直接加入亚马逊AWS中国。在大规模后台架构、企业混合IT和自动化运维等方面有着丰富的实践经验。目前在集中精力学习新一代无服务器架构设计。