Binding a Pod to a USB Drive on a Specific Node
The goal is for a Pod to use a USB hard drive physically attached to a specific Node (e.g., a worker node), the best practice is:
Technology | Purpose |
---|---|
nodeSelector (or nodeAffinity ) | Pin the Pod to the Node with the attached USB |
hostPath volume | Mount the USB driveβs path on the Node (e.g., /mnt/nas-backup ) directly into the Pod |
π‘οΈ Advantages
- Simple to implement and quick to deploy
- No need for extra NFS or distributed storage setup
- Full control over which physical disk the data lands on
- Data persists on the host; Pod crashes or restarts won't cause data loss
β οΈ Caveats
- The USB drive can only be accessed by Pods scheduled on that specific Node β no cross-node sharing
- Scheduling the Pod on other Nodes will cause volume mount failures or Pod crashes
- Make sure the USB drive is automatically mounted on the Node before Pod creation
π§ Getting Startedβ
π Create a Dummy Local Volume on the Worker Nodeβ
On the worker Node (e.g., rpi4-argon
), run:
sudo mkdir -p /tmp/mock-usb
sudo chmod 777 /tmp/mock-usb # relaxed permissions for testing ease
π§ͺ BusyBox Test Pod (Mounting the mock USB with hostPath
)β
# ./config/busybox-local-vol-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: busybox-local-volume
spec:
nodeSelector:
kubernetes.io/hostname: rpi4-argon
containers:
- name: busybox
image: busybox
command: ['sh', '-c', 'echo hello-world > /data/hello.txt && sleep 3600']
volumeMounts:
- mountPath: /data
name: local-volume
volumes:
- name: local-volume
hostPath:
path: /tmp/mock-usb
type: Directory
Stepsβ
- Apply the Pod:
kubectl apply -f config
- Exec into the Pod:
kubectl exec -it busybox-local-volume -- sh
cat /data/hello.txt
echo '---' >> /data/hello.txt
- Check on the worker Node if the file is created:
cat /tmp/mock-usb/hello.txt
If you see:
hello-world
---
it means your hostPath
volume is successfully mounted π!
π§Ό Cleanupβ
kubectl delete pod busybox-local-volume
rm /tmp/mock-usb/hello.txt