Skip to main content

Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-deployment
  namespace: production
spec:
  replicas: 3
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
        version: v1.0.0
    spec:
      containers:
        - name: web
          image: myrepo/web:v1.0.0
          ports:
            - containerPort: 8080
          resources:
            requests:
              memory: "128Mi"
              cpu: "100m"
            limits:
              memory: "256Mi"
              cpu: "500m"
          readinessProbe:
            httpGet:
              path: /health/ready
              port: 8080
            initialDelaySeconds: 10
            periodSeconds: 5
            successThreshold: 1
            failureThreshold: 3
          livenessProbe:
            httpGet:
              path: /health/live
              port: 8080
            initialDelaySeconds: 30
            periodSeconds: 10
            failureThreshold: 3
          lifecycle:
            preStop:
              exec:
                command: ["/bin/sh", "-c", "sleep 10"]

Estratégias de Deploy

RollingUpdate (default)

spec:
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1        # Pods extras permitido
      maxUnavailable: 0  # Pods indisponíveis permitido

Recreate

spec:
  strategy:
    type: Recreate

Blue-Green

---
apiVersion: apps/v1
kind: Service
metadata:
  name: web-service
spec:
  selector:
    app: web
    version: v1.0.0  # Aponta para versão atual
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-green
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
        version: v2.0.0

Canary

---
# Deployment canary (10% do tráfego)
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-canary
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web
      track: canary
  template:
    metadata:
      labels:
        app: web
        track: canary
        version: v2.0.0
---
# HPA para canary
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: web-canary-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: web-canary
  minReplicas: 1
  maxReplicas: 10
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 50

HorizontalPodAutoscaler (HPA)

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: web-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: web-deployment
  minReplicas: 2
  maxReplicas: 10
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 70
    - type: Resource
      resource:
        name: memory
        target:
          type: Utilization
          averageUtilization: 80
# Criar HPA
kubectl autoscale deployment web-deployment --cpu-percent=70 --min=2 --max=10

# Ver HPA
kubectl get hpa

# Horizontal Pod Autoscaler metrics
kubectl get hpa -o wide
kubectl describe hpa web-hpa

VerticalPodAutoscaler (VPA)

apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: web-vpa
spec:
  targetRef:
    apiVersion: "apps/v1"
    kind: Deployment
    name: web-deployment
  updatePolicy:
    updateMode: "Auto"
  resourcePolicy:
    containerPolicies:
      - containerName: '*'
        minAllowed:
          cpu: 50m
          memory: 64Mi
        maxAllowed:
          cpu: 2
          memory: 1Gi
        controlledResources: ["cpu", "memory"]

PodDisruptionBudget (PDB)

apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
  name: web-pdb
spec:
  minAvailable: 2
  # ou
  # maxUnavailable: 1
  selector:
    matchLabels:
      app: web

Rollback

# Ver histórico de revisions
kubectl rollout history deployment/web-deployment

# Rollback para versão anterior
kubectl rollout undo deployment/web-deployment

# Rollback para revision específica
kubectl rollout undo deployment/web-deployment --to-revision=2

# Ver status do rollout
kubectl rollout status deployment/web-deployment

Restart e Pause

# Restart deployment
kubectl rollout restart deployment/web-deployment

# Pausar rollout
kubectl rollout pause deployment/web-deployment

# Continuar rollout
kubectl rollout resume deployment/web-deployment

Scale

# Scale manualmente
kubectl scale deployment web-deployment --replicas=5

# Scale com HPA
kubectl autoscale deployment web-deployment --min=2 --max=10 --cpu-percent=70

Comandos

# Listar deployments
kubectl get deployments

# Ver detalhes
kubectl describe deployment web-deployment

# Editar deployment
kubectl edit deployment web-deployment

# Delete deployment
kubectl delete deployment web-deployment

# Logs do deployment
kubectl logs deployment/web-deployment

# Events
kubectl get events --field-selector involvedObject.name=web-deployment

Best Practices

  1. Sempre use readiness/liveness probes
  2. Configure recursos (CPU/memory limits)
  3. Use strategy RollingUpdate com maxUnavailable=0 para zero-downtime
  4. Configure PDB para alta disponibilidade
  5. Use HPA baseado em métricas relevantes
  6. Implemente preStop hook para graceful shutdown
  7. Configure terminationGracePeriodSeconds adequado