amazon ecs deep dive

53
© 2016, Amazon Web Serv ices, Inc. or its Af f iliates. All rights reserv ed. Konstantin Wilms, Solutions Architect June 21 st , 2016 Amazon EC2 Container Service Deep Dive

Upload: amazon-web-services

Post on 16-Apr-2017

669 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Amazon ECS Deep Dive

© 2016, Amazon Web Serv ices, Inc. or its Af f iliates. All rights reserv ed.

Konstantin Wilms, Solutions Architect

June 21st, 2016

Amazon EC2 Container

Service Deep Dive

Page 2: Amazon ECS Deep Dive
Page 3: Amazon ECS Deep Dive

Agenda

Infrastructure Setup

Infrastructure Management

PaaS on ECS

Page 4: Amazon ECS Deep Dive

Amazon ECS Infrastructure

Setup

Page 5: Amazon ECS Deep Dive

Amazon ECS Cluster Setup

Page 6: Amazon ECS Deep Dive

Cluster Setup with AWS CloudFormation

CloudFormation supports ECS cluster, service and task

definition resources

Use AWS::IAM::Role to create ECS service role and

container instances role

Launch container instances using

AWS:AutoScaling::LaunchConfiguation and

AWS:AutoScaling::AutoScalingGroup

Page 7: Amazon ECS Deep Dive

Cluster Setup with AWS CloudFormation

"Resources" : {

"ECSCluster": {

"Type": "AWS::ECS::Cluster"

},

"ECSAutoScalingGroup" : {

"Type" : "AWS::AutoScaling::AutoScalingGroup",

"Properties" : {

"VPCZoneIdentifier" : { "Ref" : "SubnetID" },

"LaunchConfigurationName" : { "Ref" : "ContainerInstances" },

"MinSize" : "1",

"MaxSize" : { "Ref" : "MaxSize" },

"DesiredCapacity" : { "Ref" : "DesiredCapacity" }

},

[…]

},

Page 8: Amazon ECS Deep Dive

Cluster Setup with AWS CloudFormation

"ContainerInstances": {

"Type": "AWS::AutoScaling::LaunchConfiguration",

"Metadata" : {

"AWS::CloudFormation::Init" : {

"config" : {

"commands" : {

"01_add_instance_to_cluster" : {

"command" : { "Fn::Join": [ "", [ "#!/bin/bash\n", "echo ECS_CLUSTER=", { "Ref": "ECSCluster" }, " >> /etc/ecs/ecs.config" ] ] }

}

},

[…]

}

}

}

Page 9: Amazon ECS Deep Dive

Cluster Setup with AWS OpsWorks

One ECS Cluster layer per stack

One cluster can only be associated with one stack

Page 10: Amazon ECS Deep Dive

Cluster Setup with AWS OpsWorks

Update OpsWorks IAM role to allow ecs:* actions

Add instances to layer (24/7, time-based, load-based)

Manage security updates, user permission and access

Page 11: Amazon ECS Deep Dive

Amazon ECR Setup

Page 12: Amazon ECS Deep Dive

Amazon ECR Setup

You have read and write access to the repositories you

create in your default registry, i.e.

<aws_account_id>.dkr.ecr.us-east-1.amazonaws.com

Repository names can support namespaces, e.g. team-

a/web-app.

Repositories can be controlled with both IAM user access

policies and repository policies.

Page 13: Amazon ECS Deep Dive

Amazon ECR Setup

# Authenticate Docker to your Amazon ECR registry

> aws ecr get-login

docker login -u AWS -p <password> -e none https://<aws_account_id>.dkr.ecr.us-east-1.amazonaws.com

> docker login -u AWS -p <password> -e none https://<aws_account_id>.dkr.ecr.us-east-1.amazonaws.com

# Create a repository called ecr-demo

> aws ecr create-repository --repository-name ecr-demo

# Push an image to your repository

> docker push <aws_account_id>.dkr.ecr.us-east-1.amazonaws.com/ecr-demo:v1

Page 14: Amazon ECS Deep Dive

Amazon ECR Docker Credential Helper

Available today - http://bit.ly/25yXdC3

Place the docker-credential-ecr-login binary on your PATH

Set the contents of ~/.docker/config.json file to be:

{ "credsStore": "ecr-login" }

Push and pull images from ECR without docker login

Page 15: Amazon ECS Deep Dive

Amazon ECS Infrastructure

Management

Page 16: Amazon ECS Deep Dive

Amazon EC2 Simple Systems

Manager (SSM)

Page 17: Amazon ECS Deep Dive

Amazon EC2 Simple Systems Manager (SSM)

Use Amazon EC2 SSM to execute commands on container

instances, e.g. yum update

• Add AmazonEC2RoleForSSM

to instances IAM role to

process Run Commands

• Install SSM Agent

• Create SSM document –

similar to CloudInit userdata

• Lock down AWS-* documents

Page 18: Amazon ECS Deep Dive

Monitoring & Logging

Page 19: Amazon ECS Deep Dive

Monitoring with Amazon CloudWatch

Metric data sent to CloudWatch in 1-minute periods and

recorded for a period of two weeks

Available metrics: CPUReservation, MemoryReservation,

CPUUtilization, MemoryUtilization

Available dimensions: ClusterName, ServiceName

Page 20: Amazon ECS Deep Dive

Monitoring with Amazon CloudWatch

Page 21: Amazon ECS Deep Dive

Monitoring with Amazon CloudWatch

Page 22: Amazon ECS Deep Dive

Monitoring with Amazon CloudWatch

Use the Amazon CloudWatch Monitoring Scripts to monitor

additional metrics, e.g. disk space:

# Edit crontab

> crontab -e

# Add command to report disk space utilization to CloudWatch every five minutes

*/5 * * * * <path_to>/mon-put-instance-data.pl --disk-space-util --disk-space-used --disk-space-avail --disk-path=/ --from-cron

Page 23: Amazon ECS Deep Dive

Configuring Logging in Task Definition

logConfiguration task definition parameter

Requires version 1.18 or greater of the Docker Remote API

Maps to docker run --log-driver option

Log drivers: json-file, syslog, journald, gelf, fluentd,

awslogs

"logConfiguration": {

"logDriver": "awslogs",

"options": {

"awslogs-group": "awslogs-wordpress",

"awslogs-region": "us-west-2"

}

}

Page 24: Amazon ECS Deep Dive

CloudWatch Logging with awslogs

Amazon CloudWatch Logs

Amazon CloudWatch Logs

Amazon CloudWatch Logs

Amazon CloudWatch Logs

Amazon S3

Amazon Kinesis

AWS Lambda

Amazon Elasticsearch Service

Amazon ECS Store

Stream

Process

Search

Page 25: Amazon ECS Deep Dive

Logging Amazon ECS API with AWS CloudTrail

{

"eventVersion": "1.03",

"userIdentity": {…},

"eventTime": "2015-10-12T13:57:33Z",

"eventSource": "ecs.amazonaws.com",

"eventName": "CreateCluster",

"awsRegion": "eu-west-1",

"sourceIPAddress": "54.240.197.227",

"userAgent": "console.amazonaws.com",

"requestParameters": {

"clusterName": "ecs-cli"

},

Page 26: Amazon ECS Deep Dive

Logging Amazon ECS API with AWS CloudTrail

"responseElements": {

"cluster": {

"clusterArn": "arn:aws:ecs:eu-west-1:560846014933:cluster/ecs-cli",

"pendingTasksCount": 0,

"registeredContainerInstancesCount": 0,

"status": "ACTIVE",

"runningTasksCount": 0,

"clusterName": "ecs-cli",

"activeServicesCount": 0

}

},

[…]

Page 27: Amazon ECS Deep Dive

Monitoring Amazon ECS with Datadog

Page 28: Amazon ECS Deep Dive

Monitoring Amazon ECS with Sysdig Cloud

Page 29: Amazon ECS Deep Dive

Scaling Amazon ECS

Page 30: Amazon ECS Deep Dive

Setup ECS Cluster with AutoScaling

Create LaunchConfiguration

• Pick instance type

depending on resource

requirements, e.g. memory

or CPU

• Use latest Amazon Linux

ECS-optimized AMI, other

distros available

Create AutoScaling group and

set to cluster initial size

Page 31: Amazon ECS Deep Dive

Auto Scaling your Amazon ECS Cluster

Create CloudWatch alarm

on a metric, e.g.

MemoryReservation

Configure scaling policies to

increase and decrease the

size of your cluster

Page 32: Amazon ECS Deep Dive

Auto Scaling your Amazon ECS services

Page 33: Amazon ECS Deep Dive

Auto Scaling your Amazon ECS services

Page 34: Amazon ECS Deep Dive

Service Discovery &

Configuration Management

Page 35: Amazon ECS Deep Dive

Service Discovery with ECS Services & Route 53

Route 53 private hosted zone

Set search path on hosts with DHCP option sets

Define ECS services with ELB

Create CNAMEs for each ELB

Page 36: Amazon ECS Deep Dive

Service Discovery with ECS Services & Route 53

Task

Task TaskTask

ECS

Service

Application

router, e.g. nginx

Internal ELB with

CNAME, e.g. api.example.com

Route 53 private

zone, e.g. example.com

Page 37: Amazon ECS Deep Dive

Service Discovery with Weaveworks

DNS interface for cross-host

container communication

Gossip protocol to share

grouped updates

Overlay network between hosts

Page 38: Amazon ECS Deep Dive

Service Discovery and Configuration

Management with ConsulThree main components:

• Consul agent - Runs on each node, responsible for checking the health of the services and of the node itself.

• One or more Consul servers - Store and replicate data, leader elected using the Raft consensus algorithm

• Registrator agent - Automatically register/deregisters services based on published ports and metadata from the container environment variables defined in the ECS task definition

Page 39: Amazon ECS Deep Dive

Service Discovery and Configuration

Management with Consul

EC

S C

lus

ter

consul-server

ECS Instance

consul-agent

registrator

ECS Instance

Back end 1

Back end 2

consul-agent

registrator

ECS Instance

Front end

EC

S C

lus

ter

Page 40: Amazon ECS Deep Dive

Service Discovery and Configuration

Management with etcd

etcd

registrator

ECS Instance

Container 1

Container 2

confd etcd

registrator

ECS Instance

Container 1

Container 2

confd etcd

registrator

ECS Instance

Container 1

Container 2

confd

Page 41: Amazon ECS Deep Dive

Security

Page 42: Amazon ECS Deep Dive

ECS IAM Policies and Roles

The ECS agent calls the ECS APIs on your behalf, so

container instances require an IAM policy and role that

allows these calls.

The ECS service scheduler calls the EC2 and ELB APIs on

your behalf to register and deregister container instances

with your load balancers.

Use AmazonEC2ContainerServiceforEC2Role and

AmazonEC2ContainerServiceRole managed policies

(respectively)

Page 43: Amazon ECS Deep Dive

ECR IAM Policies and Roles

ECR uses resource-based permissions to control access.

By default, only the repository owner has access to a

repository.

You can apply a policy document that allows others to

access your repository.

Use managed policies for IAM users or roles that allow

differing levels of control:

AmazonEC2ContainerRegistryFullAccess,

AmazonEC2ContainerRegistryPowerUser or

AmazonEC2ContainerRegistryReadOnly

Page 44: Amazon ECS Deep Dive

Image Vulnerability Scanning with Twistlock

Page 45: Amazon ECS Deep Dive

Secrets Management

Task

ECS Cluster

Container instance

Page 46: Amazon ECS Deep Dive

PaaS on ECS

Page 47: Amazon ECS Deep Dive

AWS Elastic Beanstalk

Uses Amazon ECS to coordinate deployments to

multicontainer Docker environments

Takes care of tasks including cluster creation, task

definition and execution

Page 48: Amazon ECS Deep Dive

AWS Elastic Beanstalk

Elastic Beanstalk uses a Dockerrun.aws.json file that

describes how to deploy containers.

The Dockerrun.aws.json file includes three sections:

• AWSEBDockerrunVersion: Set to "2" for multicontainer

Docker environments.

• containerDefinitions: An array of container definitions.

• volumes: Creates mount points in the container instance

that a container can use.

Page 49: Amazon ECS Deep Dive

Convox

Page 50: Amazon ECS Deep Dive

Convox

# Initialize your app and create default manifest

> convox init

# Locally build and run your app as declared in the manifest

> convox start

# Create app

> convox apps create my_app

# Deploy app, output ELB DNS name

> convox deploy

[...]

web: http://my_app-1234567890.us-east-1.elb.amazonaws.com

Page 51: Amazon ECS Deep Dive

Remind Empire

Control layer on top of Amazon ECS that provides a

Heroku like workflow

Any tagged Docker image can be deployed to Empire as

an app

• When you deploy a Docker image to Empire, it will

extract a Procfile from the WORKDIR

• Each process type in the Procfile maps directly to an

ECS Service

Page 52: Amazon ECS Deep Dive

Remind Empire

Routing layer backed by internal ELBs

• An application that specifies a web process will get an

internal ELB attached to its ECS Service

• When a new internal ELB is created, an associated

CNAME record is created in Route53 under the internal

TLD, enabling service discovery via DNS

Page 53: Amazon ECS Deep Dive

Thank You!