delivery - micro services achieving continuousfiles.meetup.com/4310272/achieving cd with micro...

47
Achieving Continuous Delivery - Micro Services - Vikram Gadang

Upload: others

Post on 28-Oct-2019

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

Achieving Continuous Delivery - Micro Services

- Vikram Gadang

Page 2: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

Agenda● Starting point● Observations and lessons learned● Architecting for CD● Build pipeline strategy● Testing strategy● Deployment strategy

Page 3: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

State of affairs● Manual process (QAR) - Creating deployable artifacts

● Manual build process to QA & Prod

● Manual regression tests followed up by actual feature tests

● Fix regression bugs and/or feature bugs and repeat build

● Move build artifact to stage env and repeat manual testing

● Install the build to production during off-peak hours (2am - 4am)

Page 4: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

Initial Attempt● Use Jenkins as CI server to create a build pipeline

● Manually triggered build, test & deploy

● Write test cases that tested functionality end-to-end

Page 5: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

ObservationsLong build & test time● Most testing involve integration testing

● Tests not very reliable due to backend dependencies

Page 6: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda
Page 7: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

ObservationsMonolithic architecture● Large and complex codebase

● Hard to scale & deploy

● Long-term commitment to a technology

Page 8: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

ObservationsCode coverage by tests.. 30% or less● High cyclomatic complexity. 10 or more per function (too many

if/then/else, switch, try/catch statements in code)

● Entangled code

● Complex dependency mesh

Page 9: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

Micro Services Architecture

Monolithic architecture puts all functionality in one process

Microservices architecture breaks down functionality into many processes

A B C D E

A

A

B

B

C

C

E

E

D

A B

C D

E

B

Page 10: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

Monolithic Architecture

Browser

Tomcat

WAR

StoreFront UI

Catalog Service

Profile Service

Billing Service

Database

Remote Services

Page 11: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

Micro Services Architecture

StoreFront UI

Catalog Service

Profile Service

Billing Service

Database

Remote Service

Page 12: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

Micro Services - Pros

● Divide and Conquer. Break down complex problems into smaller parts that can be solved independently

● Small codebase. Small enough to fit in your head● Enables frequent deployments● Enables polyglot programming & persistence● Enables independent scalability● No long-term commitment required. Rewrite over

maintain

Page 13: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

Micro Services - Hard to do

● Dealing with complexity of the system as a whole● Monitoring● Deployment in a container or outside a container● Avoiding duplication - Hint: move common code to

libraries● Service discovery● Simple != Easy

Page 14: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

Basic Requirements for CD● Make Dev, QA, Stage & Prod environments similar● Zero-downtime deployments● Testing and deployment are integral parts of delivery● Build quality in● Automate everything

○ Build○ Unit tests○ Integration tests○ Code coverage and quality analysis○ Deployment○ Acceptance tests

Page 15: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

Basic Requirements for CD● Ability to scale out individual components● Stateless systems● Convention over configuration● Commit early and often. Test early and fail fast● All triggers are data-driven. (Measure everything)

Page 16: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

Delivery Pipeline

Page 17: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

Implementing the pipeline● Started slow, added components over time

● Build → Test → DeployDev → Test → DeployQA → Test → DeployStage → Test → Deploy Prod → Test

● Continuously improve the pipeline

Page 18: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

Delivery Pipeline

Commit

Build & Unit Test

Integration Test

Deploy DevAcceptance Test

Publish Artifact

QA

Deploy Dev

Acceptance Test

Staging

Deploy Dev

Acceptance Test

Deploy Dev

Acceptance Test

Build & Unit Test

Integration Test

Acceptance Test

Publish Artifact

Deploy QA Acceptance Test

Deploy Stage

Acceptance Test

Production

Deploy Prod

Acceptance Test

Code Analysis

Quality Gate

Page 19: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

Pipeline Implementation

● Jenkins - CI Server

● Gradle - Build tasks

● Nexus - Repository

Page 20: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

Pipeline View (Jenkins)

Page 21: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

Technology Stack● Centos 6.4+

● JVM 7+

● Tomcat 7+

● Spring Boot

● Mongo DB

● MySQL DB

Page 22: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

Source Code Strategy● Minimize pre-requisites to get started

○ Only JDK 7+

● Start with a walking skeleton○ Metrics and health stats○ Unit, integration and acceptance tests○ Security○ Logging○ Build and deployment scripts

Page 23: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

Source Code Strategy● Incremental development with frequent commits

● All commits go into master and are continuously integrated and tested leading to faster feedback

● Developers pick each others changes immediately

● Branch for release, branch for feature more often leads to merge/integration hell at end of sprints

● Feature flags to enable/disable features

● Write less code

Page 24: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

More code = more testspublic void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

StringBuffer jb = new StringBuffer(); String line = null; try { BufferedReader reader = request.getReader(); while ((line = reader.readLine()) != null) jb.append(line); } catch (Exception e) { /*report an error*/ }

try { JSONObject jsonObject = JSONObject.fromObject(jb.toString()); } catch (ParseException e) { // crash and burn throw new IOException("Error parsing JSON request string"); }

// Work with the data ...

// Return response HistoryResponse historyresp = historyService.writeHistory(history); JSONObject respjson = convertToJson(historyresp); response.setContentType("application/json"); response.getWriter().write(respjson.toString()); }

Page 25: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

Less code is better@RequestMapping(value="/history", method=POST)public HistoryResponse writeHistory(@RequestBody History history ) {

return historyService.writeHistory(history);

}

Page 26: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

Testing strategy● Unit testing in 5 mins or less - fast feedback

● Integration testing in data access and remote access layer

● Acceptance tests after unit & integration tests

● Automated code review & quality analysis.

● Builds fails if any of above fail

Page 27: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

Testing● JUnit - Java Unit & Integration Tests

● Mockito - Mocking framework

● QUnit - Javascript unit tests

● Selenium - Acceptance & Web tests

● JMeter - Acceptance & Performance

Page 28: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

Testing strategy

Page 29: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

Real world scenario

Page 30: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

Code Quality Strategy● Enforce minimum code coverage by tests

○ Overall coverage (line & branch) 90% or more○ New code coverage (line & branch) 90% or more

● Enforce low cyclomatic complexity (brain overload & testability)○ 10 per method. (if, while, do, for, &&, ||, ?:, catch, switch, case, return,

throw)

● Enforce code quality○ Error Handling○ Style problems○ Ignoring conventions or skipping unit tests

Page 31: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

Code Quality Tools● Jacoco - Java code coverage

● Saga - JS code coverage

● PMD - Code analysis

● FindBugs - Potential issue finder

● JDepend - Code complexity

● SonarQube - Ties it all together

SagaJS Code Coverage

Page 32: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

Measure Code Quality (Jenkins)

Page 33: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

Measure Code Quality (SonarQube)

Page 34: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

Measure Code Quality

Page 35: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

Measure Code Coverage

Page 36: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

Measure Code Quality

Page 37: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

Deployment Strategy● Automate deployment using Chef● Systems built for zero downtime deployment● Rollback is just another deployment by a single click● Do not make any changes directly in production

environment● Emergency releases go through standard deployment

pipeline

Page 38: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

Deployment Tools

● Jenkins - CI Server

● Nexus - Repository

● Chef - Deployment

Page 39: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

Monitoring● New Relic - Monitoring platform

● Http/JMX monitoring - Health and metrics

● Java Melody - Collection of stats

● Elastic Search/Logstash/Kibana - Logs collection

ARGUS/TOGA

Page 40: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

Monitoring (New Relic)

Page 41: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

API DocumentationAutomate API documentation with Swagger UI https://github.com/wordnik/swagger-ui

Page 42: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

API Documentation

Page 43: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

Takeaways● If it hurts, do it more often

● CD forces you to do the right thing

● You cannot do it without automating the build, test, deploy and release process

● Measure everything. Make no assumptions

● Things go better if dev and ops are friends

Page 44: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

How long would it take your organization to deploy a change that involved just one single line of code?

- Mary & Tom Poppendieck

Page 45: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

How long would it take your organization to deploy a change that involved just one single line of code? Do you deploy changes at this pace on a repeatable, reliable basis?

- Mary & Tom Poppendieck

Page 46: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

References● http://martinfowler.com/books/continuousDelivery.html

● http://martinfowler.com/articles/microservices.html

● http://microservices.io/patterns/microservices.html

● http://www.infoq.com/presentations/cd-gradle-jenkins

● https://speakerdeck.com/bmuschko/building-a-continuous-delivery-

pipeline-with-gradle-and-jenkins

● http://projects.spring.io/spring-boot

● http://12factor.net

Page 47: Delivery - Micro Services Achieving Continuousfiles.meetup.com/4310272/Achieving CD with Micro Services.pdf · Achieving Continuous Delivery - Micro Services - Vikram Gadang. Agenda

Questions?

[email protected]