Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio...

52
Building APIs with Microservices Things I wish I’d known James Gough @Jim__Gough

Transcript of Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio...

Page 1: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

Building APIs with Microservices

Things I wish I’d known

James Gough @Jim__Gough

Page 2: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

Agenda

▪ What is an API? ▪ Problems faced when building APIs ▪ Designing and testing restful APIs ▪ API versioning concerns ▪ Developing APIs in a distributed team ▪ A model for operational maturity

▪ Microservices and Java ▪ Using Docker ▪ Scheduling containers ▪ Kubernetes ▪ Introducing Istio ▪ API Management

Page 3: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

James (Jim) Gough ▪ Previous: Java developer and contributor ▪ Previous: Technical Trainer at Morgan Stanley and Bloomberg ▪ Current: Java Developer/Architect and API Technical Lead at Morgan Stanley ▪ Co-Author of Optimizing Java

Page 4: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

What is an API?

“API is the acronym for Application Programming Interface, which is a software intermediary that allows two applications to talk to each other. Each time you use an app like Facebook, send an instant message, or check the weather on your phone, you’re using an API”

MuleSoft

Page 5: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

What is an API?

“An application-programming interface (API) is a set of programming instructions and standards for accessing a Web-based software application or Web tool.”

How Stuff Works

Page 6: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

What is an API?

“Application programming interfaces hide complexity from developers, extend systems to partners, organise code, and make components reusable”

InfoWorld

Page 7: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

What is an API?

ClientAPI

Services ?

HTTP REST

Page 8: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

What is an API?

Service Database

Java Library/JAR

? ?

Page 9: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

Problems Faced with all APIs

v 1.0 v 1.1

Versioning Discovery Security

Performance Quality

https://smartbear.com/SmartBear/media/ebooks/State-of-API-Report-2016.pdf

Page 10: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

Decoupled API Challenges

Runtime NetworkTest

Automation

Page 11: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

Designing Restful APIs

HATEOASWell Defined

Purposed Endpoints

Versioning

Agree Standards

Use Beta Phase

Developer Friendly Error

Structure

https://github.com/paypal/api-standards

Page 12: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

org.springframework.cloud.contract.spec.Contract.make { request { method 'POST' body([ "date": "2018-09-29" ]) url '/day' headers { header('Content-Type', 'application/json') } } response { status 200 body([ "dayOfWeek": "SATURDAY" ]) }}

Contract Driven Development

Tests

Stub Server

Page 13: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

API Spec First

API Specification First

Server Code

Client CodeCode Generator

https://editor.swagger.io

Page 14: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

Versioning APIs

App

App

R1 R2/loan

https://github.com/paypal/api-standards

{loanid:987}

{loanid:621}

{id:987}{id:987}

{id:621}

Page 15: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

API Lifecycle

Planned

Beta

Live

Deprecated

Minor Release

• Backwards compatible

• Customer does nothing

• Changes additive and optional

• Semantics don’t change

Major Release

• Can break compatibility

• Must have a migration plan

• Runs with deprecated API

Retired

https://github.com/paypal/api-standards

Page 16: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

Developing APIs in a Distributed Team

Monolith v2018.09.31

Monolith v2018.10.31

Binary compatibility

Build time consistency

Easy to reason about

Simple to deploy

Does not scale.. at all

Page 17: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

Delivering a Simple API

Customer API Portal

Monolithic Deployment

Page 18: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

Developing APIs in a Distributed Team

/product

/sales

• All API deployments must move freely

• Full automated ephemeral testing available

• Replicated in a UAT environment for testing

• Expect many deployments per day

• Easy roll back

• Enhance test cases if issues found in Prod

Page 19: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

Delivering an API Program

Customer API Portal

Ga t eway

/product

/sales

Page 20: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

Delivering an API Program

/

/

/

/

Customer API Portal

Page 21: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

Continuous API Delivery

Java SCMPR

Build Local Tests QC

ApprovedBuild Ephemeral

Tests

Daniel Bryant - Continuous Delivery in Java

Unit Tests

Integration Tests

Fault Tolerance TestsAPI Compatibility Testing (swagger-diff)

Reduce Risk Using Deployment Strategy

Acceptance Tests

Performance TestingQA

Staging

Promote

Simulated Prod Testing

Production

Promote

Page 22: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

Let’s Go! Full Microservices?

Spring 5.0 Microservices - Second Edition by Rajesh R V

Level 0 Traditional

Level 1 Basic

Level 2 Intermediate

Level 3 Advanced

Application Monolithic Service Oriented Integrations

Service Oriented Applications API Centric

Database One Size Fits All Enterprise DB

Enterprise DBs + No SQL Polyglot, DBaaS Matured Data

Lake

Infrastructure Physical Machines Virtualization Cloud Containers

Monitoring Infrastructure App and Infra Monitoring APMs

APM and Central Log

Management

Process Waterfall Agile and CI CI & CD DevOps

Page 23: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

Building Microservices Using Java

<XML />

<XML />

<XML />

Page 24: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

Introducing Spring Boot

<XML />Stand Alone

Opinionated

Page 25: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

Demo Spring Boot

Greeter

Message

Page 26: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

Java is All We Need

Spring Boot App

▪Doesn’t work on my machine! ▪ Different version of Java installed ▪ Different file system supported

▪Consistency between environments ▪ Can’t be tested locally

▪Coordinating Microservices is not just about Java ▪ Use the right language for the job

▪Coordinating Java is the wrong abstraction ▪ Lots of scripts and Java specifics

Page 27: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

The Netflix Stack

Greeter Message

Mobile App

Ribbon

Service Discovery (Eureka)

JWT API Gateway (Zuul)

Hystrix Circuit Breaking

Page 28: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

The Netflix Stack Today▪ Provided a set of libraries to solve most microservices problems

▪ Are these services still micro?

▪ Doesn’t solve the problem of where to run services and deployment strategies

▪ Absolutely key in driving forward this complex technical space

▪ Environments can be challenging - a good place to start

▪ Can we do better? Is Java the wrong building block?Greeter

Ribbon

Hystrix Circuit Breaking

1

Page 29: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

What is Docker?

Operating System Namespaces Control Groups Layer Capabilities

Other OS Functions

Libs

Greeter

Docker Engine Platform Specific

Generic

libcontainerd lib network graph Plugins

Docker Client Docker Compose Docker Swarm Docker Registry

REST API

Page 30: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

Difference between a VM and Docker

Binary/Libs

Greeter

Operating System

Docker Engine

Binary/Libs

Message

Operating System

Hypervisor

Guest OS

Binary/Libs Binary/Libs

Greeter Message

Layer Reuse

Page 31: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

Advantages of using Containers

▪ Immutability

▪Works across all environments and deployments ▪ Easier to test

▪ Fundamental building block to simplify deployment

▪ Leave the specifics of the language behind

Page 32: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

Demo Docker

Docker

Greeter

Docker

Message

$ cat Dockerfile

FROM openjdk:8-jdk-alpineVOLUME /tmpADD build/libs/greeter-0.0.1-SNAPSHOT.jar app.jarEXPOSE 8080ENV JAVA_OPTS=""ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar

Page 33: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

Demo Docker

Docker

Greeter

Docker

Message

$ docker build -t greeter:conf .

Sending build context to Docker daemon 24.29MBStep 1/6 : FROM openjdk:8-jdk-alpine ---> 54ae553cb104Step 2/6 : VOLUME /tmp ---> Using cache ---> be3f9b66169aStep 3/6 : ADD build/libs/greeter-0.0.1-SNAPSHOT.jar app.jar ---> Using cache ---> 9ebc7b30c927Step 4/6 : EXPOSE 8080 ---> Using cache ---> 13e6d1568396Step 5/6 : ENV JAVA_OPTS="" ---> Using cache ---> 9659c9657c0bStep 6/6 : ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ] ---> Using cache ---> 7622a23bac14Successfully built 7622a23bac14Successfully tagged greeter:conf

Page 34: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

Demo Docker

Docker

Greeter

Docker

Message

$ docker run -p 8080:8080 -t greeter:conf . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.0.6.RELEASE)

2018-10-27 12:24:50.059 INFO 1 --- [ main] com.oreilly.greeter.GreeterApplication : Starting GreeterApplication on 5db7d230d95e with PID 1 (/app.jar started by root in /)

$ docker run -p 8081:8081 -t message:conf . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) )

Page 35: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

Demo Docker

Docker

Greeter

Docker

Message

$ docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES224ab1a98b3f message:conf "sh -c 'java $JAVA_O…" About a minute ago Up About a minute 0.0.0.0:8081->8081/tcp naughty_leakey

912c04bda714 greeter:conf "sh -c 'java $JAVA_O…" 2 minutes ago Up 2 minutes 0.0.0.0:8080->8080/tcp boring_hermann

$ curl http://localhost:8080/simple{"message":"Hello Conference”}

$ curl http://localhost:8080/custom{"timestamp":"2018-10-27T12:29:45.679+0000","status":500,"error":"Internal Server Error"

Page 36: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

Demo Docker

Docker

Greeter

Docker

Message

http://localhost:8081

http://message-api-service

?

Page 37: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

Scheduling and Running Containers

Consumer

Invocation

Discovery

Scale

Monitoring

Page 38: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

Pod

Components of Kubernetes

Metadata and Labels

Deployment Service Ingress

Page 39: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

Kubernetes

Demo Kubernetes

M

Pod

M v1

v2

Docker

Greeter NG I NX

Greeter Service

Message Service

Service

Ingress

Deployment

2

Page 40: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

Scheduling and Running Containers Revisited

Consumer

Discovery

Invocation

Scale

Monitoring

Tracing

Page 41: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

Introducing Service Mesh

I n g r e s s

/product

/sales

Control and Management

Security

Tracing

Dealing with Failures

Page 42: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

Decoupled APIs Revisited

Greeter

Ribbon

Hystrix Circuit BreakingPod

RouterGreeter

http://message-service-api

Page 43: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

Pod

Istio - Building on the Envoy Sidecar

Service

• Dynamic service discovery• Load balancing• TLS termination• HTTP/2 and gRPC proxies• Circuit breakers• Health checks• Staged rollouts with %-based traffic split• Fault injection• Rich metrics

https://istio.io/docs/concepts/what-is-istio/

Page 44: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

Istio Main Components

Pilot Mixer Citadel

Service Discovery Traffic Management Resiliency

Converts to envoy rules

Platform independent Enforces access control Collects telemetry

TLS Certificates Service-to-service authentication

Control Plane

Page 45: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

Egress

Istio Main Components

Control Plane Pilot Mixer Citadel

Data Plane

Service Service

I n g r e s s

Page 46: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

Istio Networking Configuration

GatewayConfigure a load balancer for incoming HTTP Traffic Attach against Ingress

Virtual Service Configure how requests are routed within the Istio service mesh

Destination Rule Configure traffic after VirtualService routing, e.g Least Connected

Page 47: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

Kubernetes with Istio

Demo Istio on Google Cloud

I n g r e s s

Greeter

Message v1 Message v2

GatewayService

Deployment

Destination Rule Service

Virtual Service

3

Page 48: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

API Management - Customer View

https://market.mashape.com

Page 49: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

API Management - Customer View

https://market.mashape.com

Page 50: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

API Management - Development View

https://apigee.com/

Page 51: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

Kubernetes with Istio

Demo Istio with Apigee

I n g r e s s

Greeter

Message v1 Message v2

GatewayVirtual ServiceService

Destination Rule Service

Virtual Service

Deployment

Apigee

Page 52: Building APIs with Microservices · Using Docker Scheduling containers Kubernetes Introducing Istio API Management. James (Jim) Gough Previous: Java developer and contributor Previous:

Summary

Level 0 Traditional

Level 1 Basic

Level 2 Intermediate

Level 3 Advanced

Application Monolithic Service Oriented Integrations

Service Oriented Applications API Centric

Database One Size Fits All Enterprise DB

Enterprise DBs + No SQL Polyglot, DBaaS Matured Data Lake

Infrastructure Physical Machines Virtualization Cloud Containers

Monitoring Infrastructure App and Infra Monitoring APMs APM and Central

Log Management

Process Waterfall Agile and CI CI & CD DevOps

QualityTest

Automation

@Jim__Gough