如何無痛上雲端? 以Elastic Beanstalk Java Container為例 ! Study Hsueh <ph.study@gmail.com> 2013-11-07 !1
上雲端 Deploy application to VPS (Virtual Private Server) or Cloud Web Application !2
Cloud Web Application Web Application is powered by cloud computing Elasticity: 能隨時主動增加或刪減資源 Scalability: 資源的增減直接反應到負載上 Reliability: 應⽤用程式不會因為部分故障, 就無 法提供服務 !3
Cloud Web Application Architecture for Web Application Hosting !4 http://media.amazonwebservices.com/architecturecenter/AWS_ac_ra_web_01.pdf
Cloud Web Application Build a cloud web application in AWS, you may need to know… } Elastic Load Balancing (ELB) Auto Scaling Cloud Watch Elastic Compute Cloud (EC2) Deployment Management Monitoring !5 Infrastructure
有沒有簡單⼀一點的⽅方法上雲端? !6
有! ⽤用PaaS吧! PaaS: Platform as a Service !7
可是聽說PaaS.... White List Black Box Platform Dependent Services/Libraries Software Versions …etc !8
試試Elastic Beanstalk吧:) !9
Elastic Beanstalk (EB) Elastic Beanstalk是架構在AWS infrastructure 上的PaaS服務, 使⽤用Elastic Beanstalk可以同 時具備控制底層IaaS的能⼒力與PaaS的便利 Elastic Beanstalk的縮寫是EB (不是EBS, EBS是Elastic Block Store的縮寫) !10
Elastic Beanstalk !11
Concept(1) Elastic Beanstalk Application Environment URL(CNAME) !12
Concept(2) Applications Environments URL !13
Supported Platforms !14
Java Container OS: x32/x64 Amazon Linux (2012.09) Java: OpenJdk 1.6.0_24 Web Container: T omcat 6.0.35 & 7.0.27 !15
Customization !16
Elastic Beanstalk可以裝第三⽅方的程式嗎? 像是phantomjs, imagemagick…之類 或把T omcat換成T omEE? !17
Customizing Environment Elastic Beanstalk provides two mechanisms to customize environment Custom AMI Configuration File (Oct 2. 2012) including AWS resources (e.g. ElastiCache) !18
Configuration File(1) Configuration File in WAR file !19
Configuration File(2) Configuration File Example YAML format !20
Deployment !21
AWS Management Console Upload WAR file !22
AWS T oolkit for Eclipse !23
Command-line interface $ git init # init git repository
 $ eb init # init EB Application Configuration
 $ eb start # start EB Environment
 $ git add . # track all files
 $ git commit -m “Initial Commit” # commit
 $ git aws.push # push to EB !24
Deploying with Zero Downtime Swap Environments URLs (CNAMEs) It usually takes 2-3 minutes to swap !25
Management !26
Application versions !27
Monitoring !28
Health Check EC2 Instance TCP:80 for nonlegacy containers HTTP:80 for legacy containers Application health check URL HTTP 200 OK= Health !29
Usage !30
Processes Use Monit to monitor processes Apache HTTP Server Apache T omcat !31
Pricing !32
Pricing Elastic Beanstalk is free, you only pay for the underlying AWS resources Single-instance T ype EC2 instance Load-balancing, Autoscaling T ype EC2 instance(s) Elastic Load Balancing !33
準備好上雲端了嗎? Cloud Ready? !34
如果程式有⽤用到下列其中⼀一個... HTTP Session File System User Data Storage File Cache !35
對不起!還不能上雲端:-( !36
HTTP Session Problem !37
HTTP Session Problem(1) Server#1 Server#2 Session Session ABAB CDCD table table User#1 User#2 JSESSIONID: CDCD JSESSIONID: ABAB !38
HTTP Session Problem(2) Server#1 Server#2 Session Session table table ABAB CDCD Load Balancer 
 Invalid Session uses Round Robin for Server#2 redirect User#1 User#2 JSESSIONID: CDCD JSESSIONID: ABAB !39
Solutions for HTTP Session 改程式的做法 Keep Stateless (use access token) 不改程式的做法 Session Persistence Sticky Sessions (Not working for Auto Scaling) !40
Sticky Sessions Problem(1) Server#1 Server#2 Session Session ABAB CDCD table table Load Balancer 
 Server Session enables Sticky Sessions Server#1 ABAB Server#2 CDCD User#1 User#2 JSESSIONID: CDCD JSESSIONID: ABAB !41
Sticky Sessions Problem(2) Server#1 Server#2 Session Scale in or
 Server Failure table ABAB Invalid Session Server Session for Server#1 Server#1 ABAB Server#2 CDCD User#1 User#2 JSESSIONID: CDCD JSESSIONID: ABAB !42
Session Persistence Shared Session table Server#1 ABAB CDCD User#1 Server#2 User#2 JSESSIONID: CDCD JSESSIONID: ABAB !43
Session Managers for T omcat PersistentManager (JDBC Based Store) http://tomcat.apache.org/tomcat-7.0-doc/config/manager.html#Nested_Components Memcached Session Manager http://code.google.com/p/memcached-session-manager Redis Session Manager https://github.com/jcoleman/tomcat-redis-session-manager Amazon DynamoDB Session Manager https://github.com/aws/aws-dynamodb-session-tomcat/ !44
Session Managers for T omcat Q. 為什麼不使⽤用DeltaManager或 BackupManager呢? (in-memory-replication) A. EC2不⽀支援multicast與boardcast (不完全⽀支 援) !45
File System Problem !46
User Data Storage Problem(1) Server#1 Server#2 File#2 File#1 User#1 User#2 Upload File#1 Upload File#2 !47
Solutions for User Data Storage(1) Use FS for User Data Storage 改程式的做法 透過AWS SDK把檔案的存取改到S3 不改程式的做法 ⽤用S3FS或NFS !48
使⽤用S3FS需注意的地⽅方 流量 上傳: 檔案會先上傳⾄至Server, 在由Server寫⼊入S3 下載: 檔案需先由S3取出到Server, 在回傳給User 負載 無法降低Server負載 安全性 敏感的資料: 避免直接對外 ⼀一般資料: 直接把網址對外, 可降低負載與下載流量 !49
Solutions for File Cache Use FS for File Cache 改程式的做法 將file cache改存⾄至DB或Cache Clusters 不改程式的做法 ? !50
可以⽤用S3FS做File Cache嗎? S3不適合儲存File Cache Latency Read-After-Write Consistency Eventually Consistency (US Standard Region) !51
Summary Elastic Beanstalk is PaaS, but you retain full control over the underlying infrastructure Elastic Beanstalk is Free For Scalability, you should pay attention to HTTP Session File System !52
Thanks for your attention:) !53

如何無痛上雲端? 以Elastic Beanstalk Java Container為例