Lifecycle Callbacks — postStart and preStop hooks.
1apiVersion: apps/v12kind: Deployment3metadata:4 name: myapp5spec:6 replicas: 37 template:8 spec:9 terminationGracePeriodSeconds: 6010 containers:11 - name: app12 image: myapp:latest13 lifecycle:14 postStart:15 exec:16 command:17 - sh18 - -c19 - |20 echo "Container started at $(date)" > /tmp/startup.log21 wget -qO- http://config-server/config > /app/config22 echo "Config loaded"23 preStop:24 exec:25 command:26 - sh27 - -c28 - |29 # Drain connections30 curl -X POST http://localhost:8080/admin/drain31 # Wait for load balancer to remove32 sleep 1533 # Graceful shutdown34 kill -TERM 135 startupProbe:36 httpGet:37 path: /healthz38 port: 808039 failureThreshold: 3040 periodSeconds: 1041 readinessProbe:42 httpGet:43 path: /ready44 port: 808045 initialDelaySeconds: 546 periodSeconds: 1047 livenessProbe:48 httpGet:49 path: /healthz50 port: 808051 initialDelaySeconds: 1552 periodSeconds: 20
1# Check lifecycle hooks2kubectl describe pod my-pod | grep -A 15 "Lifecycle"34# View postStart logs5kubectl logs my-pod -c app6kubectl exec my-pod -- cat /tmp/startup.log78# Check preStop execution9kubectl get events --field-selector involvedObject.name=my-pod1011# Verify config was loaded12kubectl exec my-pod -- cat /app/config1314# Test termination behavior15kubectl delete pod my-pod && kubectl apply -f deployment.yaml
postStart:
preStop: