docker + microservices in production

49
Docker + Microservices in Production Austin PHP March 10, 2016

Upload: patrick-mizer

Post on 14-Jan-2017

70 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Docker + Microservices in Production

Docker + Microservices in Production

Austin PHPMarch 10, 2016

Page 2: Docker + Microservices in Production

Who am I?Patrick Mizer❏ Lead Engineer at SpareFoot❏ 13 years developing for consumer web❏ ZCE, AWS Associate Architect

[email protected]: maximizer

Page 3: Docker + Microservices in Production

● Think Hotels.com for storage*● Everything in AWS and (recently) in containers● Continuous Delivery: ~100 deployments per week● > 300 production containers● Services handle hundreds of requests/sec

* This kind of storage...

Page 4: Docker + Microservices in Production

What I will talk about today● Briefly: Monolithic Architectures & Microservices Architectures● Briefly: Docker Containers, what are they?● How to use Rancher & Containers to implement a production

microservices architecture ○ Deployment○ Service Discovery○ Load balancing○ Scaling

● Build something!

Page 5: Docker + Microservices in Production

Monolithic Architecture

Database

Facility Component

Reservation Component

Unit Component

ReviewComponent

A/B Testing Component

Authentication Component

Load Balancer

Page 6: Docker + Microservices in Production

Monolithic Architecture

Database

Facility Component

Reservation Component

Unit Component

ReviewComponent

A/B Testing Component

Auth Component

Load Balancer

● Single codebase● Easy to deploy and debug● All logic in memory● Easy to scale out● Centralized Ops team can

easily manage

Page 7: Docker + Microservices in Production

Monolithic Architecture

Time

Database

Facility Component

Reservation Component

Unit Component

ReviewComponent

A/B Testing Component

Auth Component

Load Balancer● Team grows● Codebase grows

○ Tight coupling of components

○ Language constraints● No clear ownership● Long deployment cycles● Scaling = all or nothing

Page 8: Docker + Microservices in Production

Microservices Architecture

Facility Service

Reservation Service

A/B Testing Service

Load Balancer

Unit Service

API GatewayAPI Gateway

A/B Testing Service

Reservation Service

Facility Service

Unit Service

A/B Testing DB

Unit DB

Page 9: Docker + Microservices in Production

Microservices Architecture

Facility Service Service

Load Balancer

Unit Service

GatewayAPI Gateway

A/B Testing Service

Reservation Service

Facility Service

Unit Service

A/B DB

Unit DB

Loosely Coupled+

Bounded Context

Page 10: Docker + Microservices in Production

Microservice Pros

Facility Service Service

Load Balancer

Unit Service

GatewayAPI Gateway

A/B Testing Service

Reservation Service

Facility Service

Unit Service

A/B DB

Unit DB

● Independent delivery● Simpler deployments and

rollbacks.● Right code or framework for the

service● Fault Isolation

Page 11: Docker + Microservices in Production

Microservice Challenges

Facility Service Service

Load Balancer

Unit Service

GatewayAPI Gateway

A/B Testing Service

Reservation Service

Facility Service

Unit Service

A/B DB

Unit DB

● Delivery automation● Service Discovery● Operational Overhead● Networking errors● DevOps

Page 12: Docker + Microservices in Production

Independent Delivery

Build Build Build

Local Integration Staging Production

Deploy

Page 13: Docker + Microservices in Production

Independent Delivery

Build Build Build

Local Integration Staging Production

Deploy

Blocked!

Page 14: Docker + Microservices in Production

Independent Delivery

Build Build Build

Local Integration Staging Production

Deploy

Build

Build Build Build Deploy

Build DeployBuild

Page 15: Docker + Microservices in Production

Independent Delivery

Build Build Build

Local Integration Staging Production

Deploy

Blocked!

Build

Build Build Build Deploy

Build DeployBuild

Page 16: Docker + Microservices in Production

What is Docker?Build, ship, and run any application, anywhere…

● Build: Package your application in a container● Ship: Move that container from one machine to another● Run: Execute your application

Page 17: Docker + Microservices in Production

The Container Metaphor

Page 18: Docker + Microservices in Production

The Container Metaphor

Things to ship

Platformsfor shipped

goods

Page 19: Docker + Microservices in Production

The Container Metaphor

Platformsfor shipped

goods

Page 20: Docker + Microservices in Production

The Container Metaphor

Page 21: Docker + Microservices in Production

The Container Metaphor

Standard Interface

Standard Tools

Page 22: Docker + Microservices in Production

The Container Metaphor

Technologies Platforms

Page 23: Docker + Microservices in Production

The Container Metaphor

Platforms

Page 24: Docker + Microservices in Production

The Container Metaphor

Page 25: Docker + Microservices in Production

The Container Metaphor

Standard Interface

Standard Tools

Page 26: Docker + Microservices in Production

Build: DockerfileFROM ubuntu:15.10

# Install PHP, and ApacheRUN apt-get update && \ apt-get -yq install \ curl \ apache2 \ libapache2-mod-php5 \ php5

# Copy codeRUN rm -r /var/www/htmlCOPY index.php /var/www/html/index.php

# Expose port and run apacheEXPOSE 80CMD ["apachectl", "-D", "FOREGROUND"]

Page 27: Docker + Microservices in Production

Build: Dockerfile

Code Repository

Host 1

Container N

ew

Docker Engine

Build

Dockerfile+

Code

Page 28: Docker + Microservices in Production

Ship: Push & Pull

Code Repository

Host 1

Docker Image

Registry

Container N

ew

Docker Engine

Build

Push

Dockerfile+

Code

Container 2

Container 1

Container 3

Host 2Docker Engine

Pull

Page 29: Docker + Microservices in Production

Run: docker run

Code Repository

Host 1

Docker Image

Registry

Container N

ew

Docker Engine

Build

Push

Dockerfile+

Code

Container 2

Container 1

Container 3

Container N

ew

Host 2Docker Engine

Pull

Run

Page 30: Docker + Microservices in Production

Run: docker run

Container 2

Container 1

Container 3

Container N

ew

Host 2Docker Engine

Run

Running a container is fast!

EC2 Instances = minutesContainers = milliseconds

$ time docker run hello-world

real 0m0.435suser 0m0.028s

Page 31: Docker + Microservices in Production

Facility Service Service

GatewayAPI Gateway

A/B Testing Service

Reservation Service

Facility Service

Microservices

Application Isolation1 microservice per container, each containers is independent.

Heterogeneous TechLanguage and framework are specific to the container.

Easy Deployments We can push and pull images for free with Docker.

Orchestration Tools Swarm, Compose, ECS, Tutum, Mesos, Kubernetes, and Rancher.

Page 32: Docker + Microservices in Production

Docker + Microservice in Prod

Our (abridged) production checklist:

● Deploy and Schedule● Build in fault tolerance● Service Discovery● Scale

Page 33: Docker + Microservices in Production

Docker Orchestration

Page 34: Docker + Microservices in Production

● Resource Management● Container Networking● Service Discovery● Load Balancing

Rancher Server

$ docker run -d --restart=always -p 8080:8080 rancher/server

Page 35: Docker + Microservices in Production

Rancher Concepts● Hosts● Stacks

Systems Architecture

(hosts)

Container Architecture

(stacks)

DeployNetworkingDiscovery

DeploySchedulingScaling

Page 36: Docker + Microservices in Production

Load Balancer

Servicecontainer container

containercontainer

container container

link

Rancher: Container Architecture● Stack

○ Services○ Containers

● Load Balancer● Links (cross stack)

LB

Page 37: Docker + Microservices in Production

Deploying containers

Code Repository

Host 1

Docker Image

Registry

Container N

ew

Docker Engine

Build

Push

Dockerfile+

Code

Container 2

Container 1

Container 3

Container N

ew

Host 2Docker Engine

Pull

Run

Page 38: Docker + Microservices in Production

MASTER

BRANCH A

Dev Staging Production

Page 39: Docker + Microservices in Production

MASTER

BRANCH A

Dev Staging Production

Page 40: Docker + Microservices in Production

MASTER

BRANCH A

Dev Staging

Service1

service1:prod

Production

Service1

service1:stage

Service1

service1:dev-branch-name

Page 41: Docker + Microservices in Production

MASTER

BRANCH A

Service1

service1:prod

rancher-compose

Page 42: Docker + Microservices in Production

MASTER

BRANCH A

Service1

service1:prod

Service1

service1:stage

Service1

service1:dev-branch-name

Service1

Service2

Service3

App1 App2

HTTP

Page 43: Docker + Microservices in Production

Fault Tolerance - AWS + Rancher

ELB

LB LB

US-EAST-1-A US-EAST-1-C

LB

US-EAST-1-B

www

Page 44: Docker + Microservices in Production

Scheduling and Scaling

AWS Autoscaling Group

Container Scale

Page 45: Docker + Microservices in Production

So, let’s build something

Public Load Balancer

Random Number Service

Web

Private Load Balancer

www Requirements● Public access on port 80 to webheads● Private access only to our super secure

Random Number Service● Fault Tolerant

○ Can persist one host failing● Can scale both web and service

independently

Page 46: Docker + Microservices in Production

Our Service

Public Load Balancer

Random Number Service

Web

Private Load Balancer

www

<?php$ret = ["server_ip" => $_SERVER['SERVER_ADDR'], "remote_ip" => $_SERVER['REMOTE_ADDR'], "x_forwarded_for" => $_SERVER['HTTP_X_FORWARDED_FOR'], "random_number_generator" => rand(0, 100)];

echo json_encode($ret);

Page 47: Docker + Microservices in Production

Our Web head

Public Load Balancer

Random Number Service

Web

Private Load Balancer

www<?php $randomNumberResponse = file_get_contents(getenv("RANDOM_NUMBER_URL")); $randomArr = json_decode( $randomNumberResponse, true);?>

<h1>Hello, your random number is: <?=$randomArr['random_number_generator']?> </h1>

Page 48: Docker + Microservices in Production

Live demo!

Page 49: Docker + Microservices in Production

Thank you!My containers:https://quay.io/repository/patjmizer/php-meetup-webhttps://quay.io/repository/patjmizer/php-meetup-service

The code: https://github.com/maximizer/php-meetup-docker

Wanna talk shop? Reach out:● Email: [email protected]

Questions?