当前位置:网站首页>kubernetes资源对象介绍及常用命令(五)-(NFS&PV&PVC)
kubernetes资源对象介绍及常用命令(五)-(NFS&PV&PVC)
2022-07-03 17:06:00 【傻啦猫@[email protected]】
存储抽象(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,这样资源利用更加合理有效。
本文由 mdnice 多平台发布
版权声明
本文为[傻啦猫@[email protected]]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_45842494/article/details/125578351
边栏推荐
- Rsync远程同步
- Shentong express expects an annual loss of nearly 1billion
- [combinatorics] polynomial theorem (polynomial coefficients | full arrangement of multiple sets | number of schemes corresponding to the ball sub model | polynomial coefficient correlation identity)
- 手把手带你入门 API 开发
- RedHat 6.2 configuring ZABBIX
- Life is still confused? Maybe these subscription numbers have the answers you need!
- UCORE overview
- How to promote cross department project collaboration | community essay solicitation
- Network security web penetration technology
- BYD and great wall hybrid market "get together" again
猜你喜欢
NLP four paradigms: paradigm 1: fully supervised learning in the era of non neural networks (Feature Engineering); Paradigm 2: fully supervised learning based on neural network (Architecture Engineeri
建立自己的网站(23)
MySQL Basics
Prepare for the golden three silver four, 100+ software test interview questions (function / interface / Automation) interview questions. win victory the moment one raises one 's standard
New library online | cnopendata complete data of Chinese insurance institution outlets
CC2530 common registers for watchdog
Recommendation of good books on learning QT programming
Bcvp developer community 2022 exclusive peripheral first bullet
13mnnimo5-4 German standard steel plate 13MnNiMo54 boiler steel 13MnNiMo54 chemical properties
设计电商秒杀
随机推荐
Take you to API development by hand
function overloading
What kind of material is 14Cr1MoR? Analysis of chemical composition and mechanical properties of 14Cr1MoR
visual studio “通常每个套接字地址(协议/网络地址/端口)只允许使用一次“
在iptables防火墙下开启vsftpd的端口
What material is sa537cl1? Sa537cl1 corresponds to the national standard material
The most complete postman interface test tutorial in the whole network, API interface test
Depth first search of graph
What is your income level in the country?
C语言字符串练习
关于学习Qt编程的好书精品推荐
[try to hack] active detection and concealment technology
How SVN views modified file records
Shentong express expects an annual loss of nearly 1billion
2022.02.14_ Daily question leetcode five hundred and forty
What is the material of 13mnnimor? 13mnnimor steel plate for medium and low temperature pressure vessels
浅谈拉格朗日插值及其应用
Bcvp developer community 2022 exclusive peripheral first bullet
mysql用户管理
Meituan side: why does thread crash not cause JVM crash