-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhaproxy.cfg
85 lines (64 loc) · 2.75 KB
/
haproxy.cfg
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
global
# enable runtime api via socket, run this inside the container
# echo "get var proc.strategy" | socat stdio unix-connect:/tmp/haproxy.sock
# see https://www.haproxy.com/documentation/haproxy-runtime-api/
stats socket /tmp/haproxy.sock user haproxy group haproxy mode 660 level admin
# strategy can be either "PERCENTAGE" or "COOKIE"
set-var proc.strategy env(STRATEGY)
# default some environment variables
presetenv PERCENTAGE_NEW "0"
presetenv PERCENTAGE_OLD "100"
presetenv SERVER_COUNT "5"
# we source them from the environment here to actually typecheck them
set-var proc.percentage_new int("${PERCENTAGE_NEW}")
set-var proc.percentage_old int("${PERCENTAGE_OLD}")
set-var proc.server_count int("${SERVER_COUNT}")
#log stdout format raw local0
defaults
timeout connect 5s
timeout client 1m
timeout server 1m
mode http
default-server check
default-server resolvers dns
default-server resolve-prefer ipv4
default-server init-addr last,libc,none
default-server ssl verify required ca-file @system-ca
#log global
#option httplog
frontend http-in
bind *:80
# the cookie name for the "COOKIE" strategy e.g. (cookie_name=cookie_value)
acl is_new_cookie hdr_sub(cookie) "$COOKIE_STRATEGY_NAME" --
# decide on the strategy
acl routing_strategy_percentage var(proc.strategy) -m str "PERCENTAGE"
acl routing_strategy_cookie var(proc.strategy) -m str "COOKIE"
# default use the old_domain backend
default_backend old_domain
# when using the "PERCENTAGE" strategy, we route to the percentage_strategy backend
use_backend percentage_strategy if routing_strategy_percentage
# when using the "COOKIE" strategy and the cookie matches, we route to the cookie_strategy backend
use_backend cookie_strategy if routing_strategy_cookie is_new_cookie
backend old_domain
http-response set-header X-Proxy-Flow route-to-legacy
option tcp-check
balance leastconn
server-template default_old "$SERVER_COUNT" "$OLD_DOMAIN"
backend percentage_strategy
http-response set-header X-Proxy-Flow route-to-percentage
option tcp-check
# https://www.haproxy.com/documentation/haproxy-configuration-manual/latest/#4.2-balance
balance static-rr
cookie "$COOKIE_PERCENTAGE_NAME" insert indirect
# old domain
server-template old_domain_percentage "$SERVER_COUNT" "$OLD_DOMAIN" cookie "old_domain_$PERCENTAGE_OLD" weight "$PERCENTAGE_OLD"
# new domain
server-template new_domain_percentage "$SERVER_COUNT" "$NEW_DOMAIN" cookie "new_domain_$PERCENTAGE_NEW" weight "$PERCENTAGE_NEW"
backend cookie_strategy
balance leastconn
http-response set-header X-Proxy-Flow route-to-new
option tcp-check
server-template default_new "$SERVER_COUNT" "$NEW_DOMAIN"
resolvers dns
parse-resolv-conf
accepted_payload_size 8192