From a479bf3696d5c5f4493c2e26ea54d693379608c9 Mon Sep 17 00:00:00 2001 From: Allyson de Paula Date: Thu, 25 Dec 2025 13:37:33 -0300 Subject: [PATCH] aula-03: Alta disponibilidade com replicas e readiness probe MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Múltiplas réplicas com readiness probe para remover pods não-saudáveis do Service enquanto liveness probe os reinicia. --- aula-03/configmap.yaml | 43 +++++++++++++++++++++++++++++++++++++ aula-03/deployment.yaml | 47 +++++++++++++++++++++++++++++++++++++++++ aula-03/service.yaml | 12 +++++++++++ 3 files changed, 102 insertions(+) create mode 100644 aula-03/configmap.yaml create mode 100644 aula-03/deployment.yaml create mode 100644 aula-03/service.yaml diff --git a/aula-03/configmap.yaml b/aula-03/configmap.yaml new file mode 100644 index 0000000..4946d2f --- /dev/null +++ b/aula-03/configmap.yaml @@ -0,0 +1,43 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: app-config +data: + MAX_REQUESTS: "10" + + app.js: | + const http = require("http"); + + const MAX_REQUESTS = Number(process.env.MAX_REQUESTS || 3); + + let requestCount = 0; + + console.log("MAX_REQUESTS =", MAX_REQUESTS); + + const server = http.createServer((req, res) => { + if (req.url === "/health") { + if (requestCount > MAX_REQUESTS) { + console.log('❌ O health check falhou'); + return; + } + + res.writeHead(200); + res.end(`ok`); + return; + } + + requestCount++; + console.log("request", requestCount); + + if (requestCount > MAX_REQUESTS) { + console.log(`🔥 App travado após ${MAX_REQUESTS} requests`); + return; + } + + res.writeHead(200); + res.end(`Req -> ${requestCount}/${MAX_REQUESTS}`); + }); + + server.listen(3000, () => { + console.log("App rodando na porta 3000"); + }); diff --git a/aula-03/deployment.yaml b/aula-03/deployment.yaml new file mode 100644 index 0000000..6b645e2 --- /dev/null +++ b/aula-03/deployment.yaml @@ -0,0 +1,47 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: node-bugado +spec: + replicas: 5 # Alta disponibilidade: enquanto um reinicia, outros atendem + selector: + matchLabels: + app: node-bugado + template: + metadata: + labels: + app: node-bugado + spec: + terminationGracePeriodSeconds: 5 + containers: + - name: app + image: node:24-alpine + command: ["node", "/app/app.js"] + env: + - name: MAX_REQUESTS + valueFrom: + configMapKeyRef: + name: app-config + key: MAX_REQUESTS + ports: + - containerPort: 3000 + readinessProbe: + httpGet: + path: /health + port: 3000 + periodSeconds: 1 # Verifica a cada 1 segundo + failureThreshold: 1 # 1 falha = remove do Service + livenessProbe: + httpGet: + path: /health + port: 3000 + initialDelaySeconds: 2 # Espera para começar a testar + periodSeconds: 1 # A cada 1 seg faz o teste novamente + failureThreshold: 2 # 2 falhas = reinicia o container + volumeMounts: + - name: app-volume + mountPath: /app + volumes: + - name: app-volume + configMap: + name: app-config diff --git a/aula-03/service.yaml b/aula-03/service.yaml new file mode 100644 index 0000000..91b8792 --- /dev/null +++ b/aula-03/service.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Service +metadata: + name: node-bugado +spec: + type: LoadBalancer + selector: + app: node-bugado + ports: + - port: 3000 + targetPort: 3000 + protocol: TCP