Slide 1

Slide 1 text

@sandeepdinesh Scalable Microservices gRPC and Kubernetes and Containers oh my! @sandeepdinesh Sandeep Dinesh, Cloud Developer Advocate

Slide 2

Slide 2 text

@sandeepdinesh Have you heard of our lord and savior, Microservices?

Slide 3

Slide 3 text

@sandeepdinesh Google Trends: Microservices

Slide 4

Slide 4 text

@sandeepdinesh “Write programs that do one thing, and do it well. Write programs that work together” - Unix Philosophy UNIX

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

Everything at Google runs in containers We launch over 2 billion containers per week. • Gmail, Web Search, Maps, ... • MapReduce, batch, ... • GFS, Colossus, ...

Slide 9

Slide 9 text

@sandeepdinesh Kubernetes Borg Stubby

Slide 10

Slide 10 text

@sandeepdinesh Kubernetes

Slide 11

Slide 11 text

@sandeepdinesh App A

Slide 12

Slide 12 text

@sandeepdinesh App A

Slide 13

Slide 13 text

@sandeepdinesh Replication Controller App A

Slide 14

Slide 14 text

@sandeepdinesh Replication Controller App A App A App A App A

Slide 15

Slide 15 text

@sandeepdinesh Replication Controller Service A App A App A App A App A

Slide 16

Slide 16 text

@sandeepdinesh Microservice A

Slide 17

Slide 17 text

@sandeepdinesh That’s it!

Slide 18

Slide 18 text

@sandeepdinesh Microservice A Microservice C Microservice D Microservice B

Slide 19

Slide 19 text

@sandeepdinesh Microservice A Microservice C Microservice D Microservice B

Slide 20

Slide 20 text

@sandeepdinesh Microservice A Microservice C Microservice D Microservice B

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

@sandeepdinesh Let’s build

Slide 23

Slide 23 text

@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

Slide 24

Slide 24 text

@sandeepdinesh Bash $ docker build -t gcr.io//frontend:1.0 $ gcloud docker push gcr.io//frontend:1.0 Build the Container

Slide 25

Slide 25 text

@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

Slide 26

Slide 26 text

@sandeepdinesh Bash $ kubectl create -f frontend-controller.yaml Launch the Controller

Slide 27

Slide 27 text

@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

Slide 28

Slide 28 text

@sandeepdinesh Bash $ kubectl create -f frontend-service.yaml Launch the Service

Slide 29

Slide 29 text

@sandeepdinesh

Slide 30

Slide 30 text

@sandeepdinesh What about Communication?

Slide 31

Slide 31 text

@sandeepdinesh Microservice A Microservice C Microservice D Microservice B

Slide 32

Slide 32 text

@sandeepdinesh “The biggest issue in changing a monolith into microservices lies in changing the communication pattern.” - Martin Fowler

Slide 33

Slide 33 text

O(1010) RPCs per second at Google

Slide 34

Slide 34 text

@sandeepdinesh

Slide 35

Slide 35 text

@sandeepdinesh Protocol Buffers

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

@sandeepdinesh Protocol Buffers HTTP/2

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

Multiplexing Bidirectional Streaming Flow Control HTTP/2 No more polling, sockets, or clunky SSE Control your congestion Single TCP connection

Slide 40

Slide 40 text

@sandeepdinesh Protocol Buffers HTTP/2 Multiple Languages

Slide 41

Slide 41 text

Multiple Languages C/C++

Slide 42

Slide 42 text

@sandeepdinesh Protocol Buffers HTTP/2 Multiple Languages Mobile First

Slide 43

Slide 43 text

@sandeepdinesh Let’s upgrade our app

Slide 44

Slide 44 text

@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

Slide 45

Slide 45 text

@sandeepdinesh JavaScript var grpc = require('grpc'), proto = grpc.load('interface.proto'), GeoService = grpc.buildServer([proto.geo.GeoService.service]); Backend Code

Slide 46

Slide 46 text

@sandeepdinesh JavaScript var server = new GeoService({ 'geo.GeoService': { distanceBetween: function(call, callback) { callback(null, getDistance(call.request)); } } }); Backend Code

Slide 47

Slide 47 text

@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

Slide 48

Slide 48 text

@sandeepdinesh JavaScript server.bind('0.0.0.0:50051'); server.listen(); Backend Code

Slide 49

Slide 49 text

@sandeepdinesh Bash $ docker build -t gcr.io//backend:1.0 $ gcloud docker push gcr.io//backend:1.0 Build the Container

Slide 50

Slide 50 text

@sandeepdinesh JavaScript var grpc = require('grpc'), proto = grpc.load('interface.proto'), client = new proto.geo.GeoService('backend:50051'); Frontend Code

Slide 51

Slide 51 text

@sandeepdinesh JavaScript var request = { origin: { lat: 0, lng: 0 }, destination: { lat: 15, lng: 15 } } Frontend Code

Slide 52

Slide 52 text

@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

Slide 53

Slide 53 text

@sandeepdinesh Bash $ docker build -t gcr.io//frontend:2.0 $ gcloud docker push gcr.io//frontend:2.0 Build the Container

Slide 54

Slide 54 text

@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

Slide 55

Slide 55 text

@sandeepdinesh Bash $ kubectl create -f backend-controller.yaml Launch the Controller

Slide 56

Slide 56 text

@sandeepdinesh YAML apiVersion: v1 kind: Service metadata: name: backend labels: name: backend spec: Backend Service ports: - port: 50051 targetPort: 50051 selector: name: backend

Slide 57

Slide 57 text

@sandeepdinesh Bash $ kubectl create -f backend-service.yaml Launch the Service

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

@sandeepdinesh

Slide 60

Slide 60 text

@sandeepdinesh Kubernetes k8s.io grpc.io

Slide 61

Slide 61 text

@sandeepdinesh Google Container Engine

Slide 62

Slide 62 text

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