K8s集群中使用NFS
之前搭建了K8s集群以及NFS,接下来介绍如何在K8s集群中使用NFS。即将NFS挂载到POD中。
创建PV
kubectl apply -f pv-nfs.yaml
pv-nfs.yaml内容如下
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-nfs
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteMany
nfs:
# NFS挂载文件路径
path: /mnt/nfs1
# NFS服务端地址
server: 172.16.5.4
创建PVC
kubectl apply -f pvc-nfs.yaml
pvc-nfs.yaml内容如下
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: pvc-nfs
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 10Gi
检查PV与PVC是否绑定
执行如下两项命令,检查status状态,如果是Bound状态,可以继续往下进行
kubectl get pvc
kubectl get pv
创建一个Deployment使用PVC
apiVersion: apps/v1
kind: Deployment
metadata:
name: busybox
labels:
app: busybox
spec:
replicas: 1
selector:
matchLabels:
app: busybox
template:
metadata:
labels:
app: busybox
spec:
containers:
- name: busybox
image: busybox
command: ['sh', '-c', 'echo "Hello, Kubernetes!" && sleep 3600']
volumeMounts:
- name: data
mountPath: /data
volumes:
- name: data
persistentVolumeClaim:
claimName: pvc-nfs
创建好Deployment,进入pod,/data目录内的内容是和NFS服务端同步的
如在服务端NFS路径下添加hello.txt,进入pod,ls /data
能看到hello.txt