Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.

Commit 468306c

Browse files
committed
added default backends
1 parent 1a88914 commit 468306c

5 files changed

Lines changed: 39 additions & 5 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ Settings in this part is immutable, you have to redeploy HAProxy service to make
170170

171171
|Environment Variable|Default|Description|
172172
|:-----:|:-----:|:----------|
173+
|ADDITIONAL_BACKENDS| |list of additional backends to balance. The format is `backend name, server name ,FORCE_SSL(True|False),host:port,options`|
173174
|ADDITIONAL_SERVICES| |list of additional services to balance (es: `prj1:web,prj2:sql`). Discovery will be based on `com.docker.compose.[project|service]` container labels. This environment variable only works on compose v2, and the referenced services must be on a network resolvable and accessible to this containers.|
174175
|BALANCE|roundrobin|load balancing algorithm to use. Possible values include: `roundrobin`, `static-rr`, `source`, `leastconn`. See:[HAProxy:balance](https://cbonte.github.io/haproxy-dconv/configuration-1.5.html#4-balance)|
175176
|CA_CERT_FILE| |the path of a ca-cert file. This allows you to mount your ca-cert file directly from a volume instead of from envvar. If set, `CA_CERT` envvar will be ignored. Possible value: `/cacerts/cert0.pem`|
@@ -182,6 +183,7 @@ Settings in this part is immutable, you have to redeploy HAProxy service to make
182183
|EXTRA_GLOBAL_SETTINGS| |comma-separated string of extra settings, and each part will be appended to GLOBAL section in the configuration file. To escape comma, use `\,`. Possible value: `tune.ssl.cachesize 20000, tune.ssl.default-dh-param 2048`|
183184
|EXTRA_ROUTE_SETTINGS| |a string which is append to the each backend route after the health check, can be over written in the linked services. Possible value: "send-proxy"|
184185
|EXTRA_SSL_CERTS| |list of extra certificate names separated by comma, eg. `CERT1, CERT2, CERT3`. You also need to specify each certificate as separate env variables like so: `CERT1="<cert-body1>"`, `CERT2="<cert-body2>"`, `CERT3="<cert-body3>"`|
186+
|FORCE_DEFAULT_BACKEND| True | set the default_service as a default backend. This is useful when you have more than one backend and you don't want your default_service as a default backend
185187
|HEALTH_CHECK|check|set health check on each backend route, possible value: "check inter 2000 rise 2 fall 3". See:[HAProxy:check](https://cbonte.github.io/haproxy-dconv/configuration-1.5.html#5.2-check)|
186188
|HTTP_BASIC_AUTH| |a comma-separated list of credentials(`<user>:<pass>`) for HTTP basic auth, which applies to all the backend routes. To escape comma, use `\,`. *Attention:* DO NOT rely on this for authentication in production|
187189
|MAXCONN|4096|sets the maximum per-process number of concurrent connections.|
@@ -477,4 +479,4 @@ Legacy links:
477479
In most cases, `dockercloud/haproxy` will configure itself automatically when the linked services change, you don't need to reload it manually. But for some reason, if you have to do so, here is how:
478480

479481
* `docker exec <haproxy_id> /reload.sh`, if you are on the node where dockercloud/haproxy deploys
480-
* `docker-cloud exec <haproxy_uuid> /reload.sh`, if you use docker-cloud cli
482+
* `docker-cloud exec <haproxy_uuid> /reload.sh`, if you use docker-cloud cli

haproxy/config.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,25 @@ def parse_extra_frontend_settings(envvars):
2828
settings_dict[port] = settings
2929
return settings_dict
3030

31+
# backend_name,server_name,settings,host:port
32+
def parse_additional_backends(additional_backend_settings):
33+
additional_backends = {}
34+
if not additional_backend_settings:
35+
return additional_backends
36+
for backend in additional_backend_settings.split(";"):
37+
parts = backend.split(",")
38+
service_name, backend_name, redirect, backend_desc,options = parts
39+
40+
if redirect=="True":
41+
route = ["redirect scheme https code 301 if !{ ssl_fc }", backend_name + ' ' + backend_desc + ' ' + options]
42+
else:
43+
route = [backend_name + ' ' + backend_desc + ' ' + options]
44+
45+
additional_backends[service_name] = route
46+
return additional_backends
3147

3248
# envvar
49+
ADDITIONAL_BACKENDS = parse_additional_backends(os.getenv("ADDITIONAL_BACKENDS"))
3350
ADDITIONAL_SERVICES = os.getenv("ADDITIONAL_SERVICES")
3451
API_AUTH = os.getenv("DOCKERCLOUD_AUTH")
3552
BALANCE = os.getenv("BALANCE", "roundrobin")
@@ -44,6 +61,7 @@ def parse_extra_frontend_settings(envvars):
4461
EXTRA_GLOBAL_SETTINGS = os.getenv("EXTRA_GLOBAL_SETTINGS")
4562
EXTRA_SSL_CERT = os.getenv("EXTRA_SSL_CERTS")
4663
EXTRA_ROUTE_SETTINGS=os.getenv("EXTRA_ROUTE_SETTINGS", "")
64+
FORCE_DEFAULT_BACKEND = os.getenv("FORCE_DEFAULT_BACKEND", "True")
4765
HAPROXY_CONTAINER_URI = os.getenv("DOCKERCLOUD_CONTAINER_API_URI")
4866
HAPROXY_SERVICE_URI = os.getenv("DOCKERCLOUD_SERVICE_API_URI")
4967
HEALTH_CHECK = os.getenv("HEALTH_CHECK", "check inter 2000 rise 2 fall 3")

haproxy/haproxycfg.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ def update(self):
111111
cfg_dict.update(self._config_tcp_sections())
112112
cfg_dict.update(self._config_frontend_sections())
113113
cfg_dict.update(self._config_backend_sections())
114+
cfg_dict.update(self._config_adittional_backend_sections())
114115

115116
cfg = prettify(cfg_dict)
116117
self._update_haproxy(cfg)
@@ -329,3 +330,12 @@ def _config_backend_sections(self):
329330
else:
330331
cfg["backend default_service"] = backend
331332
return cfg
333+
334+
def _config_adittional_backend_sections(self):
335+
cfg = OrderedDict()
336+
337+
if ADDITIONAL_BACKENDS:
338+
for key in ADDITIONAL_BACKENDS:
339+
cfg["backend %s" % key] = ADDITIONAL_BACKENDS[key]
340+
341+
return cfg

haproxy/helper/backend_helper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def get_backend_section(details, routes, vhosts, service_alias, routes_added):
1818
route_setting = " ".join([route_health_check, extra_route_settings]).strip()
1919
backend_routes = get_backend_routes(route_setting, is_sticky, routes, routes_added, service_alias)
2020
backend.extend(backend_routes)
21+
2122
return backend
2223

2324

@@ -44,7 +45,6 @@ def get_backend_routes(route_setting, is_sticky, routes, routes_added, service_a
4445

4546
return sorted(backend_routes)
4647

47-
4848
def get_route_health_check(details, service_alias, default_health_check):
4949
health_check = get_service_attribute(details, "health_check", service_alias)
5050
health_check = health_check if health_check else default_health_check

haproxy/helper/frontend_helper.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from collections import OrderedDict
22

33
from haproxy.config import EXTRA_BIND_SETTINGS, EXTRA_FRONTEND_SETTINGS, MONITOR_URI, MONITOR_PORT, MAXCONN, \
4-
SKIP_FORWARDED_PROTO
4+
SKIP_FORWARDED_PROTO, FORCE_DEFAULT_BACKEND
55

66

77
def check_require_default_route(routes, routes_added):
@@ -152,7 +152,9 @@ def config_default_frontend(ssl_bind_string):
152152
if "80" in EXTRA_FRONTEND_SETTINGS:
153153
frontend.extend(EXTRA_FRONTEND_SETTINGS["80"])
154154

155-
frontend.append("default_backend default_service")
155+
if "True" in FORCE_DEFAULT_BACKEND:
156+
frontend.append("default_backend default_service")
157+
156158
cfg["frontend default_port_80"] = frontend
157159

158160
if ssl_bind_string:
@@ -171,7 +173,9 @@ def config_default_frontend(ssl_bind_string):
171173
if "443" in EXTRA_FRONTEND_SETTINGS:
172174
ssl_frontend.extend(EXTRA_FRONTEND_SETTINGS["443"])
173175

174-
ssl_frontend.append("default_backend default_service")
176+
if "True" in FORCE_DEFAULT_BACKEND:
177+
ssl_frontend.append("default_backend default_service")
178+
175179
cfg["frontend default_port_443"] = ssl_frontend
176180

177181
return cfg, monitor_uri_configured

0 commit comments

Comments
 (0)