本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 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 請求的強化網域保護