By Kevin McAleer, 6 Minutes
Page last updated November 24, 2026

By the end of this lesson you will be able to deploy a complete multi-service app on K3s in its own namespace, with storage that survives a restart, an Ingress, and a NetworkPolicy that protects the database.
In lesson 12 we built a visit counter whose count vanished when Redis restarted. Now let’s finish the job and make it something you would be happy to leave running on your shelf. We will:
pi-counterk8s/ folder, ready for Git and the CI/CD from lesson 14flowchart LR
B["Browser<br/>counter.test"] --> T["Traefik Ingress"]
T --> W["web x2<br/>Service web"]
W -->|"allowed by NetworkPolicy"| R["Service redis"]
R --> RP["redis pod"]
RP --> V[("PVC redis-data<br/>local-path")]
X["Any other pod"] -.->|"blocked"| R
k8s/
00-namespace.yaml
redis.yaml
web.yaml
web-ingress.yaml
netpol.yaml
Copy web.yaml and web-ingress.yaml from lesson 12 unchanged, except that in web-ingress.yaml you change the host to counter.test. Kubernetes applies files in name order, so the 00- prefix puts the namespace first.
Create k8s/00-namespace.yaml:
apiVersion: v1
kind: Namespace
metadata:
name: pi-counter
Create k8s/redis.yaml. It asks for a 1Gi volume, runs Redis with its append-only file turned on so writes are saved to disk, and mounts the volume at /data, where the Redis image keeps its files:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: redis-data
spec:
accessModes:
- ReadWriteOnce
storageClassName: local-path
resources:
requests:
storage: 1Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis
spec:
replicas: 1
strategy:
type: Recreate
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
spec:
containers:
- name: redis
image: redis:7-alpine
command: ["redis-server", "--appendonly", "yes"]
ports:
- containerPort: 6379
resources:
requests:
memory: 32Mi
cpu: 50m
limits:
memory: 128Mi
volumeMounts:
- name: data
mountPath: /data
volumes:
- name: data
persistentVolumeClaim:
claimName: redis-data
---
apiVersion: v1
kind: Service
metadata:
name: redis
spec:
selector:
app: redis
ports:
- port: 6379
targetPort: 6379
strategy: Recreate matters here. The default rolling update starts the new pod before stopping the old one, and two Redis processes must not share one data folder.
Create k8s/netpol.yaml:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: redis-from-web-only
spec:
podSelector:
matchLabels:
app: redis
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
app: web
ports:
- protocol: TCP
port: 6379
This selects the Redis pods and lets only pods labelled app: web reach port 6379. The web pods stay open to Traefik, because we have not restricted them.
kubectl apply -f k8s/ -n pi-counter
kubectl get all,pvc,ingress,networkpolicy -n pi-counter
Wait for the pods to be Running and the claim to be Bound. Then visit the app:
curl -H "Host: counter.test" http://192.168.1.100/
Run it a few times and watch the number climb.
The count survives. Delete the Redis pod and let the Deployment recreate it:
kubectl delete pod -n pi-counter -l app=redis
kubectl get pods -n pi-counter
When the new Redis pod is Running, call the page again. The count carries on from where it was, instead of going back to 1.
Redis is locked down. Try to reach Redis from a pod that is not part of the web app. busybox’s nc sends a PING:
kubectl run tester -n pi-counter --rm -it --restart=Never --image=busybox:1.36 -- sh -c 'echo PING | nc -w 3 redis 6379'
This should time out with no reply, because the tester does not carry the app: web label. If you see +PONG, check the policy is applied and that K3s was not started with --disable-network-policy.
k8s/ folder to a private Git repo and deploy it with the workflow or Argo CD from lesson 14.tls: section to the Ingress (lesson 16).kubectl top pods -n pi-counter and tune the resource limits.Teachers: This is a good assessed task. A simple rubric: the app responds (deployed), the count survives a Redis restart (storage), the tester pod is blocked (policy), and the learner can explain each file in their own words. Give each group its own namespace and host name so groups do not collide.
web to four replicas and check the pod names in the replies.kubectl apply -f k8s/ complains the namespace does not exist.00-namespace.yaml file first with kubectl apply -f k8s/00-namespace.yaml, then apply the folder again.Why: Kubernetes creates objects in order, and a namespace has to exist before anything goes in it. Re-running fixes a race.
Cannot reach Redis.app: web, and run kubectl get endpoints redis -n pi-counter.Why: The NetworkPolicy only admits pods with that label, so a changed label blocks the app too.
Pending.kubectl describe pod -n pi-counter -l app=redis and check the PVC is Bound and the node has disk space.Quick quiz 1 of 3
Why does the Redis Deployment use strategy: Recreate?
Quick quiz 2 of 3
What lets the count survive deleting the Redis pod?
Quick quiz 3 of 3
Who is allowed to connect to Redis under our NetworkPolicy?
Comments