存储抽象
NFS 网络存储
服务端节点
# k8s13
# 1.安装 nfs
yum install -y nfs-utils
# 2.设置挂载路径
vim /etc/exports
/usr/local/data/k8s/data/nfs *(insecure,rw,sync,no_root_squash)
# 3.创建挂载路径(挂载路径需要在nfs服务启动前创建)
mkdir -p /usr/local/data/k8s/data/nfs
# 4配置生效
exportfs -r
# 5.启动 nfs服务
systemctl enable rpcbind --now
systemctl enable nfs-server --now
# 查询可挂载点
showmount -e 192.168.0.13
showmount -e k8s13
# 在从节点 k8s14 k8s15 节点设置
# 显示 nfs master 节点哪些目录可以挂载
showmount -e k8s13
客户端节点
yum install -y nfs-utils
k8s 中使用 nfs
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: nginx-demo
name: nginx-demo
spec:
replicas: 2
selector:
matchLabels:
app: nginx-demo
template:
metadata:
labels:
app: nginx-demo
spec:
containers:
- image: nginx
name: nginx
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html
volumes:
- name: html
nfs:
server: k8s13
path: /usr/local/data/k8s/data/nfs/nginx-pv
创建service
kubectl expose deployment nginx-demo --port=80 --target-port=80 --type=NodePort
PV&PVC
- PV:持久卷(Persistent Volume) - 生产者
持久化存储,对存储资源进行抽象,对外提供可以调用的地方( 将应用需要持久化的数据保存到指定位置 ) - PVC:持久卷申明(Persistent Volume Claim - 消费者
用于调用,不需要关心内部实现细节( 申明需要使用的持久卷规格 )
pv与pvc按照容量与权限模型来进行匹配
创建pv池
- 静态供应
# nfs 服务器节点
mkdir -p /usr/local/data/k8s/data/nfs/01
mkdir -p /usr/local/data/k8s/data/nfs/02
mkdir -p /usr/local/data/k8s/data/nfs/03
- 创建 pv
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv01-10m
spec:
capacity:
storage: 10M
accessModes:
- ReadWriteMany
storageClassName: nfs
nfs:
path: /usr/local/data/k8s/data/nfs/01
server: k8s13
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv02-1gi
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteMany
storageClassName: nfs
nfs:
path: /usr/local/data/k8s/data/nfs/02
server: 192.168.0.13
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv03-3gi
spec:
capacity:
storage: 3Gi
accessModes:
- ReadWriteMany
storageClassName: nfs
nfs:
path: /usr/local/data/k8s/data/nfs/03
server: 192.168.0.13
- PVC创建与绑定
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: nginx-pvc
spec:
accessModes:
- ReadWriteMany
storageClassName: nfs
resources:
requests:
storage: 200Mi
- 创建 Pod 绑定 PVC
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deploy-pvc
spec:
replicas: 2
selector:
matchLabels:
app: nginx-deploy-pvc
template:
metadata:
labels:
app: nginx-deploy-pvc
spec:
containers:
- image: nginx
name: nginx
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html
volumes:
- name: html
persistentVolumeClaim:
claimName: nginx-pvc