前提:需nfs服务器开启服务
PersistentVolume
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv1
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
storageClassName: nfs
nfs:
path: /data/share
server: 192.168.0.56
PersistentVolumeClaim
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc1
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: nfs
Pod
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: myfrontend
image: nginx
volumeMounts:
- mountPath: "/var/www/html"
name: mypd
volumes:
- name: mypd
persistentVolumeClaim:
claimName: pvc1
运行到k8s
kubectl apply -f pv.yml
kubectl apply -f pvc.yml
kubectl apply -f pod.yml
验证挂载是否成功
进入shell
kubectl exec myfrontend -- /bin/sh
或者
docker exec -it container /bin/bash
验证
df -h | grep 192.168.0.56
对于k8s启动pod绑定pv,缺点在于每次都需要手动创建pv,不能动态的根据pod挂载的目录自动的创建pv来绑定存储。
推荐方案:
nfs provisioner (动态的根据挂载创建pv)
参考链接:
卷 | Kubernetes
Persistent Volumes | Kubernetes
kubernetes 持久化存储(一)-阳明的博客|Kubernetes|Istio|Prometheus|Python|Golang|云原生
kubernetes 持久化存储(二)-阳明的博客|Kubernetes|Istio|Prometheus|Python|Golang|云原生