From 8bf7007c4093e8fb9ab2f9c0507cb50131785110 Mon Sep 17 00:00:00 2001 From: Manuel de Brito Fontes Date: Thu, 28 Apr 2016 01:03:59 -0300 Subject: [PATCH] Add support for sticky sessions --- controllers/nginx/controller.go | 2 ++ controllers/nginx/nginx.tmpl | 11 ++++++----- controllers/nginx/nginx/main.go | 5 +++++ 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/controllers/nginx/controller.go b/controllers/nginx/controller.go index 04ded6aa1..40f835442 100644 --- a/controllers/nginx/controller.go +++ b/controllers/nginx/controller.go @@ -694,6 +694,8 @@ func (lbc *loadBalancerController) getUpstreamServers(data []interface{}) ([]*ng return aUpstreams, aServers } +// createUpstreams creates the NGINX upstreams for each service referenced in +// Ingress rules. The servers inside the upstream are endpoints. func (lbc *loadBalancerController) createUpstreams(data []interface{}) map[string]*nginx.Upstream { upstreams := make(map[string]*nginx.Upstream) diff --git a/controllers/nginx/nginx.tmpl b/controllers/nginx/nginx.tmpl index 3b413b7ac..77ff2ac21 100644 --- a/controllers/nginx/nginx.tmpl +++ b/controllers/nginx/nginx.tmpl @@ -141,9 +141,13 @@ http { {{range $name, $upstream := .upstreams}} upstream {{$upstream.Name}} { + {{ if $cfg.enableStickySessions }} + sticky hash=sha1 httponly; + {{ else }} least_conn; - {{range $server := $upstream.Backends}}server {{$server.Address}}:{{$server.Port}}; - {{end}} + {{ end }} + {{ range $server := $upstream.Backends }}server {{ $server.Address }}:{{ $server.Port }}; + {{ end }} } {{end}} @@ -153,9 +157,6 @@ http { {{ if $server.SSL }}listen 443{{ if $cfg.useProxyProtocol }} proxy_protocol{{ end }} ssl http2; ssl_certificate {{ $server.SSLCertificate }}; ssl_certificate_key {{ $server.SSLCertificateKey }};{{ end }} - {{ if $cfg.enableVtsStatus }} - vhost_traffic_status_filter_by_set_key {{ $server.Name }} application::*; - {{ end }} server_name {{ $server.Name }}; diff --git a/controllers/nginx/nginx/main.go b/controllers/nginx/nginx/main.go index 8dfc892dc..28d1f2156 100644 --- a/controllers/nginx/nginx/main.go +++ b/controllers/nginx/nginx/main.go @@ -89,6 +89,11 @@ type nginxConfiguration struct { // Sets the maximum allowed size of the client request body BodySize string `structs:"body-size,omitempty"` + // EnableStickySessions enabled sticky sessions using cookies + // https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng + // By default this is disabled + EnableStickySessions bool `structs:"enable-sticky-sessions,omitempty"` + // EnableVtsStatus allows the replacement of the default status page with a third party module named // nginx-module-vts - https://github.com/vozlt/nginx-module-vts // By default this is disabled