Cloud Native 16 min read

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.

Open Source Linux
Open Source Linux
Open Source Linux
Mastering Kubernetes Deployments: From YAML Generation to Rolling Updates and HPA

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.yaml

The generated yaml contains fields such as

apiVersion

,

kind

,

metadata

,

spec.replicas

, and a

selector.matchLabels

that 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: IfNotPresent

Apply 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 deploy1

and 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 6

After 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.19

To record the change for history, add

--record=true

when editing. View rollout history with

kubectl rollout history deployment deploy1

. Roll back to a previous revision with

kubectl rollout undo deployment deploy1

or specify a revision.

For more details see the original article.

Kubernetes Deployment diagram
Kubernetes Deployment diagram
DeploymentKubernetesdevopsYAMLHPARolling Update
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.