Kubernetes Production Best Practices

Production Kubernetes checklist: resources, probes, high availability, security, configuration, deployments, observability and cost control.

Running Kubernetes in production is mostly about habits: every workload declares what it needs, reports its health, survives failures, and is deployed in a repeatable way. This lesson turns the earlier lessons into a practical checklist.

Production Checklist at a Glance

Area Minimum standard
Resources Requests and limits on every container
Health Readiness, liveness and startup probes
Availability 2 or more replicas, spread across nodes, with a PodDisruptionBudget
Security Non-root, read-only filesystem, least-privilege RBAC, NetworkPolicy
Configuration ConfigMaps and Secrets, no values baked into images
Delivery Versioned images, Git-based manifests, safe rollout settings
Observability Metrics, central logs, alerts
Cost Right-sized requests, autoscaling, quotas

1. Declare Resources

Requests tell the scheduler how much to reserve, and limits cap usage. Requests determine placement and QoS class, so they should reflect real usage.

resources:
  requests:
    cpu: "250m"
    memory: "512Mi"
  limits:
    memory: "512Mi"        # memory limit equal to request avoids surprise evictions
  • Set a memory limit, because memory cannot be reclaimed without killing the process.
  • Many teams omit CPU limits to avoid throttling, while keeping CPU requests. Decide this deliberately.
  • Use LimitRange and ResourceQuota per namespace so a single team cannot take over the cluster.

2. Add Health Probes

startupProbe:
  httpGet: { path: /actuator/health/liveness, port: 8080 }
  failureThreshold: 30
  periodSeconds: 5
readinessProbe:
  httpGet: { path: /actuator/health/readiness, port: 8080 }
  periodSeconds: 10
livenessProbe:
  httpGet: { path: /actuator/health/liveness, port: 8080 }
  periodSeconds: 10
  failureThreshold: 3

Readiness controls traffic, liveness restarts a stuck container, and the startup probe protects slow-starting applications. Keep liveness checks free of external dependencies, so a database outage does not restart every Pod.

3. Design for High Availability

  • Run at least two replicas of every important service.
  • Spread replicas across nodes and zones.
  • Protect against voluntary disruptions such as node drains.
spec:
  replicas: 3
  template:
    spec:
      topologySpreadConstraints:
        - maxSkew: 1
          topologyKey: topology.kubernetes.io/zone
          whenUnsatisfiable: ScheduleAnyway
          labelSelector:
            matchLabels: { app: orders }
---
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
  name: orders
spec:
  minAvailable: 2
  selector:
    matchLabels: { app: orders }

4. Safe Deployments

strategy:
  type: RollingUpdate
  rollingUpdate:
    maxSurge: 25%
    maxUnavailable: 0        # keep full capacity during a rollout
minReadySeconds: 10
revisionHistoryLimit: 5
  • Use immutable, versioned image tags or digests, never latest.
  • Add a preStop hook or sleep and a suitable terminationGracePeriodSeconds so in-flight requests finish.
  • Keep manifests in Git and apply them through CI/CD or GitOps (Argo CD, Flux), not by hand.
  • Know the rollback command: kubectl rollout undo deployment/orders.

5. Autoscale

Use the Horizontal Pod Autoscaler for stateless services and a cluster autoscaler (or Karpenter) for nodes.

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: orders
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: orders
  minReplicas: 3
  maxReplicas: 12
  metrics:
    - type: Resource
      resource:
        name: cpu
        target: { type: Utilization, averageUtilization: 70 }

The HPA compares usage with the CPU request, so accurate requests are a prerequisite.

6. Secure the Workload

securityContext:
  runAsNonRoot: true
  runAsUser: 10001
  allowPrivilegeEscalation: false
  readOnlyRootFilesystem: true
  capabilities:
    drop: ["ALL"]
  seccompProfile:
    type: RuntimeDefault
  • Enforce Pod Security Admission, for example the restricted level on application namespaces.
  • Give each application its own ServiceAccount with least-privilege RBAC, and avoid wildcard permissions.
  • Apply a default-deny NetworkPolicy and allow only required traffic.
  • Scan images, pin base images, and run as non-root.
  • Remember that Secrets are base64-encoded, not encrypted. Enable encryption at rest and consider an external secrets manager (Vault, AWS Secrets Manager with External Secrets).
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: default-deny
  namespace: production
spec:
  podSelector: {}
  policyTypes: [Ingress, Egress]

7. Configuration and State

  • Keep configuration in ConfigMaps and Secrets, separate from images, and use one image across environments.
  • Prefer mounting Secrets as files over environment variables where possible.
  • Use StatefulSets with PersistentVolumeClaims for stateful workloads, and test backup and restore.
  • Back up etcd, or rely on your managed provider's control plane backups.

8. Observability

  • Expose metrics and scrape them with Prometheus; alert on error rate, latency and saturation.
  • Log to stdout in structured form and ship logs to central storage.
  • Use tracing (OpenTelemetry) for multi-service request flows.
  • Create runbooks for each alert so the on-call engineer knows what to do.

9. Organize and Label

Use namespaces to separate teams or environments, and use consistent labels:

metadata:
  labels:
    app.kubernetes.io/name: orders
    app.kubernetes.io/version: "1.4.2"
    app.kubernetes.io/part-of: shop
    app.kubernetes.io/managed-by: argocd

10. Cluster Operations

  • Upgrade Kubernetes regularly, one minor version at a time, and test in a lower environment first.
  • Use multiple nodes across availability zones, and keep node pools sized for your workloads.
  • Review cost with right-sizing, autoscaling and quotas; remove unused resources.
  • Test disaster recovery, not just backups.

Common Mistakes

  • No requests or limits. One noisy Pod can starve others, and the scheduler cannot place Pods well.
  • A single replica. Any node drain or crash causes downtime.
  • Using the latest tag. Deployments are not reproducible and rollbacks are unclear.
  • Liveness probes that depend on the database. A short outage restarts every Pod and makes it worse.
  • Running containers as root with a wide RBAC role. A compromised Pod becomes a cluster-level risk.
  • Changing resources with kubectl edit in production. The change is not in Git and is lost on the next deploy.

Best Practices

  • Treat the checklist above as a gate for every new service.
  • Keep all manifests in Git and deploy through a pipeline or GitOps.
  • Automate policy checks (Kyverno, OPA Gatekeeper) so unsafe manifests are rejected.
  • Practice failure: drain a node, kill a Pod, and run a restore test.
  • Document ownership, runbooks and on-call procedures for every service.

Interview Questions

1. What would you check before taking a service to production on Kubernetes?

Resource requests and limits, readiness and liveness probes, at least two replicas spread across nodes with a PodDisruptionBudget, a non-root security context, least-privilege RBAC, NetworkPolicies, externalized configuration, monitoring and alerts, and a tested rollback path.

2. How do you achieve zero-downtime deployments?

Use a RollingUpdate with maxUnavailable: 0, a correct readiness probe, enough replicas, a preStop delay and a suitable termination grace period so connections drain, and a PodDisruptionBudget. Verify with kubectl rollout status.

3. What is a PodDisruptionBudget and why do you need one?

It limits how many Pods of an application can be unavailable during voluntary disruptions such as node drains and upgrades. Without it, a drain could remove all replicas at once.

4. How do requests and limits affect scheduling and stability?

The scheduler places Pods using requests. Limits are enforced at runtime: exceeding a memory limit gets the container OOMKilled, and exceeding a CPU limit causes throttling. Requests and limits also decide the QoS class, which affects eviction order.

5. How do you secure a Kubernetes workload?

Run as non-root with a read-only root filesystem and dropped capabilities, apply Pod Security Admission, restrict permissions with RBAC, use default-deny NetworkPolicies, scan and pin images, and protect Secrets with encryption at rest or an external secrets manager.

6. Why should you avoid the latest image tag?

It is mutable, so the same manifest can run different code over time. Pods may run different versions after a restart, and rollbacks become unclear. Use versioned tags or immutable digests.

7. How do you manage configuration and secrets across environments?

Build one image and inject environment-specific values through ConfigMaps and Secrets, with manifests managed by Kustomize or Helm values in Git. Use an external secrets system so sensitive values are not stored in plain text in the repository.

Summary

  • Production readiness means resources, probes, redundancy, security, observability and safe delivery on every workload.
  • Use PodDisruptionBudgets, topology spread and autoscaling to stay available under change and load.
  • Keep everything in Git, use immutable image tags, and enforce policies automatically.
  • Test failure and recovery regularly rather than assuming they work.

Continue Learning

Previous: Kubernetes Troubleshooting

Was this guide helpful?

CodeWithVenu is free to read, with no paywall. If this guide saved you time or helped you prepare, a small voluntary contribution helps me write more practical tutorials, architecture guides, and interview-prep content.