無伺服器架構和Containers on AWS入門

Preview:

Citation preview

AWS Cloud Kata for Start-Ups and Developers

Hong Kong

Getting Started with Serverless and Container Architectures

Dickson Yue Solutions Architect, AWS

AWS Cloud Kata for Start-Ups and Developers

Key take way

Deploy your Containers with ECS Develop services with Lambda Build your micro services architecture with Serverless (Lambda) and Container (ECS)

AWS Cloud Kata for Start-Ups and Developers

Operational complexity - “I want to run some code in the cloud” •  …but ops are complicated, and I don’t have an ops guy

Undifferentiated instances - “I want flexibility to use the code I like” •  …but OS and runtime configuration? Don’t really care.

Capacity management concerns - “My business scales with users and requests” •  …but I don’t want a planning exercise to reserve and provision capacity

Low utilization but high scale - “I want infinite scale” •  …but I only want to pay for calls I actually make

Run some code in the cloud

AWS Cloud Kata for Start-Ups and Developers

How do I choose? •  VMs

•  “I want to configure machines, storage, networking, and my OS”

•  Containers •  “I want to run servers, configure

applications, and control scaling”

•  Serverless •  “Run my code when it’s needed”

ECS

EC2

AWS Lambda

AWS Cloud Kata for Start-Ups and Developers

Amazon Route 53 User

Amazon CloudFront

Web

RDS Master (Multi-AZ)

Elastic Load

Balancer

RDS Slave (Multi-AZ)

Web

Availability Zone Availability Zone

AWS Cloud Kata for Start-Ups and Developers

Micro services Amazon Route 53 User

Amazon CloudFront

Static website *.html, *.js *.css *.jpg *.mp4

S3 Web

RDS Master (Multi-AZ)

RDS Slave (Multi-AZ)

Zone A Zone B

DynamoDB

Fn1()

API Gateway

Fn2() Fn3()

Redis MySQL

Web

RDS Master (Multi-AZ)

RDS Slave (Multi-AZ)

Zone A Zone B

EC2 Containter Serverless

Service Alpha Service Delta, Gamma Service Beta

AWS Cloud Kata for Start-Ups and Developers

Conatiner

AWS Cloud Kata for Start-Ups and Developers

  Self managed EC2

  Elastic Beanstalk

  Elastic container service (ECS)

Deployment options

AWS Cloud Kata for Start-Ups and Developers

Server

Guest OS

Bins/Libs Bins/Libs

App2 App1

Managing One Host is Straightforward

AWS Cloud Kata for Start-Ups and Developers

Managing a Fleet is Hard

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

Server

Guest OS

AZ 1 AZ 2

AZ 3

AWS Cloud Kata for Start-Ups and Developers

What is EC2 Container Service?

AWS Cloud Kata for Start-Ups and Developers

Cluster Management Made Easy

  No cluster software to install and manage

  Manages cluster state

  Manages containers

  Control and monitoring

  Scale from one to tens of thousands of containers

AWS Cloud Kata for Start-Ups and Developers

Designed for use with other AWS services

Elastic Load Balancing Amazon Elastic Block Store

Amazon Virtual Private Cloud AWS Identity and Access Management

AWS CloudTrail

AWS Cloud Kata for Start-Ups and Developers

Key Components

  Clusters

  Containers

  Task Definitions

  Service

AWS Cloud Kata for Start-Ups and Developers

Typical User Workflow

I have a Docker image, and I want to run the image on a cluster

AWS Cloud Kata for Start-Ups and Developers

Typical User Workflow

Push Image(s)

Amazon ECR

Docker Hub

Or

> docker build -t dicksonyue/aws-voting-app:ecsdemo . > docker push dicksonyue/aws-voting-app:ecsdemo

AWS Cloud Kata for Start-Ups and Developers

Typical User Workflow

Create Task Definition Amazon ECS

Task Definition -  Image -  CPU, memory -  Port mapping -  CMD, ENV

> aws ecs register-task-definition --cli-input-json file://ecs-task.json

AWS Cloud Kata for Start-Ups and Developers

Task {

"family": "kata-demo-task",

"containerDefinitions": [

{

"name": "kata-demo-container",

"image": "dicksonyue/aws-voting-app:alpha",

"cpu": 10, "memory": 500,

"portMappings": [{

"containerPort": 8080

}],

"essential": true,

"command": [ "npm", "start"],

"environment" : [

{ "name" : "REDIS_HOST", "value" : "ecs-demo.cw7bo2.0001.usw2.cache.amazonaws.com" },

{ "name" : "REDIS_PORT", "value" : "6379" } ]

}

]}

Resources

Docker image

CMD, ENV

AWS Cloud Kata for Start-Ups and Developers

Typical User Workflow

Run Instances EC2

Use custom AMI with Docker support and ECS Agent. Instances will register with default cluster.

> aws ecs create-cluster --cluster-name "ecs-demo" > aws autoscaling create-launch-configuration --cli-input-json file://launch-config.json --user-data file://userdata.txt > aws autoscaling create-auto-scaling-group --cli-input-json file://auto-scaling-group.json

AWS Cloud Kata for Start-Ups and Developers

User data.txt #!/bin/bash

echo ECS_CLUSTER=ecs-demo >> /etc/ecs/ecs.config

AWS Cloud Kata for Start-Ups and Developers

Cluster Management: Resource Management

Docker

EC2 Instance

Docker

EC2 Instance

Docker

EC2 Instance

AZ 1 AZ 2

Cluster:ecs-demo

AWS Cloud Kata for Start-Ups and Developers

Typical User Workflow

Run Task or

Create Service Amazon ECS

Using the task definition created above > aws ecs run-task --task-definition vote-app-task --

cluster ecs-demo

> aws ecs create-service --cli-input-json file://ecs-service.json

AWS Cloud Kata for Start-Ups and Developers

Cluster Management: Scheduling

Docker Task

EC2 Instance

Container

Docker Task

EC2 Instance

Container

Task Container

Docker

EC2 Instance

Task Container

AZ 1 AZ 2

AWS Cloud Kata for Start-Ups and Developers

Task vs Service

Task •  One time execute •  Batch job

Service

•  One or different tasks (i.e. nodejs, ngnix) •  Task count (4 nodejs containers) •  Container and ELB port binding •  Auto scaling at task level •  Always on - Web application

Containers

AWS Cloud Kata for Start-Ups and Developers

Service {

"cluster": "ecs-demo",

"serviceName": "vote-app-service",

"taskDefinition": "vote-app-task",

"loadBalancers": [

{

"targetGroupArn": "arn:aws:elasticloadbalancing:us-west-2:179303575282:targetgroup/voting-app/99ffe4fab0a151e9",

"containerName": "kata-demo-container",

"containerPort": 8080

}

],

"desiredCount": 2,

"role": "ecsServiceRole”

}

Application load balancer

Container port

IAM Role

AWS Cloud Kata for Start-Ups and Developers

DEMO

AWS Cloud Kata for Start-Ups and Developers

Serverless

AWS Cloud Kata for Start-Ups and Developers

2)Con'nuousScaling1)NoServerstoManageAWSLambdaautoma-callyscalesyour

applica-onbyrunningcodeinresponsetoeachtrigger.Yourcoderunsinparallelandprocesseseachtriggerindividually,scalingpreciselywiththesizeoftheworkload.

3)SubsecondMeteringWithAWSLambda,youarechargedforevery100msyourcodeexecutesandthenumberof-mesyourcodeistriggered.Youdon'tpayanythingwhenyourcode

isn'trunning.

AWSLambdaautoma-callyrunsyourcodewithoutrequiringyoutoprovisionor

manageservers.JustwritethecodeanduploadittoLambda.

BenefitsofAWSLambda

AWS Cloud Kata for Start-Ups and Developers

How Lambda works

S3 event notifications

DynamoDB Streams

Kinesis events

Cognito events

SNS events

Custom events

CloudTrail events Lambda DynamoDB

Kinesis S3

Any custom

Redshift

SNS

Any AWS

AWS Cloud Kata for Start-Ups and Developers

AWS Lambda, API Gateway, and AWS IoT regions

Available regions

Singapore

AWS Cloud Kata for Start-Ups and Developers

Lambda usage scenarios

AWS Cloud Kata for Start-Ups and Developers

Use case: Data processing Example: Amazon S3 bucket triggers

Amazon S3 bucket events

Original object Compressed object 1

2

3

AWS Lambda

AWS Cloud Kata for Start-Ups and Developers

Use case: Dynamic data ingestion “Iwanttoapplycustomlogictoprocesscontentbeinguploadedtomydatastore”.•  PDFwatermarking•  Imagethumbnailingandtranscoding•  DocumentmetadataIndexing•  Logaggrega-onandfiltering•  RSSfeedprocessing•  Mediacontentvalida-on

AWS Cloud Kata for Start-Ups and Developers

Use case: Realtime data stream processing: Amazon Kinesis

“IwanttoapplycustomlogictoprocesslogsbeinguploadedthroughmyKinesisstream”.•  Clientac-vitytracking•  metricsgenera-on•  datacleansing•  Logfiltering•  indexingandsearching•  Logrou-ng

AWS Cloud Kata for Start-Ups and Developers

Use case: mobile backend 1.  AWS Mobile SDK + Amazon Cognito for mobile app

Or AWS IoT for devices

2.  AWS Lambda runs the code 3.  Amazon API Gateway (if you want your own endpoint) 4.  Amazon DynamoDB holds the data

AWS Lambda Amazon DynamoDB

AWS Cloud Kata for Start-Ups and Developers

Use case: Serverless web apps

1.  Amazon S3 for serving static content 2.  AWS Lambda for dynamic content 3.  Amazon API Gateway for https access 4.  Amazon DynamoDB for NoSQL data storage

Dynamic content in AWS Lambda

Data stored in Amazon

DynamoDB

API Gateway Static content in Amazon S3

AWS Cloud Kata for Start-Ups and Developers

re:Invent 2015 •  Python •  Scheduled functions •  Longer running times (5 min.) •  Versioning

Recent launches

Since re:Invent •  Higher code storage limits (from 5 GB to

75 GB) •  Custom VPC •  1-minute schedules •  New regional launch •  Node.js 4.3.2 •  1-click CORs setup •  Stage variables •  Custom (Lambda) authorizers •  Builtin Swagger import/export •  AWS CloudFormation support for API

Gateway and versions

New!

AWS Cloud Kata for Start-Ups and Developers

DEMO

AWS Cloud Kata for Start-Ups and Developers

Container   ECS   Cluster   Task & Service   AWS CLI or Console

Summary

Serverless   Lambda   Use cases   New features

AWS Cloud Kata for Start-Ups and Developers

Hong Kong

Thank you

Recommended