From c238a1838c1f5386bfb014b27117612e94603f85 Mon Sep 17 00:00:00 2001 From: Elvin Efendi Date: Sat, 28 Jul 2018 15:55:14 -0400 Subject: [PATCH] describe under what circumstances and how we avoid Nginx reload --- docs/how-it-works.md | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/docs/how-it-works.md b/docs/how-it-works.md index 8049dc2c37..369d7fb183 100644 --- a/docs/how-it-works.md +++ b/docs/how-it-works.md @@ -1,16 +1,16 @@ # How it works -The objective of this document explains how the NGINX Ingress controller works, in particular how the NGINX model is built and why we need a one. +The objective of this document is to explain how the NGINX Ingress controller works, in particular how the NGINX model is built and why we need a one. ## NGINX configuration -The goal of this Ingress controller is the assembly of a configuration file (nginx.conf). The main implication of this requirement is the need to reload NGINX after any change in the configuration file. +The goal of this Ingress controller is the assembly of a configuration file (nginx.conf). The main implication of this requirement is the need to reload NGINX after any change in the configuration file. _Though it is important to note that we don't reload Nginx on changes that impact only an `upstream` configuration (i.e Endpoints change when you deploy your app)_. We use https://github.com/openresty/lua-nginx-module to achieve this. Check [below](#avoiding-reloads-on-endpoints-changes) to learn more about how it's done. ## NGINX model Usually, a Kubernetes Controller utilizes the [synchronization loop pattern](1) to check if the desired state in the controller is updated or a change is required. To this purpose, we need to build a model using different objects from the cluster, in particular (in no special order) Ingresses, Services, Endpoints, Secrets, and Configmaps to generate a point in time configuration file that reflects the state of the cluster. -To get this object from the cluster, we use [Kubernetes Informers](2), in particular, `FilteredSharedInformer`. This informers allows reacting to changes in using [callbacks](3) to individual changes when a new object is added, modified or removed. Unfortunately, there is no way to know if a particular change is going to affect the final configuration file. Therefore on every change, we have to rebuild a new model from scratch based on the state of cluster and compare it to the current model. If the new model equals to the current one, then we avoid generating a new NGINX configuration and [trigger a reload](7). Otherwise, we create a new NGINX configuration based on the new model, replace the current model and [trigger a reload](7). +To get this object from the cluster, we use [Kubernetes Informers](2), in particular, `FilteredSharedInformer`. This informers allows reacting to changes in using [callbacks](3) to individual changes when a new object is added, modified or removed. Unfortunately, there is no way to know if a particular change is going to affect the final configuration file. Therefore on every change, we have to rebuild a new model from scratch based on the state of cluster and compare it to the current model. If the new model equals to the current one, then we avoid generating a new NGINX configuration and triggering a reload. Otherwise, we check if the difference is only about Endpoints. If so we then send the new list of Endpoints to a Lua handler running inside Nginx using HTTP POST request and again avoid generating a new NGINX configuration and triggering a reload. If the difference between running and new model is about more than just Endpoints we create a new NGINX configuration based on the new model, replace the current model and trigger a reload. One of the uses of the model is to avoid unnecessary reloads when there's no change in the state and to detect conflicts in definitions. @@ -39,16 +39,21 @@ The next list describes the scenarios when a reload is required: - New Ingress Resource Created. - TLS section is added to existing Ingress. -- Change in Ingress annotations. +- Change in Ingress annotations that impacts more than just upstream configuration. For instance `load-balance` annotation does not require a reload. - A path is added/removed from an Ingress. - An Ingress, Service, Secret is removed. -- Some missing referenced object from the Ingress is available, like a Service, Secret or Endpoint. +- Some missing referenced object from the Ingress is available, like a Service or Secret. - A Secret is updated. ## Avoiding reloads In some cases, it is possible to avoid reloads, in particular when there is a change in the endpoints, i.e., a pod is started or replaced. It is out of the scope of this Ingress controller to remove reloads completely. This would require an incredible amount of work and at some point makes no sense. This can change only if NGINX changes the way new configurations are read, basically, new changes do not replace worker processes. +### Avoiding reloads on Endpoints changes +On every endpoint change the controller fetches endpoints from all the services it sees and generates corresponding Backend objects. It then sends these objects to a Lua handler running inside Nginx. The Lua code in turn stores those backends in a shared memory zone. Then for every request Lua code running in [`balancer_by_lua`](https://github.com/openresty/lua-resty-core/blob/master/lib/ngx/balancer.md) context detects what endpoints it should choose upstream peer from and applies the configured load balancing algorithm to choose the peer. Then Nginx takes care of the rest. This way we avoid reloading Nginx on endpoint changes. _Note_ that this includes annotation changes that affects only `upstream` configuration in Nginx as well. + +In a relatively big clusters with frequently deploying apps this feature saves significant number of Nginx reloads which can otherwise affect response latency, load balancing quality (after every reload Nginx resets the state of load balancing) and so on. + [0]: https://github.com/openresty/lua-nginx-module/pull/1259 [1]: https://coreos.com/kubernetes/docs/latest/replication-controller.html#the-reconciliation-loop-in-detail [2]: https://godoc.org/k8s.io/client-go/informers#NewFilteredSharedInformerFactory @@ -56,4 +61,4 @@ In some cases, it is possible to avoid reloads, in particular when there is a ch [4]: https://github.com/kubernetes/ingress-nginx/blob/master/internal/task/queue.go#L38 [5]: https://golang.org/pkg/sync/#Mutex [6]: https://github.com/kubernetes/ingress-nginx/blob/master/rootfs/etc/nginx/template/nginx.tmpl -[7]: http://nginx.org/en/docs/beginners_guide.html#control \ No newline at end of file +[7]: http://nginx.org/en/docs/beginners_guide.html#control