docker + microservices in production

Post on 14-Jan-2017

72 Views

Category:

Documents

3 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Docker + Microservices in Production

Austin PHPMarch 10, 2016

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

patrick@sparefoot.comGitHub: maximizer

● 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...

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!

Monolithic Architecture

Database

Facility Component

Reservation Component

Unit Component

ReviewComponent

A/B Testing Component

Authentication Component

Load Balancer

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

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

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

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

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

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

Independent Delivery

Build Build Build

Local Integration Staging Production

Deploy

Independent Delivery

Build Build Build

Local Integration Staging Production

Deploy

Blocked!

Independent Delivery

Build Build Build

Local Integration Staging Production

Deploy

Build

Build Build Build Deploy

Build DeployBuild

Independent Delivery

Build Build Build

Local Integration Staging Production

Deploy

Blocked!

Build

Build Build Build Deploy

Build DeployBuild

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

The Container Metaphor

The Container Metaphor

Things to ship

Platformsfor shipped

goods

The Container Metaphor

Platformsfor shipped

goods

The Container Metaphor

The Container Metaphor

Standard Interface

Standard Tools

The Container Metaphor

Technologies Platforms

The Container Metaphor

Platforms

The Container Metaphor

The Container Metaphor

Standard Interface

Standard Tools

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"]

Build: Dockerfile

Code Repository

Host 1

Container N

ew

Docker Engine

Build

Dockerfile+

Code

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

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

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

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.

Docker + Microservice in Prod

Our (abridged) production checklist:

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

Docker Orchestration

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

Rancher Server

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

Rancher Concepts● Hosts● Stacks

Systems Architecture

(hosts)

Container Architecture

(stacks)

DeployNetworkingDiscovery

DeploySchedulingScaling

Load Balancer

Servicecontainer container

containercontainer

container container

link

Rancher: Container Architecture● Stack

○ Services○ Containers

● Load Balancer● Links (cross stack)

LB

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

MASTER

BRANCH A

Dev Staging Production

MASTER

BRANCH A

Dev Staging Production

MASTER

BRANCH A

Dev Staging

Service1

service1:prod

Production

Service1

service1:stage

Service1

service1:dev-branch-name

MASTER

BRANCH A

Service1

service1:prod

rancher-compose

MASTER

BRANCH A

Service1

service1:prod

Service1

service1:stage

Service1

service1:dev-branch-name

Service1

Service2

Service3

App1 App2

HTTP

Fault Tolerance - AWS + Rancher

ELB

LB LB

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

LB

US-EAST-1-B

www

Scheduling and Scaling

AWS Autoscaling Group

Container Scale

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

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);

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>

Live demo!

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: patrick@sparefoot.com

Questions?

top related