From 5e98c61f65d2f52d02510e93ccf09596561a66d5 Mon Sep 17 00:00:00 2001 From: Niklas Keller Date: Fri, 9 Oct 2015 09:38:12 +0200 Subject: [PATCH 1/4] Add VIRTUAL_WEIGHT for Load Balancing --- nginx.tmpl | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/nginx.tmpl b/nginx.tmpl index 6300d89..13b8d99 100644 --- a/nginx.tmpl +++ b/nginx.tmpl @@ -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 }} } From e583285351f70cf6311771b413c2a064ff3dfbd4 Mon Sep 17 00:00:00 2001 From: Niklas Keller Date: Sat, 10 Oct 2015 01:19:06 +0200 Subject: [PATCH 2/4] Fix weight visibility / scope --- nginx.tmpl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/nginx.tmpl b/nginx.tmpl index 13b8d99..389683c 100644 --- a/nginx.tmpl +++ b/nginx.tmpl @@ -74,10 +74,9 @@ server { upstream {{ $host }} { {{ range $container := $containers }} + {{ $weight := 1 }} {{ 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 */}} From cecac88a71af392bbbe21bcb0262399b599a4c73 Mon Sep 17 00:00:00 2001 From: Niklas Keller Date: Sat, 10 Oct 2015 12:58:18 +0200 Subject: [PATCH 3/4] Use 'or' function instead of replacing an already defined variable --- nginx.tmpl | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/nginx.tmpl b/nginx.tmpl index 389683c..93d57b5 100644 --- a/nginx.tmpl +++ b/nginx.tmpl @@ -74,10 +74,7 @@ server { upstream {{ $host }} { {{ range $container := $containers }} - {{ $weight := 1 }} - {{ if $container.Env.VIRTUAL_WEIGHT }} - {{ $weight := $container.Env.VIRTUAL_WEIGHT }} - {{end }} + {{ $weight := or ($container.Env.VIRTUAL_WEIGHT) "1" }} {{ $addrLen := len $container.Addresses }} {{/* If only 1 port exposed, use that */}} {{ if eq $addrLen 1 }} From 5c8ff086f17454d7f1b8c3cd9dac6bbecdb65a23 Mon Sep 17 00:00:00 2001 From: Niklas Keller Date: Wed, 19 Oct 2016 09:18:55 +0200 Subject: [PATCH 4/4] Fix whitespace issues after merge --- nginx.tmpl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nginx.tmpl b/nginx.tmpl index c48c652..bc0a69f 100644 --- a/nginx.tmpl +++ b/nginx.tmpl @@ -91,12 +91,12 @@ upstream {{ $host }} { {{/* If only 1 port exposed, use that */}} {{ if eq $addrLen 1 }} {{ $address := index $container.Addresses 0 }} - {{ template "upstream" (dict "Container" $container "Address" $address "Network" $containerNetwork "Weight" $weight) }} + {{ template "upstream" (dict "Container" $container "Address" $address "Network" $containerNetwork "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 "Network" $containerNetwork "Weight" $weight) }} + {{ template "upstream" (dict "Container" $container "Address" $address "Network" $containerNetwork "Weight" $weight) }} {{ end }} {{ end }} {{ end }}