Kubernetes da Zero: Guida Completa all'Installazione e Gestione di un Cluster - Parte 3
Guida pratica a Kubernetes: installa un cluster su Debian 13 con kubeadm, gestisci Deployment, RBAC, Helm e risolvi i problemi più comuni.
3.1 Deployment — Applicazioni Stateless
Il Deployment è il controller più comune in Kubernetes. È ideale per applicazioni stateless come frontend web, API REST e microservizi. Mantiene sempre attivo un numero definito di repliche identiche di un Pod.
Caratteristiche principali:
- ✅ Gestione automatica del numero di repliche
- ✅ Rolling updates senza downtime
- ✅ Rollback rapido a versioni precedenti
- ✅ Self-healing: ricrea automaticamente i Pod in crash
Esempio YAML — Deployment Nginx con 3 repliche
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
Comandi utili
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Applica il Deployment
kubectl apply -f nginx-deployment.yaml
# Lista i Deployment
kubectl get deployments
# Scala a 5 repliche
kubectl scale deployment nginx-deployment --replicas=5
# Aggiorna l'immagine (rolling update)
kubectl set image deployment/nginx-deployment nginx=nginx:1.25
# Controlla lo stato del rolling update
kubectl rollout status deployment/nginx-deployment
# Rollback all'ultima versione stabile
kubectl rollout undo deployment/nginx-deployment
# Storico dei rollout
kubectl rollout history deployment/nginx-deployment
Strategia di update
1
2
3
4
5
6
spec:
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1 # max Pod in più durante l'update
maxUnavailable: 0 # nessun Pod offline durante l'update
3.2 StatefulSet — Applicazioni con Stato
Lo StatefulSet è progettato per applicazioni che richiedono identità stabile e storage persistente, come database (MySQL, PostgreSQL, MongoDB) o sistemi di messaggistica (Kafka, RabbitMQ).
Caratteristiche principali:
- ✅ Ogni Pod ha un nome stabile e prevedibile (
mysql-0,mysql-1,mysql-2) - ✅ I Pod vengono creati e terminati in ordine sequenziale
- ✅ Ogni Pod può avere il proprio Persistent Volume
- ✅ Il volume persiste anche dopo la ri-schedulazione del Pod
Esempio YAML — StatefulSet MySQL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mysql
spec:
serviceName: "mysql"
replicas: 3
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:8.0
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-secret
key: password
ports:
- containerPort: 3306
volumeMounts:
- name: mysql-data
mountPath: /var/lib/mysql
volumeClaimTemplates:
- metadata:
name: mysql-data
spec:
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 10Gi
📌 Nota: Lo StatefulSet richiede anche un Headless Service (clusterIP: None) per la risoluzione DNS per-Pod.
1
2
3
4
5
6
7
8
9
10
apiVersion: v1
kind: Service
metadata:
name: mysql
spec:
clusterIP: None # Headless Service
selector:
app: mysql
ports:
- port: 3306
3.3 DaemonSet — Un Pod su ogni Nodo
Il DaemonSet garantisce che una copia di un Pod venga eseguita su ogni nodo del cluster. È ideale per agent di monitoring, log collector, o plugin di rete.
Caratteristiche principali:
- ✅ Distribuisce automaticamente il Pod su tutti i nodi esistenti
- ✅ Aggiunge il Pod automaticamente ai nuovi nodi
- ✅ Non necessita di scaling manuale
Esempio YAML — DaemonSet per Prometheus Node Exporter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: node-exporter
namespace: monitoring
spec:
selector:
matchLabels:
app: node-exporter
template:
metadata:
labels:
app: node-exporter
spec:
hostNetwork: true
hostPID: true
containers:
- name: node-exporter
image: prom/node-exporter:latest
ports:
- containerPort: 9100
securityContext:
privileged: true
3.4 Tabella Riepilogativa
| Controller | Tipo di App | Scaling | Storage | Caso d’uso tipico |
|---|---|---|---|---|
| Deployment | Stateless | Automatico/manuale | Condiviso | Frontend, API, microservizi, proxy |
| StatefulSet | Stateful | Sequenziale | Dedicato per Pod | Database (MySQL, MongoDB), Kafka |
| DaemonSet | Per-nodo | Automatico (1 per nodo) | Opzionale | Monitoring agent, log collector, CNI |