Docker and Microservice

46
DOCKER AND MICROSERVICE What is this Docker and Microservice thing that everyone is talking about?

Transcript of Docker and Microservice

Page 1: Docker and Microservice

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

Page 2: Docker and Microservice

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

Page 3: Docker and Microservice

HOST ARCHITECTUREHow to run an application?

Page 4: Docker and Microservice

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

Page 5: Docker and Microservice

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

operating system and applications

Page 6: Docker and Microservice

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

the host OS

Page 7: Docker and Microservice

CONTAINER VIRTUALIZER• Better user space (app) isolation

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

Page 8: Docker and Microservice

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

sandbox• Docker = Container

implementation• Benefits

• Lightweight• Faster to launch

Page 9: Docker and Microservice

ISOLATED CONTAINERS

Page 10: Docker and Microservice

INFRASTRUCTURE AS CODECode your infrastructure

Page 11: Docker and Microservice

CONTAINER TECHExisted for years in Linux

Page 12: Docker and Microservice

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

Page 13: Docker and Microservice

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

Page 14: Docker and Microservice

DOCKER WORKFLOW

Page 15: Docker and Microservice

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

Page 16: Docker and Microservice

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

Page 17: Docker and Microservice

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)

Page 18: Docker and Microservice

DOCKER ON MAC AND WINDOWSRunning Docker on your Workstation

Page 19: Docker and Microservice

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

Page 20: Docker and Microservice

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

Page 21: Docker and Microservice

MAC DOCKER ARCHITECTURE

Page 22: Docker and Microservice

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

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

$ brew cask install dockertoolbox

Page 23: Docker and Microservice

MULTI-CONTAINERSRunning multiple containers

Page 24: Docker and Microservice

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

Page 25: Docker and Microservice

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

Page 26: Docker and Microservice

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

Page 27: Docker and Microservice

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

Page 28: Docker and Microservice

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

Page 29: Docker and Microservice

MICROSERVICEWhat is Microservice?

Page 30: Docker and Microservice

WHAT IS A MONOLITHIC SYSTEM

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

Page 31: Docker and Microservice

EXTENDING THE MONOLITHIC

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

Page 32: Docker and Microservice

MICROSERVICES

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

Page 33: Docker and Microservice

EXTENDING MICROSERVICES

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

Page 34: Docker and Microservice

DEMOTying together what we have learned so far

Page 36: Docker and Microservice

PROJECT ORGANIZATION• docker

• docker-compose.yml• XXX-microservice

• src/main• docker

• Dockerfile• pom.xml

• pom.xml

Page 37: Docker and Microservice

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

Page 38: Docker and Microservice

CLONE THE PROJECT$ # Clone the project$ git [email protected]:kbastani/spring-cloud-microservice-example.git

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

Page 39: Docker and Microservice

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

Page 40: Docker and Microservice

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

Page 41: Docker and Microservice

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

Page 42: Docker and Microservice

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

Page 43: Docker and Microservice

DOCKER IS A DEPLOYABLE UNIT

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

Page 44: Docker and Microservice

Q + A

Any questions?You can find me at @cybersam

Page 45: Docker and Microservice

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

Page 46: Docker and Microservice

CREDITS• Based on the Quintus template on Slides Carnival -

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