Add VIRTUAL_WEIGHT for Load Balancing

This commit is contained in:
Niklas Keller 2015-10-09 09:38:12 +02:00
parent 495b0ad8b6
commit 5e98c61f65

View file

@ -3,11 +3,11 @@
{{/* If we got the containers from swarm and this container's port is published to host, use host IP:PORT */}}
{{ if and .Container.Node.ID .Address.HostPort }}
# {{ .Container.Node.Name }}/{{ .Container.Name }}
server {{ .Container.Node.Address.IP }}:{{ .Address.HostPort }};
server {{ .Container.Node.Address.IP }}:{{ .Address.HostPort }} weight={{ .Weight }};
{{/* If there is no swarm node or the port is not published on host, use container's IP:PORT */}}
{{ else }}
# {{ .Container.Name }}
server {{ .Address.IP }}:{{ .Address.Port }};
server {{ .Address.IP }}:{{ .Address.Port }} weight={{ .Weight }};
{{ end }}
{{ else }}
# {{ .Container.Name }}
@ -74,16 +74,21 @@ server {
upstream {{ $host }} {
{{ range $container := $containers }}
{{ if $container.Env.VIRTUAL_WEIGHT }}
{{ $weight := $container.Env.VIRTUAL_WEIGHT }}
{{ else }}
{{ $weight := 1 }}
{{end }}
{{ $addrLen := len $container.Addresses }}
{{/* If only 1 port exposed, use that */}}
{{ if eq $addrLen 1 }}
{{ $address := index $container.Addresses 0 }}
{{ template "upstream" (dict "Container" $container "Address" $address) }}
{{ template "upstream" (dict "Container" $container "Address" $address "Weight" $weight) }}
{{/* If more than one port exposed, use the one matching VIRTUAL_PORT env var, falling back to standard web port 80 */}}
{{ else }}
{{ $port := coalesce $container.Env.VIRTUAL_PORT "80" }}
{{ $address := where $container.Addresses "Port" $port | first }}
{{ template "upstream" (dict "Container" $container "Address" $address) }}
{{ template "upstream" (dict "Container" $container "Address" $address "Weight" $weight) }}
{{ end }}
{{ end }}
}