-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathslardar.slide
459 lines (306 loc) · 10.4 KB
/
slardar.slide
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
OpenResty TCP 服务代理和动态路由
杭州 OpenResty Meetup
20 Dec 2017
黄励博(huangnauh)
又拍云
https://github.com/huangnauh
* slide
.image slardar/qrcode.png _ 300
https://huangnauh.github.io/2017OpenRestyMeetup.html
https://github.com/huangnauh/slides
http://go-talks.appspot.com/github.com/huangnauh/slides/OpenRestyMeetup.slide
* 测试环境
git clone [email protected]:huangnauh/slardar.git
git co docker
docker-compose up
docker-compose.yml:
version: '3.2'
services:
slardar:
links:
- consul
- mysql3307
- mysql3306
image: huangnauh/slardar:test
volumes:
- type: bind
source: ./nginx/conf
target: /usr/local/slardar/nginx/conf
* NGINX
NGINX 1.9 开始引入 stream 模块, 实现四层协议的转发和代理, 和 http 类似, 也是采用分阶段处理请求的方式
.image slardar/phase.png _ 1000
.caption Image credit: [[http://nginx.org/en/docs/stream/stream_processing.html][stream_processing]]
* SNI 代理
比如, 模块 [[http://nginx.org/en/docs/stream/ngx_stream_ssl_preread_module.html][ngx_stream_ssl_preread]] 在 preread 阶段, 从 ClientHello 消息中提取信息
stream {
server {
listen 443;
ssl_preread on;
proxy_pass $ssl_preread_server_name:$server_port;
}
}
* TCP 负载均衡
.image slardar/nginx.png _ 600
stream {
upstream mysql {
#hash $remote_addr consistent;
server 127.0.0.1:3306;
server 127.0.0.1:3307;
}
server {
listen 3151;
proxy_pass mysql;
}
}
* TCP 负载均衡
测试:
$ echo "show variables where variable_name = 'hostname'" \
| mysql --skip-column-names -h 127.0.0.1 -P 3151 -uroot -proot
hostname e3ac73dd497d <mysql3306 CONTAINER ID>
轮询:
$ !!;!!
hostname 396662e2585d <mysql3307 CONTAINER ID>
hostname e3ac73dd497d <mysql3306 CONTAINER ID>
hash:
$ !!;!!
hostname 396662e2585d <mysql3307 CONTAINER ID>
hostname 396662e2585d <mysql3307 CONTAINER ID>
* stream-lua-nginx
.image slardar/stream01.png _ 800
.image slardar/stream.png _ 800
* Hello, Lua!
和 http 类似
stream {
server {
listen 3351;
content_by_lua_block {
ngx.say("Hello, Lua!")
}
}
}
测试:
$ nc 127.0.0.1 3351
Hello, Lua!
* TCP 负载均衡
用 Lua 来管理 upstream
stream {
upstream backend {
server 0.0.0.1:4321; # an invalid address
balancer_by_lua_file balance.lua;
}
server {
listen 3131;
proxy_pass backend;
}
}
通过 balancer_by_lua* 和 ngx.balancer 来完成动态选择和重试 upstream
* lua-resty-checkups
[[https://github.com/upyun/lua-resty-checkups][https://github.com/upyun/lua-resty-checkups]]
实现动态 upstream 管理, 之前适用于 http 子系统, 现在也同样适用于 stream 子系统
.image slardar/checkups.png _ 600
* 动态选择 upstream
在 balancer_by_lua* 中, 通过 tcp 的端口来选择相关可用的 upstream
skey = ngx.var.server_port
local peer, err = checkups.select_peer(skey)
if not peer then
ngx.log(ngx.ERR, "select peer failed, ", err)
return
end
local ok, err = balancer.set_current_peer(peer.host, peer.port)
- 其中,peer.host 不支持域名
在测试服务中, `set_current_peer` 不能直接使用 { host = "mysql3306", port = 3306 }, 需要自己完成解析的操作
* checkups 配置
_M.["3131"] = {
-- 主动健康检查
enable = true,
typ = "mysql", -- connect mysql
-- mysql 信息
"user":"runner",
"pass":"runner123456",
"name":"upyun",
-- mysql 地址
cluster = {
{
servers = {
-- mysql3306 被动健康检查
{ host = "127.0.0.1", port = 3306,"fail_timeout":10, "max_fails":3 },
-- mysql3307 同上
{ host = "127.0.0.1", port = 3307 },
-- invalid
{ host = "127.0.0.1", port = 3308 },
}
}
}
}
* 管理 upstream
通过 checkups 的接口, 实现了一个自定义的 tcp 协议, 完成 upstream 的增删改查
.image slardar/protocol.png _ 500
* 管理 upstream
$ echo -ne 'PUT upstream 3131\n\x00\0x00\0x00\0x3d{"cluster":[{"servers":[{"host":"127.0.0.1",
"port":3306}]}]}' | nc 127.0.0.1 1895 | xxd
00000000: 0000 0006 0000 0000 4f4b ........OK
$ echo -ne 'get upstream info\n' | nc 127.0.0.1 1895
00000000: 0000 02e5 0000 0001 ...
info 信息:
"3131": {
"cluster":[
{ "servers":[{
"host":"127.0.0.1",
"port":3306,
"weight":1,
"fail_timeout":10,
"max_fails":3}]
}
]
}
* 健康检查
$ echo -ne 'get upstream status\n' | nc 127.0.0.1 1895 | xxd
00000000: 0000 025a 0000 0001 ...
status 信息:
{
-- checkups heartbeat timer is alive.
"checkup_timer_alive": true,
-- last heartbeat time
"last_check_time": "2017-12-20 15:40:58",
-- status for 3131 cluster
"cls:3131": [
[{
"server": "3131:127.0.0.1:3306",
"msg":null,
"status":"ok",
"lastmodified":"2017-12-20 15:53:21",
"fail_num":0
}]
]
}
* 存储 upstream
通过 checkups, 我们可以
- 选择一个工作良好的 upstream
- 对 upstream 进行增删改查
我们还需要: 一个外部数据源来载入 upstream
.image slardar/store.png _ 1000
* lua-resty-store
.image slardar/luarestystore.png _ 900
- *api*: consul 和 etcd 的 api kv 接口
- *config*: 从 consul 或 etcd 动态加载配置
- *load*: 从 consul 或 etcd 动态加载 lua 源码
* upstream in consul
.image slardar/upstreamstore.png _ 900
* code in consul
.image slardar/code.png _ 1000
* lua-resty-load
[[https://github.com/huangnauh/lua-resty-load][https://github.com/huangnauh/lua-resty-load]]
从外部数据源动态加载 lua 源码, 无需 reload nginx
.image slardar/load.png _ 700
* 操作 lua 脚本:
与操作 upstream 采用同样的 tcp 协议
$ echo -n 'ngx.exit(1)' | wc -c | xargs printf "0x%0.2x"
0x0b
$ echo -ne 'LOAD code script.preread3151\n\0x00\0x00\0x00\0x0bngx.exit(1)' |
nc 127.0.0.1 1895 | xxd
00000000: 0000 0006 0000 0000 4f4b ........OK
测试:
$ mysql -h 127.0.0.1 -P 3151 -uroot -proot
ERROR 2013 (HY000): Lost connection to MySQL server
获取脚本信息:
$ echo -ne 'GET code info\n' | nc 127.0.0.1 1895
{
"modules":[{
"time":"2017-12-20 13:54:58",
"version":"50e9bb007a4a0b3dbd22712f5453a5f1",
"name":"script.preread3151"}]
}
* 应用举例
流量控制, 以漏桶算法(Leaky Bucket) [[https://github.com/openresty/lua-resty-limit-traffic/blob/9ac7c27212474ceb20213aea4bbf6c673a009d80/lib/resty/limit/req.md][resty.limit.req]] 为例:
.code -edit slardar/limit.lua /START OMIT/,/END OMIT/
现在还不支持 access_by_lua, 在 preread 阶段完成限制功能
加载前:
.play slardar/mysql_3251.go /fmt.Printf/
加载后:
.play slardar/mysql_3151.go /fmt.Printf/
* Slardar
[[https://github.com/upyun/slardar][https://github.com/upyun/slardar]]
.image slardar/upyunslardar.png _ 700
* MySQL Proxy
* mysql packet
.image slardar/mysql-proxy.png _ 800
.caption Image credit: [[https://dev.mysql.com/doc/dev/mysql-server/8.0.0/page_protocol_basic_packets.html/][mysql packets]]
- fixed length integer
- length encoded integer 根据第一个 byte 转换 integer
- null terminated string
- length encoded string 根据开始的 integer 决定 string 长度 (客户端认证数据)
lua-resty-mysql [[https://github.com/openresty/lua-resty-mysql/pull/69][pr69]] 在获取字符串的时候没有把 null terminated string 的 null 去除掉
* MySQL 通讯协议
.image slardar/mysql3.png 500 _
* MySQL 握手协议
.image slardar/mysql-handshake02.png _ 750
* 命令消息
- COM_QUERY 包括 select,update,insert,delete 等
- COM_QUIT 客户端退出
.image slardar/mysql-command.png _ 800
lua-resty-mysql [[https://github.com/openresty/lua-resty-mysql/pull/70][pr70]] 不能处理 field_count 大于 250 的情况
* MySQL Proxy
.image slardar/proxymysql.png _ 700
* lua-resty-mysql-toolset
[[https://github.com/huangnauh/lua-resty-mysql-toolset][https://github.com/huangnauh/lua-resty-mysql-toolset]]
基于以上介绍的基本协议, 在 lua-resty-mysql 的基础上加入了 server 的协议部分, 包括一个测试用的 proxy
stream {
server {
listen 3141;
preread_by_lua_file app/src/stream/preread.lua;
content_by_lua_file app/src/stream/content.lua;
log_by_lua_file app/src/log.lua;
}
}
* lua in consul
.image slardar/content3141.png _ 1000
测试:
$echo "show variables where variable_name = 'hostname'" |
pipe> mysql --skip-column-names -h 127.0.0.1 -P 3141 -urunner -prunner123456 upyun
hostname huangnauh.local
* 读写分离
1. 通过不同端口来区分读写 upstream, 由应用程序来区分读写
stream {
upstream backend {
server 0.0.0.1:4321; # an invalid address
balancer_by_lua_file balance.lua;
}
server {
listen 3132;
proxy_pass backend;
}
server {
listen 3133;
proxy_pass backend;
}
}
* 读写分离
2. 分析 COM_QUERY sql 语句
local cmd = string.match(sql, "([^%s,/]+)")
if not cmd then
return nil, "sql error"
end
cmd = string.lower(cmd)
-- 简单 DML 语句区分读写, 不考虑带注释的情况
if cmd == "select" or cmd == "show" then
...
else
...
end
* 性能比较
sysbench --time=10 --threads=100
MySQL Proxy:
SQL statistics:
queries performed:
read: 48253
other: 96506
total: 144759
queries: 144759 (14396.37 per sec.)
MySQL:
SQL statistics:
queries performed:
read: 65328
other: 130656
total: 195984
queries: 195984 (19525.11 per sec.)