Mastering Kubernetes Deployments: From YAML Generation to Rolling Updates and HPA
This guide walks through Kubernetes Deployment controllers, showing how to generate YAML templates, manage replica counts, apply dynamic scaling with Horizontal Pod Autoscaler, and perform rolling image upgrades and rollbacks, all with practical command‑line examples.
Deployment Controller
1. Deployment and replica count
In Kubernetes the smallest scheduling unit is a pod, but pods are not self‑healing; the Deployment controller ensures a desired number of pods are running. You can create a Deployment via a YAML file or with kubectl commands (kubectl create deployment … --dry‑run=client -o yaml).
# Generate a deployment yaml template
kubectl create deployment deploy1 --image=nginx --dry-run=client -o yaml > deploy1.yamlThe generated yaml contains fields such as
apiVersion,
kind,
metadata,
spec.replicas, and a
selector.matchLabelsthat ties pods to the controller.
apiVersion: apps/v1
kind: Deployment
metadata:
name: deploy1
spec:
replicas: 3
selector:
matchLabels:
app: deploy1
template:
metadata:
labels:
app: deploy1
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresentApply the yaml with
kubectl apply -f deploy1.yaml. The controller creates the specified number of pods and will recreate any that are deleted.
2. Replica modification methods
Edit the deployment live:
kubectl edit deployment deploy1and change
replicas.
Scale via command line:
kubectl scale deployment deploy1 --replicas 5.
Modify the yaml file and re‑apply it.
3. Dynamic scaling with HPA
HPA (Horizontal Pod Autoscaler) automatically adjusts the replica count based on metrics such as CPU utilization. Install metrics‑server first.
Example:
# Set a baseline of 8 replicas
kubectl scale deployment deploy1 --replicas 8
# Create an HPA with min 2, max 6
kubectl autoscale deployment deploy1 --min 2 --max 6After adding resource requests to the pod spec (e.g.,
cpu: 400m), the HPA shows a target like
0%/80%and will increase or decrease pods accordingly.
4. Image rolling upgrade and rollback
Rolling updates replace a fraction of pods at a time. You can edit the image in the deployment yaml or use
kubectl set image:
kubectl set image deployment/deploy1 nginx=nginx:1.19To record the change for history, add
--record=truewhen editing. View rollout history with
kubectl rollout history deployment deploy1. Roll back to a previous revision with
kubectl rollout undo deployment deploy1or specify a revision.
For more details see the original article.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.