K8

Kubernetes Resource Patterns

Deployment, Service, ConfigMap, and resource management patterns

Details

Language / Topic
kubernetesKubernetes
Category
Architecture

Rules

balanced
- Always set resource requests AND limits for CPU and memory on every container
- Use Deployments for stateless workloads, StatefulSets for databases, DaemonSets for node-level agents
- Define readiness and liveness probes for every container — prevent traffic routing to unhealthy pods
- Use ConfigMaps for non-sensitive configuration; mount as files or inject as environment variables
- Set pod anti-affinity to spread replicas across nodes for high availability
- Set resource requests (scheduling guarantee) and limits (hard cap) for CPU and memory on every container
- Workload types: Deployment (stateless), StatefulSet (databases/ordered), DaemonSet (per-node), Job/CronJob (batch)
- Probes: readinessProbe (traffic routing), livenessProbe (restart on hang), startupProbe (slow-starting apps)
- ConfigMaps for config files and env vars; Secrets for sensitive data; mount as volumes for hot-reload
- Pod anti-affinity (preferredDuringScheduling) to spread replicas across nodes and availability zones
- Use Horizontal Pod Autoscaler (HPA) based on CPU, memory, or custom metrics for automatic scaling
- Set terminationGracePeriodSeconds appropriately — allow time for in-flight requests to complete
- Use preStop hooks for graceful shutdown: sleep 5 to allow Service endpoint removal before SIGTERM
- Label everything consistently: app.kubernetes.io/name, /version, /component, /part-of, /managed-by
- Use namespaces to isolate environments and teams; apply resource quotas per namespace
- Define PodDisruptionBudgets for all production workloads: minAvailable or maxUnavailable