当前位置:网站首页>kubernetes资源对象介绍及常用命令(五)-(NFS&PV&PVC)
kubernetes资源对象介绍及常用命令(五)-(NFS&PV&PVC)
2022-07-02 23:32:00 【江小南】
存储抽象(NFS&PV&PVC)

kubernetes通过NFS网络文件系统,将每个节点的挂载数据进行同步,那么就保证了pod被故障转移等情况,依然能读取到被存储的数据。

安装NFS
所有节点执行
yum install -y nfs-utils
nfs主节点执行
echo "/nfs/data/ *(insecure,rw,sync,no_root_squash)" > /etc/exports # 暴露了目录/nfs/data/,`*`表示所有节点都可以访问。
mkdir -p /nfs/data
systemctl enable rpcbind --now
systemctl enable nfs-server --now
# 配置生效
exportfs -r
# 检查验证
[[email protected] ~]# exportfs
/nfs/data <world>
[[email protected] ~]#
从节点执行
# 展示172.31.0.2有哪些目录可以挂载
showmount -e 172.31.0.2 # ip改成自己的主节点ip
mkdir -p /nfs/data
# 将本地目录和远程目录进行挂载
mount -t nfs 172.31.0.2:/nfs/data /nfs/data
# 写入一个测试文件
echo "hello nfs server" > /nfs/data/test.txt

实战测试
原生方式数据挂载
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.2
path: /nfs/data/nginx-pv
[[email protected] ~]# kubectl apply -f nfsdata.yml
deployment.apps/nginx-pv-demo created
[[email protected] ~]#
说明:在部署之前首先在节点中创建目录mkdir -p /nfs/data/nginx-pv
。通过上面的方法,就实现了将容器内的/usr/share/nginx/html
目录挂载到了nfs文件系统的/nfs/data/nginx-pv
目录,后期修改非常方便。

PV&PVC
PV:持久卷(Persistent Volume),将应用需要持久化的数据保存到指定位置。
PVC:持久卷申明(Persistent Volume Claim),申明需要使用的持久卷规格。
创建PV池
静态供应
# nfs主节点执行
[[email protected] data]# mkdir -p /nfs/data/01
[[email protected] data]# mkdir -p /nfs/data/02
[[email protected] data]# mkdir -p /nfs/data/03
[[email protected] data]# ls
01 02 03 nginx-pv test.txt
[[email protected] data]# pwd
/nfs/data
[[email protected] data]#
创建PV,pv.yml
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.2
---
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.2
---
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.2
说明:将ip修改为自己nfs的主节点ip。这里将我们上面的3个文件夹创建成为3个PV,并且设定了大小storage
。形成一个PV池。注意name不能有大写字母。
[[email protected] ~]# kubectl apply -f pv.yml
persistentvolume/pv01-10m created
persistentvolume/pv02-1gi created
persistentvolume/pv03-3gi created
[[email protected] ~]# kubectl get persistentvolume
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pv01-10m 10M RWX Retain Available nfs 45s
pv02-1gi 1Gi RWX Retain Available nfs 45s
pv03-3gi 3Gi RWX Retain Available nfs 45s
[[email protected] ~]#
创建PVC与PV绑定,pvc.yml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: nginx-pvc
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 200Mi
storageClassName: nfs
创建了pvc,使用的大小为200Mi。这里注意storageClassName需要和pv的保持一致,相当于使用同一个标签才能找得到。
[[email protected] ~]# kubectl apply -f pvc.yml
persistentvolumeclaim/nginx-pvc created
[[email protected] ~]# kubectl get pvc,pv
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
persistentvolumeclaim/nginx-pvc Bound pv02-1gi 1Gi RWX nfs 14m
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
persistentvolume/pv01-10m 10M RWX Retain Available nfs 17m
persistentvolume/pv02-1gi 1Gi RWX Retain Bound default/nginx-pvc nfs 17m
persistentvolume/pv03-3gi 3Gi RWX Retain Available nfs 17m
[[email protected] ~]#
可以发现使用200Mi的PVC,会在PV池中绑定最佳的1Gi大小的pv02-1gi
的PV去使用,使资源利用最大化。同时看到pv池中pv02-1gi
的PV已经被使用,状态为Bound。
创建Pod绑定PVC,podpvc.yml
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
思路梳理:Pod-->PVC-->PV
[[email protected] ~]# kubectl apply -f podpvc.yml
deployment.apps/nginx-deploy-pvc created
[[email protected] ~]#
这时我们修改文件同样能和pod内进行同步。

小结
上面我们说的PV池是静态供应。就是提前在nfs中创建好了相应的文件目录,并且设定了大小等,供PVC进行绑定使用。还有一种动态供应,提前不需要创建PV,而是根据PVC的要求动态创建PV,这样资源利用更加合理有效。
边栏推荐
- logback配置文件
- Sysdig analysis container system call
- Pat 1030 travel plan (30 points) (unfinished)
- form表单实例化
- collections. What is the purpose of chainmap- What is the purpose of collections. ChainMap?
- TypeError: Cannot read properties of undefined (reading ***)
- UART、RS232、RS485、I2C和SPI的介绍
- Don't want teachers to see themselves with cameras in online classes? Virtual camera you deserve!
- JSON conversion tool class
- 程序分析与优化 - 9 附录 XLA的缓冲区指派
猜你喜欢
[shutter] Introduction to the official example of shutter Gallery (project introduction | engineering construction)
Which websites can I search for references when writing a thesis?
监控容器运行时工具Falco
logback配置文件
Automated defect analysis in electron microscopic images-论文阅读笔记
写论文可以去哪些网站搜索参考文献?
Multiprocess programming (II): Pipeline
Sysdig analysis container system call
字符设备注册常用的两种方法和步骤
Basic use of shell script
随机推荐
国外的论文在那找?
maya渔屋建模
About the practice topic of screen related to unity screen, unity moves around a certain point inside
Centos7 one click compilation to build MySQL script
论文的设计方案咋写?
pageoffice-之bug修改之旅
程序分析与优化 - 9 附录 XLA的缓冲区指派
Angled detection frame | calibrated depth feature for target detection (with implementation source code)
Multi process programming (III): message queue
数组常用操作方法整理(包含es6)及详细使用
Luogu_ P2010 [noip2016 popularization group] reply date_ Half enumeration
node_ Modules cannot be deleted
Pat 1030 travel plan (30 points) (unfinished)
Where can I find foreign papers?
【luogu P4320】道路相遇(圆方树)
pod生命周期详解
CMake基本使用
LeedCode1480. Dynamic sum of one-dimensional array
【单片机项目实训】八路抢答器
Preview word documents online