Last active
November 25, 2016 08:36
-
-
Save aaronhurt/8342449ca0fa37bbfaf730f571c71897 to your computer and use it in GitHub Desktop.
HAProxy configuration generated from consul via consul-template to generate frontend/backend services across multiple datacenters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
## NOTE: Configuration generated by consul-template | |
## local changes will be overwritten. | |
##{{$myDc := key "env/dc"}} | |
## lb/haproxy/global | |
global{{range ls "lb/haproxy/global"}}{{if ne .Key "rawText"}} | |
{{.Key}} {{.Value}} | |
{{- end}}{{end -}} | |
{{range $key, $pairs := tree "lb/haproxy/global" | byKey}}{{range $pair := $pairs}} | |
{{$key}} {{.Key}}{{if ne .Value ""}} {{.Value}}{{end}} | |
{{- end}}{{end -}} | |
{{with $rawText := key "lb/haproxy/global/rawText"}}{{if ne $rawText ""}} | |
## lb/haproxy/defaults/rawText | |
{{$rawText}} | |
## end raw | |
{{- end}}{{end}} | |
## lb/haproxy/defaults | |
defaults{{range ls "lb/haproxy/defaults"}}{{if ne .Key "rawText"}} | |
{{.Key}} {{.Value}} | |
{{- end}}{{end -}} | |
{{range $key, $pairs := tree "lb/haproxy/defaults" | byKey}}{{range $pair := $pairs}} | |
{{$key}} {{.Key}}{{if ne .Value ""}} {{.Value}}{{end}} | |
{{- end}}{{end -}} | |
{{with $rawText := key "lb/haproxy/defaults/rawText"}}{{if ne $rawText ""}} | |
## lb/haproxy/defaults/rawText | |
{{$rawText}} | |
## end raw | |
{{- end}}{{end}} | |
## lb/haproxy/frontend/main | |
frontend main{{range ls "lb/haproxy/frontend/main"}}{{if ne .Key "rawText"}} | |
{{.Key}} {{.Value}} | |
{{- end}}{{end -}} | |
{{range ls "lb/haproxy/frontend/main/bind"}} | |
bind {{.Key}}{{if ne .Value ""}} {{.Value}}{{end}} | |
{{- end -}} | |
{{with $rawText := key "lb/haproxy/frontend/main/rawText"}}{{if ne $rawText ""}} | |
## lb/haproxy/frontend/main/rawText | |
{{$rawText}} | |
{{- end}}{{end -}} | |
{{range $dcIdx, $dcName := datacenters -}} | |
{{$dcAtName := printf "@%s" $dcName -}} | |
{{range services $dcAtName -}} | |
{{$svcNameLoc := printf "%s@%s" .Name $dcName -}} | |
{{if in .Tags "proxy-unique" -}} | |
{{if service $svcNameLoc -}} | |
{{with index (service $svcNameLoc) 0}} | |
## unique {{.ID}}{{$dcAtName}} | |
acl path_{{.ID}} path_beg /{{.ID}}/ | |
use_backend app_{{.ID}} if path_{{.ID}} | |
{{- end}}{{end}}{{end -}} | |
{{if in .Tags "proxy-standard" -}} | |
{{if service $svcNameLoc -}} | |
{{with index (service $svcNameLoc) 0 -}} | |
{{$myServiceName := printf "%s@%s" .Name $myDc -}} | |
{{if ne $dcName $myDc -}} | |
{{if service $myServiceName}}{{/* local service exists - skip remote service */}}{{else}} | |
## standard {{.Name}}{{$dcAtName}} | |
acl path_{{.Name}} path_beg /{{if in .Tags "proxy-dash2dots"}}{{.Name | replaceAll "-" "."}}{{else}}{{.Name}}{{end}}/ | |
use_backend app_{{.Name}} if path_{{.Name}} | |
{{- end -}} | |
{{else}}{{/* no local service - use remote */}} | |
## standard {{.Name}}{{$dcAtName}} | |
acl path_{{.Name}} path_beg /{{if in .Tags "proxy-dash2dots"}}{{.Name | replaceAll "-" "."}}{{else}}{{.Name}}{{end}}/ | |
use_backend app_{{.Name}} if path_{{.Name}} | |
{{- end}}{{end}}{{end}}{{end}}{{end}}{{end}} | |
## default backend provides root | |
default_backend rootBackend | |
## default backend | |
backend rootBackend | |
mode http{{range $dcIdx, $dcName := datacenters -}} | |
{{$dcAtName := printf "@%s" $dcName -}} | |
{{range services $dcAtName -}} | |
{{$svcNameLoc := printf "%s@%s" .Name $dcName -}} | |
{{if in .Tags "proxy-root" -}} | |
{{if service $svcNameLoc -}} | |
{{range service $svcNameLoc}} | |
server {{.ID}}:{{$dcName}} {{.Address}}:{{.Port}}{{if in .Tags "proxy-check"}} check{{end}}{{if in .Tags "proxy-ssl"}} ssl verify none{{end}}{{if ne $myDc $dcName}} backup{{end}} | |
{{- end}}{{end}}{{end}}{{end}}{{end}} | |
{{range $dcIdx, $dcName := datacenters -}} | |
{{$dcAtName := printf "@%s" $dcName -}} | |
{{range services $dcAtName -}} | |
{{$svcNameLoc := printf "%s@%s" .Name $dcName -}} | |
{{if in .Tags "proxy-unique" -}} | |
{{if service $svcNameLoc -}} | |
{{with index (service $svcNameLoc) 0}} | |
## unique backend {{.ID}} | |
backend app_{{.ID}} | |
mode http | |
{{- if in .Tags "proxy-internal"}}{{with $enaInternal := key "lb/haproxy/misc/enaInternal" -}} | |
{{if ne $enaInternal ""}} | |
{{$enaInternal}} | |
{{- end}}{{end}}{{end -}} | |
{{if in .Tags "proxy-nostrip" -}} | |
{{/* do nothing */}}{{else}} | |
reqrep ^(GET|PUT|POST|DELETE|PATCH|OPTIONS)\ /{{.ID}}/(.*) \1\ /\2 | |
{{- end}} | |
server {{.ID}}:{{$dcName}} {{.Address}}:{{.Port}}{{if in .Tags "proxy-check"}} check{{end}}{{if in .Tags "proxy-ssl"}} ssl verify none{{end -}} | |
{{end}}{{end}}{{end}}{{end}}{{end}} | |
{{range $dcIdx, $dcName := datacenters -}} | |
{{$dcAtName := printf "@%s" $dcName -}} | |
{{range services $dcAtName -}} | |
{{$svcNameLoc := printf "%s@%s" .Name $dcName -}} | |
{{if in .Tags "proxy-standard" -}} | |
{{if service $svcNameLoc -}} | |
{{$myServiceName := printf "%s@%s" .Name $myDc -}} | |
{{if ne $dcName $myDc -}} | |
{{if service $myServiceName -}} | |
{{/* do nothing */}}{{else}}{{/* services not registered locally */}} | |
## standard backend {{.Name}} | |
backend app_{{.Name}} | |
mode http | |
{{- if in .Tags "proxy-rewrite-net-cookie"}}{{with $rewriteCookie := key "lb/haproxy/misc/rewriteNetCookie" -}} | |
{{if ne $rewriteCookie ""}} | |
{{$rewriteCookie}} | |
{{- end}}{{end}}{{end -}} | |
{{if in .Tags "proxy-internal"}}{{with $enaInternal := key "lb/haproxy/misc/enaInternal" -}} | |
{{if ne $enaInternal ""}} | |
{{$enaInternal}} | |
{{- end }}{{end}}{{end -}} | |
{{if in .Tags "proxy-nostrip" -}} | |
{{/* do nothing */}}{{else}} | |
reqrep ^(GET|PUT|POST|DELETE|PATCH|OPTIONS)\ /{{if in .Tags "proxy-dash2dots"}}{{.Name | replaceAll "-" "."}}{{else}}{{.Name}}{{end}}/(.*) \1\ /\2 | |
{{- end}} | |
balance roundrobin{{$svcName := .Name -}} | |
{{range $dcIdx2, $dcName2 := datacenters -}} | |
{{$svcNameLoc2 := printf "%s@%s" $svcName $dcName2 -}} | |
{{if service $svcNameLoc2 -}} | |
{{range service $svcNameLoc2}} | |
server {{.ID}}:{{$dcName2}} {{.Address}}:{{.Port}}{{if in .Tags "proxy-check"}} check{{end}}{{if in .Tags "proxy-ssl"}} ssl verify none{{end}}{{if ne $myDc $dcName2}} backup{{end}} | |
{{- end}}{{end}}{{end}} | |
{{end}}{{else}}{{/* services registered locally - duplication here prevents duplication in the final config */}} | |
## standard backend {{.Name}} | |
backend app_{{.Name}} | |
mode http | |
{{- if in .Tags "proxy-rewrite-net-cookie"}}{{with $rewriteCookie := key "lb/haproxy/misc/rewriteNetCookie" -}} | |
{{if ne $rewriteCookie ""}} | |
{{$rewriteCookie}} | |
{{- end}}{{end}}{{end -}} | |
{{if in .Tags "proxy-internal"}}{{with $enaInternal := key "lb/haproxy/misc/enaInternal" -}} | |
{{if ne $enaInternal ""}} | |
{{$enaInternal}} | |
{{- end }}{{end}}{{end -}} | |
{{if in .Tags "proxy-nostrip" -}} | |
{{/* do nothing */}}{{else}} | |
reqrep ^(GET|PUT|POST|DELETE|PATCH|OPTIONS)\ /{{if in .Tags "proxy-dash2dots"}}{{.Name | replaceAll "-" "."}}{{else}}{{.Name}}{{end}}/(.*) \1\ /\2 | |
{{- end}} | |
balance roundrobin{{$svcName := .Name -}} | |
{{range $dcIdx2, $dcName2 := datacenters -}} | |
{{$svcNameLoc2 := printf "%s@%s" $svcName $dcName2 -}} | |
{{if service $svcNameLoc2 -}} | |
{{range service $svcNameLoc2}} | |
server {{.ID}}:{{$dcName2}} {{.Address}}:{{.Port}}{{if in .Tags "proxy-check"}} check{{end}}{{if in .Tags "proxy-ssl"}} ssl verify none{{end}}{{if ne $myDc $dcName2}} backup{{end}} | |
{{- end}}{{end}}{{end}} | |
{{end}}{{end}}{{end}}{{end}}{{end}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment