Kubernetes  静态PV使用

Kubernetes支持持久卷的存储插件:
https://kubernetes.io/docs/concepts/storage/persistent-volumes/

  • 缺点:手动创建pv比较繁琐、不适合大工程
  • 优点:小规模使用方便灵活

1、创建pvc yaml文件
vim pvc.yaml

apiVersion: v1
kind: Pod
metadata:
name: nginx6
spec:
containers:
- name: nginx6
image: nginx
# 挂在点
volumeMounts:
- name: wwwroot
mountPath: /usr/share/nginx/html
ports:
- containerPort: 80
# 挂载来源
volumes:
- name: wwwroot
# 定义PVC
persistentVolumeClaim:
# 定义PVC名称
claimName: my-pvc --- apiVersion: v1
# 使用PVC类型
kind: PersistentVolumeClaim
metadata:
# 与容器应用PVC相同
name: my-pvc
spec:
# 定义读写权限
accessModes:
- ReadWriteMany
# 请求资源
resources:
requests:
# 存储空间 5G
storage: 5Gi

2、创建pv yaml文件
vim pv.yaml

apiVersion: v1
# PV类型
kind: PersistentVolume
metadata:
# PV名称与PVC相同
name: my-pv
spec:
# 定义容量
capacity:
storage: 5Gi
# 读写权限
accessModes:
- ReadWriteMany
# nfs分配网络存储
nfs:
path: /data/nfs
server: 192.168.1.115

3、执行创建pv容器

kubectl apply -f pv.yaml 

4、查看创建pv
kubectl get pv

NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
# RWX:读写模式、Available:为可用状态
my-pv 5Gi RWX Retain Available 23s

5、创建pvc容器

kubectl apply -f pvc.yaml

6、查看pvc创建容器
kubectl get pod

NAME READY STATUS RESTARTS AGE
nginx6 1/1 Running 0 20s

7、查看pv与pvc状态
kubectl get pv,pvc

NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
# Bound:已经使用以成绑定装填
persistentvolume/my-pv 5Gi RWX Retain Bound default/my-pvc 3m44s
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
# 以绑定到pv
persistentvolumeclaim/my-pvc Bound my-pv 5Gi RWX 78s

8、nfs共享目录下创建一个index.html

echo "<h1>xxxxxxxxx</h1>" > /data/nfs/index.html

9、进入容器测试
kubectl exec -it nginx6 bash

ls /usr/share/nginx/html
index.html
05-18 05:07