29
Microservices & API Gateways Marco Palladino

Microservices & API Gateways

Embed Size (px)

Citation preview

Page 1: Microservices & API Gateways

Microservices & API GatewaysMarco Palladino

Page 2: Microservices & API Gateways

#nginx #nginxconf2

I am Marco PalladinoCTO at mashape.com

Core committer at github.com/Mashape/kong

Page 3: Microservices & API Gateways

#nginx #nginxconf3

Monolitich vs Microservices

Pros and Cons

API Gateway Pattern

With real world use-cases

Kong + NGINX

For API Management

Topics

Page 4: Microservices & API Gateways

#nginx #nginxconf4

Monolithic Architecture

Client LBCustomers Orders Invoices

Customers Orders Invoices

Database Schema

Page 5: Microservices & API Gateways

#nginx #nginxconf5

Monolithic Application Pros/Cons

Simplicity, for small codebases

Faster early development speed Easy testing IDE support

Not ideal for growing codebases

Slower iterations in the long term Harder to innovate Steep code

learning curve

Page 6: Microservices & API Gateways

#nginx #nginxconf6

Microservice-oriented architecture

Client

LB Customers

Orders

Invoices

LB

LB

Customers

Orders

Invoices

DB Schema

DB Schema

DB Schema

Event Handler + Workers

Page 7: Microservices & API Gateways

#nginx #nginxconf7

Microservice-oriented Application Pros/Cons

Better architecture for large applications

Better agility in the long term

Microservices: easy to learn

Isolation for scalability and damage control

More moving parts Complex infrastructure requirements

Consistency and availability Harder to test

Page 8: Microservices & API Gateways

#nginx #nginxconf8

Why an API Gateway?

API Gateway

Microservices

Page 9: Microservices & API Gateways

#nginx #nginxconf9

API Gateway Pattern

Client

LB Customers

Orders

Invoices

LB

LB

Customers

Orders

Invoices

DB Schema

DB Schema

DB Schema

API Gateway

• Optimized endpoints• Request collapsing• And more

Page 10: Microservices & API Gateways

#nginx #nginxconf10

Optimized Endpoints

Client

LB Customers

Orders

Invoices

LB

LB

Customers

Orders

Invoices

API Gateway

{ "id": "cus_123", "customer_name": "Bob", "address": "500 Montgomery St, SF" }

{ "id": "order_123", "customer_id": "cus_123", "item_name": "Vacuum Cleaner" }

{ "order_id": "order_123", "price": "99.99" }

GET /customers/{id}

{ "customer_id": "cus_123", "name": "Bob", "address": "500 Montgomery St, SF", "orders": […], "invoices": […], } + extra transformations

Page 11: Microservices & API Gateways

#nginx #nginxconf11

Centralized Middleware Functionality

Client

Public APIs

Private APIs

Partner APIs

API Gateway

• Authentication• Security• Traffic Control• Ops• Logging• Transformations• Etc

Available to everybody

Only for internal usage

Only for specific partners

FaaS AWS Lambda, etc

Page 12: Microservices & API Gateways

#nginx #nginxconf12

Ops: Blue/Green deployments

customers.service 1.0.0

customer.service 1.0.1

API GatewayALL TRAFFIC

NO TRAFFIC

customers.service 1.0.0

customer.service 1.0.1

API GatewayALL TRAFFIC

NO TRAFFIC

Page 13: Microservices & API Gateways

#nginx #nginxconf13

Ops: Canary Releases

customers.service 1.0.0

customer.service 1.0.1

API Gateway100% TRAFFIC

0% TRAFFIC

customers.service 1.0.0

customer.service 1.0.1

API Gateway90% TRAFFIC

10% TRAFFIC

Page 14: Microservices & API Gateways

#nginx #nginxconf14

Ops: Load Balancing

Client OrdersLBCustomersAPI Gateway

Client OrdersCustomersAPI Gateway

Client OrdersCustomersAPI Gateway

Service Discovery

• etcd• consul

1.

2.

3.

Page 15: Microservices & API Gateways

#nginx #nginxconf15

Ops: Circuit Breakers

Client OrdersOrdersAPI Gateway

OrdersInvoices

OrdersCustomers

Too many 50x errors

Page 16: Microservices & API Gateways

Building a microservice !=

Running a microservice

Page 17: Microservices & API Gateways

#nginx #nginxconf17

Page 18: Microservices & API Gateways

#nginx #nginxconf18

API Gateways, and Kong, can help

Microservice

MicroserviceMicroservice

Client

• Authentication• Security• Traffic Control• Ops• Logging• Transformations• Etc

• API for Automation• On-boarding• Developer Portal

Page 19: Microservices & API Gateways

#nginx #nginxconf19

What is Kong?Kong is an open-source management layer for APIs to secure, manage

and extend APIs and Microservices.

https://getkong.org

Page 20: Microservices & API Gateways

#nginx #nginxconf20

What is Kong?Built on top of NGINX, centralizes common middleware functionality:

Page 21: Microservices & API Gateways

#nginx #nginxconf21

Kong PluginsCan be created from scratch &

extended by the community.

Page 22: Microservices & API Gateways

#nginx #nginxconf22

Kong: OpenResty + NGINX

NGINX

OpenResty

Clustering & Datastore

Plugins

RESTful Administration API• JSON HTTP API• Extendable by Plugins• Can be integrated for automation

• Plugins created with LUA• Intercept Request/Response lifecycle• Can integrate with third-party services

• Either Cassandra or PostgreSQL• Optionally Redis for some plugins• Single or multi-DC clustering

• Underlying engine of Kong• Provides hooks for Req/Res lifecycle• Extends underlying NGINX

• The core dependency• Handles low-level operations• Solid foundation and known tech

Page 23: Microservices & API Gateways

#nginx #nginxconf23

NGINX Configurationworker_processes auto; daemon on;

pid pids/nginx.pid; error_log logs/error.log notice;

worker_rlimit_nofile 4864;

events { worker_connections 4864; multi_accept on; }

http { include 'nginx-kong.conf'; }

init_by_lua_block { .. }

init_worker_by_lua_block { .. }

server { listen 0.0.0.0:8000;

location / { access_by_lua_block { .. }

header_filter_by_lua_block { .. }

body_filter_by_lua_block {

.. }

log_by_lua_block { .. } }

}

..nginx.conf nginx-kong.conf

Page 24: Microservices & API Gateways

#nginx #nginxconf24

Kong Entry-points

$ curl 127.0.0.1:8000

$ curl 127.0.0.1:8443

$ curl 127.0.0.1:8001

Proxy

Admin API

Page 25: Microservices & API Gateways

#nginx #nginxconf25

Core Entities

$ curl 127.0.0.1:8001/apis

$ curl 127.0.0.1:8001/consumers

$ curl 127.0.0.1:8001/plugins

Page 26: Microservices & API Gateways

#nginx #nginxconf26

Plugins Configuration Matrix

1. Per every API and every Consumer2. Per every API and a specific Consumer3. Per a specific API and every Consumer4. Per a specific API and a specific Consumer

Page 27: Microservices & API Gateways

#nginx #nginxconf27

Multi-DC deployment

DC1

KONG C*

API API APIAPI API API

KONG C*

DC2

KONGC*

API API APIAPI API API

KONGC*

• Horizontal Scalability• Cassandra or PostgreSQL• Clients can be both internal and external

Client Client

Invalidation events

Data

Page 28: Microservices & API Gateways

Demo Time

Page 29: Microservices & API Gateways

#nginx #nginxconf

Thank You

29

getkong.org

linkedin.com/marcopalladino

@thefosk

mashape.com