deploying java applicationson ec2
DESCRIPTION
Traditionally, computer hardware was a scarce, expensive resource. Running performance tests often meant scavenging for machines around the office. Today, however, things are different. With Amazon’s EC2, a cluster of servers is now just a web service call away.In this presentation you will learn about design and implementation of Cloud Tools, which are a Groovy-based framework for deploying and testing Java EE applications on EC2. This framework provides a simple (internal) DSL for configuring a cluster (database + web container + apache), deploying a web application, and running performance tests using JMeter. You will learn about capabilities of EC2 and how to use it for development and deployment. We describe how we use Amazon S3 to work around EC2's lack of a persistent file system and avoid time-consuming uploads of WAR files. You will also learn how Groovy helped and hindered with the development of the Cloud Tools.TRANSCRIPT
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Running Java applications on the Amazon Elastic Compute CloudCompute Cloud
Chris RichardsonChris RichardsonAuthor of POJOs in ActionFounder of Cloud ToolsFounder of Cloud Tools
www.chrisrichardson.net
Chris Richardson – Running Java applications on Amazon EC2 Slide 1
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Overall presentation goalOverall presentation goal
Show how to useAmazon Elastic Compute CloudAmazon Elastic Compute Cloud
for developing and deploying Java applications
Chris Richardson — Running Java applications on Amazon EC2 Slide 2
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
About ChrisAbout ChrisGrew up in England and live in Oakland, CAp g ,Over twenty years of software development experience
Building object-oriented software since 1986Using Java since 1996Using J2EE since 1999Using J2EE since 1999
Author of POJOs in ActionSpeaker at JavaOne, SpringOne, NFJS, JavaPolis, Spring Experience, etc.Chair of the eBIG Java SIG in Oakland (www ebig org)(www.ebig.org)Run a consulting and training company that helps organizations build better software faster and deploy it on Amazon EC2Founder of Cloud Tools, an open-source project f d l i J li ti A EC2 for deploying Java applications on Amazon EC2: http://code.google.com/p/cloudtools
Chris Richardson — Running Java applications on Amazon EC2 Slide 3
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
AgendaAgendaCloud computing with Amazon EC2Cloud computing with Amazon EC2Using Amazon EC2Overview of Cloud ToolsOverview of Cloud ToolsDeveloping on Amazon EC2Deploying on Amazon EC2A few thoughts about GroovyA few thoughts about Groovy
Chris Richardson — Running Java applications on Amazon EC2 Slide 4
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Comp ting has come a long aComputing has come a long wayPast
Present
www.computermuseum.org.uk
www.dell.com
Yet we rarely have enough
Chris Richardson — Running Java applications on Amazon EC2 Slide 5
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Cloud computingCloud computing
A pool of highly scalable, abstracted A pool of highly scalable, abstracted infrastructure that hosts your application, and is billed by
consumptionp
Chris Richardson — Running Java applications on Amazon EC2 Slide 6
By James Staten of Forrester Research
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Power generationPower generationPast Present
Chris Richardson — Running Java applications on Amazon EC2 Slide 7
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Ama on St le Clo d Comp tingAmazon-Style Cloud ComputingElastic Compute Cloud (EC2)
On-demand computingElastic Block Storage (EBS)
"SAN on demand"SAN on demandSimple Storage Service (S3)
Stores blobs of dataSi l Q S i (SQS)Simple Queue Service (SQS)
Hosted queue-based messaging systemSimpleDBSimpleDB
Store data setsExecute queries
Pay per use services managed
by Amazon
Chris Richardson — Running Java applications on Amazon EC2
by Amazon
Slide 8
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
What is Amazon EC2?What is Amazon EC2?Virtualized computing environmentS i d h h b i APIServer instances managed through a web service APIIP addresses and host names assigned dynamicallyPay by the hour ($0.10-0.80/hour) + external bandwidth ($0 10-0 18/Gbyte)($0.10 0.18/Gbyte)
https://ec2.amazonaws.com/?Action=RunInstances&ImageId=ami-398438493
<RunInstancesResponse>…
&MaxCount=3&MinCount=3
k
</RunInstancesResponse>
cer@arrakis ~$ ssh … [email protected] login: Sun Dec 30 18:54:43 2007 from 71.131.29.181[root@domU-12-31-36-00-38-23:~]
Chris Richardson — Running Java applications on Amazon EC2 Slide 9
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Instance typesInstance typesVirtual Compute 32/ Memory Storage $/hrCores
pUnits /core*
/64Bit
y g $/
Small 1 1 32 bit 1.7G 160G 0.10
High-CPUMedium
2 2.5 32 bit 1.7G 350G 0.20
Large 2 2 64 bit 7.5G 850G 0.40
Extra Large
4 2 64 bit 15G 1690G 0.80
High-CPU XL
8 2.5 64 bit 7G 1690G 0.80
Chris Richardson — Running Java applications on Amazon EC2
* EC2 Compute Unit = 1.0-1.2 GHz 2007 Opteron or 2007 Xeon processor
Slide 10
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Operating systemsOperating systemsUse Amazon provided Machine Image (AMI)
32 bit Fedora Core 432-bit Fedora Core 464-bit Fedora Core 6
Many 3rd parties have public AMIsVarious Linux distributionsVarious Linux distributionsE.g. Redhat, RightScale
Sun provides OpenSolarisWindows is in beta:Windows is in beta:
Windows Server 2003 ($0.125-$2/hour)Optional SQL Server Standard ($1.10-3.20/hour)
Build your own Linux:Build your own Linux:Install applications starting with someone else's AMI and save itCreate an AMI from scratch
Chris Richardson — Running Java applications on Amazon EC2 Slide 11
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
One minor thingOne minor thing…
T i t i t Terminate your instance ⇒⇒
your local data is lost.y
Either very good or very bad
Chris Richardson — Running Java applications on Amazon EC2 Slide 12
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Elastic Block StorageElastic Block StorageMountable storage volumes
"On-demand SAN"Size: 1 GB to 1 TBMount on a single instanceMount on a single instance
Create snapshotsStored in S3Create new volumes from the snapshot
Cost:$0.10/GByte/month$0.10 per 1 million I/O requests
Chris Richardson — Running Java applications on Amazon EC2 Slide 13
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Elastic IP addressesElastic IP addressesInstance IP addresses are dynamically allocated on start upon start-up
Does not work well for publicly accessible services, e.g. a website
Elastic IP addresses:Elastic IP addresses:Statically allocated addressesAssociated with your account (max. 5)Att h d t i t ( bli f i b Attached to an instance (e.g. public facing web server)You configure DNS to resolve to the elastic IP address
P i iPricing:Non-attached Elastic IP address - $0.01/hour$0.10 per remap (if > 100 in a month)
Chris Richardson — Running Java applications on Amazon EC2 Slide 14
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Regions and a ailabilit onesRegions and availability zonesBy default, your database master and slave could run on the same physical host!the same physical host!Regions:
Geographically dispersed locationsCurrently only oneCurrently only one
Availability zone:Part of a regionEngineered to be insulated from failure in other zonesEngineered to be insulated from failure in other zones
Specify availability zone when launching instances:Same zone as other instances for free data transferDifferent zone for higher-availabilityg y
SLA with 99.95% region availabilityInstances have external connectivityYou can launch new instances
Chris Richardson — Running Java applications on Amazon EC2 Slide 15
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
What is the Amazon Simple Sto age Se ice (S3)?Storage Service (S3)?
Flat storage model consisting of buckets and objectsBucket has a name and contains objectsBucket – has a name and contains objectsObjects – has a key, stores 1 byte - 5GObject key can look like a path ☺
Cost:Cost:$0.15/GB-Month$0.10-0.18/GB of data transferred$0.00001-$0.000001/Web Service call$0.00001 $0.000001/Web Service callData transfers between EC2 and S3 are free of bandwidth charges
Buckets and objects can be:Public – accessible by anyonePrivate – accessible to owner, acl member
Chris Richardson — Running Java applications on Amazon EC2 Slide 16
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
S3 REST APIS3 REST APIPUT / HTTP/1.1Host: <BucketName>.s3.amazonaws.com
Create a bucket
PUT /<ObjectName> HTTP/1.1Host: <BucketName>.s3.amazonaws.com
Authorization: AWS AWSAccessKeyId:Signature…
Host: <BucketName>.s3.amazonaws.com Authorization: AWS AWSAccessKeyId:Signature……Bytes…
Create an item in a bucket
GET /<ObjectName> HTTP/1.1Host: <BucketName>.s3.amazonaws.com Authorization: AWS AWSAccessKeyId:Signature…
Download an item
DELETE /<ObjectName> HTTP/1.1Host: <BucketName>.s3.amazonaws.com Authorization: AWS AWSAccessKeyId:Signature…
Delete an item
Chris Richardson — Running Java applications on Amazon EC2
…
Slide 17
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Using EC2 and S3 togetherUsing EC2 and S3 togetherAMIs are stored in S3AMIs are stored in S3EC2 instances use S3:
Use REST APIUse REST APIStore database snapshots in S3Use 3rd party Linux file system that stores Use 3rd party Linux file system that stores data in S3Store EBS volume snapshots in S3Store EBS volume snapshots in S3
Chris Richardson — Running Java applications on Amazon EC2 Slide 18
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
So what does this mean?So what does this mean?For developersFor developers
Immediate access to many serversSimplified setupSimplified setupGreat for testing
For deploymentFor deploymentEliminates capital expensesR d i k f t t hReduces risk of success catastrophe
Chris Richardson — Running Java applications on Amazon EC2 Slide 19
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
AgendaAgendaCloud computing with Amazon EC2Cloud computing with Amazon EC2Using Amazon EC2Overview of Cloud ToolsOverview of Cloud ToolsDeveloping on Amazon EC2Deploying on Amazon EC2A few thoughts about GroovyA few thoughts about Groovy
Chris Richardson — Running Java applications on Amazon EC2 Slide 20
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Signing up for Amazon Web Se icesServices
AWS access identifiers:
Account IdAccess IdSecret keyPrivate key and Private key and certificate
Only takes a few minutes
Chris Richardson — Running Java applications on Amazon EC2 Slide 21
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
EC2 API and ToolsEC2 API and ToolsSOAP and Query APIs
Launch and manage instances etcAmazon provided CLI tools
CLI equivalents of APIsCLI equivalents of APIsAMI creation tools
AWS CLI tools from Tim KayCLI f S3 d EC2CLI for S3 and EC2Alternatives to Amazon CLI tools
ElasticFoxa oAwesome Firefox pluginLaunch and manage instances
Chris Richardson — Running Java applications on Amazon EC2 Slide 22
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Using the Query APIUsing the Query APIhttps://ec2.amazonaws.com/?queryparameters...M d Mandatory parameters:
Action – what to doAWSAccessKeyId – your access idAWSAccessKeyId your access idVersion – API versionTimestamp – when request was madeExpires when it expiresExpires – when it expiresSignature – digest of parameters and secret key SignatureVersion – set to 1
Other parameters depend on ActionReturns an XML document
Chris Richardson — Running Java applications on Amazon EC2 Slide 23
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Example EC2 requestsExample EC2 requests
Action ParametersAction Parameters
RunInstances MinCount, MaxCount, ImageId, InstanceType, …
TerminateInstances InstanceId nTerminateInstances InstanceId.n
DescribeInstances InstanceId.n
CreateSecurityGroup GroupName, GroupDescription
AuthorizeSecurityGroupIngress GroupName, SourceSecurityGroupName, IpProtocol
h S GDeauthorizeSecurityGroupIngress …
…
Chris Richardson — Running Java applications on Amazon EC2 Slide 24
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
aws - simple access to EC2 and S3S3
http://timkay.com/aws/Easy to use CLI for EC2 and S3Implemented in PerlAuthenticates using access id and secret key stored in ~/.awssecret
Chris Richardson — Running Java applications on Amazon EC2 Slide 25
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
ElasticFox Firefox pluginElasticFox– Firefox plugin
Chris Richardson — Running Java applications on Amazon EC2 Slide 26
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Launching instancesLaunching instances
TIP: launch with a key ypair or else you won't have access
Chris Richardson — Running Java applications on Amazon EC2 Slide 27
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Creating your own imageCreating your own imageEasier: Modify an existing AMIy g
Launch AMIConfigure: e.g. yum install …
Harder: Build one from scratchLaunch AMIC f l OS llCreate a file to contain OS installationMount as a loopback fileInstall OS: yum installrootInstall OS: yum –-installroot
Use AMI tools to bundle and upload to S3
Chris Richardson — Running Java applications on Amazon EC2 Slide 28
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
AgendaAgendaCloud computing with Amazon EC2Cloud computing with Amazon EC2Using Amazon EC2Overview of Cloud ToolsOverview of Cloud ToolsDeveloping on Amazon EC2Deploying on Amazon EC2A few thoughts about GroovyA few thoughts about Groovy
Chris Richardson — Running Java applications on Amazon EC2 Slide 29
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Deploying a web application on EC2EC2
Chris Richardson — Running Java applications on Amazon EC2
Not rocket science but there are many servers to configure and multiple files to upload
Slide 30
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
What's Cloud Tools?What s Cloud Tools?Open source project32 and 64 bit AMIs
CentOS 5.10Apache/Tomcat/MySQL/JMeter/JetS3t installedp y Q
EC2Deploy frameworkLaunches instancesConfigures Tomcat MySQL ApacheConfigures Tomcat, MySQL, ApacheDeploys web applicationsRuns Jmeter testsWritten in GroovyWritten in Groovy
Maven and Grails pluginsQuick and easy deployment to EC2
Chris Richardson — Running Java applications on Amazon EC2 Slide 31
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
EC2Deploy frameworkEC2Deploy frameworkProvides a DSL for describing a cluster:
Number of Tomcats, MySQL slavesDatabase scriptsLocation of web applicationso a o o b app a o
Launches EC2 instancesConfigures MySQLC fi T d d l b Configures Tomcat and deploys web applicationsConfigures Apache to proxy the Tomcat Configures Apache to proxy the Tomcat serversRuns JMeter tests
Chris Richardson — Running Java applications on Amazon EC2 Slide 32
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Example EC2Deploy ScriptExample EC2Deploy Scriptdef ec2 = new EC2(…)
ClusterSpec clusterSpec = new ClusterSpec().tomcats(1).instanceType(EC2InstanceType.SMALL).slaves(1)
ebApp('/home/cer/ /ptrack' "ptrack").webApp('/home/cer/…/ptrack', "ptrack").catalinaOptsBuilder({optsBuilder, databaseHost, slaves ->
optsBuilder.arg("-Xmx500m")optsBuilder.prop("jdbc.db.server", databaseHost)})
.schema("ptrack", ["ptrack": "ptrack"], ["src/test/resources/testdml1.sql", [ s c/test/ esou ces/testd .sq ,"src/test/resources/testdml2.sql"])
SimpleCluster cluster = new SimpleCluster(ec2, clusterSpec)
cluster.start()
cluster.loadTest("/home/cer/…/jmeter/SimpleTest.jmx", [5, 10, 15])
cluster.stop()
Chris Richardson — Running Java applications on Amazon EC2 Slide 33
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Domain modelDomain model<<façade>>SimpleCluster
name
ClusterSpec
ApacheServerTomcatServer MySqlServerJmeter
Application
stop()start()loadTest()
namenumberOfTomcatsnumberOfMySqlSlaves...
contextexplodedWar
WebApp
<<abstract>>Application
uploadToS3()updateTomcat()
startPolling()
EC2
waitUntilRunning()
EC2Server
instanceId
EC2InstanceState
Application
startPolling()stopPolling()newServers(n)...
waitUntilRunning()stop()ssh(command)...
instanceIdstatepublicDnsNameprivateDnsName
Uses Query API to run, terminate and describe Executes ssh and
scp commands
Chris Richardson — Running Java applications on Amazon EC2
executeRequest(params)
EC2RequestExecutor
ssh(dns, command, …)
Sshinstances scp commands
Slide 34
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Configuration DSLConfiguration DSLclass ApacheServer extends Application {
def configure() {writeFile fileName: "$apacheConfDir/cluster.conf",
templateName: "/templates/cluster.conf", templateArgs: [tomcats: tomcats]
exec "$apacheBinDir/apachectl restart"waitForHttp port:80, path: tomcats[0].contexts[0]
}…}
class MySqlServer extends Application {
d f fi A M t () {def configureAsMaster() {writeFile fileName: "/etc/my.cnf", templateName: "/templates/master.my.cnf"
restartService "mysqld"
exec command: "mysql -u root", templateName: "/templates/createSchema.sql", templateArgs: [schemaSpec: schemaSpec]
executeSchemaScripts()
Chris Richardson — Running Java applications on Amazon EC2
}}
Slide 35
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Efficiently uploading web applications etcapplications, etc.
Non-durable disks = upload the entire web applicationapplication
20+ MBs of jars, etc.Takes a long time (over a DSL connection)
W b li i i fWeb application consists of:90% 3rd party libraries – rarely changing10% application code and content – only some of it hchanges
Use JetS3t to accelerate uploadsIncremental upload of exploded web application to S3 b kbucketIncremental download to Tomcat webapps/ directoryFirst upload is slow but subsequent uploads are fast
Chris Richardson — Running Java applications on Amazon EC2 Slide 36
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Maven PluginMaven Plugin<plugin>
<groupId>net chrisrichardson</groupId>
Goals:• deploy• redeploy• stop<groupId>net.chrisrichardson</groupId>
<artifactId>cloudtools-maven-plugin</artifactId><configuration><schemaName>ptrack</schemaName><schemaUsers>
stop• dbsave• dbrestore• jmeter
<schemaUsers><param>ptrack:ptrack</param>
</schemaUsers><catalinaOptsBuilder>{builder, databasePrivateDnsName ->{ ,
builder.arg("-Xmx1000m")builder.prop("jdbc.db.server", databasePrivateDnsName)}
</catalinaOptsBuilder></configuration>
</plugin>
mvn cloudtools:deploy
Chris Richardson — Running Java applications on Amazon EC2 Slide 37
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Grails PluginGrails PluginPackages E2Deploy as a Grails Packages E2Deploy as a Grails framework pluginDeploys a Grails application to EC2Deploys a Grails application to EC2
$ grails install-plugin <path to plugin>$ grails cloud-tools-deploy$ g p y
Chris Richardson — Running Java applications on Amazon EC2 Slide 38
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
AgendaAgendaCloud computing with Amazon EC2Cloud computing with Amazon EC2Using Amazon EC2Overview of Cloud ToolsOverview of Cloud ToolsDeveloping on Amazon EC2Deploying on Amazon EC2A few thoughts about GroovyA few thoughts about Groovy
Chris Richardson — Running Java applications on Amazon EC2 Slide 39
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Collecting pe fo mance met icsCollecting performance metrics
700070
4000
5000
6000
40
50
60
TPS
R i 1000
2000
3000
10
20
30TPS
ART
Requires hardwareTime consuming
00
1 2 4 8 16 32 64
Measure transactions/second (TPS), average response time (ART), utilization, etc.
Multiple test runs with different loads, number of servers, etc.
Time consuming
Chris Richardson — Running Java applications on Amazon EC2 Slide 40
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Load testing with Cloud ToolsLoad testing with Cloud ToolsRuns JMeter with
<performanceReport><cpus>1</cpus><threads>10</threads>
specified number of threads
<host><name>database</name><cpuUtil>3.2757014224403784</cpuUtil>
</host><host><name>tomcat0</name>
Collects machine utilization stats
<name>tomcat0</name><cpuUtil>94.32473318917411</cpuUtil>
</host><host><name>apache</name><cpuUtil>0.12280614752518504</cpuUtil>
Generates reportsExecutes multiple
p / p</host><host><name>jmeter</name><cpuUtil>7.033683910704496</cpuUtil>
</host>Executes multiple test runs simultaneously
…<duration>557.943</duration><tps>10.753786677133686</tps><art>916.6578333333</art>
</performanceReport>
Chris Richardson — Running Java applications on Amazon EC2
simultaneouslymvn cloudtools:jmeter -Dcloudtools.thread.count=1,4,8
Slide 41
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Other kinds of testingOther kinds of testingTesting failoverg
Launch clusterTake down serversTest recovery scripts, e.g. MySQL slave->master
Testing database upgradesTesting database upgradesLaunch clusterInstall snapshot of production dataInstall snapshot of production dataApply database migration scriptVerify that it works
Chris Richardson — Running Java applications on Amazon EC2
y
Slide 42
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Functional testingFunctional testingTests can be slow, e.g.
Web testsDatabase intensive tests
Run tests in parallel on EC2Run tests in parallel on EC2Multiple test drivers, app servers, DBsRelatively cheap: >$75/hour developer vs. $0 10/hour machine$0.10/hour machine
Selenium Grid from ThoughtworksOpen Source frameworkRuns Selenium web tests in parallel on EC2
Stay tuned for more general solutions
Chris Richardson — Running Java applications on Amazon EC2 Slide 43
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Building on a fresh machineBuilding on a fresh machineDebug builds that fail because of a Debug builds that fail because of a missing dependency
Maven dependencyMaven dependencyManually installed 3rd party library
Build on a fresh EC2 instance Build on a fresh EC2 instance Great for open-source projects
Chris Richardson — Running Java applications on Amazon EC2 Slide 44
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
AgendaAgendaCloud computing with Amazon EC2Cloud computing with Amazon EC2Using Amazon EC2Overview of Cloud ToolsOverview of Cloud ToolsDeveloping on Amazon EC2Deploying on Amazon EC2A few thoughts about GroovyA few thoughts about Groovy
Chris Richardson — Running Java applications on Amazon EC2 Slide 45
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Deploying applications on Ama on EC2Amazon EC2
Great for startups (especially those without a business model)those without a business model)
Get up and running ready quicklyNo upfront hardware costsS l /d ith l dScale up/down with loadReduces the risk of a success catastrophe
G t f t iGreat for enterprisesNo need to wait for corporate ITUse for short-term projects
Cloud Computing Survey: IT Cloud Computing Survey: IT Leaders See Big Promise, Have Big Security Questions
www.cio.com/article/455832/Cloud_Computing_Survey_IT_Leaders_See_Big_Promise_Have_Big_Security_Questions
Chris Richardson — Running Java applications on Amazon EC2 Slide 46
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Issues with AWSIssues with AWSSecurity:
Lack of PCI complianceLack of PCI complianceDiscomfort with sending customer data to a 3rd party
Technology:Technology:Not yet suitable for extremely large relational databasesLack of very large machines,
64G e.g. 64G memoryLack of multicast
Financials:Cost of bandwidthCost of bandwidthSteady state costs > your own hardware
Chris Richardson — Running Java applications on Amazon EC2 Slide 47
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Starter website $Starter website - $www.acme.com
EC2 Instance
Elastic IP A
Apache
Tomcat
MySQL
Chris Richardson — Running Java applications on Amazon EC2 Slide 48
EBS Volume
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Highly available $$Highly available - $$www.acme.com
Availability Zone A Availability Zone B
Elastic IP A Elastic IP B
Apache Apache
TomcatTomcat Tomcat
Tomcat
MySQL(Master)
MySQL(slave)
Chris Richardson — Running Java applications on Amazon EC2 Slide 49
EBS Volume
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Batch processing architectureBatch processing architecture
e g media transcodinge.g. media transcoding
Chris Richardson — Running Java applications on Amazon EC2 Slide 50
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Easy upgradesEasy upgradesClone production environmentClone production environmentApply upgradesTerminate old instances once you are Terminate old instances once you are sure that everything works
Chris Richardson — Running Java applications on Amazon EC2 Slide 51
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Cloud burstingCloud burstingHost application on your own hardware Host application on your own hardware Use AWS for short-term spikes
e g use EC2 instances with slave DBs to e.g. use EC2 instances with slave DBs to handle read-only requests
Periodic batch jobsPeriodic batch jobsE.g. content rendering/transformation
Chris Richardson — Running Java applications on Amazon EC2 Slide 52
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Using AWS in o applicationUsing AWS in your applicationSimple Storage Service (S3)
St bl b f d tStores blobs of dataEg. Photo sharing websiteStore mediaHand out URLs to S3 objects
Simple Queue Service (SQS)Hosted queue-based messaging systemq g g yAlternative to JMSLoosely coupling between systems
SimpleDBSimpleDBStore data setsExecute queries
Chris Richardson — Running Java applications on Amazon EC2 Slide 53
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Java libraries for AWSJava libraries for AWSJetS3t
Rich API for accessing S3jets3t.dev.java.net/
T iTypicaAPI for SQS, EC2, SimpleDBpcode.google.com/p/typica
SimpleJPASubset of JPA on Simple DBcode.google.com/p/simplej
Chris Richardson — Running Java applications on Amazon EC2
code.google.com/p/simplejpa
Slide 54
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
AgendaAgendaCloud computing with Amazon EC2Cloud computing with Amazon EC2Using Amazon EC2Overview of Cloud ToolsOverview of Cloud ToolsDeveloping on Amazon EC2Deploying on Amazon EC2A few thoughts about GroovyA few thoughts about Groovy
Chris Richardson — Running Java applications on Amazon EC2 Slide 55
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
About GroovyAbout GroovyObject-oriented, dynamic languageObject oriented, dynamic languageJava compatibleRuns on the JVMRuns on the JVM
Chris Richardson — Running Java applications on Amazon EC2 Slide 56
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Getting Groovy code to work can be frustratingcan be frustrating
Dynamic language = less information Dynamic language less information for IDE:
Limited compile-time checkingLimited compile time checkingLimited refactoringsLimited completionLimited completion
Chris Richardson — Running Java applications on Amazon EC2 Slide 57
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Groovy fans say "write unit tests“ BUTtests“ BUT…
Groovy
versus
When you have typos
Java
Chris Richardson — Running Java applications on Amazon EC2 Slide 58
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Unit tests don't always catch e o serrors
void testPollStatus_discoveringNewServer() {mockEC2RequestExecutor.demand.executeRequst {params ->….
}def mockEC2Server = new MockFor(EC2Server.class)….mockEC2Server.use {mockEC2Server.use {
mockEC2RequestExecutor.use {ec2 = new EC2(awsProperties)ec2.pollStatus()assertEquals 1, ec2.servers.size()
}
public class EC2 {
}}
}
public pollStatus() {def params = ['Action':
'DescribeInstances']def p =
requestor executeRequst(params)class EC2RequestExecutor {
public Node executeRequest(Map parameters) {
requestor.executeRequst(params)….
}…}
Chris Richardson — Running Java applications on Amazon EC2
…} Same typo in test and in code
Slide 59
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Things I like Ja a compatibleThings I like: Java compatibleimport javax.crypto.Mac;import javax.crypto.spec.SecretKeySpec;
class EC2RequestExecutor {
Log logger = LogFactory.getLog(getClass())
Java-like syntaxUse Java libraries
public String calculateRFC2104HMAC(String data, String key) {try {SecretKeySpec signingKey = new SecretKeySpec(key.getBytes("UTF8"),
HMAC SHA1 ALGORITHM)HMAC_SHA1_ALGORITHM)Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM)mac.init(signingKey)byte[] rawHmac = mac.doFinal(data.getBytes())return new String(Base64.encodeBase64(rawHmac))g( ( ))
}catch (Exception e) {throw new RuntimeException("Failed to generate HMAC : ", e)
}}
Chris Richardson — Running Java applications on Amazon EC2
}
Slide 60
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Groovy is concise and e p essi eexpressive
def configureAsMaster() {writeFile fileName: "/etc/my.cnf", templateName: "/templates/master.my.cnf"
restartService "mysqld"
exec command: "mysql -u root", templateName: "/templates/createSchema.sql", No parenstemplateName: /templates/createSchema.sql , templateArgs: [schemaSpec: schemaSpec]
executeSchemaScripts()}
No parensKeyword parameters
class TomcatServer { tomcatServer.contextsN t ()
{def getContexts() {
webApps.context}
}
No get…()
Chris Richardson — Running Java applications on Amazon EC2 Slide 61
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
ClosuresClosures
def sortServers(servers) {servers.sort {a, b -> a.instanceId <=> b.instanceId}
}}
public EC2Server findInstance(String instanceId) {def server = servers.find {instanceId == it.instanceId}{ }if (server)return server
else throw new RuntimeException(….)}
Chris Richardson — Running Java applications on Amazon EC2 Slide 62
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
XML and GPath expressionsXML and GPath expressions<RunInstancesResponse>…<instancesSet>
<item><instanceId>i-4ef21327</instanceId><imageId>ami-3795705e</imageId><instanceState>
<code>0</code>def client = new HttpClient()…def responseStream = getMethod getResponseBodyAsStream()
<code>0</code><name>pending</name>
</instanceState><dnsName/>
…getMethod.getResponseBodyAsStream()def parser = new XmlParser(false, false)def response = parser.parseText(responseStream)
def newServers = response instancesSet item collect {
</RunInstancesResponse>
def newServers response.instancesSet.item.collect {new EC2Server(this, awsProperties, ssh,
it.instanceId[0].text(), it.instanceState[0].children()[1].value()[0])
}
Chris Richardson — Running Java applications on Amazon EC2
}
Slide 63
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
GStrings and templatesGStrings and templatesdef schemaScript = """
DROP SCHEMA IF EXISTS ${schemaSpec.name};CREATE SCHEMA ${schemaSpec.name};
"""
String process(String templateName, Map params) {InputStream stream = getClass().getResourceAsStream(templateName)def engine = new groovy.text.SimpleTemplateEngine()engine.createTemplate(new InputStreamReader(stream)).make(params).toString()
}
…<%
contexts.each {%>ProxyPass /$it balancer://mycluster/$it stickysession=jsessionid<%}%><Proxy balancer://mycluster><%
tomcats.each {%>BalancerMember ajp://${it.hostPrivateDnsName}:8009 route=${it.jvmRoute} min=1 keepalive=On retry=5<% }
Chris Richardson — Running Java applications on Amazon EC2
<% }%>…
Slide 64
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
BuildersBuildersdef report(String path, hosts, cpuCount, threadCount) {
def builder = new groovy.xml.MarkupBuilder(new OutputStreamWriter(new FileOutputStream(path))) builder.performanceReport {builder.performanceReport {cpus cpuCountthreads threadCounthosts.entrySet().each { hostEntry ->host {name hostEntry.keycpuUtil hostEntry value getAverageBusy()
<performanceReport><cpus>1</cpus>cpuUtil hostEntry.value.getAverageBusy()
}}requests {
timings.entrySet().sort{ a, b-> a.key <=> b.key}.each{ pair ->request {name pair key
<cpus>1</cpus><threads>10</threads><host>
<name>database</name><cpuUtil>3.27</cpuUtil>
</host>name pair.keyart pair.value.average()errors pair.value.errorPercentage()}
}}d f d ti V l ((fl t)( dTi t tTi ))/1000 0
</host><host>
<name>tomcat0</name><cpuUtil>94.32</cpuUtil>
</host>
def durationValue = ((float)(endTime - startTime))/1000.0duration durationValuedef tpsValue = transactionCount/ durationValuetps tpsValueart averageResponseTime()
}
…<duration>557.943</duration><tps>10.753786677133686</tps><art>916.6578333333</art>
</performanceReport>
Chris Richardson — Running Java applications on Amazon EC2
}
Slide 65
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
SummarySummaryCloud Computing
Immediate access to many serversImmediate access to many serversPay as you go – no upfront investment/commitment requiredEasily scale up/downEasily scale up/down
Cloud ToolsEasy deployment and testing from Maven and GrailsConfigure multiple clustersg pRun JMeter tests in parallel
Using GroovyHas some nice featuresDynamic language features are double-edged swordWe need better statically typed languages
Chris Richardson — Running Java applications on Amazon EC2 Slide 66
© Copyright 2008, Chris RichardsonColorado Software Summit: October 19 – 24, 2008
Final thoughtsFinal thoughtsDownload Cloud Tools today:
http://code.google.com/p/cloudtools
Buy my book ☺Buy my book ☺
Send email:
Visit my website:
http://www.chrisrichardson.net
Talk to me about consulting and t i i
Chris Richardson — Running Java applications on Amazon EC2
training
Slide 67