前面的一篇笔记中已经记录了如何搭建一个NFS系统。这篇笔记就记录如何将Pod中容器目录挂载到文件系统
1.原生方式挂载
原生方式挂载
- 在NFS上创建指定目录
- 在Deployment的yaml中配置要挂载路径
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: nginx-pv-demo
name: nginx-pv-demo
spec:
replicas: 2
selector:
matchLabels:
app: nginx-pv-demo
template:
metadata:
labels:
app: nginx-pv-demo
spec:
containers:
- image: nginx
name: nginx
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html
volumes:
- name: html
nfs:
server: 172.31.0.4
path: /nfs/data/nginx-pv
在NFS上创建目录
mkdir -p /nfs/data/nginx-pv
编写配置yaml并部署
vi mountdemo.yaml
kubectl apply -f mountdemo.yaml
在宿主机的/nfs/data/nginx-pv中写一个index.html
- 访问Pod
2.PV和PVC使用
上面的挂载方式弊端
- Pod被清除,存储空间不会自动被释放
- 需要手动创建好对应目录
2.1PV和PVC的概念
PV:持久卷(Persistent Volume),将应用需要持久化的数据保存到指定位置 PVC:持久卷申明(Persistent Volume Claim),申明需要使用的持久卷规格
- PV:就是一个个目录
- PVC:Pod需要PV的信息声明(要xx大小的PV)
- PV池:好多PV的集合。Pod需要从池中取
上图画的PV分布在不同主机,其实PV就是在一个NFS里。不存在在哪个主机之说
2.2PV的创建
PV本质上是目录,所以第一步创建PV对应的目录
#nfs主节点
mkdir -p /nfs/data/01
mkdir -p /nfs/data/02
mkdir -p /nfs/data/03
将目录映射成PV ```yaml
vi pv.yaml
kubectl apply -f xxx.yaml
apiVersion: v1 kind: PersistentVolume metadata: name: pv01-10m spec: capacity: storage: 10M accessModes:
- ReadWriteMany storageClassName: nfs nfs: path: /nfs/data/01 server: 172.31.0.4
apiVersion: v1 kind: PersistentVolume metadata: name: pv02-1gi spec: capacity: storage: 1Gi accessModes:
- ReadWriteMany
storageClassName: nfs nfs: path: /nfs/data/02
server: 172.31.0.4
apiVersion: v1 kind: PersistentVolume metadata: name: pv03-3gi spec: capacity: storage: 3Gi accessModes:
- ReadWriteMany
storageClassName: nfs nfs: path: /nfs/data/03 server: 172.31.0.4
3. 查看pv
```yaml
[root@k8s-master ~]# kubectl get pv
2.3PVC的创建
创建yaml并apply
#vi pvc.yaml
#kubectl apply -f pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: nginx-pvc
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 200Mi
storageClassName: nfs
查看pvc
kubectl get pvc
同时查看pvc和pv
kubectl get pv,pvc
pv有 10m 1g 3g的但是pvc需要200m,最优的是使用 1G的
2.4PVC方式挂载
#vi nginx-pvc.yaml
#kubectl apply -f xxx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: nginx-deploy-pvc
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