aula-04: NGINX Ingress com Keep Request (Lua)

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.
This commit is contained in:
Allyson de Paula
2025-12-25 13:38:46 -03:00
parent a479bf3696
commit 9e834de48d
6 changed files with 245 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
# 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