Module 7 · Project 07 · Run the App on Kubernetes · Lesson 26 of 48

Autoscaling, storage, network policies and Helm

Let the cluster add Pods under load, keep data on persistent volumes, lock down Pod-to-Pod traffic, and package everything as a Helm chart.

In this lesson · about 30 minutes

  • Explain how the Horizontal Pod Autoscaler decides the replica count
  • Use PersistentVolumeClaims and StatefulSets for data
  • Write a default-deny NetworkPolicy, and template manifests with Helm

The Horizontal Pod Autoscaler

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata: { name: notes-api, namespace: notes }
spec:
  scaleTargetRef: { apiVersion: apps/v1, kind: Deployment, name: notes-api }
  minReplicas: 2
  maxReplicas: 10
  metrics:
    - type: Resource
      resource:
        name: cpu
        target: { type: Utilization, averageUtilization: 60 }

Every 15 seconds or so the HPA reads average CPU use as a percentage of the Pods' CPU request (this is why requests matter) and works out:

desired Pods = ceil( current Pods × current CPU ÷ target CPU )

Four Pods at 90% with a 60% target: 4 × 90 ÷ 60 = 6, so it scales to six. It then clamps the answer between min and max, and scales down slowly (by default it waits five minutes) so a short dip doesn't throw away Pods you'll need again. When the nodes themselves fill up, a cluster autoscaler (or Karpenter on AWS) adds nodes.

Try it · watch it run

Horizontal Pod Autoscaler

Drag the traffic up and down and run the autoscaler. Watch the CPU line, the formula and the Pod count. Try a spike past what 10 Pods can handle.

Pods

2

min 2 · max 10

Avg CPU per pod

50%

target 60%

HPA wants

2

ceil(pods × cpu ÷ target)

pod-1pod-2
Drag the traffic up: CPU climbs past the 60% target and the autoscaler adds pods until each one is back near the target. Drag it down: it removes pods slowly, so a short dip doesn't cause flapping. Pods need resource requests for this to work, because CPU % is measured against what each pod requested.

Whiteboard · watch and try

Work the HPA formula

Press Play, then try the exercise yourself.

5 steps
Press Play and the tutor will talk you through it while writing on the board. Pause any time to write on the board yourself.

Tip: press Pause, then use the pen to work along on the board. Space plays and pauses; Ctrl+Z undoes your last mark.

Data: persistent volumes

A Pod's filesystem disappears with the Pod. Data that must survive goes on a PersistentVolume, which you ask for with a PersistentVolumeClaim:

apiVersion: v1
kind: PersistentVolumeClaim
metadata: { name: notes-db-data, namespace: notes }
spec:
  accessModes: [ReadWriteOnce]
  storageClassName: gp3          # on AWS, an EBS disk created for you
  resources:
    requests: { storage: 10Gi }

Databases run as a StatefulSet, which gives each Pod a stable name (notes-db-0) and its own volume that follows it. Many teams skip all this in production and use a managed database such as RDS (Project 06), keeping Kubernetes for stateless apps.

Network policies: default deny

By default every Pod can talk to every other Pod in the cluster. A NetworkPolicy locks that down, just like the security groups you chained in Project 06:

# 1. deny all incoming traffic in the namespace
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata: { name: default-deny, namespace: notes }
spec:
  podSelector: {}
  policyTypes: [Ingress]
---
# 2. then allow only the API to reach the database
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata: { name: db-from-api, namespace: notes }
spec:
  podSelector: { matchLabels: { app: notes-db } }
  ingress:
    - from:
        - podSelector: { matchLabels: { app: notes-api } }
      ports:
        - port: 5432

The cluster's network plugin (such as Calico or Cilium) must support policies for them to have any effect.

Helm: package it

By now you have a pile of YAML that's almost the same for dev, staging and production. Helm turns it into a chart: templates plus a values.yaml of the parts that change.

notes-chart/
├── Chart.yaml
├── values.yaml              # replicas: 2, image.tag: 1.4.0, host: notes.example.com
├── values-production.yaml   # replicas: 4, host: notes.com
└── templates/
    ├── deployment.yaml      # replicas: {{ .Values.replicas }}
    ├── service.yaml  ingress.yaml  hpa.yaml  configmap.yaml

helm install notes ./notes-chart -n notes
helm upgrade notes ./notes-chart -f values-production.yaml
helm rollback notes 3        # back to release revision 3

Check your understanding0 of 3 correct

Question 1

1.The HPA's CPU percentage is measured against what?

Question 2

2.Four Pods at 30% CPU, target 60%, min 2. What does the HPA want?

Question 3

3.After applying a default-deny policy, the API can't reach the database. What's next?

Key terms

Say the meaning to yourself, then tap a card to check.

Quiz score: 0 of 3

Tip: answer every quiz question correctly first.

Written by Noviq Academy in its own words. The labs are simulations: no real servers are touched. When you build the projects yourself, use a practice virtual machine or a free-tier cloud account you control, watch your cloud spending, and never run cleanup or delete commands on a machine you care about. Tool names belong to their owners; versions and output are shown for learning and may differ slightly from yours. Read-aloud and the whiteboard voice use your device's built-in voices.