Elastic Beanstalk 不额外收费,只需为存储和运行应用程序所需的 AWS 资源付费
EB CLI 是 Amazon Elastic Beanstalk 的命令行界面,它提供了可简化从本地存储库创建、更新和监控环境的交互式命令
安装eb cli
$ pip install virtualenv
$ virtualenv ebvenv
$ source venv/bin/activate
$ (ebvenv) pip install awsebcli --upgrade
$ $ eb --version
EB CLI 3.20.3 (Python 3.7.1)
帮助信息
commands:
abort Cancels an environment update or deployment.
appversion Listing and managing application versions
clone Clones an environment.
codesource Configures the code source for the EB CLI to use by default.
config Modify an environment's configuration. Use subcommands to manage saved configurations.
console Opens the environment in the AWS Elastic Beanstalk Management Console.
create Creates a new environment.
deploy Deploys your source code to the environment.
events Gets recent events.
health Shows detailed environment health.
init Initializes your directory with the EB CLI. Creates the application.
labs Extra experimental commands.
list Lists all environments.
local Runs commands on your local machine.
logs Gets recent logs.
open Opens the application URL in a browser.
platform Commands for managing platforms.
printenv Shows the environment variables.
restore Restores a terminated environment.
scale Changes the number of running instances.
setenv Sets environment variables.
ssh Opens the SSH client to connect to an instance.
status Gets environment information and status.
swap Swaps two environment CNAMEs with each other.
tags Allows adding, deleting, updating, and listing of environment tags.
terminate Terminates the environment.
upgrade Updates the environment to the most recent platform version.
use Sets default environment.
初始化
初始化eb环境,可以指定凭证profile
$ mkdir test-beangolang && cd test-beangolang
$ eb init
$ #eb init --profile profilename
初始化使用默认的aws凭证,要求选择region,application,ssh,platform(此处选择go)等。新的application默认以当前文件夹名称创建。
初始化完成后在本地生成如下文件。其中config.yml
包含配置信息
$ tree -a
.
├── .elasticbeanstalk
│ └── config.yml
└── .gitignore
$ cat .elasticbeanstalk/config.yml
branch-defaults:
default:
environment: null
group_suffix: null
global:
application_name: test-beangolang
branch: null
default_ec2_keyname: dev-key
default_platform: Go 1 running on 64bit Amazon Linux 2
default_region: cn-north-1
include_git_submodules: true
instance_profile: null
platform_name: null
platform_version: null
profile: null
repository: null
sc: null
workspace_type: Application
初始化之后只会创建application,不会启动environment(即实例,负载均衡器,数据库等资源)
高级配置
如果
.ebignore
不存在,但存在.gitignore
,EB CLI 将忽略.gitignore
中指定的文件。如果存在.ebignore
,EB CLI 将不会读取.gitignore
eb cli工具的凭证优先级
创建环境
$ eb create
环境初始化会选择环境name,dns,lb,是否下载sample,最后启动环境,启动过程中会输出日志
最终所有资源都会通过创建cloudformation的方式进行创建
Would you like to enable Spot Fleet requests for this environment? (y/N): n
NOTE: The current directory does not contain any source code. Elastic Beanstalk is launching the sample application instead.
Do you want to download the sample application into the current directory? (Y/n): y
INFO: Downloading sample application to the current directory.
INFO: Download complete.
Environment details for: test-beangolang-dev
Application name: test-beangolang
Region: cn-north-1
Deployed Version: Sample Application
Environment ID: e-254v23ad2m
Platform: arn:aws-cn:elasticbeanstalk:cn-north-1::platform/Go 1 running on 64bit Amazon Linux 2/3.6.1
Tier: WebServer-Standard-1.0
CNAME: test-beangolang-dev.cn-north-1.eb.amazonaws.com.cn
Updated: 2022-11-13 15:35:24.287000+00:00
Printing Status:
2022-11-13 15:35:23 INFO createEnvironment is starting.
2022-11-13 15:35:24 INFO Using elasticbeanstalk-cn-north-1-xxxxxxxxxxxx as Amazon S3 storage bucket for environment data.
2022-11-13 15:35:46 INFO Created target group named: arn:aws-cn:elasticloadbalancing:cn-north-1:xxxxxxxxxxxx:targetgroup/awseb-test-bea-default-5gkjd/6de4b45758a2fab6
2022-11-13 15:35:46 INFO Created security group named: awseb-e-254v23ad2m-stack-AWSEBSecurityGroup-1X8RQFJTFG3WA
2022-11-13 15:36:03 INFO Created Auto Scaling launch configuration named: awseb-e-254v23ad2m-stack-AWSEBAutoScalingLaunchConfiguration-vWQ0HP7XfOUA
2022-11-13 15:36:03 INFO Created Load Balancer listener rule named: arn:aws-cn:elasticloadbalancing:cn-north-1:xxxxxxxxxxxx:listener-rule/app/main-alb/17ec2f283414a86a/80c5154ba2598551/d2ff9a73b3344c39
-- Events -- (safe to Ctrl+C)
下载sample程序,一个简单的go web程序
$ tree
.
├── application.go
├── cron.yaml
└── public
└── index.html
查看当前状态和健康状况
$ eb status
Environment details for: test-beangolang-dev
Application name: test-beangolang
Region: cn-north-1
Deployed Version: Sample Application
Environment ID: e-254v23ad2m
Platform: arn:aws-cn:elasticbeanstalk:cn-north-1::platform/Go 1 running on 64bit Amazon Linux 2/3.6.1
Tier: WebServer-Standard-1.0
CNAME: test-beangolang-dev.cn-north-1.eb.amazonaws.com.cn
Updated: 2022-11-13 15:37:06.756000+00:00
Status: Launching
Health: Grey
$ eb health
查看并修改eb的配置,指定默认编辑器为vim
export EDITOR="vim"
eb config
查看事件输出,包括环境构建,重新部署,配置修改等事件日志,和图形控制台一致
$ eb evnets
查看控制台
无论是在控制台还是通过ebcli工具创建环境,最终都会使用cloudformation创建资源,清单如下
- 目标组
- 安全组
- 启动模板或启动配置
- 2个cloudwatch告警
- 2个asg扩展策略
- 自动扩展组asg
查看下启动模板(nodejs)的userdata部分如下
可以看到对于每种支持的language,都有对应的bootstrap脚本,通过向启动脚本传参实现环境的初始化
Content-Type: multipart/mixed; boundary="===============5189065377222898407=="
MIME-Version: 1.0
--===============5189065377222898407==
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"
#cloud-config
repo_upgrade: none
repo_releasever: 2.0
cloud_final_modules:
- [scripts-user, always]
--===============5189065377222898407==
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="user-data.txt"
#!/bin/bash
exec > >(tee -a /var/log/eb-cfn-init.log|logger -t [eb-cfn-init] -s 2>/dev/console) 2>&1
echo [`date -u +"%Y-%m-%dT%H:%M:%SZ"`] Started EB User Data
set -x
function sleep_delay
{
if (( $SLEEP_TIME < $SLEEP_TIME_MAX )); then
echo Sleeping $SLEEP_TIME
sleep $SLEEP_TIME
SLEEP_TIME=$(($SLEEP_TIME * 2))
else
echo Sleeping $SLEEP_TIME_MAX
sleep $SLEEP_TIME_MAX
fi
}
# Executing bootstrap script
SLEEP_TIME=2
SLEEP_TIME_MAX=3600
while true; do
curl https://elasticbeanstalk-platform-assets-public-beta-cn-north-1.s3.cn-north-1.amazonaws.com.cn/stalks/eb_nodejs16_amazon_linux_2_1.0.1696.0_20220929200609/lib/UserDataScript.sh > /tmp/ebbootstrap.sh
RESULT=$?
if [[ "$RESULT" -ne 0 ]]; then
sleep_delay
else
/bin/bash /tmp/ebbootstrap.sh 'https://cloudformation-waitcondition-cn-north-1.s3.cn-north-1.amazonaws.com.cn/arn%3Aaws-cn%3Acloudformation%3Acn-north-1%3A037047667284%3Astack/awseb-e-tuypgrwuf3-stack/cd3970d0-47e5-11ed-b386-066b31cc2db4/AWSEBInstanceLaunchWaitHandle?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20221009T151948Z&X-Amz-SignedHeaders=host&X-Amz-Expires=86399&X-Amz-Credential=AKIAOMTUJJXMXKXJZEPQ%2F20221009%2Fcn-north-1%2Fs3%2Faws4_request&X-Amz-Signature=6a920d4ee295c755dc47d599cfcb7e87a3885b72f0e3429bdee6a5ed8ab75764' 'arn:aws-cn:cloudformation:cn-north-1:037047667284:stack/awseb-e-tuypgrwuf3-stack/cd3970d0-47e5-11ed-b386-066b31cc2db4' '52ec14ac-5685-404a-b42e-3b5e92239559' 'https://elasticbeanstalk-health.cn-north-1.amazonaws.com.cn' '' 'https://elasticbeanstalk-platform-assets-public-beta-cn-north-1.s3.cn-north-1.amazonaws.com.cn/stalks/eb_nodejs16_amazon_linux_2_1.0.1696.0_20220929200609' 'cn-north-1'
RESULT=$?
if [[ "$RESULT" -ne 0 ]]; then
sleep_delay
else
exit 0
fi
fi
done
--===============5189065377222898407==--
默认创建的2个告警
对应的event rule
{
"source": [
"aws.cloudwatch"
],
"detail-type": [
"CloudWatch Alarm State Change"
],
"resources": [
"arn:aws-cn:cloudwatch:cn-north-1:xxxxxxx:alarm:awseb-e-tuypgrwuf3-stack-AWSEBCloudwatchAlarmHigh-xxxxxxxx"
]
}
默认创建的两个扩展策略
将asg扩展策略和cw告警联系,当网络请求大于6M/s,就会触发asg扩容1个实例
通过webbench
测试加压,eb实例扩容成功
webbench -c 300 -t 60 http://xxxxxxx-xxxxxxxx.cn-north-1.elb.amazonaws.com.cn:xxxxx/