upstream upstream_balancer { ### Attention!!! # # We no longer create "upstream" section for every backend. # Backends are handled dynamically using Lua. If you would like to debug # and see what backends ingress-nginx has in its memory you can # install our kubectl plugin https://kubernetes.github.io/ingress-nginx/kubectl-plugin. # Once you have the plugin you can use "kubectl ingress-nginx backends" command to # inspect current backends. # ### server0.0.0.1; # placeholder balancer_by_lua_block { balancer.balance() } keepalive32; keepalive_timeout60s; keepalive_requests100; }
// internal/ingress/controller/controller.go // syncIngress collects all the pieces required to assemble the NGINX // configuration file and passes the resulting data structures to the backend // (OnUpdate) when a reload is deemed necessary. func(n *NGINXController) syncIngress(interface{}) error { // ... // 获取每个应用的upstream信息 hosts, servers, pcfg := n.getConfiguration(ings)
// 发送请求的代码比较简单, 就直接贴到下面了 // internal/nginx/main.go // NewPostStatusRequest creates a new POST request to the internal NGINX status server funcNewPostStatusRequest(path, contentType string, data interface{}) (int, []byte, error) { url := fmt.Sprintf("http://127.0.0.1:%v%v", StatusPort, path)
-- rootfs/etc/nginx/lua/configuration.lua function_M.call() if ngx.var.request_method ~= "POST"and ngx.var.request_method ~= "GET"then ngx.status = ngx.HTTP_BAD_REQUEST ngx.print("Only POST and GET requests are allowed!") return end
if ngx.var.request_uri == "/configuration/servers"then handle_servers() return end
if ngx.var.request_uri == "/configuration/general"then handle_general() return end
if ngx.var.uri == "/configuration/certs"then handle_certs() return end
if ngx.var.request_uri ~= "/configuration/backends"then ngx.status = ngx.HTTP_NOT_FOUND ngx.print("Not found!") return end
-- 这里, 如果是以GET请求发送, 你可以获得当前的backend数据 if ngx.var.request_method == "GET"then ngx.status = ngx.HTTP_OK ngx.print(_M.get_backends_data()) return end
-- 非GET请求会执行下面的语句 local backends = fetch_request_body() ifnot backends then ngx.log(ngx.ERR, "dynamic-configuration: unable to read valid request body") ngx.status = ngx.HTTP_BAD_REQUEST return end
-- 这里对backend进行修改. local success, err = configuration_data:set("backends", backends) ifnot success then ngx.log(ngx.ERR, "dynamic-configuration: error updating configuration: " .. tostring(err)) ngx.status = ngx.HTTP_BAD_REQUEST return end