aws essentials ilt version 2 - meetupfiles.meetup.com/1744630/ec2 spot_sea meetup_063415.pdf · •...

20
© 2013 Amazon Web Services, Inc. and its affiliates. All rights reserved. EC2 Spot Spot Instances can significantly lower your computing costs for time-flexible, interruption-tolerant tasks. You simply bid on spare Amazon EC2 instances and they run whenever your bid exceeds the current Spot Price, which varies in real time, based on supply and demand.

Upload: dangthuy

Post on 30-Mar-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

© 2013 Amazon Web Services, Inc. and its affiliates. All rights reserved.

EC2 Spot

Spot Instances can significantly lower your computing

costs for time-flexible, interruption-tolerant tasks.

You simply bid on spare Amazon EC2 instances and they

run whenever your bid exceeds the current Spot Price,

which varies in real time, based on supply and demand.

© 2013 Amazon Web Services, Inc. and its affiliates. All rights reserved.

EC2 spot architecture

Elastic LoadBalancing

Stateless Web Servers

Stateless Web Servers

On Demand

Autoscaling group

Session State Data

Stateless Web Servers (spot)

Stateless Web Servers (spot)

Spot Autoscaling

group

Availability Zone A

Availability Zone B

Stateless Web Servers (spot)

Stateless Web Servers (spot)

Spot Autoscaling

group

© 2013 Amazon Web Services, Inc. and its affiliates. All rights reserved.

High Availability – “Everything fails, all the time”

High availability

The system will continue to function despite the complete failure

of any component of the architecture.

Fault tolerance

The system will continue to function without degradation in

performance despite the complete failure of any component of

the architecture.

© 2013 Amazon Web Services, Inc. and its affiliates. All rights reserved.

Scalability

Loosely Couple

Design Independent architectures with independent components

Use SQS and SNS to share information between components

Become Stateless

Determine how to maintain application persistence and state within and between each component

Use Amazon services such as DynamoDB and ElasticCache to maintain state between applications

Leverage Parallelism

One server running for 5 hours is the same price as 5 servers running for one hour.

The efficacy and cost of the compute power you want to use can determine if you need to vertically scale versus horizontally scale.

© 2013 Amazon Web Services, Inc. and its affiliates. All rights reserved.

Scalability

Elasticity

Use Auto Scaling as much as possible

Do not assume health, availability, or fixed location of

components

“Automate everything”

• Installation and configuration of environments

• Dynamic configurations

© 2013 Amazon Web Services, Inc. and its affiliates. All rights reserved.

Self-Healing Architectures

Implementing architecture components that are aware of

their health (AWS Cloudwatch)

Implement applications that are resilient to reboot and

re-launch.

Bootstrap your instances.

Storing configuration and personalization off-instance

(e.g. S3, DynamoDB)

[AWS Certification Exam Prep Bootcamp – Domain 4

© 2013 Amazon Web Services, Inc. and its affiliates. All rights reserved.

EC2 spot architecture

© 2013 Amazon Web Services, Inc. and its affiliates. All rights reserved.

EC2 spot architecture

• Auto Scaling groups running on-demand instances can be placed

together with multiple Auto Scaling groups running Spot instances

• Use different Spot bid prices behind the same Elastic Load

Balancer to provide more flexibility and to meet changing traffic

demands.

• Pick the right instance time for the job based on the price history.

aws autoscaling create-launch-configuration --launch-configuration-name spot-08 --image-id ami-

b43b32dc --instance-type m3.medium --spot-price "0.008" --iam-instance-profile spotlab --

security-groups sg-a5a722c1 --region us-east-1 --user-data <firstname>spotbucket --key-name

Default

aws autoscaling create-auto-scaling-group --auto-scaling-group-name spot-asg-m3-medium --

launch-configuration-name spot-08 --max-size 5 --min-size 1 --desired-capacity 2 --load-

balancer-names "spot-load-balancer" --vpc-zone-identifier subnet-ec47d887 --region us-east-1

© 2013 Amazon Web Services, Inc. and its affiliates. All rights reserved.

You can assign more aggressive scaling policies to Auto

Scaling groups that run Spot instances ( e.g. to scale up

when instances reach 75% CPU utilization and scale down

when they reach 25% CPU utilization with a large capacity

range), and assign more conservative scaling policies to

Auto Scaling groups that run on-demand instances.

EC2 spot architecture

© 2013 Amazon Web Services, Inc. and its affiliates. All rights reserved.

EC2 spot architecture

Session state for the web application can be stored in

DynamoDB.

DynamoDB is a regional service, meaning that the data is

automatically replicated across availability zones for fault

tolerance.

You can also choose other databases to maintain state in

your architecture.

aws dynamodb create-table --table-name SpotTable --attribute-definitions

AttributeName=Artist,AttributeType=S AttributeName=Title,AttributeType=S --

key-schema AttributeName=test,KeyType=HASH

AttributeName=SongTitle,KeyType=RANGE --provisioned-throughput

ReadCapacityUnits=1,WriteCapacityUnits=1

© 2013 Amazon Web Services, Inc. and its affiliates. All rights reserved.

EC2 spot architecture

The availability of Spot instances can vary depending on

how many unused Amazon EC2 instances are available,

hence you should architect it to be resilient to instance

termination.

When the Spot price exceeds the price you named (i.e.

the bid price), the instance will receive a two-minute

warning that the instance will be terminated.

© 2013 Amazon Web Services, Inc. and its affiliates. All rights reserved.

EC2 spot architecture

You can manage this by creating IAM roles from the Spot

instances that they use to de-register themselves from the

ELB and run shutdown scripts once they receive

notification that they will be terminated.

© 2013 Amazon Web Services, Inc. and its affiliates. All rights reserved.

EC2 spot architecture

A script like the following can be placed in a loop and can be run

on startup (e.g via systemd or rc.local) to detect Spot instance

termination every 5 seconds and then place any session

information into DynamoDB as well as de-register itself from the

ELB so that it will not receive any more requests.

$ if curl -s http://169.254.169.254/latest/meta-

data/spot/termination-time | \ grep -q .*T.*Z; then

instance_id=$(curl -s http://169.254.169.254/latest/meta-

data/instance-id); \ aws elb deregister-instances-from-load-

balancer \ --load-balancer-name my-load-balancer \ --instances

$instance_id; /env/bin/flushsessiontoDBonterminationscript.sh; fi

© 2013 Amazon Web Services, Inc. and its affiliates. All rights reserved.

Elastic Load Balancers use the least outstanding request

(for HTTP/HTTPS connections) routing algorithm which

favors back-end instances with the fewest outstanding

requests.

Since we are working with multiple Auto Scaling groups

spanning across multiple availability zones, we highly

recommend enabling cross-zone load balancing for the

load balancer.

EC2 spot architecture

© 2013 Amazon Web Services, Inc. and its affiliates. All rights reserved.

• To allow in-flight requests to complete when de-registering

Spot instances that are about to be terminated, connection

draining can be enabled on the load balancer with a timeout

of 90 seconds.

• Connection draining causes the ELB load balancer to stop

sending new requests to a deregistering instance or an

unhealthy instance, while keeping the existing connections

open.

EC2 spot architecture

aws elb create-load-balancer --region us-east-1 --load-balancer-name spot-load-balancer

--listeners "Protocol=HTTP,LoadBalancerPort=80,InstanceProtocol=HTTP,InstancePort=80" -

-subnets subnet-ec47d887--security-groups sg-a5a722c1

© 2013 Amazon Web Services, Inc. and its affiliates. All rights reserved.

EC2 spot architecture

Elastic LoadBalancing

Stateless Web Servers

Stateless Web Servers

On Demand

Autoscaling

Session State Data

Stateless Web Servers (spot)

Stateless Web Servers (spot)

Spot

Autoscaling

Availability Zone A

Availability Zone B

Stateless Web Servers (spot)

Stateless Web Servers (spot)

Spot

Autoscaling

© 2013 Amazon Web Services, Inc. and its affiliates. All rights reserved.

Where to go from here?

Create a Lambda function to check for spot market prices

and create a new autoscaling launch configuration and

group as required in an automatic manner.

This could also delete any spot autoscaling groups and

loan configurations that have zero instances.

Use data pipeline to invoke the lambda function at regular

intervals by scheduling pipelines.

© 2013 Amazon Web Services, Inc. and its affiliates. All rights reserved.

© 2013 Amazon Web Services, Inc. and its affiliates. All rights reserved.

So-net uses EMR with spot for

batch processing of logs

© 2013 Amazon Web Services, Inc. and its affiliates. All rights reserved.

Questions?