PV与PVC

  • Post author:
  • Post category:Kubernetes
  • Page Views 869 阅读

1.Master安装nfs服务器

fxx3@kube-node-1:~$ sudo apt-get install nfs-kernel-server

2.Node安装nfs客户端

fxx3@kube-node-2:~$ sudo apt install nfs-common

3.创建共享路径

fxx2@kube-node-1:~/yaml$ cat /etc/exports   
/nfs/k8s 192.168.244.0/24(rw,sync,no_root_squash)

fxx2@kube-node-1:~/yaml$ mkdir -p /nfs/k8s/pv
fxx2@kube-node-1:~/yaml$ sudo systemctl start nfs-kernel-server && sudo systemctl enable nfs-kernel-server
fxx2@kube-node-1:~/yaml$ showmount -e
Export list for kube-node-1:
/nfs/k8s 192.168.244.0/24

4.创建pv

fxx2@kube-node-1:~/yaml$ cat nfs-pv1.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: mypv1
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle #recycle:pvc回收后数据将被清理
#retain:即使pvc被回收,数据不会被清理,需要手动清理
storageClassName: nfs
nfs:
path: /nfs/k8s/pv
server: 192.168.244.148

fxx2@kube-node-1:~/yaml$ sudo kubectl apply -f nfs-pv1.yaml
fxx2@kube-node-1:~/yaml$ kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
mypv1 10Gi RWO Recycle Bound default/mypvc1 nfs 36m

5.创建pvc

fxx2@kube-node-1:~/yaml$ cat nfs-pvc1.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: mypvc1
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: nfs

fxx2@kube-node-1:~/yaml$ sudo kubectl apply -f nfs-pvc1.yaml
fxx2@kube-node-1:~/yaml$ sudo kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
mypvc1 Bound mypv1 10Gi RWO nfs 37m

6.pod挂载pvc

fxx2@kube-node-1:~/yaml$ cat pod1.yaml
apiVersion: v1
kind: Pod
metadata:
name: mypod1
spec:
containers:
- name: mypod
image: nginx:latest
args:
- /bin/sh
- -c
- sleep 3000
volumeMounts:
- mountPath: "/mydata"
name: mydata
volumes:
- name: mydata
persistentVolumeClaim:
claimName: mypvc1

fxx2@kube-node-1:~/yaml$ sudo kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
mypod1 1/1 Running 0 37m 10.244.0.48 kube-node-1 <none> <none>

7.测试pvc是否可用

fxx2@kube-node-1:~/yaml$ kubectl exec mypod1 touch /mydata/hello.txt
fxx2@kube-node-1:~/yaml$ ll /nfs/k8s/pv/
total 8
drwxr-xr-x 2 root root 4096 Feb 17 22:07 ./
drwxr-xr-x 3 root root 4096 Feb 17 21:23 ../
-rw-r--r-- 1 root root 0 Feb 17 22:07 hello.txt

 


「 文章如果对你有帮助,请点个赞哦^^ 」 

0