当前位置:网站首页>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,这样资源利用更加合理有效。
边栏推荐
- TypeError: Cannot read properties of undefined (reading ***)
- Two common methods and steps of character device registration
- 详解用OpenCV的轮廓检测函数findContours()得到的轮廓拓扑结构(hiararchy)矩阵的意义、以及怎样用轮廓拓扑结构矩阵绘制轮廓拓扑结构图
- 在线预览Word文档
- 多进程编程(二):管道
- maya渔屋建模
- Automated defect analysis in electron microscopic images-论文阅读笔记
- [IELTS reading] Wang Xiwei reading P1 (reading judgment question)
- Centos7 one click compilation to build MySQL script
- Introduction of UART, RS232, RS485, I2C and SPI
猜你喜欢
Rust所有权(非常重要)
What are the recommended thesis translation software?
Bigder: how to deal with the bugs found in the 32/100 test if they are not bugs
Introduction and use of ftrace tool
logback配置文件
详解用OpenCV的轮廓检测函数findContours()得到的轮廓拓扑结构(hiararchy)矩阵的意义、以及怎样用轮廓拓扑结构矩阵绘制轮廓拓扑结构图
多进程编程(一):基本概念
Automated defect analysis in electron microscopic images-论文阅读笔记
Automated defect analysis in electronic microscopic images
Seckill system design
随机推荐
DotNet圈里一个优秀的ORM——FreeSql
简单聊聊运维监控的其他用途
监控容器运行时工具Falco
An excellent orm in dotnet circle -- FreeSQL
Luogu_ P2010 [noip2016 popularization group] reply date_ Half enumeration
NC24325 [USACO 2012 Mar S]Flowerpot
Centos7 one click compilation to build MySQL script
setInterval定时器在ie不生效原因之一:回调的是箭头函数
The most painful programming problem in 2021, adventure of code 2021 Day24
Rust字符串切片、结构体和枚举类
kubernetes编写yml简单入门
Where can I check the foreign literature of economics?
[target detection] r-cnn, fast r-cnn, fast r-cnn learning
使用jenkins之二Job
Install docker and use docker to install MySQL
Should you study kubernetes?
University of Toronto:Anthony Coache | 深度强化学习的条件可诱导动态风险度量
【单片机项目实训】八路抢答器
One of the reasons why setinterval timer does not take effect in ie: the callback is the arrow function
关于QByteArray存储十六进制 与十六进制互转