Weitere ähnliche Inhalte
Ähnlich wie [AWSマイスターシリーズ] AWS Elastic Beanstalk (20)
Mehr von Amazon Web Services Japan (20)
Kürzlich hochgeladen (12)
[AWSマイスターシリーズ] AWS Elastic Beanstalk
- 1. re:G
ene
AWS マイスターシリーズ
rate
AWS ElasticBeanstalk
アマゾン データサービス ジャパン株式会社
ソリューションアーキテクト
安川 健太
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 2. Agenda
! AWS Elastic Beanstalkとは?
• Elastic Beanstalkの概要と⽤用語説明
! 開発ワークフローに沿った各種機能紹介
• デプロイ
• ログの確認や監視、Auto Scalingの設定
• 環境の追加とスワッピング
! Advanced Tips
• VPCへのデプロイ
• 設定ファイルによるコンテナカスタマイズ
• 他サービスとの連携
! まとめ
2
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 6. 各環境の中には
! ⾃自動で構築された⾼高可⽤用構成
! カスタマイズも容易易
• SSHでログイン, パッケージ追加
• AMIも差替え可
Application
Environment
URL
Environment Configuration
AMI
Admin
AWS Management
Console
Instance
Users
Elastic Load
Balancer
Amazon RDS
SSH
Instance
CloudWatch
Auto Scaling Group
Developer
WAR
6
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 8. ⽤用語説明
! Application
• トップレベルの論論理理単位
• Applicationの中に、EnvironmentやVersionが⼊入る
• デプロイするアプリケーション(WAR/ZIPファイル)と混同し
やすいので注意
! Environment
• アプリをデプロイする環境
• 環境毎にURLが付与される
• URLは、XXX.elasticbeanstalk.com
• 各環境毎にEC2、ELBなどが⽴立立ち上がる
• ログも環境毎に取得できる(1時間おきにS3へ送信可能)
8
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 10. サポートするアプリケーションコンテナ
! 使⽤用可能な⾔言語/環境
Java
Python
PHP
.NET
Ruby
言語
コンテナ
Java
Apache +Tomcat6,
Apache+ Tomcat7
.NET
IIS7.5, IIS8
PHP
Apache + mod_php
Python
Apache + mod_wsgi
Ruby
Nginx + Passenger
Node.js
nodeJS
Nginx / Apache + node.js
10
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 11. 環境の作成や操作の⽅方法
! マネージメントコンソール
! 各種IDE
マネージメントコンソール
• Eclipse
à Javaアプリのデプロイに対応
• VisualStudio
à .Netアプリのデプロイに対応
! AWS SDK
Eclipse
• Java, PHP, Ruby, Python, .Net …
! AWS CLI
$ aws elasticbeanstalk …
! コマンドラインツール eb
11
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 12. eb: CLIでElasticBeanstalk環境を操作
! eb init
§ アプリケーションの初期設定
! eb start
§ アプリケーション環境の開始
! eb status
§ ステータスの確認
! eb update
§ アプリケーション環境の設定更更新
! eb stop
§ アプリケーション環境の停⽌止
! eb delete
§ アプリケーションの削除
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 13. ebによるライフサイクル管理理
ローカルに設定
ファイルを⽣生成
eb
init
アプリケーションの作成
環境の起動
• インスタンス起動
• ELB設置
• Autoscaling設定
eb
update
eb
start
Configuration
Created
Launching
eb
delete
Ready
eb stop
eb
delete
Updating
eb start
Terminating
アプリケーションの
削除
各種構成変更更, e.g.:
• インスタンスタイプ変更更
• Autoscaling設定変更更
Not
Running
環境の停⽌止
• インスタンスの破棄
• ELBの破棄
(アプリケーションは残る)
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 14. Agenda
! Elastic Beanstalkとは?
• Elastic Beanstalkの概要と⽤用語説明
! 開発ワークフローに沿った各種機能紹介
• デプロイ
• ログの確認や監視、Auto Scalingの設定
• 環境の追加とスワッピング
! Advanced Tips
• VPCへのデプロイ
• 設定ファイルによるコンテナカスタマイズ
• 他サービスとの連携
! まとめ
14
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 18. アプリケーションと環境の作成
! 初期バージョンにアクセス
Click
18
18
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 20. コードのデプロイ
! ⼤大きく分けて2種類
• WARやZIPなどのアーカイブをアップロード
• GitでPush
! いずれにせよS3にアーカイブが作られ、各インスタンスはそれをダ
ウンロードしてデプロイ作業を⾏行行う
コード
WAR/ZIP
Developer
デプロイ
指示
20
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 21. ZIPやWARをアップロード
! マネージメントコンソールから
21
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 23. Gitでコードをデプロイ
〜~準備編〜~
! ElasticBeanstalkのCLIツールをダウンロード
• 下記URLから最新版のURLを取得
• http://aws.amazon.com/code/6752709412171743
• ダウンロードと解凍(下記例例ではVersion 2.5.1)
$ wget
https://s3.amazonaws.com/elasticbeanstalk/cli/AWS-ElasticBeanstalkCLI-2.5.1.zip
$ unzip AWS-ElasticBeanstalk-CLI-2.5.1.zip
23
23
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 24. Gitでコードをデプロイ
〜~プロジェクトセットアップ編〜~
! プロジェクトを作成して初期化
$ mkdir ebapp # rails なら rails new ebapp
$ cd ebapp
$ git init
! AWS Dev Toolを使ってデプロイに関するコンフィグ
• スクリプトを⾛走らせてgit の拡張コマンドをインストール
$ ~/AWS-ElasticBeanstalk-CLI-2.5.1/AWSDevTools/Linux/AWSDevTools-RepositorySetup.sh
• aws.configコマンドでデプロイに関するコンフィグを設定
24
$ git aws.config
AWS Access Key: AKIXXXXXXXXXXXXXXXX
AWS Secret Key: zGt9B9XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
AWS Region [default to us-east-1]: ap-northeast-1
AWS Elastic Beanstalk Application: My First Elastic Beanstalk Application
AWS Elastic Beanstalk Environment: Default-Environment
24
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 25. Security Credentialsの取得
! 作成したIAMユーザの権限を設定
25
25
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 26. Security Credentialsの取得
26
26
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 27. Gitでコードをデプロイ
〜~実施編〜~
! Git でローカルレポジトリに変更更をコミット
$ git add .
$ git commit -m "initial commit"
! Beanstalkの環境にコードをデプロイ
$ git aws.push
Counting objects: 3, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 306 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
remote:
To https://AKIA....
* [new branch]
HEAD -> master
27
27
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 29. バージョンレポジトリの確認
1. Running Version下のUpload and Deployをクリック
バージョンタブを選択
2. All Versionsをクリック
29
29
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 32. RDSインスタンスの追加と参照
! RDSインスタンスを環境に紐紐づ
けて作成可能
! RDSインスタンスへの接続情報
は環境変数に⾃自動で設定される
production:
adapter: mysql2
encoding: utf8
database: <%= ENV['RDS_DB_NAME'] %>
username: <%= ENV['RDS_USERNAME'] %>
password: <%= ENV['RDS_PASSWORD'] %>
host: <%= ENV['RDS_HOSTNAME'] %>
port: <%= ENV['RDS_PORT'] %>
Instance
Amazon
RDS
Elastic
Load
Balancer
Instance
CloudWatch
Auto scaling Group
32
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 33. 環境設定:S3へのログローテーション
! S3へのログのローテーションを設定
• S3へのアクセスに⽤用いるIAM Roleを設定
• ログのローテーションを有効化
ログ
33
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 34. 環境設定:環境変数の設定
! 任意の環境変数を設定可能
• 設定した値は環境変数としてアプリケーションから参照可
34
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 37. Auto Scalingの設定
! Scalingに関する設定画⾯面を開く
37
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 38. Auto Scalingの設定投⼊入
! CPU負荷でスケールアウト・インする設定の例例
画面下までスクロール
変更 à CPUUtilization
変更 à Percent
変更 à 70
変更 à 30
38
38
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 39. イベントログの確認
39
39
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 44. Route53を使えば柔軟な移⾏行行や併⽤用も可
! DNSの切切り替えでリクエストを新環境へ移⾏行行
! その際、Weighted Round Robinを使えば任意の⽐比率率率で徐々に移
⾏行行可
Weighted Transitionパターン
Route 53
Old Environment
44
Auto scaling Group
New Environment
Auto scaling Group
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 46. eb + gitでコマンドラインのみで開発
!
ElasticBeanstalk⽤用CLIであるebとGitを⽤用いるとコマンドライ
ンのみで開発からデプロイまで実⾏行行可
§ プロジェクトディレクトリの作成と初期化
$
$
$
$
mkdir project
cd project
git init
eb init
# AWSのCredential、リージョン、コンテナ等を設定
§ アプリケーションの開始
$ eb start
# 設定内容に従ってアプリケーション環境が起動
§ アプリケーション環境の状態確認
$ eb staus
# StatusがReadyになれば起動完了了
§ コードをデプロイ
$ git aws.push
# コンテナのカスタマイズ、コードのデプロイが実⾏行行される
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 47. eb + gitでコマンドラインのみで開発
! eb branchコマンドでgitブランチごとにデプロイ先環境
を設定する事も
§ 新しいgitブランチを作成
$ git checkout –b new_branch
Switched to a new branch 'new_branch'
§ new_̲branch⽤用の環境を作成(環境設定を引き継ぎ)
$ eb branch
The current branch is "new_branch".
Enter an AWS Elastic Beanstalk environment name (auto-generated value
is “sample-newbranch-env"):
Do you want to copy the settings from environment "nodesample-env" for
the new branch? [y/n]: y
§ new_̲branch⽤用の新環境をスタート
$ eb start
Starting application "sample".
Waiting for environment "sample-newbranch-env" to launch.
2013-10-08 08:12:59
INFO
createEnvironment is starting.
…
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 48. Agenda
! Elastic Beanstalkとは?
• Elastic Beanstalkの概要と⽤用語説明
! 開発ワークフローに沿った各種機能紹介
• デプロイ
• ログの確認や監視、Auto Scalingの設定
• 環境の追加とスワッピング
! Advanced Tips
• VPCへのデプロイ
• 設定ファイルによるコンテナカスタマイズ
• 他サービスとの連携
! まとめ
48
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 49. Advanced Tips
-‐‑‒VPCへのデプロイ-‐‑‒
49
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 50. VPC内へのデプロイ
! Elastic BeanstalkアプリケーションはVPCへデプロイ可(*)
• VPCは要別途作成
! ELBとEC2が別々の
subnetにデプロイされる
• VPCの参考例例
http://docs.aws.amazon.com/
elasticbeanstalk/latest/dg/
AWSHowTo-vpc-basic.html
(*) 2013年4月に.NETコンテナもVPC対応。
Legacyコンテナを除く
50
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 51. マネージメントコンソールで
デプロイ先サブネットを選択
! 設定時の注意
• ELB, EC2は各AZに1つ
サブネットが必要
• ELBとEC2は異異なる
サブネットが必要
• RDSは最低2つの
サブネットが必要
51
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 53. コンフィグファイル
! アプリケーションコンテナをカスタマイズ
• 独⾃自AMIを⽤用いなくても必要なコンポーネントの追加が可能に
! アプリケーションリポジトリ内に下記の形式で配置
• /.ebextensions/*.config
• ファイルは複数配置可能。ファイルは名前の順に処理理される
• リポジトリ内に配置するのでgit等での管理理も容易易
yumでgitをインストールしつつ、
特定のシェルスクリプトを実行する例
packages:
yum:
git: []
commands:
01prepare:
command: “scripts/prepare.sh”
53
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 54. コンフィグファイルで実⾏行行可能な操作
! packages
• yumやrpmを利利⽤用したパッケージのインストール
! sources
• 外部からのアーカイブ(tarなど)をダウンロードして指定した場所に展開
! files
• 指定した場所にファイルを作成
• 外部からのダウンロードしたファイルの配置も可
! services
• serviceを起動したり、起動設定を変更更したりする
! users/groups
• 任意のユーザー/グループを作成
詳細情報:
http://docs.amazonwebservices.com/elasticbeanstalk/latest/dg/customize-‐‑‒containers.html
54
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 55. コンフィグファイルで実⾏行行可能な操作
! commands
• デプロイ処理理前に実⾏行行すべきコマンドやスクリプトを指定
• 例例:OSやミドルウェアの設定変更更
!
container_̲commands
• 新バージョンの展開後に実⾏行行すべきコマンドやスクリプトを指定
• 例例:アプリ内のリソースや設定の環境に応じた変更更など
!
option_̲settings
• 環境変数の設定
! Resources
• 追加のリソースを定義
• 例例: SQSのキュー、DynamoDBのテーブル、CloudWatchのアラーム
55
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 56. コンフィグ例例ファイル例例 1
! 例例:サーバ監視のためにNew Relicエージェントを
インストール
.ebextensions/01newrelic.config
packages:
yum:
newrelic-sysmond: []
rpm:
newrelic:
http://yum.newrelic.com/pub/newrelic/el5/x86_64/newrelicrepo-5-3.noarch.rpm
commands:
configure_new_relic:
command: nrsysmond-config --set license_key=XXXXX
56
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 57. コマンド実⾏行行の設定に関する詳細
! CommandsとContainer Commandsの違い
Commands
Container Commands
実行タイミング
デプロイ直後、バージョン
更新前
更新バージョンのセット
アップ後、切り替え直前
コンテナ環境変数の
自動読み込み
無し
有り
leader_onlyフラグの 無し
サポート
有り
container_commands:
leader_only_command:
command: <command to run>
leader_only: true
every_node_command:
command: <command to run>
Leader
57
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 59. コンフィグ例例ファイル例例 2
! 例例:コンパイル済みアセットをダウンロードしてデプロ
イ時間を短縮
files:
"/opt/elasticbeanstalk/hooks/appdeploy/pre/01a_bootstraup.sh":
mode: "000755"
owner: root
group: root
content: |
#!/usr/bin/env bash
cd /var/app/ondeck/public
s3cmd get s3://mybucket/assets.tgz
tar xvzf assets.tgz
59
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 60. コンフィグ例例ファイル例例 3
! 例例:デプロイ後に実⾏行行されるフックを利利⽤用してバックグ
ラウンドワーカーを動かす
files:
"/opt/elasticbeanstalk/hooks/appdeploy/post/99_start_resque_worker.sh":
mode: "000755"
owner: root
group: root
content: |
#!/usr/bin/env bash
. /opt/elasticbeanstalk/support/envvars
cd $EB_CONFIG_APP_CURRENT
if [ -e /var/run/resque.pid ]; then
kill `cat /var/run/resque.pid`
fi
chown $EB_CONFIG_APP_USER /var/run/resque.pid
su -c "PIDFILE=/var/run/resque.pid BACKGROUND=yes RAILS_ENV=production
QUEUE=default bundle exec rake environment resque:work" $EB_CONFIG_APP_USER
60
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 62. 他サービスのリソースを定義する例例
! コンフィグファイルのResourcesセクションに定義
Resources:
<name of resource>:
Type: <resource type identifier>
Properties:
<property name>: <value>
! サポートするサービス
•
•
•
•
•
DynamoDB
ElastiCache
SQS
SNS
CloudWatch
注:デプロイするユーザに各サービスを
操作する権限が必要
62
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 63. 例例:DynamoDBのテーブルの作成と参照
.ebextensions/sessiontable.config
Resources:
SessionTable:
Type: AWS::DynamoDB::Table
Properties:
KeySchema:
HashKeyElement:
AttributeName: "username"
AttributeType: "S"
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
files:
"/var/app/support/sessiontable":
mode: "000444"
content: |
`̀{"Ref" : "SessionTable"}`̀
`̀{"Ref" : "AWS::Region"}`̀
63
リソースタイプ識別子
リソースのプロパティを設定
(リソースタイプごとにプロパティは異なる)
Ref演算子を使った参照
Fn::GetAtt, Fn::Join
などの演算子や、
CloudFormationで定
義された変数も利用可
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 64. Agenda
! Elastic Beanstalkとは?
• Elastic Beanstalkの概要と⽤用語説明
! 開発ワークフローに沿った各種機能紹介
• デプロイ
• ログの確認や監視、Auto Scalingの設定
• 環境の追加とスワッピング
! Advanced Tips
• VPCへのデプロイ
• 設定ファイルによるコンテナカスタマイズ
• 他サービスとの連携
! まとめ
64
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 65. まとめ
! ElasticBeanstalkを使えばアプリケー
ション環境の構築やデプロイを⾃自動化
可能
! 他のAWSのサービスとの連携も充実
! 設定ファイルを⽤用いれば柔軟な環境カ
スタマイズも可
環境構築やデプロイはElasticBeanstalkに任せて
Happy Coding!
65
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.