当前位置:网站首页>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
边栏推荐
- [JDBC] API parsing
- [combinatorics] recursive equation (definition of general solution | structure theorem of general solution of recursive equation without multiple roots)
- vs code 插件 koroFileHeader
- RF analyze demo build step by step
- How do large consumer enterprises make digital transformation?
- 【Try to Hack】主动侦查隐藏技术
- Daily code 300 lines learning notes day 10
- One brush 142 monotone stack next larger element II (m)
- 简单配置PostFix服务器
- What material is sa537cl2? Analysis of mechanical properties of American standard container plate
猜你喜欢

13mnnimo5-4 German standard steel plate 13MnNiMo54 boiler steel 13MnNiMo54 chemical properties

What is the pledge pool and how to pledge?

免费数据 | 新库上线 | CnOpenData中国保险中介机构网点全集数据

图之深度优先搜索

人生还在迷茫?也许这些订阅号里有你需要的答案!

How do large consumer enterprises make digital transformation?

The most complete postman interface test tutorial in the whole network, API interface test

美团一面:为什么线程崩溃崩溃不会导致 JVM 崩溃

New features of C 10

Depth first search of graph
随机推荐
【Try to Hack】主动侦查隐藏技术
[2. Basics of Delphi grammar] 1 Identifiers and reserved words
MySQL user management
Meituan side: why does thread crash not cause JVM crash
Static program analysis (I) -- Outline mind map and content introduction
Capacités nécessaires à l'analyse des données
One brush 142 monotone stack next larger element II (m)
Daily code 300 lines learning notes day 10
New library online | cnopendata China bird watching record data
匯編實例解析--實模式下屏幕顯示
Host based intrusion system IDS
静态程序分析(一)—— 大纲思维导图与内容介绍
數據分析必備的能力
[2. Basics of Delphi grammar] 2 Object Pascal data type
27. Input 3 integers and output them in descending order. Pointer method is required.
CC2530 common registers for timer 1
[combinatorial mathematics] counting model, common combinatorial numbers and combinatorial identities**
Rsync远程同步
Apache service suspended asynchronous acceptex failed
HP 阵列卡排障一例