Cum să montezi volume în Kubernetes — emptyDir, hostPath, PVC
DodaTech
Updated 2025-01-15
2 min read
In this tutorial, you'll learn about Cum să montezi volume în Kubernetes. We cover key concepts, practical examples, and best practices.
Problema
Datele din containere se pierd când container-ul se repornește. Ai nevoie de stocare care să persiste sau să fie partajată între containere.
Soluția Pas cu Pas
1. emptyDir — Stocare temporară partajată
apiVersion: v1
kind: Pod
metadata:
name: shared-volume-pod
spec:
containers:
- name: writer
image: busybox
command: ["sh", "-c", "echo Hello > /data/hello.txt && sleep 3600"]
volumeMounts:
- name: shared-data
mountPath: /data
- name: reader
image: busybox
command: ["sh", "-c", "cat /data/hello.txt && sleep 3600"]
volumeMounts:
- name: shared-data
mountPath: /data
volumes:
- name: shared-data
emptyDir: {}
kubectl apply -f emptydir.yaml
kubectl logs shared-volume-pod reader
Output:
Hello
2. hostPath — Stocare pe nod
apiVersion: v1
kind: Pod
metadata:
name: hostpath-pod
spec:
containers:
- name: test
image: nginx
volumeMounts:
- name: host-logs
mountPath: /var/log/nginx
volumes:
- name: host-logs
hostPath:
path: /var/log/nginx
type: DirectoryOrCreate
3. persistentVolumeClaim — Stocare persistentă
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: data-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
apiVersion: v1
kind: Pod
metadata:
name: pvc-pod
spec:
containers:
- name: app
image: nginx
volumeMounts:
- name: data
mountPath: /usr/share/nginx/html
volumes:
- name: data
persistentVolumeClaim:
claimName: data-pvc
kubectl apply -f pvc.yaml -f pod.yaml
4. Verifică volumele montate
kubectl exec pvc-pod -- df -h /usr/share/nginx/html
Prevenție
- Folosește
emptyDirpentru date temporare și cache. - Folosește
hostPathdoar pentru logging sau acces la dispozitive de pe nod. - Folosește PVC pentru date care trebuie să persiste și să fie portabile.
- Definește
storageClassNamepentru a controla tipul de stocare.
Greșeli Comune
- hostPath fără restricții — Orice Pod poate accesa sistemul de fișiere al nodului.
- PVC fără StorageClass — Nu toate clusterele au un StorageClass implicit.
- AccessMode greșit —
ReadWriteOncenu permite montarea pe mai multe noduri simultan. - Resurse insuficiente — PVC-ul eșuează dacă nu există PV cu spațiu suficient.
- emptyDir pentru date persistente — emptyDir se șterge când Pod-ul dispare.
Exercițiu
Creează un Pod cu două containere care partajează un volum emptyDir. Un container scrie un fișier, celălalt îl citește. Verifică rezultatul.
FAQ
Construit de dezvoltatorii Doda Browser, DodaZIP și Durga Antivirus Pro. Uneltele DodaTech se integrează seamless cu Kubernetes pentru productivitate și securitate sporite.
← Previous
How to Back Up and Restore Kubernetes with Velero
Next →
How to Fix Kafka Broker Not Available / Leader Not Found
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro