使用 EB CLI 管理 Elastic Beanstalk 环境 - AWS Elastic Beanstalk

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用 EB CLI 管理 Elastic Beanstalk 环境

安装 EB CLI配置项目目录之后,您便可使用 EB CLI 创建 Elastic Beanstalk 环境、部署源和配置更新以及拉取日志和事件。

注意

使用 EB CLI 创建环境需要服务角色。您可以通过在 Elastic Beanstalk 控制台中创建环境来创建服务角色。如果您没有服务角色,EB CLI 会尝试在您运行 eb create 时创建一个。

EB CLI 将对所有成功的命令返回零 (0) 退出代码,并在遇到任何错误时返回非零退出代码。

下面的示例使用一个名为 eb 的空项目文件夹,该文件夹是用 EB CLI 初始化来供示例 Docker 应用程序使用的。

Eb create

要创建您的第一个环境,请运行 eb create 并按照提示操作。如果项目目录中有源代码,EB CLI 会将源代码捆绑并部署到环境。否则将使用示例应用程序。

~/eb$ eb create Enter Environment Name (default is eb-dev): eb-dev Enter DNS CNAME prefix (default is eb-dev): eb-dev WARNING: The current directory does not contain any source code. Elastic Beanstalk is launching the sample application instead. Environment details for: elasticBeanstalkExa-env Application name: elastic-beanstalk-example Region: us-west-2 Deployed Version: Sample Application Environment ID: e-j3pmc8tscn Platform: 64bit Amazon Linux 2015.03 v1.4.3 running Docker 1.6.2 Tier: WebServer-Standard CNAME: eb-dev.elasticbeanstalk.com Updated: 2015-06-27 01:02:24.813000+00:00 Printing Status: INFO: createEnvironment is starting. -- Events -- (safe to Ctrl+C) Use "eb abort" to cancel the command.

环境可能需要几分钟才能就绪。创建环境后,按 Ctrl+C 可返回命令行。

Eb status

运行 eb status 可查看环境的当前状态。如果状态为 ready,则示例应用程序在 elasticbeanstalk.com 可用,并且环境可以更新。

~/eb$ eb status Environment details for: elasticBeanstalkExa-env Application name: elastic-beanstalk-example Region: us-west-2 Deployed Version: Sample Application Environment ID: e-gbzqc3jcra Platform: 64bit Amazon Linux 2015.03 v1.4.3 running Docker 1.6.2 Tier: WebServer-Standard CNAME: elasticbeanstalkexa-env.elasticbeanstalk.com Updated: 2015-06-30 01:47:45.589000+00:00 Status: Ready Health: Green

Eb health

使用 eb health 命令查看有关您环境中的实例的运行状况信息以及环境的整体状态。使用 --refresh 选项可在一个每 10 秒更新一次的交互式视图中查看运行状况。

~/eb$ eb health api Ok 2016-09-15 18:39:04 WebServer Java 8 total ok warning degraded severe info pending unknown 3 3 0 0 0 0 0 0 instance-id status cause health Overall Ok i-0ef05ec54918bf567 Ok i-001880c1187493460 Ok i-04703409d90d7c353 Ok instance-id r/sec %2xx %3xx %4xx %5xx p99 p90 p75 p50 p10 Overall 8.6 100.0 0.0 0.0 0.0 0.083* 0.065 0.053 0.040 0.019 i-0ef05ec54918bf567 2.9 29 0 0 0 0.069* 0.066 0.057 0.050 0.023 i-001880c1187493460 2.9 29 0 0 0 0.087* 0.069 0.056 0.050 0.034 i-04703409d90d7c353 2.8 28 0 0 0 0.051* 0.027 0.024 0.021 0.015 instance-id type az running load 1 load 5 user% nice% system% idle% iowait% i-0ef05ec54918bf567 t2.micro 1c 23 mins 0.19 0.05 3.0 0.0 0.3 96.7 0.0 i-001880c1187493460 t2.micro 1a 23 mins 0.0 0.0 3.2 0.0 0.3 96.5 0.0 i-04703409d90d7c353 t2.micro 1b 1 day 0.0 0.0 3.6 0.0 0.2 96.2 0.0 instance-id status id version ago deployments i-0ef05ec54918bf567 Deployed 28 app-bc1b-160915_181041 20 mins i-001880c1187493460 Deployed 28 app-bc1b-160915_181041 20 mins i-04703409d90d7c353 Deployed 28 app-bc1b-160915_181041 27 mins

Eb events

使用 eb events 可查看 Elastic Beanstalk 输出的事件列表。

~/eb$ eb events 2015-06-29 23:21:09 INFO createEnvironment is starting. 2015-06-29 23:21:10 INFO Using elasticbeanstalk-us-east-2-EXAMPLE as Amazon S3 storage bucket for environment data. 2015-06-29 23:21:23 INFO Created load balancer named: awseb-e-g-AWSEBLoa-EXAMPLE 2015-06-29 23:21:42 INFO Created security group named: awseb-e-gbzqc3jcra-stack-AWSEBSecurityGroup-EXAMPLE ...

Eb logs

使用 eb logs 可从环境中的实例推送日志。默认情况下,eb logs 从启动的第一个实例推送日志并将其显示在标准输出中。通过 --instance 选项指定实例 ID 可从特定实例获取日志。

--all 选项从所有实例提取日志并将它们保存到 .elasticbeanstalk/logs 下的子目录中。

~/eb$ eb logs --all Retrieving logs... Logs were saved to /home/local/ANT/mwunderl/ebcli/environments/test/.elasticbeanstalk/logs/150630_201410 Updated symlink at /home/local/ANT/mwunderl/ebcli/environments/test/.elasticbeanstalk/logs/latest

Eb open

要在浏览器中打开环境的网站,请使用 eb open

~/eb$ eb open

在窗口式环境中,默认浏览器将在新窗口中打开。在终端环境中,如果可用,将使用命令行浏览器 (例如 w3m)。

Eb deploy

环境启动并就绪后,可以使用 eb deploy 更新环境。

此命令与一些源代码使用可以更好地捆绑和部署,对于此示例,我们已在项目目录中使用以下内容创建了一个 Dockerfile

~/eb/Dockerfile

FROM ubuntu:12.04 RUN apt-get update RUN apt-get install -y nginx zip curl RUN echo "daemon off;" >> /etc/nginx/nginx.conf RUN curl -o /usr/share/nginx/www/master.zip -L https://codeload.github.com/gabrielecirulli/2048/zip/master RUN cd /usr/share/nginx/www/ && unzip master.zip && mv 2048-master/* . && rm -rf 2048-master master.zip EXPOSE 80 CMD ["/usr/sbin/nginx", "-c", "/etc/nginx/nginx.conf"]

Dockerfile 部署一个 Ubuntu 12.04 映像并安装游戏 2048。运行 eb deploy 可将该应用程序上传到您的环境中:

~/eb$ eb deploy Creating application version archive "app-150630_014338". Uploading elastic-beanstalk-example/app-150630_014338.zip to S3. This may take a while. Upload Complete. INFO: Environment update is starting. -- Events -- (safe to Ctrl+C) Use "eb abort" to cancel the command.

运行 eb deploy 时,EB CLI 会捆绑项目目录中的内容并将其部署到环境中。

注意

如果您已在项目文件夹中初始化了一个 Git 存储库,EB CLI 将始终部署最新提交,即使您有挂起的更改。在运行 eb deploy 前提交更改可将它们部署到环境中。

Eb config

使用 eb config 命令可查看您的运行环境可用的配置选项:

~/eb$ eb config ApplicationName: elastic-beanstalk-example DateUpdated: 2015-06-30 02:12:03+00:00 EnvironmentName: elasticBeanstalkExa-env SolutionStackName: 64bit Amazon Linux 2015.03 v1.4.3 running Docker 1.6.2 settings: AWSEBAutoScalingScaleDownPolicy.aws:autoscaling:trigger: LowerBreachScaleIncrement: '-1' AWSEBAutoScalingScaleUpPolicy.aws:autoscaling:trigger: UpperBreachScaleIncrement: '1' AWSEBCloudwatchAlarmHigh.aws:autoscaling:trigger: UpperThreshold: '6000000' ...

此命令将可用配置选项列表填充在文本编辑器中。所示的很多选项带有 null 值,这不是默认设置,可以进行修改以更新环境中的资源。有关这些选项的更多信息,请参阅配置选项

Eb terminate

如果现在环境使用完毕,可使用 eb terminate 将其终止。

~/eb$ eb terminate The environment "eb-dev" and all associated instances will be terminated. To confirm, type the environment name: eb-dev INFO: terminateEnvironment is starting. INFO: Deleted CloudWatch alarm named: awseb-e-jc8t3pmscn-stack-AWSEBCloudwatchAlarmHigh-1XLMU7DNCBV6Y INFO: Deleted CloudWatch alarm named: awseb-e-jc8t3pmscn-stack-AWSEBCloudwatchAlarmLow-8IVI04W2SCXS INFO: Deleted Auto Scaling group policy named: arn:aws:autoscaling:us-east-2:123456789012:scalingPolicy:1753d43e-ae87-4df6-a405-11d31f4c8f97:autoScalingGroupName/awseb-e-jc8t3pmscn-stack-AWSEBAutoScalingGroup-90TTS2ZL4MXV:policyName/awseb-e-jc8t3pmscn-stack-AWSEBAutoScalingScaleUpPolicy-A070H1BMUQAJ INFO: Deleted Auto Scaling group policy named: arn:aws:autoscaling:us-east-2:123456789012:scalingPolicy:1fd24ea4-3d6f-4373-affc-4912012092ba:autoScalingGroupName/awseb-e-jc8t3pmscn-stack-AWSEBAutoScalingGroup-90TTS2ZL4MXV:policyName/awseb-e-jc8t3pmscn-stack-AWSEBAutoScalingScaleDownPolicy-LSWFUMZ46H1V INFO: Waiting for EC2 instances to terminate. This may take a few minutes. -- Events -- (safe to Ctrl+C)

有关可用 EB CLI 命令的完整列表,请参阅 EB CLI 命令参考

重要

如果您终止环境,则还必须删除您创建的任何 CNAME 映射,因为其他客户可能会重用可用的主机名。请务必删除指向已终止环境的 DNS 记录,以防出现 悬空 DNS 条目。悬空 DNS 条目可能会使指向您的域的互联网流量出现安全漏洞,此外还可能带来其他风险。

有关更多信息,请参阅《Amazon Route 53 开发人员指南》中的 防止 Route 53 中悬挂委派记录。您还可以访问《AWS 安全博客》中的 针对 Amazon CloudFront 请求的增强域保护,了解有关悬空 DNS 条目的更多信息。