Scalable Microservices by Sandeep Dinesh

Scalable Microservices by Sandeep Dinesh

Transcript

  1. @sandeepdinesh Scalable Microservices gRPC and Kubernetes and Containers oh my!

    @sandeepdinesh Sandeep Dinesh, Cloud Developer Advocate
  2. @sandeepdinesh Have you heard of our lord and savior, Microservices?

  3. @sandeepdinesh Google Trends: Microservices

  4. @sandeepdinesh “Write programs that do one thing, and do it

    well. Write programs that work together” - Unix Philosophy UNIX
  5. @sandeepdinesh Why Microservices? Scalability Security Polyglot Development Maintainability Continuous Deployment

    Decentralization Robustness
  6. @sandeepdinesh Why Microservices? Scalability Security Polyglot Development Maintainability Continuous Deployment

    Decentralization Robustness How?
  7. None
  8. Everything at Google runs in containers We launch over 2

    billion containers per week. • Gmail, Web Search, Maps, ... • MapReduce, batch, ... • GFS, Colossus, ...
  9. @sandeepdinesh Kubernetes Borg Stubby

  10. @sandeepdinesh Kubernetes

  11. @sandeepdinesh App A

  12. @sandeepdinesh App A

  13. @sandeepdinesh Replication Controller App A

  14. @sandeepdinesh Replication Controller App A App A App A App

    A
  15. @sandeepdinesh Replication Controller Service A App A App A App

    A App A
  16. @sandeepdinesh Microservice A

  17. @sandeepdinesh That’s it!

  18. @sandeepdinesh Microservice A Microservice C Microservice D Microservice B

  19. @sandeepdinesh Microservice A Microservice C Microservice D Microservice B

  20. @sandeepdinesh Microservice A Microservice C Microservice D Microservice B

  21. @sandeepdinesh Microservice A Microservice C Microservice D (2.0) Microservice B

  22. @sandeepdinesh Let’s build

  23. @sandeepdinesh JavaScript var http = require('http'); var server = http.createServer(function

    (request, response) { response.writeHead(200, {"Content-Type": "text/plain"}); response.end("Hello World\n"); }); server.listen(3000); App
  24. @sandeepdinesh Bash $ docker build -t gcr.io/<project>/frontend:1.0 $ gcloud docker

    push gcr.io/<project>/frontend:1.0 Build the Container
  25. @sandeepdinesh YAML apiVersion: v1 kind: ReplicationController metadata: labels: name: frontend

    name: frontend spec: replicas: 2 selector: name: frontend Frontend Replication Controller template: metadata: labels: name: frontend spec: containers: - image: gcr.io/project-id/frontend:1.0 name: frontend ports: - containerPort: 3000 name: http-server
  26. @sandeepdinesh Bash $ kubectl create -f frontend-controller.yaml Launch the Controller

  27. @sandeepdinesh YAML apiVersion: v1 kind: Service metadata: name: frontend labels:

    name: frontend spec: type: LoadBalancer Frontend Service ports: - port: 80 targetPort: 3000 protocol: TCP selector: name: frontend
  28. @sandeepdinesh Bash $ kubectl create -f frontend-service.yaml Launch the Service

  29. @sandeepdinesh

  30. @sandeepdinesh What about Communication?

  31. @sandeepdinesh Microservice A Microservice C Microservice D Microservice B

  32. @sandeepdinesh “The biggest issue in changing a monolith into microservices

    lies in changing the communication pattern.” - Martin Fowler
  33. O(1010) RPCs per second at Google

  34. @sandeepdinesh

  35. @sandeepdinesh Protocol Buffers

  36. message HelloRequest { string greeting = 1; } message HelloResponse

    { string reply = 1; } service HelloService { rpc SayHello(HelloRequest) returns (HelloResponse); } IDL (Interface definition language) Describe once and generate interfaces for any language. Data Model Structure of the request and response. Binary format for network transmission. Wire Format Protocol Buffers
  37. @sandeepdinesh Protocol Buffers HTTP/2

  38. @sandeepdinesh HTTP/2 HTTP/1.1 http://www.http2demo.io/

  39. Multiplexing Bidirectional Streaming Flow Control HTTP/2 No more polling, sockets,

    or clunky SSE Control your congestion Single TCP connection
  40. @sandeepdinesh Protocol Buffers HTTP/2 Multiple Languages

  41. Multiple Languages C/C++

  42. @sandeepdinesh Protocol Buffers HTTP/2 Multiple Languages Mobile First

  43. @sandeepdinesh Let’s upgrade our app

  44. @sandeepdinesh ProtoBuff package geo; service GeoService { rpc DistanceBetween (Points)

    returns (Distance) {} } message Point { float lat = 1; float lng = 2; } message Points { Point origin = 1; Point destination = 2; } message Distance { float distance = 1; } .proto
  45. @sandeepdinesh JavaScript var grpc = require('grpc'), proto = grpc.load('interface.proto'), GeoService

    = grpc.buildServer([proto.geo.GeoService.service]); Backend Code
  46. @sandeepdinesh JavaScript var server = new GeoService({ 'geo.GeoService': { distanceBetween:

    function(call, callback) { callback(null, getDistance(call.request)); } } }); Backend Code
  47. @sandeepdinesh JavaScript function distance(a,b,c,d){//haversine formula stuff} function getDistance(points){ return distance(points.origin.lat,

    points.origin.lng, points.destination.lat, points.destination.lng); } Backend Code
  48. @sandeepdinesh JavaScript server.bind('0.0.0.0:50051'); server.listen(); Backend Code

  49. @sandeepdinesh Bash $ docker build -t gcr.io/<project>/backend:1.0 $ gcloud docker

    push gcr.io/<project>/backend:1.0 Build the Container
  50. @sandeepdinesh JavaScript var grpc = require('grpc'), proto = grpc.load('interface.proto'), client

    = new proto.geo.GeoService('backend:50051'); Frontend Code
  51. @sandeepdinesh JavaScript var request = { origin: { lat: 0,

    lng: 0 }, destination: { lat: 15, lng: 15 } } Frontend Code
  52. @sandeepdinesh JavaScript client.distanceBetween(request, function(error, distance) { if (error) { response.end(JSON.stringify(error));

    } else { response.end("Distance = " + JSON.stringify(distance) + "\n"); } }); Frontend Code
  53. @sandeepdinesh Bash $ docker build -t gcr.io/<project>/frontend:2.0 $ gcloud docker

    push gcr.io/<project>/frontend:2.0 Build the Container
  54. @sandeepdinesh YAML apiVersion: v1 kind: ReplicationController metadata: labels: name: backend

    name: backend spec: replicas: 4 selector: name: backend Backend Replication Controller template: metadata: labels: name: backend spec: containers: - image: gcr.io/project-id/backend:1.0 name: backend ports: - containerPort: 50051 name: grpc-server
  55. @sandeepdinesh Bash $ kubectl create -f backend-controller.yaml Launch the Controller

  56. @sandeepdinesh YAML apiVersion: v1 kind: Service metadata: name: backend labels:

    name: backend spec: Backend Service ports: - port: 50051 targetPort: 50051 selector: name: backend
  57. @sandeepdinesh Bash $ kubectl create -f backend-service.yaml Launch the Service

  58. @sandeepdinesh Bash $ kubectl rolling-update frontend \ --image=gcr.io/smart-spark-93622/frontend:2.0 \ --update-period="5m"

    Upgrade the Frontend
  59. @sandeepdinesh

  60. @sandeepdinesh Kubernetes k8s.io grpc.io

  61. @sandeepdinesh Google Container Engine

  62. Thank you! Thank you! Kubernetes: k8s.io gRPC: grpc.io Code: git.io/v4S8y

    Me: @sandeepdinesh Slack: bit.ly/gcp-slack Cloud: cloud.google.com