Deploying Spring Boot applications with Docker (east bay cloud meetup dec 2014)

  • Published on

  • View

  • Download

Embed Size (px)


<ol><li> 1. @crichardson Deploying Spring Boot applications with Docker Chris Richardson Author of POJOs in Action Founder of the original @crichardson </li><li> 2. @crichardson Presentation goal Deploying Spring-based microservices using Docker </li><li> 3. @crichardson About Chris </li><li> 4. @crichardson About Chris Founder of a buzzword compliant (stealthy, social, mobile, big data, machine learning, ...) startup Consultant helping organizations improve how they architect and deploy applications using cloud, micro services, polyglot applications, NoSQL, ... Creator of Organizer of @oakjug - Co-organizer of </li><li> 5. @crichardson Agenda Introduction to Spring Boot Why immutable infrastructure/containerization Spring Boot and Docker Docker-based deployment pipeline </li><li> 6. @crichardson User registration microservices User Registration Service RabbitMQ MongoDB POST /user { emailAddress: "", password: "xyz" } NewUserNotication User Management Service Email Service Exchange Queue Queue User Registration Web App Registration Form Confirmation page </li><li> 7. @crichardson Spring-based components </li><li> 8. @crichardson Building microservices with Spring Boot Makes it easy to create stand-alone, production ready Spring applications Automatically congures Spring using Convention over Conguration Externalizes conguration Generates standalone executable JARs with embedded web server Provides a standard foundation for all your microservices </li><li> 9. @crichardson Spring Boot simplies conguration Spring Container Application components Fully congured application Conguration Metadata Typesafe JavaCong Annotations Legacy XML Default Conguration Metadata Spring Boot You write less of this Inferred from CLASSPATH </li><li> 10. @crichardson Tiny Spring conguration Scan for controllers Customize JSON serialization </li><li> 11. @crichardson About auto-conguration Builds on Spring framework features @EnableAutoConguration - triggers the inclusion of default conguration @Conditional - beans only active if condition is satised Conditional on class dened on class path e.g. Mongo Driver implies Mongo beans Conditional on bean dened/undened e.g. dene Mongo beans if you havent </li><li> 12. @crichardson The Main program </li><li> 13. @crichardson Building with Gradle buildscript { repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:1.1.8.RELEASE") } } apply plugin: 'scala' apply plugin: 'spring-boot' dependencies { compile "org.springframework.boot:spring-boot-starter-web" compile "org.springframework.boot:spring-boot-starter-data-mongodb" compile "org.springframework.boot:spring-boot-starter-amqp" compile "org.springframework.boot:spring-boot-starter-actuator" testCompile "org.springframework.boot:spring-boot-starter-test" } ... Ensures correct dependencies </li><li> 14. @crichardson Running the microservice $ java -jar build/libs/spring-boot-restful-service.jar --server.port=8081 ... 2014-12-03 16:32:04.671 INFO 93199 --- [ main] n.c.m.r.main.UserRegistrationMain$ : Started UserRegistrationMain. in 5.707 seconds (JVM running for 6.553) $ curl localhost:8081/health {"status":"UP", "mongo":{"status":"UP","version":"2.4.10"}, "rabbit":{"status":"UP", ...} } Built in health checks Command line arg processing </li><li> 15. @crichardson Agenda Introduction to Spring Boot Why immutable infrastructure/containerization Spring Boot and Docker Docker-based deployment pipeline </li><li> 16. @crichardson Spring Boot simplies deployment Spring Boot creates self-contained JAR le No separate application server to install/congure Externalize conguration = immutable application Just need Java But which version of Java? 7.x? 8.y? And, what about the other applications? Tomcat, Play, NodeJS, ... Deploying a system is complex </li><li> 17. @crichardson Package service as an RPM Benets: Encapsulates language, framework, application server, ... Handles dependencies ... But Conicting dependency versions Conicting ports, ... </li><li> 18. @crichardson Lets have immutable infrastructure </li><li> 19. @crichardson Package as AMI, </li><li> 20. @crichardson Service-as-AMI is great BUT... Building is so slow! Booting is so slow! AMIs arent portable - need to build for multiple platforms Heavy-weight: Not practical to run multiple VMs on a developer machine ... </li><li> 21. @crichardson Package a service as a Docker image Lightweight, OS-level virtualization mechanism Runs on Linux (directly or via, e.g., Virtual Box) Docker image: Portable application packaging format Self-contained, read-only le-system image of an operating system + application Layered structure = sharing and caching very, very fast 5 seconds to package application! Docker container: Running Docker image Group of sandboxed processes Builds on control groups and namespaces Contains entire OS but typically the only process is the application (JVM) fast startup </li><li> 22. @crichardson Agenda Introduction to Spring Boot Why immutable infrastructure/containerization Spring Boot and Docker Docker-based deployment pipeline </li><li> 23. @crichardson Packaging a Spring Boot application as a Docker image Install Java Install application JAR le Congure image to run Java on startup Handle externalized conguration </li><li> 24. @crichardson Docker and Java </li><li> 25. @crichardson Dockerle for packaging a Spring Boot application Base image Copy JAR into image /data is base images CWD Expose 8080 Bonus question: why is the ADD command last? </li><li> 26. @crichardson Building the Spring Boot application copy jar to subdir so it can be referenced by Dockerle Build image using ./Dockerle </li><li> 27. @crichardson Running the Spring Boot container dockerrun-d-p8080:8080 -eSPRING_DATA_MONGODB_URI=mongodb:// -eSPRING_RABBITMQ_HOST= --namesb_rest_svc sb_rest_svc Map container port to host port Run as daemon Container name Image name Specify environment variables </li><li> 28. @crichardson Agenda Introduction to Spring Boot Why immutable infrastructure/containerization Spring Boot and Docker Docker-based deployment pipeline </li><li> 29. @crichardson My application architecture API gateway Event Store Service 1 Service 2 Service ... Event Archiver Indexer AWS Cloud S3 NodeJS Scala/Spring Boot </li><li> 30. @crichardson Jenkins-based deployment pipeline Build &amp; Test microservice Build &amp; Test Docker image Deploy Docker image to registry One pipeline per microservice </li><li> 31. @crichardson Smoke testing docker images Smoke test Docker daemon Service containerGET /health POST /containers/create creates POST /containers/{id}/start Docker daemon must listen on TCP port </li><li> 32. @crichardson Publishing Docker images docker tag service-${VERSION}:latest${REGISTRY_HOST_AND_PORT}/service-${VERSION} docker push ${REGISTRY_HOST_AND_PORT}/service-${VERSION} docker/ Pushing only takes 25 seconds! </li><li> 33. @crichardson CI environment runs on Docker EC2 Instance Jenkins Container Artifactory container EBS volume /jenkins- home /gradle-home /artifactory- home </li><li> 34. @crichardson Updating production environment Large EC2 instance running Docker Deployment tool: 1. Compares running containers with whats been built by Jenkins 2. Pulls latest images from Docker registry 3. Stops old versions 4. Launches new versions One day: use Docker clustering solution and a service discovery mechanism, Most likely, AWS container service Mesos and Marathon + Zookeeper, Kubernetes or ??? </li><li> 35. @crichardson Summary Spring Boot is a great way to build Spring-based microservices Docker is a great way to package microservices </li><li> 36. @crichardson @crichardson </li></ol>