Where to find generated config for nginx.ingress.kubernetes.io/affinity annotation

6/19/2019

When setting the following annotations:

nginx.ingress.kubernetes.io/affinity: "cookie"
nginx.ingress.kubernetes.io/session-cookie-name: "ALPHA"
nginx.ingress.kubernetes.io/session-cookie-path: /

Where do they end up in nginx.conf?

I'm comparing nginx.conf before and after by using a difftool but the config is identical.

If I e.g. add a:

nginx.ingress.kubernetes.io/rewrite-target /$1

nginx.conf gets updated.

Results in:

rewrite "(?i)/myapp(/|$)(.*)" /$2 break;
-- PussInBoots
kubernetes
kubernetes-ingress
nginx
nginx-ingress

1 Answer

6/19/2019

The short answer is that these settings exist in memory of the lua nginx module used by nginx-ingress.

The longer answer and explanation of how this works is in the documentation at https://kubernetes.github.io/ingress-nginx/how-it-works. Particularly:

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 to learn more about how it's done.

The referenced below section then mentions:

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 context detects what endpoints it should choose upstream peer from and applies the configured load balancing algorithm to choose the peer.

The backend object in question has the session and cookie information. The code for receiving this is at https://github.com/kubernetes/ingress-nginx/blob/57a0542fa356c49a6afb762cddf0c7dbf0b156dd/rootfs/etc/nginx/lua/balancer/sticky.lua#L151-L166. In particular, there is this line in the sync function:

ngx_log(INFO, string_format("[%s] nodes have changed for backend %s", self.name, backend.name))

Which indicates you should see a log entry for the change in the nginx log when making a change like this to the backends.

-- Andy Shinn
Source: StackOverflow