# PV PVC
# 创建PV
[13:34:33 root@master1 storage]#cat 05-storage-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-test
spec:
capacity:
storage: 3Gi
accessModes:
- ReadWriteOnce
nfs:
path: /nfs-data
server: 10.0.0.58
# 创建PVC
[13:34:36 root@master1 storage]#cat 06-storage-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-test
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
# 创建nginx,用PVC挂载数据卷
[13:37:45 root@master1 storage]#cat 08-storage-nginx-pvc-subdir.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
spec:
volumes:
- name: nginx-volume
persistentVolumeClaim:
claimName: pvc-test
containers:
- name: nginx-pv
image: 10.0.0.55:80/mykubernetes/nginx:1.21.3
volumeMounts:
- name: nginx-volume
mountPath: "/usr/share/nginx/html"
subPath: web1
---
apiVersion: v1
kind: Pod
metadata:
name: nginx-flask
spec:
volumes:
- name: nginx-volume
persistentVolumeClaim:
claimName: pvc-test
containers:
- name: nginx-flask
image: 10.0.0.55:80/mykubernetes/nginx:1.21.3
volumeMounts:
- name: nginx-volume
mountPath: "/usr/share/nginx/html"
subPath: web2
# 开始配置
[13:37:57 root@master1 storage]#kubectl apply -f 05-storage-pv.yaml
persistentvolume/pv-test created
[13:38:29 root@master1 storage]#kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pv-test 3Gi RWO Retain Available 12s
# PVC自动绑定PV
[13:38:34 root@master1 storage]#kubectl apply -f 06-storage-pvc.yaml
persistentvolumeclaim/pvc-test created
[13:38:52 root@master1 storage]#kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
pvc-test Bound pv-test 3Gi RWO 4s
[13:38:56 root@master1 storage]#kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pv-test 3Gi RWO Retain Bound default/pvc-test 37s
# 创建pod
[13:38:59 root@master1 storage]#kubectl apply -f 08-storage-nginx-pvc-subdir.yaml
pod/nginx-pod created
pod/nginx-flask created
[13:43:27 root@master1 storage]#kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-flask 1/1 Running 0 5s 10.244.3.4 node1.noisedu.cn <none> <none>
nginx-pod 1/1 Running 0 5s 10.244.4.13 node2.noisedu.cn <none> <none>
# 测试, 若未准备主页,会出现403错误
[13:43:32 root@master1 storage]#curl 10.244.3.4
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.21.4</center>
</body>
</html>
# 根据subpath在nfs主机准备主页,
[13:43:50 root@nfs web1]#echo 'web1 pv and pvc' > index.html
[13:44:04 root@nfs web1]#cd ..
[13:44:06 root@nfs nfs-data]#cd web2/
[13:44:08 root@nfs web2]#echo 'web2 pv and pvc' > index.html
# 再继续访问
[13:43:38 root@master1 storage]#curl 10.244.3.4
web2 pv and pvc
[13:44:21 root@master1 storage]#curl 10.244.4.13
web1 pv and pvc