aula-02: Kubernetes com liveness probe

Deployment com liveness probe para detectar quando a app
trava e reiniciar o container automaticamente.
This commit is contained in:
Allyson de Paula
2025-12-25 13:32:29 -03:00
parent 1ea1a98e60
commit b9ab2d281f
3 changed files with 96 additions and 0 deletions

43
aula-02/configmap.yaml Normal file
View File

@@ -0,0 +1,43 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
MAX_REQUESTS: "3"
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");
});

41
aula-02/deployment.yaml Normal file
View File

@@ -0,0 +1,41 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: node-bugado
spec:
replicas: 1
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
livenessProbe:
httpGet:
path: /health
port: 3000
initialDelaySeconds: 5 # Espera para começar a testar
periodSeconds: 2 # A cada 5 segs faz o teste novamente
failureThreshold: 2 # Vezes seguidas antes de ser considerado morto
volumeMounts:
- name: app-volume
mountPath: /app
volumes:
- name: app-volume
configMap:
name: app-config

12
aula-02/service.yaml Normal file
View File

@@ -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