[Mandy Waite] Scalable Microservices with gRPC, Kubernetes, and Containers

[Mandy Waite] Scalable Microservices with gRPC, Kubernetes, and Containers

Presentation from GDG DevFest Ukraine 2015 - the biggest Google related event in the country. October 23-24, Lviv. Learn more at http://devfest.gdg.org.ua/

3a6de6bc902de7f75c0e753b3202ed52?s=128

Google Developers Group Lviv

October 23, 2015
Tweet

Transcript

  1. Scalable Microservices with gRPC, Kubernetes, and Containers

  2. #dfua, @tekgrrl, @googlecloud Mandy Waite Developer Advocate +MandyWaite @tekgrrl

  3. Image by Connie Zhou

  4. #dfua, @tekgrrl, @googlecloud job hello_world = { runtime = {

    cell = 'ic' } // Cell (cluster) to run in binary = '.../hello_world_webserver' // Program to run args = { port = '%port%' } // Command line parameters requirements = { // Resource requirements ram = 100M disk = 100M cpu = 0.1 } replicas = 5 // Number of tasks } 10000 Developer View
  5. #dfua, @tekgrrl, @googlecloud Developer View

  6. #dfua, @tekgrrl, @googlecloud web browsers BorgMaster link shard UI shard

    BorgMaster link shard UI shard BorgMaster link shard UI shard BorgMaster link shard UI shard Scheduler borgcfg web browsers scheduler Borglet Borglet Borglet Borglet Config file BorgMaster link shard UI shard persistent store (Paxos) Binary What just happened? Cell Storage Developer View
  7. Hello world! Hello world! Hello world! Hello world! Hello world!

    Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Image by Connie Zhou Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world!
  8. #dfua, @tekgrrl, @googlecloud Images by Connie Zhou Observations: • Containers

    make everyone more productive • Everything at Google runs in a Container! • The Datacenter is one big System ◦ On which we schedule Containers See this video for more on Scheduling at Google: https://youtu.be/elHbkoJOBNE?t=4426 http://goo.gl/1C4nuo (Borg paper)
  9. Kubernetes

  10. #dfua, @tekgrrl, @googlecloud Greek for “Helmsman”; also the root of

    the word “Governor” • Orchestrator for Docker containers • Supports multi-cloud environments • Inspired and informed by Google’s experiences and internal systems • Open source, written in Go Manage applications, not machines Kubernetes
  11. #dfua, @tekgrrl, @googlecloud web browsers y Kubelet Kubelet Kubelet Kubelet

    Kubernetes Master Replication Controller Scheduler API Server Kube-UI Container Registry kubectl Proxy <Your App> web browsers Developer View (Kubernetes)
  12. #dfua, @tekgrrl, @googlecloud The atom of scheduling for containers Represents

    an application specific logical host Hosts containers and volumes Each has its own routable (no NAT) IP address Ephemeral • Pods are functionally identical and therefore ephemeral and replaceable Pod Web Server Volume Consumers Pods
  13. #dfua, @tekgrrl, @googlecloud Can be used to group multiple containers

    & shared volumes Containers within a pod are tightly coupled Shared namespaces • Containers in a pod share a network IP and port namespace • Pods Pod Git Synchronizer Node.js App Container Volume Consumers Github
  14. #dfua, @tekgrrl, @googlecloud Pod Networking (across nodes) Pods have IPs

    which are routable Pods can reach each other without NAT • Even across nodes No Brokering of Port Numbers These are fundamental requirements Many solutions • Flannel, Weave, Cloud Provider 10.1.2.0/24 10.1.1.0/24 10.1.1.211 10.1.1.2 10.1.2.106 10.1.3.0/24 10.1.3.45 10.1.3.17 10.1.3.0/24
  15. #dfua, @tekgrrl, @googlecloud Dashboard show: type = FE Pod Pod

    frontend Pod frontend Pod Pod Dashboard show: version = v2 type = FE version = v2 type = FE version = v2 • Metadata with semantic meaning • Membership identifier • The only Grouping Mechanism Behavior Benefits ➔ Allow for intent of many users (e.g. dashboards) ➔ Build higher level systems … ➔ Queryable by Selectors Labels ← These are important
  16. #dfua, @tekgrrl, @googlecloud Replication Controller Pod Pod frontend Pod frontend

    Pod Pod Replication Controller #pods = 1 version = v2 show: version = v2 version= v1 version = v1 version = v2 Replication Controller #pods = 2 version = v1 show: version = v2 Behavior Benefits • Keeps Pods running • Gives direct control of Pod #s • Grouped by Label Selector ➔ Recreates Pods, maintains desired state ➔ Fine-grained control for scaling ➔ Standard grouping semantics Replication Controllers
  17. #dfua, @tekgrrl, @googlecloud Portal (VIP) Client Pod Container Pod Container

    Pod Container Container A logical grouping of pods that perform the same function • grouped by label selector Load balances incoming requests across constituent pods Choice of pod is random but supports session affinity (ClientIP) Gets a stable virtual IP and port • also a DNS name Services
  18. #dfua, @tekgrrl, @googlecloud Developer View (Replication Controller) spec: containers: -

    name: php-guestbook image: php-guestbook:containercamp resources: limits: memory: "128Mi" cpu: "500m" ports: - containerPort: 80 protocol: TCP replicas: 1 10000
  19. #dfua, @tekgrrl, @googlecloud Kubernetes Cluster Applications Ops Dev App A

    App B App C App A
  20. HTTP/2, ProtoBufs and gRPC

  21. • Improve end-user perceived latency • Address the "head of

    line blocking" • Not require multiple connections • Retain the semantics of HTTP/1.1 "HTTP/2 is a protocol designed for low- latency transport of content over the World Wide Web"
  22. #dfua, @tekgrrl, @googlecloud HTTP/2 HTTP/1.1 http://www.http2demo.io/

  23. #dfua, @tekgrrl, @googlecloud • One TCP connection • Request →

    Stream ◦ Streams are multiplexed ◦ Streams are prioritized • Binary framing layer ◦ Prioritization ◦ Flow control ◦ Server push • Header compression HTTP/2 in one slide…
  24. #dfua, @tekgrrl, @googlecloud What are Protocol Buffers? Structured representation of

    data Google's lingua franca for data • 48k+ Message Types • 12k+ Proto files Evolutionary Development Incrementally solved problems, Now used for: • RPC Systems • Persistent Data Storage
  25. #dfua, @tekgrrl, @googlecloud Protocol buffers: • Efficient and compact binary

    data representation • Clear compatibility rules; can easily be extended over time • Generates idiomatic, easy to use classes for many languages • Strongly typed; less error prone Why Protocol Buffers? Why?
  26. #dfua, @tekgrrl, @googlecloud Protocol Buffers language version 3 Specified by

    syntax = “proto3”; proto2 continues to be supported All fields are optional in proto3 No user specified default values No groups (FYI for those that use them) Message Format (proto3) syntax = “proto3”; message Person { string name = 1; int32 id = 2; string email = 3; enum PhoneType { MOBILE = 0; HOME = 1; WORK = 2; } message PhoneNumber { string number = 1; PhoneType type = 2; } repeated PhoneNumber phone = 4; }
  27. #dfua, @tekgrrl, @googlecloud Add new fields without breaking backwards-compatibility old

    implementations ignore the new fields when parsing In proto3 any field can be removed, but don’t renumber existing fields Extensible syntax = “proto3”; message Person { string name = 1; int32 id = 2; string email = 3; address addr = 4; message address { string firstLine = 1; string secondLine = 2; string postalCode = 3; string country = 4; } ... }
  28. #dfua, @tekgrrl, @googlecloud https://github.com/google/protobuf Protocol Buffer Compiler

  29. #dfua, @tekgrrl, @googlecloud gRPC goals Enable developers to build micro-

    service-based applications Build an open source, standards-based, best-of-breed, feature-rich RPC system Create easy-to-use, efficient and idiomatic libraries micro-services performant and scalable efficient and idiomatic Provide a performant and scalable RPC framework
  30. “The biggest issue in changing a monolith into microservices lies

    in changing the communication pattern.” - Martin Fowler
  31. #dfua, @tekgrrl, @googlecloud IDL to describe an API Automatically generated

    servers and clients in 10+ languages Takes advantage of feature set of HTTP/2 Lightweight open connections Point to point Streaming! Bidirectional streaming! gRPC in a nutshell
  32. #dfua, @tekgrrl, @googlecloud Getting Started Define a service in a

    .proto file using Protocol Buffers IDL Generate server and client code using the protocol buffer compiler Use the gRPC API to write a simple client and server for your service in the languages of your choice
  33. #dfua, @tekgrrl, @googlecloud gRPC Language Support Implementations • C core

    ◦ Native bindings in C++, Node.js, Python, Ruby, ObjC, PHP, C# • Java using Netty or OkHttp (+ inProcess for testing) • Go
  34. Microservices

  35. 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); A Hello World Microservice
  36. #dfua, @tekgrrl, @googlecloud Build and tag a Container Image Push

    Image to Container Repository Create Service and ReplicationController config files Create ReplicationController Create Service From Code to deployed Microservice Microservice A Microservice C Microservice D Microservice B
  37. #dfua, @tekgrrl, @googlecloud YAML spec: replicas: 2 selector: type: FE

    version: 1.0 template: metadata: labels: type: FE version: 1.0 spec: containers: - image: gcr.io/project-id/frontend:1.0 name: frontend ports: - containerPort: 3000 name: http-server Frontend Replication Controller Spec or ‘DesiredState’ for Pods Specifies: • The number of Pods to maintain • A template for Containers in the pod ◦ Container Image ◦ Labels ◦ Ports ◦ Env Vars ◦ Volume Mount Points • Volume Configurations • Label Selector for Pod constituency
  38. #dfua, @tekgrrl, @googlecloud YAML apiVersion: v1 kind: Service metadata: name:

    frontend labels: name: frontend spec: type: LoadBalancer ports: - port: 80 targetPort: 3000 protocol: TCP selector: name: frontend Frontend Service Service Definition Defines: • Port Mappings • Label Selector for Service constituency • Whether the Service has an external Load Balancer
  39. #dfua, @tekgrrl, @googlecloud Build and tag a Container Images Push

    Images to Container Repository Create backend Service and ReplicationController config files Create backend ReplicationController Create backend Service Update frontend to use new image Scale as needed Add a Backend and Update Frontend Microservice A Microservice C Microservice D Microservice B
  40. #dfua, @tekgrrl, @googlecloud ProtoBuf 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; } IDL File (ProtoBuf)
  41. JavaScript var grpc = require('grpc'), proto = grpc.load('interface.proto'), GeoService =

    grpc.buildServer([proto.geo.GeoService.service]); var server = new GeoService({ 'geo.GeoService': { distanceBetween: function(call, callback) { callback(null, getDistance(call.request)); } } }); … server.bind('0.0.0.0:50051'); server.listen(); Backend Code
  42. JavaScript var grpc = require('grpc'), proto = grpc.load('interface.proto'), client =

    new proto.geo.GeoService('backend:50051'); var request = { origin: { lat: 0, lng: 0 }, destination: { lat: 15, lng: 15 } } client.distanceBetween(request, function(error, distance) { // error handling response.end("Distance = " + JSON.stringify(distance) + "\n"); }); Frontend Code
  43. YAML Backend Replication Controller YAML spec: replicas: 4 selector: name:

    backend template: metadata: labels: name: backend spec: containers: - image: gcr.io/project-id/backend:1.0 name: backend ports: - containerPort: 50051 name: grpc-server
  44. YAML Backend Service YAML apiVersion: v1 kind: Service metadata: name:

    backend labels: name: backend spec: ports: - port: 50051 targetPort: 50051 selector: name: backend
  45. Demo - Visualization

  46. #dfua, @tekgrrl, @googlecloud Node3 Kubelet Proxy Pod Container Container Container

    Container Pod Container Container Container Container Node3 Kubelet Proxy Pod Container Container Container Container Pod Container Container Container Container Node1 Kubelet Proxy Pod Container Container Pod $ kubectl proxy --www=k8s-visualizer/ Visualizing Kubernetes Master APIs Scheduling REST (pods, services, controllers) AuthN Scheduler Replication Controller Container
  47. #dfua, @tekgrrl, @googlecloud State of our Microservices Service Label selectors:

    version = 1.0 type = Frontend Service name = Frontend Label selector: type = FE Replication Controller version= 1.0 Replication Controller version = v1 #pods = 1 show: version = v2 type = FE Replication Controller version = v1 #pods = show: version = v2 Pod Replication Controller version = 1.0 type = FE #pods = 2 Pod frontend Pod version = 1.0 type = FE Service Label selectors: version = 1.0 type = Frontend Service name = backend Label selector: type = BE Replication Controller Pod Pod frontend Pod version= 1.0 version = 1.0 Replication Controller version = 1.0 type = BE #pods = 2 type = BE type = BE
  48. #dfua, @tekgrrl, @googlecloud Service Label selectors: version = 1.0 type

    = Frontend Service name = frontend Label selector: type = BE Replication Controller Pod frontend Pod version= v1 version = v1 Replication Controller version = v1 #pods = 1 show: version = v2 type = FE type = FE Scaling Example Pod frontend Pod version = v1 type = FE Replication Controller version = v1 #pods = 2 show: version = v2 Pod Pod Replication Controller version = v1 type = FE #pods = 4 show: version = v2 version = v1 type = FE
  49. #dfua, @tekgrrl, @googlecloud Replication Controller Replication Controller - Name =

    “backend” - Selector = {“name”: “backend”} - Template = { ... } - NumReplicas = 4 API Server 3 Start 1 more OK 4 How many? How many? Canonical example of control loops Have one job: ensure N copies of a pod • if too few, start new ones • if too many, kill some • group == selector Replicated pods are fungible • No implied order or identity Replication Controllers
  50. #dfua, @tekgrrl, @googlecloud Rolling Update Example Service Label selectors: version

    = 1.0 type = Frontend Service name = backend Label selector: type = BE Replication Controller Pod Pod frontend Pod version= v1 version = v1 Replication Controller version = v1 type = BE #pods = 2 show: version = v2 type = BE type = BE Replication Controller version = v2 type = BE #pods = 2 show: version = v2 Pod version = v2 type = BE version = v2
  51. #dfua, @tekgrrl, @googlecloud Freedom to pick the language independently for

    each micro-service, based on performance, library availability, team expertise etc MicroServices using gRPC Loosely coupled development Blocks of functionality can be broken off into separate MicroService. Allows organic growth Multi-language High Performance Make use of the strengths of HTTP/2 and Protocol Buffers
  52. #dfua, @tekgrrl, @googlecloud Kubernetes 1.0 as of mid July •

    Formerly announced at OSCON in July Open sourced in June, 2014 • won the BlackDuck “rookie of the year” award Google launched Google Container Engine (GKE) • hosted Kubernetes • https://cloud.google.com/container-engine/ Roadmap: • https://github.com/GoogleCloudPlatform/kubernetes/milestones Kubernetes Status
  53. #dfua, @tekgrrl, @googlecloud Google Container Engine (GA) Managed Kubernetes (Kubernetes

    v1) Manages Kubernetes master uptime Manages Updates Cluster Resize via Managed Instance Groups Centralised Logging Google Cloud VPN support
  54. #dfua, @tekgrrl, @googlecloud Kubernetes is Open Source We want your

    help! http://kubernetes.io https://github.com/GoogleCloudPlatform/kubernetes irc.freenode.net #google-containers @kubernetesio
  55. #dfua, @tekgrrl, @googlecloud grpc is Open Source We want your

    help! http://grpc.io/contribute https://github.com/grpc irc.freenode.net #grpc @grpcio grpc-io@googlegroups.com
  56. #dfua, @tekgrrl, @googlecloud Tweet questions to: @tekgrrl Slides: http://bit.ly/1LQVhJN Questions

  57. #dfua, @tekgrrl, @googlecloud Service Label selectors: version = 1.0 type

    = Frontend Service name = backend Label selector: type = BE Replication Controller Pod Pod frontend Pod version= v1 version = v1 Replication Controller version = v1 type = BE #pods = 2 show: version = v2 type = BE type = BE Canary Example Replication Controller Replication Controller version = v2 type = BE #pods = 1 show: version = v2 Pod frontend Pod version = v2 type = BE