diff --git a/docs/user-guide/nginx-configuration/configmap.md b/docs/user-guide/nginx-configuration/configmap.md index cd0e8035d..1849d4bd4 100644 --- a/docs/user-guide/nginx-configuration/configmap.md +++ b/docs/user-guide/nginx-configuration/configmap.md @@ -585,12 +585,13 @@ Sets the algorithm to use for load balancing. The value can either be: - round_robin: to use the default round robin loadbalancer -- least_conn: to use the least connected method (_note_ that this is available only in non-dynamic mode: `--enable-dynamic-configuration=false`) -- ip_hash: to use a hash of the server for routing (_note_ that this is available only in non-dynamic mode: `--enable-dynamic-configuration=false`, but alternatively you can consider using `nginx.ingress.kubernetes.io/upstream-hash-by`) - ewma: to use the Peak EWMA method for routing ([implementation](https://github.com/kubernetes/ingress-nginx/blob/master/rootfs/etc/nginx/lua/balancer/ewma.lua)) The default is `round_robin`. +- To load balance using consistent hashing of IP or other variables, consider the `nginx.ingress.kubernetes.io/upstream-hash-by` annotation. +- To load balance using session cookies, consider the `nginx.ingress.kubernetes.io/affinity` annotation. + _References:_ [http://nginx.org/en/docs/http/load_balancing.html](http://nginx.org/en/docs/http/load_balancing.html) diff --git a/internal/ingress/controller/controller.go b/internal/ingress/controller/controller.go index 5a63fd335..90f789770 100644 --- a/internal/ingress/controller/controller.go +++ b/internal/ingress/controller/controller.go @@ -619,18 +619,16 @@ func (n *NGINXController) createUpstreams(data []*ingress.Ingress, du *ingress.B klog.V(3).Infof("Creating upstream %q", defBackend) upstreams[defBackend] = newUpstream(defBackend) - if upstreams[defBackend].SecureCACert.Secret == "" { - upstreams[defBackend].SecureCACert = anns.SecureUpstream.CACert - } - if upstreams[defBackend].UpstreamHashBy.UpstreamHashBy == "" { - upstreams[defBackend].UpstreamHashBy.UpstreamHashBy = anns.UpstreamHashBy.UpstreamHashBy - upstreams[defBackend].UpstreamHashBy.UpstreamHashBySubset = anns.UpstreamHashBy.UpstreamHashBySubset - upstreams[defBackend].UpstreamHashBy.UpstreamHashBySubsetSize = anns.UpstreamHashBy.UpstreamHashBySubsetSize - } + upstreams[defBackend].SecureCACert = anns.SecureUpstream.CACert + upstreams[defBackend].UpstreamHashBy.UpstreamHashBy = anns.UpstreamHashBy.UpstreamHashBy + upstreams[defBackend].UpstreamHashBy.UpstreamHashBySubset = anns.UpstreamHashBy.UpstreamHashBySubset + upstreams[defBackend].UpstreamHashBy.UpstreamHashBySubsetSize = anns.UpstreamHashBy.UpstreamHashBySubsetSize + + upstreams[defBackend].LoadBalancing = anns.LoadBalancing if upstreams[defBackend].LoadBalancing == "" { - upstreams[defBackend].LoadBalancing = anns.LoadBalancing + upstreams[defBackend].LoadBalancing = n.store.GetBackendConfiguration().LoadBalancing } svcKey := fmt.Sprintf("%v/%v", ing.Namespace, ing.Spec.Backend.ServiceName) @@ -687,18 +685,15 @@ func (n *NGINXController) createUpstreams(data []*ingress.Ingress, du *ingress.B upstreams[name] = newUpstream(name) upstreams[name].Port = path.Backend.ServicePort - if upstreams[name].SecureCACert.Secret == "" { - upstreams[name].SecureCACert = anns.SecureUpstream.CACert - } + upstreams[name].SecureCACert = anns.SecureUpstream.CACert - if upstreams[name].UpstreamHashBy.UpstreamHashBy == "" { - upstreams[name].UpstreamHashBy.UpstreamHashBy = anns.UpstreamHashBy.UpstreamHashBy - upstreams[name].UpstreamHashBy.UpstreamHashBySubset = anns.UpstreamHashBy.UpstreamHashBySubset - upstreams[name].UpstreamHashBy.UpstreamHashBySubsetSize = anns.UpstreamHashBy.UpstreamHashBySubsetSize - } + upstreams[name].UpstreamHashBy.UpstreamHashBy = anns.UpstreamHashBy.UpstreamHashBy + upstreams[name].UpstreamHashBy.UpstreamHashBySubset = anns.UpstreamHashBy.UpstreamHashBySubset + upstreams[name].UpstreamHashBy.UpstreamHashBySubsetSize = anns.UpstreamHashBy.UpstreamHashBySubsetSize + upstreams[name].LoadBalancing = anns.LoadBalancing if upstreams[name].LoadBalancing == "" { - upstreams[name].LoadBalancing = anns.LoadBalancing + upstreams[name].LoadBalancing = n.store.GetBackendConfiguration().LoadBalancing } svcKey := fmt.Sprintf("%v/%v", ing.Namespace, path.Backend.ServiceName) diff --git a/test/e2e/loadbalance/configmap.go b/test/e2e/loadbalance/configmap.go new file mode 100644 index 000000000..abd4c500a --- /dev/null +++ b/test/e2e/loadbalance/configmap.go @@ -0,0 +1,72 @@ +/* +Copyright 2018 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package loadbalance + +import ( + "encoding/json" + "strings" + "time" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + + "k8s.io/ingress-nginx/test/e2e/framework" +) + +const ( + waitForLuaSync = 5 * time.Second +) + +var _ = framework.IngressNginxDescribe("Load Balance - Configmap value", func() { + f := framework.NewDefaultFramework("lb-configmap") + + BeforeEach(func() { + f.NewEchoDeploymentWithReplicas(1) + }) + + AfterEach(func() { + }) + + It("should apply the configmap load-balance setting", func() { + host := "load-balance.com" + + f.UpdateNginxConfigMapData("load-balance", "ewma") + + f.EnsureIngress(framework.NewSingleIngress(host, "/", host, f.Namespace, "http-svc", 80, nil)) + f.WaitForNginxServer(host, + func(server string) bool { + return strings.Contains(server, "server_name load-balance.com") + }) + time.Sleep(waitForLuaSync) + + getCmd := "/dbg backends all" + output, err := f.ExecIngressPod(getCmd) + Expect(err).Should(BeNil()) + + var backends []map[string]interface{} + unmarshalErr := json.Unmarshal([]byte(output), &backends) + Expect(unmarshalErr).Should(BeNil()) + + for _, backend := range backends { + if backend["name"].(string) != "upstream-default-backend" { + lb, ok := backend["load-balance"].(string) + Expect(ok).Should(Equal(true)) + Expect(lb).Should(Equal("ewma")) + } + } + }) +}) diff --git a/test/e2e/loadbalance/round_robin.go b/test/e2e/loadbalance/round_robin.go index e64abbc13..5b16f2ad9 100644 --- a/test/e2e/loadbalance/round_robin.go +++ b/test/e2e/loadbalance/round_robin.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package settings +package loadbalance import ( "regexp"