Quando todos os pods estão indisponíveis, o Ingress mantém a requisição por até 99s aguardando o backend voltar, alcançando zero falhas visíveis ao usuário.
61 lines
2.0 KiB
YAML
61 lines
2.0 KiB
YAML
# NGINX Ingress com "Keep Request" - Mantém request até backend disponível
|
|
# Quando todos os pods estão indisponíveis, aguarda até 99s antes de retornar 503
|
|
apiVersion: networking.k8s.io/v1
|
|
kind: Ingress
|
|
metadata:
|
|
name: node-bugado-nginx
|
|
annotations:
|
|
# Location interno que aguarda o backend ficar disponível
|
|
nginx.ingress.kubernetes.io/server-snippet: |
|
|
error_page 502 503 504 = @wait_for_backend;
|
|
|
|
location @wait_for_backend {
|
|
internal;
|
|
content_by_lua_block {
|
|
local max_wait = 99
|
|
local interval = 1
|
|
local start = ngx.now()
|
|
local backend_host = "node-bugado.default.svc.cluster.local"
|
|
local backend_port = 3000
|
|
|
|
ngx.log(ngx.INFO, "[KeepRequest] Aguardando backend por ate ", max_wait, "s...")
|
|
|
|
while (ngx.now() - start) < max_wait do
|
|
local sock = ngx.socket.tcp()
|
|
sock:settimeout(1000)
|
|
|
|
local ok, err = sock:connect(backend_host, backend_port)
|
|
if ok then
|
|
sock:close()
|
|
local elapsed = math.floor(ngx.now() - start)
|
|
ngx.log(ngx.INFO, "[KeepRequest] Backend ready apos ", elapsed, "s")
|
|
-- Redirect interno (não retorna ao cliente)
|
|
return ngx.exec(ngx.var.request_uri)
|
|
end
|
|
|
|
ngx.log(ngx.WARN, "[KeepRequest] Backend indisponivel: ", err)
|
|
ngx.sleep(interval)
|
|
end
|
|
|
|
ngx.status = 503
|
|
ngx.header["Content-Type"] = "text/plain"
|
|
ngx.say("Servico indisponivel apos ", max_wait, "s de espera")
|
|
ngx.exit(503)
|
|
}
|
|
}
|
|
# Timeouts curtos para falhar rápido e ir para o handler Lua
|
|
nginx.ingress.kubernetes.io/proxy-connect-timeout: "2"
|
|
nginx.ingress.kubernetes.io/proxy-read-timeout: "3"
|
|
spec:
|
|
ingressClassName: nginx
|
|
rules:
|
|
- http:
|
|
paths:
|
|
- path: /
|
|
pathType: Prefix
|
|
backend:
|
|
service:
|
|
name: node-bugado
|
|
port:
|
|
number: 3000
|