-
Notifications
You must be signed in to change notification settings - Fork 1
/
proxy.html
1548 lines (1502 loc) · 120 KB
/
proxy.html
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<!--[if lt IE 7]> <html class="lt-ie9 lt-ie8 lt-ie7 documentation"> <![endif]-->
<!--[if IE 7]> <html class="lt-ie9 lt-ie8 documentation"> <![endif]-->
<!--[if IE 8]> <html class="lt-ie9 documentation"> <![endif]-->
<!--[if gt IE 8]><!--> <html lang="en" class="documentation"> <!--<![endif]-->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>User Manual — Vulcand documentation 2.0 documentation</title>
<link rel="stylesheet" href="_static/main.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '2.0',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="top" title="Vulcand documentation 2.0 documentation" href="index.html" />
<link rel="next" title="Middlewares" href="middlewares.html" />
<link rel="prev" title="Quick Start" href="quickstart.html" />
<link media="only screen and (max-device-width: 480px)" href="_static/mobile.css" type="text/css" rel="stylesheet" />
<link rel="icon" type="image/png" href="_static/img/favicon.png" />
<!-- <link rel="stylesheet" href="_static/docs.css" /> -->
<link rel="stylesheet" href="http://www.mailgun.com/static/css/main.css" />
<meta name="viewport" content="width=device-width">
</head>
<body role="document">
<header style="content:none">
<div class="container">
<a href="http://github.com/mailgun/vulcand/" style="float:left;margin-top:20px;margin-bottom:20px;background:url(_static/img/vulcan.png) no-repeat">
<img src="_static/img/vulcan.png">
</img>
</a>
<nav class="header-nav" id="js-header-nav">
<a href="//www.vulcanproxy.com" class="active"><b>Documentation</b></a>
</nav>
<button id="js-show-nav"><i class="icon-reorder"></i></button>
</div>
</header>
<div id="body">
<section class="subnav">
<div class="container">
<a target="blank" href="https://github.com/mailgun/vulcand-docs" class="edit-docs-link" onclick="mixpanel.track('Clicked Edit Docs Link');">Edit</a>
<ul id="headerButtons" class="nav nav-pills">
<li class="lang-label">
Samples:
</li>
<li class="lang">
<a href="" id="lang_etcd">Etcd</a>
</li>
<li class="lang">
<a href="" id="lang_cli">Vulcanctl</a>
</li>
<li class="lang">
<a href="" id="lang_api">API</a>
</li>
</ul>
</div>
</section>
<section class="main">
<div class="container">
<aside id="sphinxsidebar" class="docs-sidebar">
<div class="sphinxsidebarwrapper">
<ul><li class="toctree-l1"><a href="index.html">Main Page</a></li></ul>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="quickstart.html">Quick Start</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="">User Manual</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#glossary">Glossary</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#host">Host</a></li>
<li class="toctree-l3"><a class="reference internal" href="#listener">Listener</a></li>
<li class="toctree-l3"><a class="reference internal" href="#frontend">Frontend</a></li>
<li class="toctree-l3"><a class="reference internal" href="#backend">Backend</a></li>
<li class="toctree-l3"><a class="reference internal" href="#server">Server</a></li>
<li class="toctree-l3"><a class="reference internal" href="#middleware">Middleware</a></li>
<li class="toctree-l3"><a class="reference internal" href="#circuit-breaker">Circuit Breaker</a></li>
<li class="toctree-l3"><a class="reference internal" href="#secret-storage">Secret storage</a></li>
<li class="toctree-l3"><a class="reference internal" href="#failover-predicates">Failover Predicates</a></li>
<li class="toctree-l3"><a class="reference internal" href="#route">Route</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#configuration">Configuration</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#backends-and-servers">Backends and servers</a></li>
<li class="toctree-l3"><a class="reference internal" href="#frontends">Frontends</a></li>
<li class="toctree-l3"><a class="reference internal" href="#hosts">Hosts</a></li>
<li class="toctree-l3"><a class="reference internal" href="#routing-language">Routing Language</a></li>
<li class="toctree-l3"><a class="reference internal" href="#listeners">Listeners</a></li>
<li class="toctree-l3"><a class="reference internal" href="#middlewares">Middlewares</a></li>
<li class="toctree-l3"><a class="reference internal" href="#rate-limits">Rate Limits</a></li>
<li class="toctree-l3"><a class="reference internal" href="#connection-limits">Connection Limits</a></li>
<li class="toctree-l3"><a class="reference internal" href="#rewrites-and-redirects">Rewrites and redirects</a></li>
<li class="toctree-l3"><a class="reference internal" href="#structured-logs">Structured logs</a></li>
<li class="toctree-l3"><a class="reference internal" href="#circuit-breakers">Circuit Breakers</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#tls">TLS</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#managing-certificates">Managing certificates</a></li>
<li class="toctree-l3"><a class="reference internal" href="#ocsp">OCSP</a></li>
<li class="toctree-l3"><a class="reference internal" href="#id2">SNI</a></li>
<li class="toctree-l3"><a class="reference internal" href="#session-tickets">Session Tickets</a></li>
<li class="toctree-l3"><a class="reference internal" href="#cipher-suites">Cipher Suites</a></li>
<li class="toctree-l3"><a class="reference internal" href="#tls-options">TLS options</a></li>
<li class="toctree-l3"><a class="reference internal" href="#https-listeners">HTTPS listeners</a></li>
<li class="toctree-l3"><a class="reference internal" href="#https-backends">HTTPS Backends</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#metrics">Metrics</a></li>
<li class="toctree-l2"><a class="reference internal" href="#logging">Logging</a></li>
<li class="toctree-l2"><a class="reference internal" href="#process-management">Process management</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#startup-and-configuration">Startup and configuration</a></li>
<li class="toctree-l3"><a class="reference internal" href="#binary-upgrades">Binary upgrades</a></li>
<li class="toctree-l3"><a class="reference internal" href="#log-control">Log control</a></li>
<li class="toctree-l3"><a class="reference internal" href="#id4">Metrics</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#installation">Installation</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#docker-builds">Docker builds</a></li>
<li class="toctree-l3"><a class="reference internal" href="#docker-trusted-build">Docker trusted build</a></li>
<li class="toctree-l3"><a class="reference internal" href="#manual-installation">Manual installation</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="middlewares.html">Middlewares</a></li>
<li class="toctree-l1"><a class="reference internal" href="api.html">API Reference</a></li>
</ul>
</div>
</aside>
<div class="document">
<div id="st-results-container"></div>
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="user-manual">
<span id="proxy"></span><h1>User Manual<a class="headerlink" href="#user-manual" title="Permalink to this headline">¶</a></h1>
<div class="section" id="glossary">
<h2>Glossary<a class="headerlink" href="#glossary" title="Permalink to this headline">¶</a></h2>
<p>Familiarizing with the glossary would help to understand the rest of this guide.</p>
<div class="section" id="host">
<h3>Host<a class="headerlink" href="#host" title="Permalink to this headline">¶</a></h3>
<p>Hostname is defined by incoming <code class="docutils literal"><span class="pre">Host</span></code> header. E.g. <code class="docutils literal"><span class="pre">curl</span> <span class="pre">http://example.com/alice</span></code> generates the following request:</p>
<div class="highlight-sh"><div class="highlight"><pre>GET /alice HTTP/1.1
User-Agent: curl/7.35.0
Host: example.com
</pre></div>
</div>
<p>Vulcand hosts contain associated information and settings, such as SNI options and TLS certificates.</p>
</div>
<div class="section" id="listener">
<h3>Listener<a class="headerlink" href="#listener" title="Permalink to this headline">¶</a></h3>
<p>Listener is a dynamic socket that can be attached or detached to Vulcand without restart. Vulcand can have multiple http and https listeners
attached to it, providing service on multiple interfaces and protocols.</p>
</div>
<div class="section" id="frontend">
<h3>Frontend<a class="headerlink" href="#frontend" title="Permalink to this headline">¶</a></h3>
<p>Frontends match the requests and forward it to the backends.
Each frontend defines a route - a special expression that matches the request, e.g. <code class="docutils literal"><span class="pre">Path("/v1/path")</span></code>.
Frontends are linked to backend and Vulcand will use the servers from this backend to serve the request.</p>
</div>
<div class="section" id="backend">
<h3>Backend<a class="headerlink" href="#backend" title="Permalink to this headline">¶</a></h3>
<p>Backend is a collection of servers, they control connection pools to servers and transport options, such as connection, read and write timeouts.</p>
</div>
<div class="section" id="server">
<h3>Server<a class="headerlink" href="#server" title="Permalink to this headline">¶</a></h3>
<p>Server is a final destination of the incoming request, each server is defined by URL <code class="docutils literal"><span class="pre"><schema>://<host>:<port></span></code>, e.g. <code class="docutils literal"><span class="pre">http://localhost:5000</span></code>.</p>
</div>
<div class="section" id="middleware">
<h3>Middleware<a class="headerlink" href="#middleware" title="Permalink to this headline">¶</a></h3>
<p>Vulcand supports pluggable middlewares. Middlewares can intercept or transform the request to any frontend. Examples of the supported middlewares are rate limits and connection limits.
You can add or remove middlewares using command line, API or directly via backends.</p>
</div>
<div class="section" id="circuit-breaker">
<h3>Circuit Breaker<a class="headerlink" href="#circuit-breaker" title="Permalink to this headline">¶</a></h3>
<p>Circuit breakers are special type of middlewares that observe various metrics for a particular frontend and can activate failover scenario whenever the condition matches e.g. error rate exceeds the threshold.</p>
</div>
<div class="section" id="secret-storage">
<h3>Secret storage<a class="headerlink" href="#secret-storage" title="Permalink to this headline">¶</a></h3>
<p>Vulcand supports secret storage - running process acts like encryption/decryption service every time it reads and writes sensitive data, e.g. TLS certificates to the backend.
To use this feature, users generate <code class="docutils literal"><span class="pre">sealKey</span></code> using command line utility and pass this key to the process for encryption and decryption of the data in the backends.</p>
</div>
<div class="section" id="failover-predicates">
<h3>Failover Predicates<a class="headerlink" href="#failover-predicates" title="Permalink to this headline">¶</a></h3>
<p>Sometimes it is handy to retry the request on error. The good question is what constitutes an error? Sometimes it’s a read/write timeout, and somethimes it’s a special error code.
Failover predicates are expressions that define when the request can be failed over, e.g. <code class="docutils literal"><span class="pre">IsNetworkError()</span> <span class="pre">&&</span> <span class="pre">Attempts</span> <span class="pre"><=</span> <span class="pre">2</span></code></p>
<div class="highlight-bash"><div class="highlight"><pre>IsNetworkError<span class="o">()</span> <span class="c"># failover on network error</span>
Attempts<span class="o">()</span> <<span class="o">=</span> <span class="m">1</span> <span class="c"># allows only 1 failover attempt</span>
RequestMethod<span class="o">()</span> <span class="o">==</span> <span class="s2">"GET"</span> <span class="c"># failover for GET requests only</span>
ResponseCode<span class="o">()</span> <span class="o">==</span> <span class="m">408</span> <span class="c"># failover on 408 HTTP response code</span>
</pre></div>
</div>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">if you omit <cite>Attempts</cite>, failover will max out after 10 attempts.</p>
</div>
</div>
<div class="section" id="route">
<h3>Route<a class="headerlink" href="#route" title="Permalink to this headline">¶</a></h3>
<p>Route is a simple routing language for matching http requests with Go syntax: <code class="docutils literal"><span class="pre">Method("POST")</span> <span class="pre">&&</span> <span class="pre">Path("/path")</span></code>, here are a couple of examples:</p>
<div class="highlight-go"><div class="highlight"><pre><span class="nx">Host</span><span class="p">(</span><span class="s">"<user>.example.com"</span><span class="p">)</span> <span class="c1">// Match by host with trie syntax</span>
<span class="nx">HostRegexp</span><span class="p">(</span><span class="s">".*.example.com"</span><span class="p">)</span> <span class="c1">// Match by host with regexp syntax</span>
<span class="nx">Path</span><span class="p">(</span><span class="s">"/v1/users/<user>"</span><span class="p">)</span> <span class="c1">// Match by path with trie syntax</span>
<span class="nx">Method</span><span class="p">(</span><span class="s">"POST"</span><span class="p">)</span> <span class="o">&&</span> <span class="nx">Path</span><span class="p">(</span><span class="s">"/v1/users"</span><span class="p">)</span> <span class="c1">// Match by method and path with trie syntax</span>
<span class="nx">PathRegexp</span><span class="p">(</span><span class="s">"/v1/users/.*"</span><span class="p">)</span> <span class="c1">// Match by path with regexp syntax</span>
<span class="nx">MethodRegexp</span><span class="p">(</span><span class="s">"DELETE|GET"</span><span class="p">)</span> <span class="o">&&</span> <span class="nx">PathRegexp</span><span class="p">(</span><span class="s">"/v1/users/.*"</span><span class="p">)</span> <span class="c1">// Match by method and path with regexp syntax</span>
<span class="nx">Header</span><span class="p">(</span><span class="s">"Content-Type"</span><span class="p">,</span> <span class="s">"application/<subtype>"</span><span class="p">)</span> <span class="c1">// trie-based matcher for headers</span>
<span class="nx">HeaderRegexp</span><span class="p">(</span><span class="s">"Content-Type"</span><span class="p">,</span> <span class="s">"application/.*"</span><span class="p">)</span> <span class="c1">// regexp based matcher for headers</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="configuration">
<h2>Configuration<a class="headerlink" href="#configuration" title="Permalink to this headline">¶</a></h2>
<p>Vulcand can be configured via Etcd, API or command line tool - <code class="docutils literal"><span class="pre">vctl</span></code>. You can switch between different configuration examples using the samples switch.</p>
<div class="section" id="backends-and-servers">
<h3>Backends and servers<a class="headerlink" href="#backends-and-servers" title="Permalink to this headline">¶</a></h3>
<div class="figure align-left">
<img alt="_images/VulcanUpstream.png" src="_images/VulcanUpstream.png" />
</div>
<p>Backend is a collection of servers. Vulcand load-balances requests within the backend and keeps the connection pool to every server.
Frontends using the same backend will share the connections.</p>
<p>Adding and removing servers to the backend will change the traffic in real-time, removing the backend will lead to graceful drain off of the connections.</p>
<div class="highlight-etcd"><div class="highlight"><pre><span class="c"># Upsert backend and add a server to it</span>
etcdctl <span class="nb">set</span> /vulcand/backends/b1/backend <span class="s1">'{"Type": "http"}'</span>
etcdctl <span class="nb">set</span> /vulcand/backends/b1/servers/srv1 <span class="s1">'{"URL": "http://localhost:5000"}'</span>
</pre></div>
</div>
<div class="highlight-cli"><div class="highlight"><pre><span class="c"># Upsert backend and add a server to it</span>
vctl backend upsert -id b1
vctl server upsert -b b1 -id srv1 -url http://localhost:5000
</pre></div>
</div>
<div class="highlight-api"><div class="highlight"><pre><span class="c"># Upsert backend and add a server to it</span>
curl -X POST -H <span class="s2">"Content-Type: application/json"</span> http://localhost:8182/v2/backends<span class="se">\</span>
-d <span class="s1">'{"Backend": {"Id":"b1", "Type":"http"}}'</span>
curl -X POST -H <span class="s2">"Content-Type: application/json"</span> http://localhost:8182/v2/backends/b1/servers<span class="se">\</span>
-d <span class="s1">'{"Server": {"Id":"srv1", "URL":"http://localhost:5000"}}'</span>
</pre></div>
</div>
<p><strong>Backend settings</strong></p>
<p>Backends define the configuration options to the servers, such as the amount of idle connections and timeouts.
Backend options are represented as JSON dictionary.</p>
<div class="highlight-javascript"><div class="highlight"><pre><span class="p">{</span>
<span class="s2">"Timeouts"</span><span class="o">:</span> <span class="p">{</span>
<span class="s2">"Read"</span><span class="o">:</span> <span class="s2">"1s"</span><span class="p">,</span> <span class="c1">// Socket read timeout (before we receive the first reply header)</span>
<span class="s2">"Dial"</span><span class="o">:</span> <span class="s2">"2s"</span><span class="p">,</span> <span class="c1">// Socket connect timeout</span>
<span class="s2">"TLSHandshake"</span><span class="o">:</span> <span class="s2">"3s"</span><span class="p">,</span> <span class="c1">// TLS handshake timeout</span>
<span class="p">},</span>
<span class="s2">"KeepAlive"</span><span class="o">:</span> <span class="p">{</span>
<span class="s2">"Period"</span><span class="o">:</span> <span class="s2">"4s"</span><span class="p">,</span> <span class="c1">// Keepalive period for idle connections</span>
<span class="s2">"MaxIdleConnsPerHost"</span><span class="o">:</span> <span class="mi">3</span><span class="p">,</span> <span class="c1">// How many idle connections will be kept per host</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>You can update the settings at any time, that will initiate graceful reload of the underlying settings in Vulcand.</p>
<div class="highlight-etcd"><div class="highlight"><pre>etcdctl <span class="nb">set</span> /vulcand/backends/b1/backend <span class="s1">'{"Type": "http", "Settings": {"KeepAlive": {"MaxIdleConnsPerHost": 128, "Period": "4s"}}}'</span>
</pre></div>
</div>
<div class="highlight-cli"><div class="highlight"><pre>vctl backend upsert -id b1 <span class="se">\</span>
-readTimeout<span class="o">=</span>1s -dialTimeout<span class="o">=</span>2s -handshakeTimeout<span class="o">=</span>3s<span class="se">\</span>
-keepAlivePeriod<span class="o">=</span>4s -maxIdleConns<span class="o">=</span>128
</pre></div>
</div>
<div class="highlight-api"><div class="highlight"><pre>curl -X POST -H <span class="s2">"Content-Type: application/json"</span> http://localhost:8182/v2/backends<span class="se">\</span>
-d <span class="s1">'{"Backend": {"Id":"b1", "Type":"http", "Settings": {"KeepAlive": {"MaxIdleConnsPerHost": 128, "Period": "4s"}}}}'</span>
</pre></div>
</div>
<p><strong>Server heartbeat</strong></p>
<p>Heartbeat allows to automatically de-register the server when it crashes or wishes to be de-registered.
Server can heartbeat it’s presense, and once the heartbeat is stopped, Vulcand will gracefully remove the server from the rotation.</p>
<div class="highlight-etcd"><div class="highlight"><pre><span class="c"># Upsert a server with TTL 5 seconds</span>
etcdctl <span class="nb">set</span> --ttl <span class="m">5</span> /vulcand/backends/b1/servers/srv2 <span class="s1">'{"URL": "http://localhost:5001"}'</span>
</pre></div>
</div>
<div class="highlight-cli"><div class="highlight"><pre><span class="c"># Upsert a server with TTL 5 seconds</span>
vctl server upsert -b b1 -id srv2 -ttl 5s -url http://localhost:5002
</pre></div>
</div>
<div class="highlight-api"><div class="highlight"><pre><span class="c"># Upsert a server with TTL 5 seconds</span>
curl -X POST -H <span class="s2">"Content-Type: application/json"</span> http://localhost:8182/v2/backends/b1/servers<span class="se">\</span>
-d <span class="s1">'{"Server": {"Id":"srv2", "URL":"http://localhost:5001"}, "TTL": "5s"}'</span>
</pre></div>
</div>
</div>
<div class="section" id="frontends">
<h3>Frontends<a class="headerlink" href="#frontends" title="Permalink to this headline">¶</a></h3>
<div class="figure align-left">
<img alt="_images/VulcanFrontend.png" src="_images/VulcanFrontend.png" />
</div>
<p>If request matches a frontend route it is redirected to one of the servers of the associated backend.
It is recommended to specify a frontend per API method, e.g. <code class="docutils literal"><span class="pre">Host("api.example.com")</span> <span class="pre">&&</span> <span class="pre">Method("POST")</span> <span class="pre">&&</span> <span class="pre">Path("/v1/users")</span></code>.</p>
<p>Route can be any valid route expression, e.g. <code class="docutils literal"><span class="pre">Path("/v1/users")</span></code> will match for all hosts and
<code class="docutils literal"><span class="pre">Host("api.example.com")</span> <span class="pre">&&</span> <span class="pre">Path("/v1/users")</span></code> will match only for <code class="docutils literal"><span class="pre">api.example.com</span></code>.</p>
<div class="highlight-etcd"><div class="highlight"><pre><span class="c"># upsert frontend connected to backend b1 and matching path "/"</span>
etcdctl <span class="nb">set</span> /vulcand/frontends/f1/frontend <span class="s1">'{"Type": "http", "BackendId": "b1", "Route": "Path(`/`)"}'</span>
</pre></div>
</div>
<div class="highlight-cli"><div class="highlight"><pre><span class="c"># upsert frontend connected to backend b1 and matching path "/"</span>
vctl frontend upsert -id f1 -b b1 -route <span class="s1">'Path("/")'</span>
</pre></div>
</div>
<div class="highlight-api"><div class="highlight"><pre><span class="c"># upsert frontend connected to backend b1 and matching path "/"</span>
curl -X POST -H <span class="s2">"Content-Type: application/json"</span> http://localhost:8182/v2/frontends<span class="se">\</span>
-d <span class="s1">'{"Frontend": {"Id":"f1", "Type": "http", "BackendId": "b1", "Route": "Path(\"/\")"}}'</span>
</pre></div>
</div>
<p><strong>Frontend settings</strong></p>
<p>Frontends control various limits, forwarding and failover settings.</p>
<div class="highlight-javascript"><div class="highlight"><pre><span class="p">{</span>
<span class="s2">"Limits"</span><span class="o">:</span> <span class="p">{</span>
<span class="s2">"MaxMemBodyBytes"</span><span class="o">:</span> <span class="mi">12</span><span class="p">,</span> <span class="c1">// Maximum request body size to keep in memory before buffering to disk</span>
<span class="s2">"MaxBodyBytes"</span><span class="o">:</span> <span class="mi">400</span><span class="p">,</span> <span class="c1">// Maximum request body size to allow for this frontend</span>
<span class="p">},</span>
<span class="s2">"FailoverPredicate"</span><span class="o">:</span> <span class="s2">"IsNetworkError() && Attempts() <= 1"</span><span class="p">,</span> <span class="c1">// Predicate that defines when requests are allowed to failover</span>
<span class="s2">"Hostname"</span><span class="o">:</span> <span class="s2">"host1"</span><span class="p">,</span> <span class="c1">// Host to set in forwarding headers</span>
<span class="s2">"TrustForwardHeader"</span><span class="o">:</span> <span class="kc">true</span><span class="p">,</span> <span class="c1">// Time provider (useful for testing purposes)</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Setting frontend settings upates the limits and parameters for the newly arriving requests in real-time.</p>
<div class="highlight-etcd"><div class="highlight"><pre>etcdctl <span class="nb">set</span> /vulcand/frontends/f1/frontend <span class="s1">'{"Id": "f1", "Type": "http", "BackendId": "b1", "Route": "Path(`/`)", "Settings": {"FailoverPredicate":"(IsNetworkError() || ResponseCode() == 503) && Attempts() <= 2"}}'</span>
</pre></div>
</div>
<div class="highlight-cli"><div class="highlight"><pre>vctl frontend upsert<span class="se">\</span>
-id<span class="o">=</span>f1<span class="se">\</span>
-route<span class="o">=</span><span class="s1">'Path("/")'</span><span class="se">\</span>
-b<span class="o">=</span>b1<span class="se">\</span>
-maxMemBodyKB<span class="o">=</span><span class="m">6</span> -maxBodyKB<span class="o">=</span>7<span class="se">\</span>
-failoverPredicate<span class="o">=</span><span class="s1">'IsNetworkError()'</span><span class="se">\</span>
-trustForwardHeader<span class="se">\</span>
-forwardHost<span class="o">=</span>host1
</pre></div>
</div>
<div class="highlight-api"><div class="highlight"><pre>curl -X POST -H <span class="s2">"Content-Type: application/json"</span> http://localhost:8182/v2/frontends<span class="se">\</span>
-d <span class="s1">'{"Frontend": {"Id": "f1", "Type": "http", "BackendId": "b1", "Route": "Path(`/`)", "Settings": {"FailoverPredicate":"(IsNetworkError() || ResponseCode() == 503) && Attempts() <= 2"}}}'</span>
</pre></div>
</div>
<p><strong>Switching backends</strong></p>
<p>Updating frontend’s backend property gracefully re-routes the traffic to the new servers assigned to this backend:</p>
<div class="highlight-etcd"><div class="highlight"><pre><span class="c"># redirect the traffic of the frontend "loc1" to the servers of the backend "b2"</span>
etcdctl <span class="nb">set</span> /vulcand/frontends/f1/frontend <span class="s1">'{"Type": "http", "BackendId": "b2", "Route": "Path(`/`)"}'</span>
</pre></div>
</div>
<div class="highlight-cli"><div class="highlight"><pre><span class="c"># redirect the traffic of the frontend "f1" to the servers of the backend "b2"</span>
vctl frontend upsert -id<span class="o">=</span>f1 -route<span class="o">=</span><span class="s1">'Path("/")'</span> -b<span class="o">=</span>b2
</pre></div>
</div>
<div class="highlight-api"><div class="highlight"><pre><span class="c"># redirect the traffic of the frontend "loc1" to the servers of the backend "up2"</span>
curl -X POST -H <span class="s2">"Content-Type: application/json"</span> http://localhost:8182/v2/frontends -d <span class="s1">'{"Frontend": {"Id": "f1", "Type": "http", "BackendId": "b2", "Route": "Path(`/`)"}}'</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">you can add and remove servers to the existing backend, and Vulcand will start redirecting the traffic to them automatically</p>
</div>
</div>
<div class="section" id="hosts">
<h3>Hosts<a class="headerlink" href="#hosts" title="Permalink to this headline">¶</a></h3>
<p>One can use Host entries to specify host-related settings, such as TLS certificates and SNI options.</p>
<p><strong>TLS Certificates</strong></p>
<p>Certificates are stored as encrypted JSON dictionaries. Updating a certificate will gracefully reload it for all running HTTP servers.</p>
<div class="highlight-etcd"><div class="highlight"><pre><span class="c"># Set keypair</span>
etcdctl <span class="nb">set</span> /vulcand/hosts/localhost/host <span class="s1">'{"Settings": {"KeyPair": {...}}}'</span>
</pre></div>
</div>
<div class="highlight-cli"><div class="highlight"><pre>vctl host upsert -name <host> -cert<span class="o">=</span></path-to/chain.crt> -privateKey<span class="o">=</span></path-to/key>
</pre></div>
</div>
<div class="highlight-api"><div class="highlight"><pre>curl -X POST -H <span class="s2">"Content-Type: application/json"</span> http://localhost:8182/v2/hosts<span class="se">\</span>
-d <span class="s1">'{"Host": { "Name": "localhost", "Settings": {"KeyPair": {"Cert": "base64", Key: "base64"}}}}'</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">When setting keypair via Etcd you need to encrypt keypair. This is explained in <a class="reference internal" href="#tls">TLS</a> section of this document.</p>
</div>
<p><strong>OCSP</strong></p>
<p><a class="reference external" href="http://en.wikipedia.org/wiki/Online_Certificate_Status_Protocol">Online Certificate Status Protocol</a> is a protocol for certificate revocation checking. Vulcand checks OCSP status in the background and
includes the OCSP staple response in the TLS handshake when this feature turned on.</p>
<p>Read more about turning OCSP for hosts in <a class="reference internal" href="#ocsp">OCSP</a> section of this document.</p>
</div>
<div class="section" id="routing-language">
<h3>Routing Language<a class="headerlink" href="#routing-language" title="Permalink to this headline">¶</a></h3>
<p>Vulcand uses a special type of a routing language to match requests - called <code class="docutils literal"><span class="pre">route</span></code> and implemented as a <a class="reference external" href="https://github.com/mailgun/route">standalone library</a>
It uses Go syntax to route http requests by by hostname, method, path and headers. Every Vulcand frontend has a special <code class="docutils literal"><span class="pre">Route</span></code> field for routing requests.</p>
<p>Here is the syntax explained:</p>
<div class="highlight-go"><div class="highlight"><pre><span class="nx">Matcher</span><span class="p">(</span><span class="s">"value"</span><span class="p">)</span> <span class="c1">// matches value using trie</span>
<span class="nx">Matcher</span><span class="p">(</span><span class="s">"<string>.value"</span><span class="p">)</span> <span class="c1">// uses trie-based matching for a.value and b.value</span>
<span class="nx">MatcherRegexp</span><span class="p">(</span><span class="s">".*value"</span><span class="p">)</span> <span class="c1">// uses regexp-based matching</span>
</pre></div>
</div>
<p>Host matcher:</p>
<div class="highlight-go"><div class="highlight"><pre><span class="nx">Host</span><span class="p">(</span><span class="s">"<subdomain>.localhost"</span><span class="p">)</span> <span class="c1">// trie-based matcher for a.localhost, b.localhost, etc.</span>
<span class="nx">HostRegexp</span><span class="p">(</span><span class="s">".*localhost"</span><span class="p">)</span> <span class="c1">// regexp based matcher</span>
</pre></div>
</div>
<p>Path matcher:</p>
<div class="highlight-go"><div class="highlight"><pre><span class="nx">Path</span><span class="p">(</span><span class="s">"/hello/<value>"</span><span class="p">)</span> <span class="c1">// trie-based matcher for raw request path</span>
<span class="nx">PathRegexp</span><span class="p">(</span><span class="s">"/hello/.*"</span><span class="p">)</span> <span class="c1">// regexp-based matcher for raw request path</span>
</pre></div>
</div>
<p>Method matcher:</p>
<div class="highlight-go"><div class="highlight"><pre><span class="nx">Method</span><span class="p">(</span><span class="s">"GET"</span><span class="p">)</span> <span class="c1">// trie-based matcher for request method</span>
<span class="nx">MethodRegexp</span><span class="p">(</span><span class="s">"POST|PUT"</span><span class="p">)</span> <span class="c1">// regexp based matcher for request method</span>
</pre></div>
</div>
<p>Header matcher:</p>
<div class="highlight-go"><div class="highlight"><pre><span class="nx">Header</span><span class="p">(</span><span class="s">"Content-Type"</span><span class="p">,</span> <span class="s">"application/<subtype>"</span><span class="p">)</span> <span class="c1">// trie-based matcher for headers</span>
<span class="nx">HeaderRegexp</span><span class="p">(</span><span class="s">"Content-Type"</span><span class="p">,</span> <span class="s">"application/.*"</span><span class="p">)</span> <span class="c1">// regexp based matcher for headers</span>
</pre></div>
</div>
<p>Matchers can be combined using <code class="docutils literal"><span class="pre">&&</span></code> operator:</p>
<div class="highlight-go"><div class="highlight"><pre><span class="nx">Host</span><span class="p">(</span><span class="s">"localhost"</span><span class="p">)</span> <span class="o">&&</span> <span class="nx">Method</span><span class="p">(</span><span class="s">"POST"</span><span class="p">)</span> <span class="o">&&</span> <span class="nx">Path</span><span class="p">(</span><span class="s">"/v1"</span><span class="p">)</span>
</pre></div>
</div>
<p>Vulcan will join the trie-based matchers into one trie matcher when possible, for example:</p>
<div class="highlight-go"><div class="highlight"><pre><span class="nx">Host</span><span class="p">(</span><span class="s">"localhost"</span><span class="p">)</span> <span class="o">&&</span> <span class="nx">Method</span><span class="p">(</span><span class="s">"POST"</span><span class="p">)</span> <span class="o">&&</span> <span class="nx">Path</span><span class="p">(</span><span class="s">"/v1"</span><span class="p">)</span>
<span class="nx">Host</span><span class="p">(</span><span class="s">"localhost"</span><span class="p">)</span> <span class="o">&&</span> <span class="nx">Method</span><span class="p">(</span><span class="s">"GET"</span><span class="p">)</span> <span class="o">&&</span> <span class="nx">Path</span><span class="p">(</span><span class="s">"/v2"</span><span class="p">)</span>
</pre></div>
</div>
<p>Will be combined into one trie for performance. If you add a third route:</p>
<div class="highlight-go"><div class="highlight"><pre><span class="nx">Host</span><span class="p">(</span><span class="s">"localhost"</span><span class="p">)</span> <span class="o">&&</span> <span class="nx">Method</span><span class="p">(</span><span class="s">"GET"</span><span class="p">)</span> <span class="o">&&</span> <span class="nx">PathRegexp</span><span class="p">(</span><span class="s">"/v2/.*"</span><span class="p">)</span>
</pre></div>
</div>
<p>It wont be joined ito the trie, and would be matched separately instead.</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">Vulcan can not merge regexp-based routes into efficient structure, so if you have hundreds/thousands of frontends, use trie-based routes!</p>
</div>
<div class="section" id="host-based-routing">
<h4>Host based routing<a class="headerlink" href="#host-based-routing" title="Permalink to this headline">¶</a></h4>
<p>Vulcand does not require host-specific routing, e.g. the frontend with the following route will match all requests regardless of their hostname:</p>
<div class="highlight-go"><div class="highlight"><pre><span class="nx">PathRegexp</span><span class="p">(</span><span class="s">"/.*"</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-bash"><div class="highlight"><pre>curl -H <span class="s2">"Host:example.com"</span> http://localhost/hello <span class="c"># works</span>
curl -H <span class="s2">"Host:hello.com"</span> http://localhost/hello <span class="c"># also works</span>
</pre></div>
</div>
<p>In case if you need Host-based routing (just as Apache’s <code class="docutils literal"><span class="pre">VHost</span></code> or Nginx’s <code class="docutils literal"><span class="pre">Server</span></code> names), you can use the routes:</p>
<div class="highlight-go"><div class="highlight"><pre><span class="nx">Host</span><span class="p">(</span><span class="s">"example.com"</span><span class="p">)</span> <span class="o">&&</span> <span class="nx">PathRegexp</span><span class="p">(</span><span class="s">"/.*"</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-bash"><div class="highlight"><pre>curl -H <span class="s2">"Host:example.com"</span> http://localhost/hello <span class="c"># works</span>
curl -H <span class="s2">"Host:hello.com"</span> http://localhost/hello <span class="c"># not found</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The example above do not set up host entries in Vulcand. You only need them when using HTTPS to supply certificates.</p>
</div>
</div>
<div class="section" id="method-matching">
<h4>Method matching<a class="headerlink" href="#method-matching" title="Permalink to this headline">¶</a></h4>
<p>Vulcand works better when creating a separate frontend for each HTTP method in your API:</p>
<div class="highlight-go"><div class="highlight"><pre><span class="nx">Host</span><span class="p">(</span><span class="s">"localhost"</span><span class="p">)</span> <span class="o">&&</span> <span class="nx">Method</span><span class="p">(</span><span class="s">"POST"</span><span class="p">)</span> <span class="o">&&</span> <span class="nx">Path</span><span class="p">(</span><span class="s">"/users"</span><span class="p">)</span>
<span class="nx">Host</span><span class="p">(</span><span class="s">"localhost"</span><span class="p">)</span> <span class="o">&&</span> <span class="nx">Method</span><span class="p">(</span><span class="s">"GET"</span><span class="p">)</span> <span class="o">&&</span> <span class="nx">Path</span><span class="p">(</span><span class="s">"/users"</span><span class="p">)</span>
</pre></div>
</div>
<p>In this case each frontend collects separate set of realtime metrics that are different for creating and gettings users. This separation will provide separate histograms and separate load balancing logic for different request types what helps to understand the performance better.</p>
</div>
</div>
<div class="section" id="listeners">
<h3>Listeners<a class="headerlink" href="#listeners" title="Permalink to this headline">¶</a></h3>
<div class="figure align-left">
<img alt="_images/VulcanListener.png" src="_images/VulcanListener.png" />
</div>
<p>Listeners allow attaching and detaching sockets on various interfaces and networks.
Vulcand can have multiple listeners attached and share the same listener.</p>
<div class="highlight-javascript"><div class="highlight"><pre><span class="p">{</span>
<span class="s2">"Protocol"</span><span class="o">:</span><span class="s2">"http"</span><span class="p">,</span> <span class="c1">// 'http' or 'https'</span>
<span class="s2">"Scope"</span><span class="o">:</span> <span class="s2">""</span><span class="p">,</span> <span class="c1">// optional scope field, read below for details</span>
<span class="s2">"Address"</span><span class="o">:</span><span class="p">{</span>
<span class="s2">"Network"</span><span class="o">:</span><span class="s2">"tcp"</span><span class="p">,</span> <span class="c1">// 'tcp' or 'unix'</span>
<span class="s2">"Address"</span><span class="o">:</span><span class="s2">"localhost:8183"</span> <span class="c1">// 'host:port' or '/path/to.socket'</span>
<span class="p">},</span>
<span class="p">}</span>
</pre></div>
</div>
<div class="highlight-etcd"><div class="highlight"><pre><span class="c"># Add http listener accepting requests on 127.0.0.1:8183</span>
etcdctl <span class="nb">set</span> /vulcand/listeners/ls1<span class="se">\</span>
<span class="s1">'{"Protocol":"http", "Address":{"Network":"tcp", "Address":"127.0.0.1:8183"}}'</span>
</pre></div>
</div>
<div class="highlight-cli"><div class="highlight"><pre><span class="c"># Add http listener accepting requests on 127.0.0.1:80</span>
vctl listener upsert --id ls1 --proto<span class="o">=</span>http --net<span class="o">=</span>tcp -addr<span class="o">=</span>127.0.0.1:8080
</pre></div>
</div>
<div class="highlight-api"><div class="highlight"><pre><span class="c"># Add http listener accepting requests on 127.0.0.1:8183</span>
curl -X POST -H <span class="s2">"Content-Type: application/json"</span> http://localhost:8182/v2/listeners<span class="se">\</span>
-d <span class="s1">'{"Listener":{"Id": "ls1", "Protocol":"http", "Address":{"Network":"tcp", "Address":"127.0.0.1:8183"}}}'</span>
</pre></div>
</div>
<p><strong>Listener scopes</strong></p>
<p>Listeners support scopes as the way to limit operational scope of socket.
Scope field uses Vulcand <a class="reference internal" href="#routing-language">Routing Language</a>.
Here’s an example of Listener that only allows requests with hostname <code class="docutils literal"><span class="pre">example.com</span></code></p>
<div class="highlight-javascript"><div class="highlight"><pre><span class="p">{</span>
<span class="s2">"Protocol"</span><span class="o">:</span><span class="s2">"http"</span><span class="p">,</span> <span class="c1">// 'http' or 'https'</span>
<span class="s2">"Scope"</span><span class="o">:</span> <span class="s2">"Host(`example.com`)"</span><span class="p">,</span> <span class="c1">// operational scope</span>
<span class="s2">"Address"</span><span class="o">:</span><span class="p">{</span>
<span class="s2">"Network"</span><span class="o">:</span><span class="s2">"tcp"</span><span class="p">,</span> <span class="c1">// 'tcp' or 'unix'</span>
<span class="s2">"Address"</span><span class="o">:</span><span class="s2">"0.0.0.0:8183"</span> <span class="c1">// 'host:port' or '/path/to.socket'</span>
<span class="p">},</span>
<span class="p">}</span>
</pre></div>
</div>
<p>E.g. if we have two frontends defined:</p>
<div class="highlight-javascript"><div class="highlight"><pre><span class="nx">Host</span><span class="p">(</span><span class="s2">"example.com"</span><span class="p">)</span> <span class="o">&&</span> <span class="nx">Path</span><span class="p">(</span><span class="s2">"/users"</span><span class="p">)</span>
<span class="nx">Host</span><span class="p">(</span><span class="s2">"localhost"</span><span class="p">)</span> <span class="o">&&</span> <span class="nx">Path</span><span class="p">(</span><span class="s2">"/users"</span><span class="p">)</span>
</pre></div>
</div>
<p>Only first frontend is reachable for requests coming to port <code class="docutils literal"><span class="pre">8183</span></code>.</p>
</div>
<div class="section" id="middlewares">
<h3>Middlewares<a class="headerlink" href="#middlewares" title="Permalink to this headline">¶</a></h3>
<div class="figure align-left">
<img alt="_images/VulcanMiddleware.png" src="_images/VulcanMiddleware.png" />
</div>
<p>Middlewares are allowed to observe, modify and intercept http requests and responses. Vulcand provides several middlewares.
Users can write their own middlewares for Vulcand in Go.</p>
<p>To specify execution order of the middlewares, one can define the priority. Middlewares with smaller priority values will be executed first.</p>
</div>
<div class="section" id="rate-limits">
<h3>Rate Limits<a class="headerlink" href="#rate-limits" title="Permalink to this headline">¶</a></h3>
<p>Vulcan supports controlling request rates. Rate can be checked against different request parameters and is set up via limiting variable.</p>
<div class="highlight-bash"><div class="highlight"><pre>client.ip <span class="c"># client ip</span>
request.header.X-Special-Header <span class="c"># request header</span>
</pre></div>
</div>
<p>Adding and removing middlewares will modify the frontend behavior in real time. One can set expiring middlewares as well.</p>
<div class="highlight-etcd"><div class="highlight"><pre><span class="c"># Update or set rate limit the request to frontend "f1" to 1 request per second per client ip</span>
<span class="c"># with bursts up to 3 requests per second.</span>
etcdctl <span class="nb">set</span> /vulcand/frontends/f1/middlewares/rl1 <span class="s1">'{</span>
<span class="s1"> "Priority": 0,</span>
<span class="s1"> "Type": "ratelimit",</span>
<span class="s1"> "Middleware":{</span>
<span class="s1"> "Requests":1,</span>
<span class="s1"> "PeriodSeconds":1,</span>
<span class="s1"> "Burst":3,</span>
<span class="s1"> "Variable": "client.ip"}}'</span>
</pre></div>
</div>
<div class="highlight-cli"><div class="highlight"><pre><span class="c"># Update or set rate limit the request to frontend "f1" to 1 request per second per client ip</span>
<span class="c"># with bursts up to 3 requests per second.</span>
vctl ratelimit upsert -id<span class="o">=</span>rl1 -frontend<span class="o">=</span>f1 -requests<span class="o">=</span><span class="m">1</span> -burst<span class="o">=</span><span class="m">3</span> -period<span class="o">=</span><span class="m">1</span> --priority<span class="o">=</span>0
</pre></div>
</div>
<div class="highlight-api"><div class="highlight"><pre><span class="c"># Update or set rate limit the request to frontend "f1" to 1 request per second per client ip</span>
<span class="c"># with bursts up to 3 requests per second.</span>
curl -X POST -H <span class="s2">"Content-Type: application/json"</span> http://localhost:8182/v2/frontends/f1/middlewares<span class="se">\</span>
-d <span class="s1">'{"Middleware": {</span>
<span class="s1"> "Priority": 0,</span>
<span class="s1"> "Type": "ratelimit",</span>
<span class="s1"> "Id": "rl1",</span>
<span class="s1"> "Middleware":{</span>
<span class="s1"> "Requests":1,</span>
<span class="s1"> "PeriodSeconds":1,</span>
<span class="s1"> "Burst":3,</span>
<span class="s1"> "Variable": "client.ip"}}}'</span>
</pre></div>
</div>
<p><strong>Programmatic rate limits</strong></p>
<p>Sometimes you have to change rate limits based on various parameters, e.g. account billing plan. Instead of setting hard-coded rate limits, Vulcand can accept rate limits
set via headers for each individual request.</p>
<p>Each HTTP header should contain a JSON-encoded list with rates in the following format:</p>
<div class="highlight-json"><div class="highlight"><pre><span class="p">[{</span><span class="nt">"PeriodSeconds"</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="nt">"Requests"</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="nt">"Burst"</span><span class="p">:</span> <span class="mi">3</span><span class="p">}]</span>
</pre></div>
</div>
<p>That means that you should write a middleware that sets the header to the right value and place it before the ratelimit middleware.</p>
<p>After it’s done you can activate the ratelimit plugin:</p>
<div class="highlight-etcd"><div class="highlight"><pre><span class="c"># Update or set rate limit the request to frontend "f1" to get the rates from the X-Custom-Rates.</span>
<span class="c"># in case if the header is missing, ratelimit will default to 1 request per second per client ip</span>
<span class="c"># with bursts up to 3 requests per second.</span>
etcdctl <span class="nb">set</span> /vulcand/frontends/f1/middlewares/rl1 <span class="s1">'{</span>
<span class="s1"> "Id":"rl1",</span>
<span class="s1"> "Priority":0,</span>
<span class="s1"> "Type":"ratelimit",</span>
<span class="s1"> "Middleware":{</span>
<span class="s1"> "PeriodSeconds":1,</span>
<span class="s1"> "Requests":1,</span>
<span class="s1"> "Burst":3,</span>
<span class="s1"> "Variable":"client.ip",</span>
<span class="s1"> "RateVar":"request.header.X-Custom-Rates"}}'</span>
</pre></div>
</div>
<div class="highlight-cli"><div class="highlight"><pre><span class="c"># Update or set rate limit the request to frontend "f1" to get the rates from the X-Custom-Rates.</span>
<span class="c"># in case if the header is missing, ratelimit will default to 1 request per second per client ip</span>
<span class="c"># with bursts up to 3 requests per second.</span>
vctl ratelimit upsert -id<span class="o">=</span>rl1 -frontend<span class="o">=</span>f1 -requests<span class="o">=</span><span class="m">1</span> -burst<span class="o">=</span><span class="m">3</span> -period<span class="o">=</span><span class="m">1</span> --priority<span class="o">=</span><span class="m">0</span> --rateVar<span class="o">=</span><span class="s2">"request.header.X-Custom-Rates"</span>
</pre></div>
</div>
<div class="highlight-api"><div class="highlight"><pre><span class="c"># Update or set rate limit the request to frontend "f1" to get the rates from the X-Custom-Rates.</span>
<span class="c"># in case if the header is missing, ratelimit will default to 1 request per second per client ip</span>
<span class="c"># with bursts up to 3 requests per second.</span>
curl -X POST -H <span class="s2">"Content-Type: application/json"</span> http://localhost:8182/v2/frontends/f1/middlewares -d <span class="s1">'{</span>
<span class="s1"> "Middleware": {</span>
<span class="s1"> "Id":"rl1",</span>
<span class="s1"> "Priority":0,</span>
<span class="s1"> "Type":"ratelimit",</span>
<span class="s1"> "Middleware":{</span>
<span class="s1"> "PeriodSeconds":1,</span>
<span class="s1"> "Requests":1,</span>
<span class="s1"> "Burst":3,</span>
<span class="s1"> "Variable":"client.ip",</span>
<span class="s1"> "RateVar":"request.header.X-Custom-Rates"}}}'</span>
</pre></div>
</div>
</div>
<div class="section" id="connection-limits">
<h3>Connection Limits<a class="headerlink" href="#connection-limits" title="Permalink to this headline">¶</a></h3>
<p>Connection limits control the amount of simultaneous connections per frontend. Frontends re-use the same variables as rate limits.</p>
<div class="highlight-etcd"><div class="highlight"><pre><span class="c"># limit the amount of connections per frontend to 16 per client ip</span>
etcdctl <span class="nb">set</span> /vulcand/frontends/f1/middlewares/cl1<span class="se">\</span>
<span class="s1">'{"Priority": 0, "Type": "connlimit", "Middleware":{"Connections":16, "Variable": "client.ip"}}'</span>
</pre></div>
</div>
<div class="highlight-cli"><div class="highlight"><pre><span class="c"># limit the amount of connections per frontend to 16 per client ip</span>
vctl connlimit upsert -id<span class="o">=</span>cl1 -frontend<span class="o">=</span>f1 -connections<span class="o">=</span><span class="m">1</span> --priority<span class="o">=</span><span class="m">0</span> --variable<span class="o">=</span>client.ip
</pre></div>
</div>
<div class="highlight-api"><div class="highlight"><pre><span class="c"># limit the amount of connections per frontend to 16 per client ip</span>
curl -X POST -H <span class="s2">"Content-Type: application/json"</span> http://localhost:8182/v2/frontends/f1/middlewares<span class="se">\</span>
-d <span class="s1">'{"Middleware": {"Id": "cl1", "Priority": 0, "Type": "connlimit", "Middleware":{"Connections":16, "Variable": "client.ip"}}}'</span>
</pre></div>
</div>
</div>
<div class="section" id="rewrites-and-redirects">
<h3>Rewrites and redirects<a class="headerlink" href="#rewrites-and-redirects" title="Permalink to this headline">¶</a></h3>
<p>Rewrite plugin enables rewriting request URLs, returning redirect responses and changing response bodies.</p>
<p><strong>Rewrites</strong></p>
<div class="highlight-etcd"><div class="highlight"><pre><span class="c"># remove /foo prefix from the url</span>
etcdctl <span class="nb">set</span> /vulcand/frontends/f1/middlewares/r1 <span class="s1">'{</span>
<span class="s1"> "Id":"r1",</span>
<span class="s1"> "Priority":1,</span>
<span class="s1"> "Type":"rewrite",</span>
<span class="s1"> "Middleware":{</span>
<span class="s1"> "Regexp":"/foo(.*)",</span>
<span class="s1"> "Replacement":"$1",</span>
<span class="s1"> "RewriteBody":false,</span>
<span class="s1"> "Redirect":false}}'</span>
</pre></div>
</div>
<div class="highlight-cli"><div class="highlight"><pre><span class="c"># remove /foo prefix from the url, note the single quotes for '$1'</span>
vctl rewrite upsert -f f1 -id r1 --regexp<span class="o">=</span><span class="s2">"/foo(.*)"</span> --replacement<span class="o">=</span><span class="s1">'$1'</span>
</pre></div>
</div>
<div class="highlight-api"><div class="highlight"><pre><span class="c"># remove /foo prefix from the url</span>
curl -X POST -H <span class="s2">"Content-Type: application/json"</span> http://localhost:8182/v2/frontends/f1/middlewares<span class="se">\</span>
-d <span class="s1">'{"Middleware": {</span>
<span class="s1"> "Id":"r1",</span>
<span class="s1"> "Priority":1,</span>
<span class="s1"> "Type":"rewrite",</span>
<span class="s1"> "Middleware":{</span>
<span class="s1"> "Regexp":"/foo(.*)",</span>
<span class="s1"> "Replacement":"$1",</span>
<span class="s1"> "RewriteBody":false,</span>
<span class="s1"> "Redirect":false}}}'</span>
</pre></div>
</div>
<p><strong>Redirects</strong></p>
<p>Setting a <code class="docutils literal"><span class="pre">redirect</span></code> parameter to rewrite will make it to generate <code class="docutils literal"><span class="pre">302</span> <span class="pre">Found</span></code> response with <code class="docutils literal"><span class="pre">Location</span></code> header
set to the new URL:</p>
<div class="highlight-etcd"><div class="highlight"><pre><span class="c"># remove /foo prefix from the url</span>
etcdctl <span class="nb">set</span> /vulcand/frontends/f1/middlewares/r1 <span class="s1">'{</span>
<span class="s1"> "Id":"r1",</span>
<span class="s1"> "Priority":1,</span>
<span class="s1"> "Type":"rewrite",</span>
<span class="s1"> "Middleware":{</span>
<span class="s1"> "Regexp":"^http://localhost/(.*)",</span>
<span class="s1"> "Replacement":"https://localhost/$1",</span>
<span class="s1"> "RewriteBody":false,</span>
<span class="s1"> "Redirect":true}}'</span>
</pre></div>
</div>
<div class="highlight-cli"><div class="highlight"><pre><span class="c"># redirect http requests to https location</span>
vctl rewrite upsert -f f1 -id r1 --regexp<span class="o">=</span><span class="s2">"^http://localhost/(.*)"</span> --replacement<span class="o">=</span><span class="s1">'https://localhost/$1'</span> --redirect
</pre></div>
</div>
<div class="highlight-api"><div class="highlight"><pre><span class="c"># remove /foo prefix from the url</span>
curl -X POST -H <span class="s2">"Content-Type: application/json"</span> http://localhost:8182/v2/frontends/f1/middlewares<span class="se">\</span>
-d <span class="s1">'{"Middleware": {</span>
<span class="s1"> "Id":"r1",</span>
<span class="s1"> "Priority":1,</span>
<span class="s1"> "Type":"rewrite",</span>
<span class="s1"> "Middleware":{</span>
<span class="s1"> "Regexp":"^http://localhost/(.*)",</span>
<span class="s1"> "Replacement":"https://localhost/$1",</span>
<span class="s1"> "RewriteBody":false,</span>
<span class="s1"> "Redirect":true}}}'</span>
</pre></div>
</div>
<p><strong>Templating</strong></p>
<p>Rewrite can treat the response body as a template. Consider the following example:</p>
<div class="highlight-etcd"><div class="highlight"><pre><span class="c"># treat response body as a template</span>
etcdctl <span class="nb">set</span> /vulcand/frontends/f1/middlewares/r1 <span class="s1">'{</span>
<span class="s1"> "Id":"r1",</span>
<span class="s1"> "Priority":1,</span>
<span class="s1"> "Type":"rewrite",</span>
<span class="s1"> "Middleware":{"RewriteBody":true}}'</span>
</pre></div>
</div>
<div class="highlight-cli"><div class="highlight"><pre><span class="c"># treat response body as a template</span>
vctl rewrite upsert -f f1 -id r1 --rewriteBody
</pre></div>
</div>
<div class="highlight-api"><div class="highlight"><pre><span class="c"># treat response body as a template</span>
curl -X POST -H <span class="s2">"Content-Type: application/json"</span> http://localhost:8182/v2/frontends/f1/middlewares<span class="se">\</span>
-d <span class="s1">'{"Middleware": {</span>
<span class="s1"> "Id":"r1",</span>
<span class="s1"> "Priority":1,</span>
<span class="s1"> "Type":"rewrite",</span>
<span class="s1"> "Middleware":{"RewriteBody":true}}}'</span>
</pre></div>
</div>
<p>The backend server can now reply:</p>
<div class="highlight-go"><div class="highlight"><pre><span class="nx">handler</span> <span class="o">:=</span> <span class="nx">http</span><span class="p">.</span><span class="nx">HandlerFunc</span><span class="p">(</span><span class="kd">func</span><span class="p">(</span><span class="nx">w</span> <span class="nx">http</span><span class="p">.</span><span class="nx">ResponseWriter</span><span class="p">,</span> <span class="nx">req</span> <span class="o">*</span><span class="nx">http</span><span class="p">.</span><span class="nx">Request</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">w</span><span class="p">.</span><span class="nx">WriteHeader</span><span class="p">(</span><span class="mi">200</span><span class="p">)</span>
<span class="nx">w</span><span class="p">.</span><span class="nx">Write</span><span class="p">([]</span><span class="nb">byte</span><span class="p">(</span><span class="s">`{"foo": "{{.Request.Header.Get "variable-value"}}"}`</span><span class="p">))</span>
<span class="p">})</span>
</pre></div>
</div>
<p>And the client will get as a response:</p>
<div class="highlight-go"><div class="highlight"><pre><span class="p">{</span><span class="s">"foo"</span><span class="p">:</span> <span class="s">"variable-value"</span><span class="p">}</span>
</pre></div>
</div>
</div>
<div class="section" id="structured-logs">
<h3>Structured logs<a class="headerlink" href="#structured-logs" title="Permalink to this headline">¶</a></h3>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">We are still polishing the log format, so it may change soon.</p>
</div>
<p><code class="docutils literal"><span class="pre">trace</span></code> plugin supports output in syslog-compatible format of the structured logs to UDP or Unix socket.</p>
<p>Here’s the example of the log entry:</p>
<div class="highlight-bash"><div class="highlight"><pre>Jan <span class="m">13</span> 15:07:51 vulcan pid:<span class="o">[</span>3634<span class="o">]</span>: @cee: <span class="o">{</span><span class="s2">"request"</span>:<span class="o">{</span><span class="s2">"method"</span>:<span class="s2">"GET"</span>,<span class="s2">"url"</span>:<span class="s2">"http://h:5000"</span><span class="o">}</span>,<span class="s2">"response"</span>:<span class="o">{</span><span class="s2">"code"</span>:404,<span class="s2">"roundtrip"</span>:0.333712<span class="o">}}</span>
</pre></div>
</div>
<p>The prefix is a standard syslog prefix, and the part after <code class="docutils literal"><span class="pre">@cee:</span></code> is a structured log entry. Here’s the entry format explained:</p>
<div class="highlight-js"><div class="highlight"><pre><span class="p">{</span>
<span class="s2">"request"</span><span class="o">:</span> <span class="p">{</span>
<span class="s2">"method"</span><span class="o">:</span> <span class="s2">"GET"</span><span class="p">,</span> <span class="c1">// request method</span>
<span class="s2">"url"</span><span class="o">:</span> <span class="s2">"http://localhost:5000"</span><span class="p">,</span> <span class="c1">// request URL</span>
<span class="s2">"headers"</span><span class="o">:</span> <span class="p">{</span> <span class="c1">// optional captured request headers</span>
<span class="s2">"User-Agent"</span><span class="o">:</span> <span class="p">[</span> <span class="c1">// captured request User-Agent header values</span>
<span class="s2">"curl\/7.35.0"</span>
<span class="p">]</span>
<span class="p">},</span>
<span class="s2">"tls"</span><span class="o">:</span> <span class="p">{</span> <span class="c1">// tls is an optonal field, used when it's a TLS connection</span>
<span class="s2">"version"</span><span class="o">:</span> <span class="s2">"TLS12"</span><span class="p">,</span> <span class="c1">// TLS version used</span>
<span class="s2">"resume"</span><span class="o">:</span> <span class="kc">false</span><span class="p">,</span> <span class="c1">// whether it's a session resumed with session ticket</span>
<span class="s2">"cipher_suite"</span><span class="o">:</span> <span class="s2">"TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA"</span><span class="p">,</span> <span class="c1">// cipher used in a connection</span>
<span class="s2">"server"</span><span class="o">:</span> <span class="s2">"vulcand.io"</span> <span class="c1">// server name used in SNI</span>
<span class="p">}</span>
<span class="p">},</span>
<span class="s2">"response"</span><span class="o">:</span> <span class="p">{</span>
<span class="s2">"code"</span><span class="o">:</span> <span class="mi">404</span><span class="p">,</span> <span class="c1">// response code</span>
<span class="s2">"roundtrip"</span><span class="o">:</span> <span class="mf">0.408372</span><span class="p">,</span> <span class="c1">// roundtrip in milliseconds, part after '.' is microseconds</span>
<span class="s2">"headers"</span><span class="o">:</span> <span class="p">{</span> <span class="c1">// optional captured response headers</span>
<span class="s2">"Content-Type"</span><span class="o">:</span> <span class="p">[</span>
<span class="s2">"text\/plain; charset=utf-8"</span> <span class="c1">// captured response Content-Type header values</span>
<span class="p">]</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Adding and removing trace middleware will turn on/off tracing in real time.</p>
<div class="highlight-etcd"><div class="highlight"><pre><span class="c"># turn tracing on, pointing output to unix syslog facility.</span>
<span class="c"># capture request header values 'X-A' and 'X-B' and response headers 'X-C' and 'X-D'</span>
etcdctl <span class="nb">set</span> /vulcand/frontends/f1/middlewares/t1 <span class="s1">'{</span>
<span class="s1"> "Id":"t1",</span>
<span class="s1"> "Priority":1,</span>
<span class="s1"> "Type":"trace",</span>
<span class="s1"> "Middleware":{</span>
<span class="s1"> "ReqHeaders":["X-A","X-B"],</span>
<span class="s1"> "RespHeaders":["X-C","X-D"],</span>
<span class="s1"> "Addr":"syslog://"}}'</span>
</pre></div>
</div>
<div class="highlight-cli"><div class="highlight"><pre><span class="c"># turn tracing on, pointing output to unix syslog facility.</span>
<span class="c"># capture request header values 'X-A' and 'X-B' and response headers 'X-C' and 'X-D'</span>
vctl trace upsert -f f1 -id t1 --addr<span class="o">=</span><span class="s1">'syslog://'</span><span class="se">\</span>
--reqHeader<span class="o">=</span>X-A --reqHeader<span class="o">=</span>X-B --respHeader<span class="o">=</span>X-C --respHeader<span class="o">=</span>X-D
</pre></div>
</div>
<div class="highlight-api"><div class="highlight"><pre><span class="c"># turn tracing on, pointing output to unix syslog facility.</span>
<span class="c"># capture request header values 'X-A' and 'X-B' and response headers 'X-C' and 'X-D'</span>
curl -X POST -H <span class="s2">"Content-Type: application/json"</span> http://localhost:8182/v2/frontends/f1/middlewares -d <span class="s1">'{</span>
<span class="s1"> "Middleware": {</span>
<span class="s1"> "Id":"t1",</span>
<span class="s1"> "Priority":1,</span>
<span class="s1"> "Type":"trace",</span>
<span class="s1"> "Middleware":{</span>
<span class="s1"> "ReqHeaders":["X-A","X-B"],</span>
<span class="s1"> "RespHeaders":["X-C","X-D"],</span>
<span class="s1"> "Addr":"syslog://"}}}'</span>
</pre></div>
</div>
<p><strong>Controlling output</strong></p>
<p>You can control output using the following form of address values:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="c"># UDP socket formats</span>
syslog://localhost:5000 <span class="c"># host localhost, port 5000, LOG_LOCAL0 facility</span>
syslog://localhost:5000?f<span class="o">=</span>MAIL<span class="p">&</span><span class="nv">sev</span><span class="o">=</span>INFO <span class="c"># host localhost, port 5000, MAIL facility, INFO severity</span>
syslog://localhost:5000?f<span class="o">=</span>MAIL <span class="c"># host localhost, port 5000, MAIL facility, INFO severity</span>
syslog://localhost:5000?f<span class="o">=</span>LOG_LOCAL0<span class="p">&</span><span class="nv">sev</span><span class="o">=</span>DEBUG <span class="c"># host localhost, port 5000, LOG_LOCAL0 facility, INFO severity</span>
<span class="c"># unixgram socket format</span>
syslog:///tmp/out.sock <span class="c"># /tmp/out.sock unixgram socket</span>
syslog:///tmp/out.sock?f<span class="o">=</span>MAIL <span class="c"># /tmp/out.sock unixgram socket</span>
<span class="c"># default syslog</span>
syslog:// <span class="c"># default OS-specific unix/unixgram socket</span>
syslog://?f<span class="o">=</span>LOG_LOCAL0<span class="p">&</span><span class="nv">sev</span><span class="o">=</span>INFO <span class="c"># default OS-specific unix/unixgram socket</span>
</pre></div>
</div>
</div>
<div class="section" id="circuit-breakers">
<h3>Circuit Breakers<a class="headerlink" href="#circuit-breakers" title="Permalink to this headline">¶</a></h3>
<div class="figure align-left">
<img alt="_images/CircuitStandby.png" src="_images/CircuitStandby.png" />
</div>
<p>Circuit breaker is a special middleware that is designed to provide a fail-over action in case if service has degraded.
It is very helpful to prevent cascading failures - where the failure of the one service leads to failure of another.
Circuit breaker observes requests statistics and checks the stats against special error condition.</p>
<div class="figure align-left">
<img alt="_images/CircuitTripped.png" src="_images/CircuitTripped.png" />
</div>
<p>In case if condition matches, CB activates the fallback scenario: returns the response code or redirects the request to another frontend.</p>
<p><strong>Circuit Breaker states</strong></p>
<p>CB provides a set of explicit states and transitions explained below:</p>
<div class="figure align-left">
<img alt="_images/CBFSM.png" src="_images/CBFSM.png" />
</div>
<ul class="simple">
<li>Initial state is <code class="docutils literal"><span class="pre">Standby</span></code>. CB observes the statistics and does not modify the request.</li>
<li>In case if condition matches, CB enters <code class="docutils literal"><span class="pre">Tripped</span></code> state, where it responds with predefines code or redirects to another frontend.</li>
<li>CB can execute the special HTTP callback when going from <code class="docutils literal"><span class="pre">Standby</span></code> to <code class="docutils literal"><span class="pre">Tripped</span></code> state</li>
<li>CB sets a special timer that defines how long does it spend in the <code class="docutils literal"><span class="pre">Tripped</span></code> state</li>
<li>Once <code class="docutils literal"><span class="pre">Tripped</span></code> timer expires, CB enters <code class="docutils literal"><span class="pre">Recovering</span></code> state and resets all stats</li>
<li>In <code class="docutils literal"><span class="pre">Recovering</span></code> state Vulcand will start routing the portion of the traffic linearly increasing it over the period specified in <code class="docutils literal"><span class="pre">Recovering</span></code> timer.</li>
<li>In case if the condition matches in <code class="docutils literal"><span class="pre">Recovering</span></code> state, CB enters <code class="docutils literal"><span class="pre">Tripped</span></code> state again</li>
<li>In case if the condition does not match and recovery timer expries, CB enters <code class="docutils literal"><span class="pre">Standby</span></code> state.</li>
<li>CB can execute the special HTTP callback when going from <code class="docutils literal"><span class="pre">Recovering</span></code> to <code class="docutils literal"><span class="pre">Standby</span></code> state</li>
</ul>
<p><strong>Conditions</strong></p>
<p>CB defines a simple language that allows us to specify simple conditions that watch the stats for a frontend:</p>
<div class="highlight-javascript"><div class="highlight"><pre><span class="nx">NetworkErrorRatio</span><span class="p">()</span> <span class="o">></span> <span class="mf">0.5</span> <span class="c1">// watch error ratio over 10 second sliding window for a frontend</span>
<span class="nx">LatencyAtQuantileMS</span><span class="p">(</span><span class="mf">50.0</span><span class="p">)</span> <span class="o">></span> <span class="mi">50</span> <span class="c1">// watch latency at quantile in milliseconds.</span>
<span class="nx">ResponseCodeRatio</span><span class="p">(</span><span class="mi">500</span><span class="p">,</span> <span class="mi">600</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">600</span><span class="p">)</span> <span class="o">></span> <span class="mf">0.5</span> <span class="c1">// ratio of response codes in range [500-600) to [0-600)</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Quantiles should be provided as floats - don’t forget to add .0 to hint it as float</p>
</div>
<p><strong>Response fallback</strong></p>
<p>Response fallback will tell CB to reply with a predefined response instead of forwarding the request to the backend</p>
<div class="highlight-javascript"><div class="highlight"><pre><span class="p">{</span>
<span class="s2">"Type"</span><span class="o">:</span> <span class="s2">"response"</span><span class="p">,</span>
<span class="s2">"Action"</span><span class="o">:</span> <span class="p">{</span>
<span class="s2">"ContentType"</span><span class="o">:</span> <span class="s2">"text/plain"</span><span class="p">,</span>
<span class="s2">"StatusCode"</span><span class="o">:</span> <span class="mi">400</span><span class="p">,</span>
<span class="s2">"Body"</span><span class="o">:</span> <span class="s2">"Come back later"</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p><strong>Redirect fallback</strong></p>
<p>Redirect fallback will redirect the request to another frontend.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">It won’t work for frontends not defined in the Vulcand config.</p>
</div>
<div class="highlight-javascript"><div class="highlight"><pre><span class="p">{</span>
<span class="s2">"Type"</span><span class="o">:</span> <span class="s2">"redirect"</span><span class="p">,</span>
<span class="s2">"Action"</span><span class="o">:</span> <span class="p">{</span>
<span class="s2">"URL"</span><span class="o">:</span> <span class="s2">"https://example.com/fallback"</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p><strong>Webhook Action</strong></p>
<p>Circuit breaker can notify extenral sources on it’s state transitions, e.g. it can create a pager duty incident by issuing a webhook:</p>
<div class="highlight-javascript"><div class="highlight"><pre><span class="p">{</span>
<span class="s2">"Body"</span><span class="o">:</span> <span class="p">{</span>
<span class="s2">"client"</span><span class="o">:</span> <span class="s2">"Sample Monitoring Service"</span><span class="p">,</span>
<span class="s2">"client_url"</span><span class="o">:</span> <span class="s2">"https://example.com"</span><span class="p">,</span>
<span class="s2">"description"</span><span class="o">:</span> <span class="s2">"FAILURE for production/HTTP on machine srv01.acme.com"</span><span class="p">,</span>
<span class="s2">"event_type"</span><span class="o">:</span> <span class="s2">"trigger"</span><span class="p">,</span>
<span class="s2">"incident_key"</span><span class="o">:</span> <span class="s2">"srv01/HTTP"</span><span class="p">,</span>
<span class="s2">"service_key"</span><span class="o">:</span> <span class="s2">"-pager-duty-service-key"</span>
<span class="p">},</span>
<span class="s2">"Headers"</span><span class="o">:</span> <span class="p">{</span>
<span class="s2">"Content-Type"</span><span class="o">:</span> <span class="p">[</span>
<span class="s2">"application/json"</span>
<span class="p">]</span>
<span class="p">},</span>
<span class="s2">"Method"</span><span class="o">:</span> <span class="s2">"POST"</span><span class="p">,</span>
<span class="s2">"URL"</span><span class="o">:</span> <span class="s2">"https://events.pagerduty.com/generic/2010-04-15/create_event.json"</span>
<span class="p">}</span>
</pre></div>
</div>
<p><strong>Setup</strong></p>
<p>Circuit breaker setup is can be done via Etcd, command line or API:</p>
<div class="highlight-etcd"><div class="highlight"><pre>etcdctl <span class="nb">set</span> /vulcand/frontends/f1/middlewares/cb1 <span class="s1">'{</span>
<span class="s1"> "Id":"cb1",</span>
<span class="s1"> "Priority":1,</span>
<span class="s1"> "Type":"cbreaker",</span>
<span class="s1"> "Middleware":{</span>
<span class="s1"> "Condition":"NetworkErrorRatio() > 0.5",</span>
<span class="s1"> "Fallback":{"Type": "response", "Action": {"StatusCode": 400, "Body": "Come back later"}},</span>
<span class="s1"> "FallbackDuration": 10000000000,</span>
<span class="s1"> "RecoveryDuration": 10000000000,</span>
<span class="s1"> "CheckPeriod": 100000000</span>
<span class="s1"> }</span>
<span class="s1"> }'</span>
</pre></div>
</div>
<div class="highlight-cli"><div class="highlight"><pre>vctl cbreaker upsert <span class="se">\</span>
--frontend<span class="o">=</span>f1 <span class="se">\</span>
--id<span class="o">=</span>cb1<span class="se">\</span>
--condition<span class="o">=</span><span class="s2">"NetworkErrorRatio() > 0.5"</span> <span class="se">\</span>
--fallback<span class="o">=</span><span class="s1">'{"Type": "response", "Action": {"StatusCode": 400, "Body": "Come back later"}}'</span>
</pre></div>
</div>
<div class="highlight-api"><div class="highlight"><pre>curl -X POST -H <span class="s2">"Content-Type: application/json"</span><span class="se">\</span>
http://localhost:8182/v2/frontends/f1/middlewares<span class="se">\</span>
-d <span class="s1">'{</span>
<span class="s1"> "Middleware": {</span>
<span class="s1"> "Id":"cb1",</span>
<span class="s1"> "Priority":1,</span>
<span class="s1"> "Type":"cbreaker",</span>
<span class="s1"> "Middleware":{</span>
<span class="s1"> "Condition":"NetworkErrorRatio() > 0.5",</span>
<span class="s1"> "Fallback":{</span>
<span class="s1"> "Type": "response",</span>
<span class="s1"> "Action": {"StatusCode": 400, "Body": "Come back later"}</span>
<span class="s1"> },</span>
<span class="s1"> "FallbackDuration": 10000000000,</span>
<span class="s1"> "RecoveryDuration": 10000000000,</span>
<span class="s1"> "CheckPeriod": 100000000</span>
<span class="s1"> }</span>
<span class="s1"> }</span>
<span class="s1"> }'</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="tls">
<h2>TLS<a class="headerlink" href="#tls" title="Permalink to this headline">¶</a></h2>
<p>Vulcand supports HTTPS via <a class="reference external" href="http://en.wikipedia.org/wiki/Server_Name_Indication">SNI</a>, certificate management and multiple HTTPS servers per running process.
This sections below contain all the steps required to enable TLS support in Vulcand</p>
<div class="section" id="managing-certificates">
<h3>Managing certificates<a class="headerlink" href="#managing-certificates" title="Permalink to this headline">¶</a></h3>
<p>Vulcand encrypts certificates when storing them in the backends and uses <a class="reference external" href="https://godoc.org/code.google.com/p/go.crypto/nacl/secretbox">Nacl secretbox</a> to seal the data.
The running server acts as an encryption/decryption point when reading and writing certificates.</p>
<p>This special key has to be generated by Vulcand using command line utility:</p>
<p><strong>Setting up seal key</strong></p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ </span>vctl secret new_key
</pre></div>
</div>
<p>Once we got the key, we can pass it to the running daemon. This key will be used by Vulcand to encrypt and decrypt stored certificates and private keys.</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ </span>vulcand -sealKey<span class="o">=</span><span class="s2">"the-seal-key"</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Add space before command to avoid leaking seal key in bash history, or use <code class="docutils literal"><span class="pre">HISTIGNORE</span></code></p>
</div>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>