Docker and Microservice

Post on 22-Jan-2017

359 views 2 download

Transcript of Docker and Microservice

DOCKER AND MICROSERVICEWhat is this Docker and Microservice thing that everyone is talking about?

YOU BUILD IT, YOU RUN IT

“The best way to completely automate operations is to have to developers be responsible for running the software they develop. It is painful at times, but also means considerable creativity gets applied to a very important aspect of the software stack. It also brings developers into direct contact with customers and a very effective feedback loop starts. There is no separate operations department at Amazon: you build it; you run it.

Werner Vogels, CTO Amazon – 2006

HOST ARCHITECTUREHow to run an application?

Infrastructure • Can be a computer, cluster of computers, or the Cloud• Abstract users from servers and infrastructure• There’s a server but it’s hidden from you

SINGLE HOST• Shared resources• No user space (app) isolation• Fairly tight coupling of

operating system and applications

VIRTUAL MACHINE• Better user space (app) isolation• Not very resource efficient• Slower• Guest OS can be different from

the host OS

CONTAINER VIRTUALIZER• Better user space (app) isolation

• Not as strong as that of VM• Lightweight• Operating system virtualization• Base OS

WHAT IS DOCKER?• Container = isolated (user space)

sandbox• Docker = Container

implementation• Benefits

• Lightweight• Faster to launch

ISOLATED CONTAINERS

INFRASTRUCTURE AS CODECode your infrastructure

CONTAINER TECHExisted for years in Linux

Docker (the company) just figured a clever way to packaging it and adding a rich toolset around it.

DOCKER INTERNALS• Docker runs on Linux x64 (only)• Dependent on libcontainer, a Linux container platform

• Container isolation: filesystem, process, network• Layered filesystem

• Benefits• Versioning• Portability

DOCKER WORKFLOW

SAMPLE DOCKERFILEFROM ubuntu:16.04

# Set up RedisRUN apt-get updateRUN DEBIAN_FRONTEND=noninteractive apt-get -y install redis-server redis-tools

# Run Redis and expose portEXPOSE 6379ENTRYPOINT [ "/usr/bin/redis-server" ]CMD []

DOCKER IMAGE AND LAYERED FILESYSTEM• Docker image is a read-only template and is

used to create containers• Docker image consists of filesystems layered

on top of each other• Docker uses Union File Systems UFS to

build an image• Any update to an image adds a new layer

instead of rebuilding the entire image• Container = images + top writable layer• Images are shared across different containers

BASE IMAGE SIZES

Reference: https://www.brianchristner.io/docker-image-base-os-size-comparison/

Ubuntu Fedora CentOS Debian Cirros Alpine BusyBox020406080

100120140160180200 188 187

172

125

8 5 2

Ubuntu Fedora CentOS Debian Cirros Alpine BusyBox.00M

1.00M2.00M3.00M4.00M5.00M6.00M7.00M 6.50M

1.20M 1.20M

3.30M

.02M .04M

2.50M

Size (MB)

# Downloa

ds(Populari

ty)

DOCKER ON MAC AND WINDOWSRunning Docker on your Workstation

DOCKER ON MAC• Let’s focus on running Docker on the Mac• Remember Docker only runs on Linux x64• How do I run it on the Mac?• Need Virtual Machine to emulate a Linux host

• Virtual machine (VM) running Linux x64• Docker engine running on VM• Mac client to communicate with Docker engine on Linux VM

DOCKER FOR MAC VS DOCKER TOOLBOX

Docker for Mac Docker Toolbox

# VMs 1 Multiple

Underlying VM Hypervisor.framework (xhyve) VirtualBox

Base OS Alpine Boot2Docker

(VM) Management Tool Docker.app docker-machine

(VM) Management UI GUI CLI

MAC DOCKER ARCHITECTURE

DOWNLOADhttps://www.docker.com/products/docker-toolbox

https://docs.docker.com/docker-for-mac/Or

$ brew cask install dockertoolbox

MULTI-CONTAINERSRunning multiple containers

USE CASE• Web application running Ruby on Rails

• Common website root directory• 1 Container running Ruby on Rails• 1 Container running Nginx

• Connection to database• 1 Container running Postgres

DOCKER VOLUME• Volume can be created and mounted for more than 1 container to share

resources• Put the website root folder in the volume• Persist data• Volume isn’t Union Filesystems, it exists as the host’s filesystem

DOCKER LINKING• Docker linking relies on the Container names

• Launch your Docker container with names• Link the 2 Docker containers via the parameter --link

• Docker establishes a secure tunnel between the container• Does not expose any ports• Does expose environment variables and /etc/hosts

DOCKER COMPOSE• Before Compose there was Fig• 1 configuration file = Compose file• Use the configuration file to define and launch multiple Docker containers• It basically helps you to organize and configure your multiple Docker

container instances

DOCKER CLUSTERING TOOL• There are some great tools that you can use to elegantly manage multiple

containers at scale• Large scale coordination• Service discovery• Automatic failover

• Examples• Swarm• Kubernetes• Mesos

MICROSERVICEWhat is Microservice?

WHAT IS A MONOLITHIC SYSTEM

Reference: https://github.com/kbastani/spring-cloud-microservice-example

EXTENDING THE MONOLITHIC

Reference: https://github.com/kbastani/spring-cloud-microservice-example

MICROSERVICES

Reference: https://github.com/kbastani/spring-cloud-microservice-example

EXTENDING MICROSERVICES

Reference: https://github.com/kbastani/spring-cloud-microservice-example

DEMOTying together what we have learned so far

PROJECT ORGANIZATION• docker

• docker-compose.yml• XXX-microservice

• src/main• docker

• Dockerfile• pom.xml

• pom.xml

MICROSERVICE DOCKERFILEFROM java:8VOLUME /tmpADD users-microservice-0.1.0.jar app.jarRUN bash –c 'touch /app.jar'EXPOSE 9000ENTRYPOINT [ "java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

CLONE THE PROJECT$ # Clone the project$ git git@github.com:kbastani/spring-cloud-microservice-example.git

$ # Install prerequisites to build project$ brew cask install java$ brew install maven

RUN DOCKER-MACHINE ON LOCAL VM$ # Launch Docker machine, set the VM RAM to 6GB$ docker-machine create --driver virtualbox --virtualbox-memory 6144 default$ docker-machine env$ eval "$(docker-machine env default)"$ docker-machine lsNAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORSdefault * virtualbox Running tcp://192.168.99.100:2376 v1.12.0

RUN DOCKER-MACHINE ON AWS$ # Launch Docker machine on AWS EC2$ vi ~/.aws/credentials$ cat ~/.aws/credentials[default]aws_access_key_id = AKIXXXXXXXXXXXXXaws_secret_access_key = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX$ # Launch an AWS EC2 t2.large instance in us-west-2 zone$ docker-machine create --driver amazonec2 --amazonec2-region us-west-2 --amazonec2-instance-type t2.large docker-aws

BUILD$ # Build Java application and Docker images$ docker images$ mvn clean install$ docker images

$ cd docker $ less docker-compose.yml # Take a peek at the compose config file$ docker-compose up

TEST THE APPLICATIONS$ # Run Eureka – API gateway/router$ open $(echo \"$(echo $DOCKER_HOST)\"| \sed 's/tcp:\/\//http:\/\//g'| \sed 's/[0-9]\{4,\}/8761/g'| \sed 's/\"//g')

$ # Run the movie API – this should register all the microservices$ curl $(echo \"$(echo $DOCKER_HOST)/movie\"| \sed 's/tcp:\/\//http:\/\//g'| \sed 's/[0-9]\{4,\}/10000/g'| \sed 's/\"//g')

DOCKER IS A DEPLOYABLE UNIT

You build it, you run it with Docker. Developer now has the freedom to develop and deploy services.

Q + A

Any questions?You can find me at @cybersam

REFERENCE• Docker Toolbox - https://www.docker.com/products/docker-

toolbox • Docker Documentation - https://docs.docker.com/• Pros and Cons of Microservices -

https://smartbear.com/learn/api-design/what-are-microservices/

• Spring Cloud Microservice Example - https://github.com/kbastani/spring-cloud-microservice-example

CREDITS• Based on the Quintus template on Slides Carnival -

http://www.slidescarnival.com/quintus-free-presentation-template/1405