Running Production-Ready Kubernetes Deployments
From kubectl apply demos to repeatable production rollouts: declarative manifests, probes, resource budgets, RBAC, namespaces, and rollout strategy.
- Difficulty
- intermediate
- Prerequisites
- A staging cluster, kubectl configured
- Tools
- Kubernetes, Prometheus
On this page
A kubectl apply in a demo is not a deployment strategy. In production, the difference between a quiet rollout and an outage is usually how you declare the desired state, how you probe it, and how you roll it out. This article maps the pieces a production Kubernetes deployment actually needs before it gets real traffic.
Declare the desired state in git
Kubernetes is declarative by design. The live state you inspect with kubectl get is reconciled from manifests stored in git. Anything created imperatively will drift, and nobody will know why the cluster changed when it did.
apiVersion: apps/v1
kind: Deployment
metadata:
name: api
namespace: production
spec:
replicas: 3
revisionHistoryLimit: 5
progressDeadlineSeconds: 600
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 0
maxSurge: 1
selector:
matchLabels:
app: api
template:
metadata:
labels:
app: api
spec:
containers:
- name: api
image: registry.internal/api:1.4.0
ports:
- containerPort: 8080
readinessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 10
periodSeconds: 10
livenessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 30
periodSeconds: 15
resources:
requests:
cpu: 250m
memory: 256Mi
limits:
cpu: 500m
memory: 512Mi
envFrom:
- secretRef:
name: api-env
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: api
namespace: production
spec:
minAvailable: 2
selector:
matchLabels:
app: api
Make probes a contract
Readiness gates traffic, liveness decides restarts, and startup absorbs slow boot. Each answers a different question, and mixing them up produces either unreachable pods or crash loops that hide real failures.
Budget CPU and memory explicitly
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: api
namespace: production
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: api
minReplicas: 3
maxReplicas: 12
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
Isolate namespaces and enforce RBAC
apiVersion: v1
kind: Namespace
metadata:
name: production
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: production
name: deployment-editor
rules:
- apiGroups: ["apps"]
resources: ["deployments"]
verbs: ["get", "list", "update", "patch"]
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: deployment-editor-binding
namespace: production
subjects:
- kind: User
name: ci-deployer
namespace: production
roleRef:
kind: Role
name: deployment-editor
namespace: production
Keep the blast radius small
Before the first production rollout
- Done: Probes are defined for startup, readiness, and liveness
- Done: Resource requests and limits are set
- To do: PodDisruptionBudget is in place
- To do: Rollback revision is recorded
| Probe | Purpose | Typical timing |
|---|---|---|
| startup | Wait for slow boot before liveness starts | initialDelaySeconds / periodSeconds |
| readiness | Gate traffic on serving | periodSeconds after start |
| liveness | Restart the container when unhealthy | periodSeconds, failureThreshold |
The pattern is unglamorous: declare state, probe honestly, budget resources, isolate the namespace, and shrink the rollout blast radius. None of it is exciting, which is exactly why most deployments skip it.
References
- Kubernetes documentation: Deployments — RollingUpdate strategy fields.
- Kubernetes documentation: Assign pod resources — Requests, limits, and scheduling.
- Kubernetes RBAC documentation — Roles, bindings, and least privilege.