Skip to content

Instantly share code, notes, and snippets.

@odacremolbap
Last active December 1, 2016 09:32
Show Gist options
  • Save odacremolbap/b9d4f57d3e7fde5f5c2ddb14f2fd1f44 to your computer and use it in GitHub Desktop.
Save odacremolbap/b9d4f57d3e7fde5f5c2ddb14f2fd1f44 to your computer and use it in GitHub Desktop.
Customize HAProxy on Stackpoint Cloud (beta)
# Customizing Configuration
HAProxy ingress controller 0.1.4 image has predefined these environment variables
ENV CONFIG_MAP_NAME haproxy-config
ENV CONFIG_MAP_NAMESPACE stackpoint-system
Which refer to the namespace and name where the configmap with the configuration template is to be found.
The name of the configuration item inside that configmap is `full-template-balancer`, this command will create a new template which haproxy will use:
kubectl create configmap --namespace stackpoint-system haproxy-config --from-file=examples/configmap/full-template-balancer
The contents of the default template are kept in the source code, and will be used when the configmap is not found:
# Generated HAProxy
{{ with .Global}}
global
daemon
pidfile /var/run/haproxy.pid
stats socket /var/run/haproxy.stat mode 777
# need to check logging
maxconn {{ .Maxconn }}
maxpipes {{ .Maxpipes }}
spread-checks {{ .SpreadChecks }}{{ if .Debug }}
debug{{ end }}{{ end }}
{{ with .Defaults }}
defaults
log global
mode {{ .Mode }}
balance {{ .Balance }}
maxconn {{ .Maxconn }}
{{ if .TCPLog }}option tcplog{{ end }}
{{ if .HTTPLog }}option httplog{{ end }}
{{ if .AbortOnClose }}option abortonclose{{ end }}
{{ if .HTTPServerClose }}option httpclose{{ end }}
{{ if .ForwardFor }}option forwardfor{{ end }}
retries {{ .Retries }}
{{ if .Redispatch }}option redispatch{{ end }}
timeout client {{ .TimeoutClient }}
timeout connect {{ .TimeoutConnect }}
timeout server {{ .TimeoutServer }}
{{ if .DontLogNull }}option dontlognull{{ end }}
timeout check {{ .TimeoutCheck }}
{{ end }}{{$certs_dir:= .CertsDir }}{{ range .Frontends }}
frontend {{ .Name }}{{ with .Bind }}
bind {{ .IP }}:{{ .Port }}{{ if .IsTLS }} ssl {{ range .Certs }}crt {{$certs_dir}}/{{.Name}}.pem {{ end }}{{ end }}{{ end }}{{ if .DefaultBackend.Backend }}
default_backend {{ .DefaultBackend.Backend }}{{end}}{{ range .ACLs }}
acl {{ .Name }} {{.Content}}{{end}}{{ range .UseBackendsByPrio }}
use_backend {{ .Backend }} if {{ range .ACLs }}{{ .Name }} {{end}}{{end}}
{{ end }}
{{range $name, $be := .Backends}}
backend {{$name}}{{ range $sname, $server := .Servers}}
server {{ $sname }} {{ $server.Address }}:{{ $server.Port }} check inter {{ $server.CheckInter}}{{end}}
{{end}}
Let's modify it to collect and expose statistics
# Generated HAProxy
{{ with .Global}}
global
daemon
pidfile /var/run/haproxy.pid
stats socket /var/run/haproxy.stat mode 777
# need to check logging
maxconn {{ .Maxconn }}
maxpipes {{ .Maxpipes }}
spread-checks {{ .SpreadChecks }}{{ if .Debug }}
debug{{ end }}{{ end }}
{{ with .Defaults }}
defaults
log global
mode {{ .Mode }}
balance {{ .Balance }}
maxconn {{ .Maxconn }}
{{ if .TCPLog }}option tcplog{{ end }}
{{ if .HTTPLog }}option httplog{{ end }}
{{ if .AbortOnClose }}option abortonclose{{ end }}
{{ if .HTTPServerClose }}option httpclose{{ end }}
{{ if .ForwardFor }}option forwardfor{{ end }}
retries {{ .Retries }}
{{ if .Redispatch }}option redispatch{{ end }}
timeout client {{ .TimeoutClient }}
timeout connect {{ .TimeoutConnect }}
timeout server {{ .TimeoutServer }}
{{ if .DontLogNull }}option dontlognull{{ end }}
timeout check {{ .TimeoutCheck }}
{{ end }}{{$certs_dir:= .CertsDir }}{{ range .Frontends }}
frontend {{ .Name }}{{ with .Bind }}
bind {{ .IP }}:{{ .Port }}{{ if .IsTLS }} ssl {{ range .Certs }}crt {{$certs_dir}}/{{.Name}}.pem {{ end }}{{ end }}{{ end }}{{ if .DefaultBackend.Backend }}
default_backend {{ .DefaultBackend.Backend }}{{end}}{{ range .ACLs }}
acl {{ .Name }} {{.Content}}{{end}}{{ range .UseBackendsByPrio }}
use_backend {{ .Backend }} if {{ range .ACLs }}{{ .Name }} {{end}}{{end}}
{{ end }}
{{range $name, $be := .Backends}}
backend {{$name}}{{ range $sname, $server := .Servers}}
server {{ $sname }} {{ $server.Address }}:{{ $server.Port }} check inter {{ $server.CheckInter}}{{end}}
{{end}}
listen stats :6543
mode http
stats enable
stats uri /stats
stats auth admin:changeme
With this other example, you get websockets suppot (NOT tested yet!!)
# Generated HAProxy
{{ with .Global}}
global
daemon
pidfile /var/run/haproxy.pid
stats socket /var/run/haproxy.stat mode 777
# need to check logging
maxconn {{ .Maxconn }}
maxpipes {{ .Maxpipes }}
spread-checks {{ .SpreadChecks }}{{ if .Debug }}
debug{{ end }}{{ end }}
{{ with .Defaults }}
defaults
log global
option http-server-close
mode {{ .Mode }}
balance {{ .Balance }}
maxconn {{ .Maxconn }}
{{ if .TCPLog }}option tcplog{{ end }}
{{ if .HTTPLog }}option httplog{{ end }}
{{ if .ForwardFor }}option forwardfor{{ end }}
retries {{ .Retries }}
{{ if .Redispatch }}option redispatch{{ end }}
timeout client {{ .TimeoutClient }}
timeout connect {{ .TimeoutConnect }}
timeout server {{ .TimeoutServer }}
{{ if .DontLogNull }}option dontlognull{{ end }}
timeout check {{ .TimeoutCheck }}
{{ end }}{{$certs_dir:= .CertsDir }}{{ range .Frontends }}
frontend {{ .Name }}{{ with .Bind }}
bind {{ .IP }}:{{ .Port }}{{ if .IsTLS }} ssl {{ range .Certs }}crt {{$certs_dir}}/{{.Name}}.pem {{ end }}{{ end }}{{ end }}{{ if .DefaultBackend.Backend }}
default_backend {{ .DefaultBackend.Backend }}{{end}}{{ range .ACLs }}
acl {{ .Name }} {{.Content}}{{end}}{{ range .UseBackendsByPrio }}
use_backend {{ .Backend }} if {{ range .ACLs }}{{ .Name }} {{end}}{{end}}
{{ end }}
{{range $name, $be := .Backends}}
backend {{$name}}{{ range $sname, $server := .Servers}}
server {{ $sname }} {{ $server.Address }}:{{ $server.Port }} check inter {{ $server.CheckInter}}{{end}}
{{end}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment