Slide 1

Slide 1 text

Elastic Scaling in a (Micro)service oriented Architecture @BastianHofmann

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Microservices

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Service Oriented Architecture

Slide 6

Slide 6 text

Monolith

Slide 7

Slide 7 text

http://blog.philipphauer.de/microservices-nutshell-pros-cons/ Monolith Microservices

Slide 8

Slide 8 text

Benefits

Slide 9

Slide 9 text

Stricter separation of concerns

Slide 10

Slide 10 text

Diverse technology stacks

Slide 11

Slide 11 text

Things that you don’t want to do in language X

Slide 12

Slide 12 text

Problems

Slide 13

Slide 13 text

Problems

Slide 14

Slide 14 text

Challenges

Slide 15

Slide 15 text

Performance

Slide 16

Slide 16 text

Latency

Slide 17

Slide 17 text

Stability

Slide 18

Slide 18 text

Reliability

Slide 19

Slide 19 text

Transparency

Slide 20

Slide 20 text

Monitoring

Slide 21

Slide 21 text

Learning Curves

Slide 22

Slide 22 text

Code Reuse

Slide 23

Slide 23 text

Maintenance

Slide 24

Slide 24 text

How to solve these

Slide 25

Slide 25 text

How to elastically scale

Slide 26

Slide 26 text

•Deploying •Running •Releasing •Configuring •Discovering •Scaling Agenda

Slide 27

Slide 27 text

A lot of this is also useful for monoliths

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

11 million users

Slide 31

Slide 31 text

193 countries

Slide 32

Slide 32 text

~1800 request/s

Slide 33

Slide 33 text

lots of data

Slide 34

Slide 34 text

>100 million publications

Slide 35

Slide 35 text

~ 140 components

Slide 36

Slide 36 text

~ 400 repositories

Slide 37

Slide 37 text

haproxy node memcache postgresql mongodb solr infinispan hbase mongodb solr community services

Slide 38

Slide 38 text

+ async events, stream and batch processing

Slide 39

Slide 39 text

https://www.flickr.com/photos/npobre/2601582256/

Slide 40

Slide 40 text

Deployment

Slide 41

Slide 41 text

How to get the services on our servers?

Slide 42

Slide 42 text

Diverse technology stacks

Slide 43

Slide 43 text

The same for every service

Slide 44

Slide 44 text

One Click Deployment

Slide 45

Slide 45 text

•Ansible •Capistrano •Saltstack •Custom •….

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

Automation

Slide 48

Slide 48 text

Build/Test/Release pipeline

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

Availability

Slide 51

Slide 51 text

Zero Downtime Deployments

Slide 52

Slide 52 text

Server Server Server Server

Slide 53

Slide 53 text

Server Server Server Server

Slide 54

Slide 54 text

Server Server Server Server

Slide 55

Slide 55 text

Server Server Server Server

Slide 56

Slide 56 text

Server Server Server Server

Slide 57

Slide 57 text

Server Server Server Server

Slide 58

Slide 58 text

Stability

Slide 59

Slide 59 text

Canary environments

Slide 60

Slide 60 text

Server Server Server Server

Slide 61

Slide 61 text

Server Server Server Server Test with low amount of traffic

Slide 62

Slide 62 text

Fast deployments

Slide 63

Slide 63 text

Fast rollbacks

Slide 64

Slide 64 text

https://www.flickr.com/photos/40987321@N02/5580348753/

Slide 65

Slide 65 text

Different libraries, packages, web servers, configurations, versions

Slide 66

Slide 66 text

Provisioned Base boxes

Slide 67

Slide 67 text

Services installed in a sandbox

Slide 68

Slide 68 text

https://www.docker.com/

Slide 69

Slide 69 text

Running the service

Slide 70

Slide 70 text

How do I stop and start a service and ensure it keeps running?

Slide 71

Slide 71 text

Diverse technology stacks

Slide 72

Slide 72 text

The same for every service

Slide 73

Slide 73 text

•Supervisord •Upstart •S6 •Ruine •Monit •Circus •Restartd •Docker •…

Slide 74

Slide 74 text

https://www.docker.com/

Slide 75

Slide 75 text

docker run my-service

Slide 76

Slide 76 text

Releases

Slide 77

Slide 77 text

How to synchronize changes over services?

Slide 78

Slide 78 text

APIs

Slide 79

Slide 79 text

API Versioning

Slide 80

Slide 80 text

GET /v23/foo/abr Host: myservice.local

Slide 81

Slide 81 text

GET /foo/abr Host: myservice.local X-Version: 23

Slide 82

Slide 82 text

GET /foo/abr?version=23 Host: myservice.local

Slide 83

Slide 83 text

GET /foo/abr Host: myservice.local Accept: application/vnd.company.v23+json

Slide 84

Slide 84 text

No backwards compatibility breaks

Slide 85

Slide 85 text

Feature Flags

Slide 86

Slide 86 text

public function hasAccess() { return featureFlag()->isActive( FeatureFlag::TEST_ONE ); }

Slide 87

Slide 87 text

No content

Slide 88

Slide 88 text

No content

Slide 89

Slide 89 text

Configuration Management

Slide 90

Slide 90 text

How do I synchronize configuration over services?

Slide 91

Slide 91 text

[ "db_user": "user", "db_pw": "pw", "serviceA": "serviceA.local:8018" ]

Slide 92

Slide 92 text

Config file on disk

Slide 93

Slide 93 text

Duplication

Slide 94

Slide 94 text

Inconsistencies

Slide 95

Slide 95 text

Consul https://www.consul.io/

Slide 96

Slide 96 text

•Consul •Zookeeper •etcd •…

Slide 97

Slide 97 text

Consul Server Consul Server Consul Server Consul Agent ver Consul Agent Server Consul Agent Server Co Ag Server

Slide 98

Slide 98 text

Key/Value Store

Slide 99

Slide 99 text

$kv->put('test/foo/bar', 'bazinga'); $kv->get('test/foo/bar', ['raw' => true]); $kv->delete('test/foo/bar');

Slide 100

Slide 100 text

Credentials

Slide 101

Slide 101 text

$kv->put('test/db/pw', 'secret_pw');

Slide 102

Slide 102 text

https://www.vaultproject.io/

Slide 103

Slide 103 text

Cycling of credentials

Slide 104

Slide 104 text

Service Discovery

Slide 105

Slide 105 text

How does one service know where another service is?

Slide 106

Slide 106 text

Hostname/IP:Port

Slide 107

Slide 107 text

Server Service A Server Service B Service C Service C

Slide 108

Slide 108 text

Configuration

Slide 109

Slide 109 text

$config = [ 'serviceA' => [ '192.168.0.1:8001', '192.168.0.2:8001', ], 'serviceB' => [ '192.168.0.1:8002', ], 'serviceC' => [ '192.168.0.2:8003', ] ];

Slide 110

Slide 110 text

Consul https://www.consul.io/

Slide 111

Slide 111 text

Load balancing?

Slide 112

Slide 112 text

Round robin in the client

Slide 113

Slide 113 text

$config = [ 'serviceA' => [ '192.168.0.1:8001', '192.168.0.2:8001', ], 'serviceB' => [ '192.168.0.1:8002', ], 'serviceC' => [ '192.168.0.2:8003', ] ];

Slide 114

Slide 114 text

Service/Server down?

Slide 115

Slide 115 text

$config = [ 'serviceA' => [ '192.168.0.1:8001', '192.168.0.2:8001', ], 'serviceB' => [ '192.168.0.1:8002', ], 'serviceC' => [ '192.168.0.2:8003', ] ];

Slide 116

Slide 116 text

Health checks

Slide 117

Slide 117 text

GET /health HTTP/1.1 Host: serviceA.local HTTP/1.1 200 OK

Slide 118

Slide 118 text

Central load balancer

Slide 119

Slide 119 text

HAproxy http://www.haproxy.org/

Slide 120

Slide 120 text

Server Service A Server Service B Service C Service C Load balancer

Slide 121

Slide 121 text

Scalability?

Slide 122

Slide 122 text

Load balancer

Slide 123

Slide 123 text

Load balancer

Slide 124

Slide 124 text

Elasticity?

Slide 125

Slide 125 text

Load balancer

Slide 126

Slide 126 text

Consul https://www.consul.io/

Slide 127

Slide 127 text

Consul Server Consul Server Consul Server Consul Agent ver Consul Agent Server Consul Agent Server Co Ag Server

Slide 128

Slide 128 text

Consul for Service Discovery

Slide 129

Slide 129 text

Consul Agent Server Service A Registration Health check

Slide 130

Slide 130 text

No content

Slide 131

Slide 131 text

Load balance directly in the client

Slide 132

Slide 132 text

Consul API

Slide 133

Slide 133 text

$ curl http://localhost:8500/v1/catalog/service/refind- service [ { "ServicePort": 10780, "ServiceAddress": "", "ServiceTags": [ "env:rg_dev", "protocol:http" ], "ServiceName": "refind-service", "ServiceID": "refind-service", "Address": "172.20.4.61", "Node": "refind-1.ipbl.rgoffice.net" }, { "ServicePort": 10780, "ServiceAddress": "", "ServiceTags": [ "env:rg_dev", "protocol:http"

Slide 134

Slide 134 text

DNS

Slide 135

Slide 135 text

$ dig -p 8600 @localhost refind- service.service.rgoffice.consul. ANY ; <<>> DiG 9.9.5-3ubuntu0.11-Ubuntu <<>> -p 8600 @localhost refind-service.service.rgoffice.consul. ANY ; (1 server found) ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 19315 ;; flags: qr aa rd; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 0 ;; WARNING: recursion requested but not available ;; QUESTION SECTION: ;refind-service.service.rgoffice.consul. IN ANY ;; ANSWER SECTION: refind-service.service.rgoffice.consul. 0 IN A 172.20.4.61 refind-service.service.rgoffice.consul. 0 IN A 172.20.4.58

Slide 136

Slide 136 text

No content

Slide 137

Slide 137 text

Metrics

Slide 138

Slide 138 text

Flexible routing options

Slide 139

Slide 139 text

Circuit breakers

Slide 140

Slide 140 text

Tracing

Slide 141

Slide 141 text

No content

Slide 142

Slide 142 text

Server Service A Server Service B Service C Service C Linkerd Consul

Slide 143

Slide 143 text

Single Point of Failure

Slide 144

Slide 144 text

Server Service A Server Service B Service C Service C Linkerd Consul Linkerd Consul

Slide 145

Slide 145 text

Scaling?

Slide 146

Slide 146 text

Cloud Solutions

Slide 147

Slide 147 text

No content

Slide 148

Slide 148 text

Using the cloud is not always possible

Slide 149

Slide 149 text

… or even desirable

Slide 150

Slide 150 text

https://mesosphere.github.io/marathon/

Slide 151

Slide 151 text

No content

Slide 152

Slide 152 text

Very Powerful

Slide 153

Slide 153 text

Learning curve

Slide 154

Slide 154 text

Kubernetes Cluster

Slide 155

Slide 155 text

Image • A docker image built from a Dockerfile that contains everything a service needs to run

Slide 156

Slide 156 text

• A container runs a docker image. • Only 1 process can run inside of a container Container

Slide 157

Slide 157 text

Pod • A group of 1 or more containers • Same port space • Ports are not accessible from outside of the pod

Slide 158

Slide 158 text

Replica Set • Defines and manages how many instances of a pod should run

Slide 159

Slide 159 text

Deployment • Manages updates and rollbacks of replica sets

Slide 160

Slide 160 text

Service • Makes a port of a pod accessible to other pods

Slide 161

Slide 161 text

Ingress • Makes a service accessible to the outside of Kubernetes

Slide 162

Slide 162 text

Node • A physical server • Containers get distributed automatically

Slide 163

Slide 163 text

ConfigMaps & Secrets • Configuration that can be mounted inside of a container

Slide 164

Slide 164 text

Volumes • Volumes can be mounted into a container to access a ConfigMap, Secret or a folder on the host

Slide 165

Slide 165 text

Namespaces • Dedicated environment to deploy services in

Slide 166

Slide 166 text

Example

Slide 167

Slide 167 text

PHP-FPM NGINX LINKERD STATSD MEM CACHED MONGO ROUTER PHP Application POD

Slide 168

Slide 168 text

PHP-FPM NGINX LINKERD STATSD MEM CACHED MONGO ROUTER PHP Application POD ReplicaSet: 2 instances PHP-FPM NGINX LINKERD STATSD MEM CACHED MONGO ROUTER PHP Application POD

Slide 169

Slide 169 text

PHP-FPM NGINX LINKERD STATSD MEM CACHED MONGO ROUTER ReplicaSet: 2 instances PHP-FPM NGINX LINKERD STATSD MEM CACHED MONGO ROUTER CONFIG WEB :80 PHP Application POD PHP Application POD

Slide 170

Slide 170 text

PHP-FPM NGINX LINKERD STATSD MEM CACHED MONGO ROUTER ReplicaSet: 2 instances PHP-FPM NGINX LINKERD STATSD MEM CACHED MONGO ROUTER CONFIG WEB :80 https://php-app.k8s.foo.com:443/ PHP Application POD PHP Application POD

Slide 171

Slide 171 text

FROM node:7 WORKDIR /opt/appmiral ADD . /opt/appmiral RUN apt-get install -y curl git && \ npm install bower@latest -g && npm install grunt@latest -g && \ npm install && bower install --allow- root && grunt build EXPOSE 9012 CMD node /opt/appmiral/dist/server.js

Slide 172

Slide 172 text

docker build -t appmiral . docker run appmiral

Slide 173

Slide 173 text

ApiVersion: extensions/v1beta1 kind: Deployment metadata: name: appmiral spec: replicas: 2 template: spec: containers: - name: appmiral image: your-registry/researchgate/appmiral resources: requests: cpu: 1 memory: 200Mi env: - name: NODE_ENV value: "production" ports: - containerPort: 9012 livenessProbe: httpGet: path: /health port: 9012

Slide 174

Slide 174 text

- name: appmiral image: your-registry/researchgate/appmiral resources: requests: cpu: 1 memory: 200Mi env: - name: NODE_ENV value: "production" ports: - containerPort: 9012 livenessProbe: httpGet: path: /health port: 9012

Slide 175

Slide 175 text

kind: Service apiVersion: v1 metadata: name: appmiral spec: ports: - name: http port: 9012 targetPort: 9012 protocol: TCP selector: app: appmiral

Slide 176

Slide 176 text

apiVersion: extensions/v1beta1 kind: Ingress metadata: name: appmiral-ing spec: rules: - host: appmiral.kluster-01.rgoffice.net http: paths: - path: / backend: serviceName: appmiral servicePort: 9012

Slide 177

Slide 177 text

kubectl create -f k8s_appmiral.yaml

Slide 178

Slide 178 text

Rolling Deployments

Slide 179

Slide 179 text

kubectl

Slide 180

Slide 180 text

REST API

Slide 181

Slide 181 text

No content

Slide 182

Slide 182 text

Helm The package manager for Kubernetes https://helm.sh/

Slide 183

Slide 183 text

No content

Slide 184

Slide 184 text

Service Discovery

Slide 185

Slide 185 text

Service Virtual IP address

Slide 186

Slide 186 text

Environment Variables

Slide 187

Slide 187 text

APPMIRAL_SERVICE_HOST=10.0.162.149 APPMIRAL_SERVICE_PORT=80

Slide 188

Slide 188 text

DNS

Slide 189

Slide 189 text

$ nslookup appmiral Server: 10.0.0.10 Address 1: 10.0.0.10 Name: appmiral Address 1: 10.0.162.149

Slide 190

Slide 190 text

LinkerD in Kubernetes

Slide 191

Slide 191 text

PHP-FPM NGINX LINKERD STATSD MEM CACHED MONGO ROUTER PHP Application POD

Slide 192

Slide 192 text

No content

Slide 193

Slide 193 text

Manual Scaling

Slide 194

Slide 194 text

kubectl scale --replicas=3 deployment/my-app

Slide 195

Slide 195 text

AutoScaling

Slide 196

Slide 196 text

No content

Slide 197

Slide 197 text

https://kubernetes.io/docs/user-guide/horizontal-pod- autoscaling/

Slide 198

Slide 198 text

https://www.flickr.com/photos/darkdwarf/19701555974/

Slide 199

Slide 199 text

Expect the un- expected: How to handle errors gracefully Saturday, 9:00 am, Track B

Slide 200

Slide 200 text

http://speakerdeck.com/u/bastianhofmann

Slide 201

Slide 201 text

http://twitter.com/BastianHofmann http://lanyrd.com/people/BastianHofmann http://speakerdeck.com/u/bastianhofmann [email protected]