1. 静态PV
此方式的特点,需要手动创建pvc,pv ,增加了维护成本
1.1 nfs服务端
bash
#创建共享目录
[root@Rocky nfs]# mkdir -p /data/nfs/pv{01,02,03}
#配置
[root@Rocky nfs]# cat /etc/exports
/data/nfs/pv01 *(insecure,rw,sync,no_root_squash)
/data/nfs/pv02 *(insecure,rw,sync,no_root_squash)
/data/nfs/pv03 *(insecure,rw,sync,no_root_squash)
#热加载配置
[root@Rocky nfs]# showmount -e
Export list for Rocky:
/data/nfs/pv03 *
/data/nfs/pv02 *
/data/nfs/pv01 *
#创建共享目录
[root@Rocky nfs]# mkdir -p /data/nfs/pv{01,02,03}
#配置
[root@Rocky nfs]# cat /etc/exports
/data/nfs/pv01 *(insecure,rw,sync,no_root_squash)
/data/nfs/pv02 *(insecure,rw,sync,no_root_squash)
/data/nfs/pv03 *(insecure,rw,sync,no_root_squash)
#热加载配置
[root@Rocky nfs]# showmount -e
Export list for Rocky:
/data/nfs/pv03 *
/data/nfs/pv02 *
/data/nfs/pv01 *
1.2 创建pv和storageclass
yaml
[root@kube-master nfs]# cat 1.nfs-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-100m
spec:
capacity:
storage: 100Mi
accessModes:
- ReadWriteMany
storageClassName: "nfs-storage"
nfs:
server: 10.103.236.199
path: /data/nfs/pv01
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-1g
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteMany
storageClassName: "nfs-storage"
nfs:
server: 10.103.236.199
path: /data/nfs/pv02
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-2g
spec:
capacity:
storage: 2Gi
accessModes:
- ReadWriteMany
storageClassName: "nfs-storage"
nfs:
server: 10.103.236.199
path: /data/nfs/pv03
[root@kube-master nfs]# cat 1.nfs-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-100m
spec:
capacity:
storage: 100Mi
accessModes:
- ReadWriteMany
storageClassName: "nfs-storage"
nfs:
server: 10.103.236.199
path: /data/nfs/pv01
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-1g
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteMany
storageClassName: "nfs-storage"
nfs:
server: 10.103.236.199
path: /data/nfs/pv02
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-2g
spec:
capacity:
storage: 2Gi
accessModes:
- ReadWriteMany
storageClassName: "nfs-storage"
nfs:
server: 10.103.236.199
path: /data/nfs/pv03
- 查看
[root@kube-master nfs]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pv-100m 100Mi RWX Retain Available nfs-storage 44m
pv-1g 1Gi RWX Retain Available nfs-storage 44m
pv-2g 2Gi RWX Retain Available nfs-storage 44m
[root@kube-master nfs]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pv-100m 100Mi RWX Retain Available nfs-storage 44m
pv-1g 1Gi RWX Retain Available nfs-storage 44m
pv-2g 2Gi RWX Retain Available nfs-storage 44m
1.3 创建pvc
yaml
[root@kube-master nfs]# cat 2.nfs-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: caim-pvc
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 200Mi
storageClassName: nfs-storage #pv 名字
[root@kube-master nfs]# cat 2.nfs-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: caim-pvc
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 200Mi
storageClassName: nfs-storage #pv 名字
- 查看pvc
bash
[root@kube-master nfs]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
caim-pvc Bound pv-1g 1Gi RWX nfs-storage 47m
[root@kube-master nfs]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
caim-pvc Bound pv-1g 1Gi RWX nfs-storage 47m
1.4 创建pod
yaml
[root@kube-master nfs]# cat 3.nfs-pod.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: pod-nfs-deployment
labels:
app: pod-nfs-app
spec:
replicas: 1
selector:
matchLabels:
app: pod-nfs-app
template:
metadata:
labels:
app: pod-nfs-app
spec:
containers:
- name: pod-nfs-container
image: nginx:latest
ports:
- containerPort: 80
volumeMounts:
- name: pod-nfs-volume
mountPath: /usr/share/nginx/html
[root@kube-master nfs]# cat 3.nfs-pod.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: pod-nfs-deployment
labels:
app: pod-nfs-app
spec:
replicas: 1
selector:
matchLabels:
app: pod-nfs-app
template:
metadata:
labels:
app: pod-nfs-app
spec:
containers:
- name: pod-nfs-container
image: nginx:latest
ports:
- containerPort: 80
volumeMounts:
- name: pod-nfs-volume
mountPath: /usr/share/nginx/html
- 执行apply
bash
[root@kube-master nfs]# kubectl apply -f 3.nfs-pod.yaml
deployment.apps/pod-nfs-deployment created
[root@kube-master nfs]# kubectl apply -f 3.nfs-pod.yaml
deployment.apps/pod-nfs-deployment created
- 查看pod
bash
[root@kube-master nfs]# kubectl get pod -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod-nfs-deployment-5b5478668f-vxfks 1/1 Running 0 42m 172.30.0.176 kube-node01 <none> <none>
[root@kube-master nfs]# kubectl get pod -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod-nfs-deployment-5b5478668f-vxfks 1/1 Running 0 42m 172.30.0.176 kube-node01 <none> <none>
- 验证效果
bash
[root@kube-master nfs]# kubectl get pvc,pv
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
persistentvolumeclaim/caim-pvc Bound pv-1g 1Gi RWX nfs-storage 61m
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
persistentvolume/pv-100m 100Mi RWX Retain Available nfs-storage 72m
persistentvolume/pv-1g 1Gi RWX Retain Bound default/caim-pvc nfs-storage 72m
persistentvolume/pv-2g 2Gi RWX Retain Available nfs-storage 72m
[root@kube-master nfs]# kubectl get pvc,pv
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
persistentvolumeclaim/caim-pvc Bound pv-1g 1Gi RWX nfs-storage 61m
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
persistentvolume/pv-100m 100Mi RWX Retain Available nfs-storage 72m
persistentvolume/pv-1g 1Gi RWX Retain Bound default/caim-pvc nfs-storage 72m
persistentvolume/pv-2g 2Gi RWX Retain Available nfs-storage 72m
bash
[root@kube-master nfs]# curl 172.30.0.176
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.27.0</center>
</body>
</html>
#写入数据之后
[root@kube-master nfs]# curl 172.30.0.176
hello pv02
[root@kube-master nfs]# curl 172.30.0.176
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.27.0</center>
</body>
</html>
#写入数据之后
[root@kube-master nfs]# curl 172.30.0.176
hello pv02