2015年7月27日 星期一

AWS 筆記 - 使用 AWS Elastic Beanstalk 發佈 Docker 維護的專案 @ Mac OS X 10.10.4

用 Docker 是有用過,但統一流程發佈出去倒沒幾次,現在就抽空碰一下 AWS Elastic Beanstalk 吧!目標就是只需提供 Docker 維護的 Project 直接交遞到 AWS 上,接著就要生出一台 app service 就是了,過程中可不是要自己先開台 EC2 、裝 Docker 環境等。

參考文件:
連續動作:

$ mkdir project && cd project
$ sudo pip install awsebcli
$ eb init
You have not yet set up your credentials or your credentials are incorrect
You must provide your credentials.
(aws-access-id): XXXXXXXXX
(aws-secret-key): XXXXXXXXX

Enter Application Name
(default is "project"):


結果發生忘了設定 IAM 權限,立馬添加 AWSElasticBeanstalkFullAccess 啦,再重新執行一次

$ eb init
Select a default region
1) us-east-1 : US East (N. Virginia)
2) us-west-1 : US West (N. California)
3) us-west-2 : US West (Oregon)
4) eu-west-1 : EU (Ireland)
5) eu-central-1 : EU (Frankfurt)
6) ap-southeast-1 : Asia Pacific (Singapore)
7) ap-southeast-2 : Asia Pacific (Sydney)
8) ap-northeast-1 : Asia Pacific (Tokyo)
9) sa-east-1 : South America (Sao Paulo)

Enter Application Name
(default is "project"):

Select a platform.
1) Node.js
2) PHP
3) Python
4) Ruby
5) Tomcat
6) IIS
7) Docker
8) Multi-container Docker
9) GlassFish
10) Go
(default is 1): 7

Do you want to set up SSH for your instances?
(y/n): y

Select a keypair.
1) changyy
2) [ Create new KeyPair ]
(default is 2):


以上就收工了 XD 在本機端的 project 裡頭,會有 .elasticbeanstalk 跟 .gitignore 的產生,接著再把一些 Dockerfile 與相關程式碼資料都搬進來吧,此例以架設 web server 為例

$ wget https://raw.githubusercontent.com/changyy/docker-study/web-server/Dockerfile
$ eb create dev-web
Creating application version archive "app-1507##_######".
Uploading project/app-1507##_######.zip to S3. This may take a while.
Upload Complete.
Environment details for: dev-web
  Application name: project
  Region: us-west-2
  Deployed Version: app-1507##_######
  Environment ID: e-#########
  Platform: 64bit Amazon Linux 2015.03 v1.4.3 running Docker 1.6.2
  Tier: WebServer-Standard
  CNAME: UNKNOWN
  Updated: 2015-07-27 03:52:28.037000+00:00
Printing Status:
INFO: createEnvironment is starting.
INFO: Using elasticbeanstalk-us-west-2-######### as Amazon S3 storage bucket for environment data.
INFO: Created load balancer named: awseb-e-p-AWSEBLoa-##########
INFO: Created security group named: awseb-e-#########-stack-AWSEBSecurityGroup-########
INFO: Created Auto Scaling launch configuration named: awseb-e-#########-stack-AWSEBAutoScalingLaunchConfiguration-NU2G8F2JA2BT
INFO: Created Auto Scaling group named: awseb-e-#########-stack-AWSEBAutoScalingGroup-############
INFO: Waiting for EC2 instances to launch. This may take a few minutes.
INFO: Created Auto Scaling group policy named: arn:aws:autoscaling:us-west-2:############:scalingPolicy:####-####-####-####-#####:autoScalingGroupName/awseb-e-#########-stack-AWSEBAutoScalingGroup-############:policyName/awseb-e-#########-stack-AWSEBAutoScalingScaleUpPolicy-####
INFO: Created Auto Scaling group policy named: arn:aws:autoscaling:us-west-2:############:####-####-####-####-#####:autoScalingGroupName/awseb-e-#########-stack-AWSEBAutoScalingGroup-############:policyName/awseb-e-#########-stack-AWSEBAutoScalingScaleDownPolicy-####
INFO: Created CloudWatch alarm named: awseb-e-#########-stack-AWSEBCloudwatchAlarmHigh-############
INFO: Created CloudWatch alarm named: awseb-e-#########-stack-AWSEBCloudwatchAlarmLow-############
INFO: Successfully pulled ubuntu:14.04
INFO: Successfully built aws_beanstalk/staging-app
INFO: Docker container ############ is running aws_beanstalk/current-app.
INFO: Added EC2 instance 'i-############' to Auto Scaling Group 'awseb-e-#########-stack-AWSEBAutoScalingGroup-############'.
INFO: Adding instance 'i-############' to your environment.
INFO: Successfully launched environment: dev-web


如此一來,服務就跑起來了 :P 整個過程 AWS Elastic Beanstalk 使用了 S3 儲存開發環境、開了一台 EC2 機器、啟用一個 ELB 單位、添加兩個 cloudwatch 關注服務情況(開新機器或關機器)以及一個 Auto scaling group 來確保服務的不中斷。

如此一來,可以再用此指令開啟瀏覽位置

$ eb open

接著就會開啟 browser 瀏覽了,不需自行在找尋找位置。

另外,有興趣的可以用以下指令研究:

$ eb status
Environment details for: dev-web
  Application name: project
  Region: us-west-2
  Deployed Version: 4a06
  Environment ID: e-######
  Platform: 64bit Amazon Linux 2015.03 v1.4.3 running Docker 1.6.2
  Tier: WebServer-Standard
  CNAME: dev-web-######.elasticbeanstalk.com
  Updated: 2015-07-27 06:15:45.859000+00:00
  Status: Ready
  Health: Green


$ eb logs
...
-------------------------------------
/var/log/docker-ps.log
-------------------------------------
'docker ps' ran at Mon Jul 27 06:34:11 UTC 2015:
CONTAINER ID        IMAGE                              COMMAND               CREATED             STATUS              PORTS                     NAMES
#########        aws_beanstalk/staging-app:latest   "/usr/sbin/apache2ctl -D FOREGROUND"   19 minutes ago      Up 19 minutes       80/tcp, 443/tcp   furious_feynman  
...


$ eb config
...


$ eb terminate
The environment "dev-web" and all associated instances will be terminated.
To confirm, type the environment name:



以上就差不多了,若有興趣的可以試一下另一個 dockfile: https://github.com/changyy/docker-study/tree/web-ssh-server,除了要下載 Dockfile 外,還要下載 start_service.sh ,其中後者可以參考 Docker 官方文件,改用 Supervisor 也行(https://docs.docker.com/articles/using_supervisord/)。機器開啟後,多了 ssh server 在執行。這時先去 EC2 查詢是哪一台機器,用 ssh ec2-user@server_ip 登入看看。

沒有留言:

張貼留言