pv 分静态和动态两种
静态:
需要我们自己去手动创建,业务少的情况下可以用手动创建。
动态:
根据 yaml 文件里面的参数自动创建合适的 pv,业务量大或者静态 pv 无法满足需求的时候需要动态创建 pv。
pod 绑定 pvc,pvc 绑定 pv。(pod 消耗的是节点,pvc 消耗的是 pv 的资源。)
一、静态绑定 pv
1、创建 pv.yaml 文件并部署
cat >> pv.yaml >> EOF
apiVersion: v1
kind: PersistentVolume
metadata:
name: pvc-nfs
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
nfs:
path: /data/k8s
server: 10.151.30.57
2、创建 pvc.yaml 文件并部署
cat >> pvc.yaml << EOF
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-nfs
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
3、查看之前创建的 pv
[root@master1 ~]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvc-nfs 1Gi RWO Recycle Available
可以看到 pv 状态是可用的,还没有 pvc 绑定。
4、部署 pvc.yaml
kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
pvc-nfs Bound pv-nfs 1Gi RWO 12s
再查看 pv 状态
kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pv-nfs 1Gi RWO Recycle Bound default/pvc-nfs
看到 PV 是 Bound 状态了,说明已经绑定成功了。
二、动态绑定 pv
1、部署后端 NFS 或者其他存储并设置默认 storageclass 略,前面有过讲解。这里用 NFS 作为 K8S 后端存储
https://www.cnblogs.com/lfl17718347843/p/14131062.html
2、创建测试文件
cat >> statefulset-nfs.yaml << EOF
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
ports:
- port: 80
name: web
clusterIP: None
selector:
app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: nfs-web
spec:
serviceName: "nginx"
replicas: 3
selector:
matchLabels:
app: nfs-web # has to match .spec.template.metadata.labels
template:
metadata:
labels:
app: nfs-web
spec:
terminationGracePeriodSeconds: 10
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
name: web
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
volumeClaimTemplates:
- metadata:
name: www
annotations:
volume.beta.kubernetes.io/storage-class: managed-nfs-storage 默认后端存储
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 1Gi
3、部署并查看是否自动创建动态 pv
[root@master1 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nfs-web-0 1/1 Running 0 44m
nfs-web-1 1/1 Running 0 44m
nfs-web-2 1/1 Running 0 43m
[root@master1 ~]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvc-23e3cdb2-a365-43ff-8936-d0e3df30ffac 1Gi RWO Delete Bound default/www-nfs-web-1 managed-nfs-storage 45m
pvc-2c34b87d-4f09-4063-aea9-9ae1e7567194 1Gi RWO Delete Bound default/www-nfs-web-2 managed-nfs-storage 45m
pvc-7cdcdc4c-e9d2-4848-b434-9caf7e72db5a 1Gi RWO Delete Bound default/www-nfs-web-0 managed-nfs-storage 46m
[root@master1 ~]# ll /data/volumes/v1
total 0
drwxrwxrwx 2 root root 6 Feb 13 14:30 default-www-nfs-web-0-pvc-7cdcdc4c-e9d2-4848-b434-9caf7e72db5a
drwxrwxrwx 2 root root 6 Feb 13 14:31 default-www-nfs-web-1-pvc-23e3cdb2-a365-43ff-8936-d0e3df30ffac
drwxrwxrwx 2 root root 6 Feb 13 14:31 default-www-nfs-web-2-pvc-2c34b87d-4f09-4063-aea9-9ae1e7567194