安装及配置NFS(参考本人博客)
创建NFS共享目录
mkdir -p /data/volumes/v{1,2,3,4,5,6,7,8,9,10}
新增共享目录
[root@k8smaster1 ~]# cat /etc/exports
/data/v1 *(rw,no_root_squash)
/data/volumes/v1 192.168.1.0/24(rw,no_root_squash)
/data/volumes/v2 192.168.1.0/24(rw,no_root_squash)
/data/volumes/v3 192.168.1.0/24(rw,no_root_squash)
/data/volumes/v4 192.168.1.0/24(rw,no_root_squash)
/data/volumes/v5 192.168.1.0/24(rw,no_root_squash)
/data/volumes/v6 192.168.1.0/24(rw,no_root_squash)
/data/volumes/v7 192.168.1.0/24(rw,no_root_squash)
/data/volumes/v8 192.168.1.0/24(rw,no_root_squash)
/data/volumes/v9 192.168.1.0/24(rw,no_root_squash)
/data/volumes/v10 192.168.1.0/24(rw,no_root_squash)
[root@k8smaster1 ~]#
启动共享
创建PV
[root@k8smaster1 pv]# cat pv-test.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: v1
labels:
app: v1
spec:
capacity:
storage: 1Gi
accessModes: ["ReadWriteOnce"]
nfs:
server: 192.168.1.170
path: /data/volumes/v1
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: v2
labels:
app: v2
spec:
capacity:
storage: 2Gi
accessModes: ["ReadWriteMany"]
nfs:
server: 192.168.1.170
path: /data/volumes/v1
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: v3
labels:
app: v3
spec:
capacity:
storage: 2Gi
accessModes: ["ReadOnlyMany"]
nfs:
server: 192.168.1.170
path: /data/volumes/v1
创建PVC
[root@k8smaster1 pv]# cat pvc-test.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-v1
labels:
app: pvc-v1
spec:
accessModes: ["ReadWriteOnce"]
selector:
matchLabels:
app: v1
resources:
requests:
storage: 1Gi
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-v2
labels:
app: v2
spec:
accessModes: ["ReadWriteMany"]
selector:
matchLabels:
app: v2
resources:
requests:
storage: 2Gi
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-v3
labels:
app: v3
spec:
accessModes: ["ReadOnlyMany"]
selector:
matchLabels:
app: v3
resources:
requests:
storage: 2Gi
挂载Pod
[root@k8smaster1 pv]# cat pvc-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: pvc-v1
labels:
app: pvc-nginx
spec:
containers:
- name: nginx
image: docker.io/library/nginx
imagePullPolicy: IfNotPresent
volumeMounts:
- name: nginx-html
mountPath: /usr/share/nginx/html/
ports:
- containerPort: 80
protocol: TCP
volumes:
- name: nginx-html
persistentVolumeClaim:
claimName: pvc-v1