一、概述
Docker使用数据卷和数据卷容器来进行数据的持久化。
Pod是Kubernetes的基本单位,包含一个或多个容器。
Pod内部的容器存在共享数据存储的需求,Kubernetes给出了类似容器Volum(卷)的概念,与Pod具有相同的生命周期。
使用Volum,就需要知道存储方案的技术细节,通常由运维人员来维护。而对开发人员来说,只想知道我需要多大的存储空间、I/O是否能满足要求等等这些细节。
为此,Kubernetes在Volum的基础上做了进一步抽象,提出了PV(PersistentVolume,持久化卷)、PVC(PersistentVolumeClaim,持久化卷声明)。
PV相当于一块硬盘,由运维人员提供。
PVC是对存储需求的声明,由开发人员提供。
PVC可类比POD来理解,POD申请的是CPU和内存资源,而PVC则是申请存储资源。
PV一般是预先手动创建的,开发人员每次都申请,则比较麻烦。是否可以根据PVC的要求,自动创建呢?
Kubernetes提出了 Sotrage Class ,实现PV的动态供给。
**二、实践
**
(一)配置NFS服务器
NFS(Network File System)是一种分布式文件系统协议,可以通过网络,让不同的客户端,可以彼此访问共同的文件系统 ,来实现文件的共享。NFS服务器提供可以挂载的目录,客户端直接挂载在本地端的目录。
- 安装NFS服务器
apt-get install nfs-kernel-server
- 配置NFS服务器
/etc/exports为NFS服务器的配置文件。
root@linux:/tmp# vi /etc/exports
# /etc/exports: the access control list for filesystems which may be exported
#to NFS clients. See exports(5).
# 访问控制列表,以便NFS客户端配置
# Example for NFSv2 and NFSv3:
# /srv/homes hostname1(rw,sync,no_subtree_check) hostname2(ro,sync,no_subtree_check)
#
# Example for NFSv4:
# /srv/nfs4 gss/krb5i(rw,sync,fsid=0,crossmnt,no_subtree_check)
# /srv/nfs4/homes gss/krb5i(rw,sync,no_subtree_check)
#
/tmp *(rw,no_root_squash,sync)
参数说明:
rw:设置目录可读写;
no_root_squash:客户端连接服务端时,如果是root用户,那么,将拥有服务端目录的root权限;
sync:将数据同步写入内存缓冲区与磁盘中,效率低,但可以保证数据的一致性;
- NFS服务器的启动
root@linux:/# service nfs-kernel-server start
- NFS客户端配置
(1)安装nfs-common
root@linux:/# apt install nfs-common
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following package was automatically installed and is no longer required:
grub-pc-bin
Use 'apt autoremove' to remove it.
The following additional packages will be installed:
keyutils libnfsidmap2 libtirpc1 rpcbind
Suggested packages:
watchdog
The following NEW packages will be installed:
keyutils libnfsidmap2 libtirpc1 nfs-common rpcbind
0 upgraded, 5 newly installed, 0 to remove and 345 not upgraded.
Need to get 399 kB of archives.
...
(2)将本地/mnt/nfs目录挂载在NFS服务器上/tmp
root@linux:/# mount -t nfs 30.0.1.90:/tmp /mnt/nfs
root@linux:/#
(5)新建测试文件:
root@linux:/# cd /mnt/nfs/
root@linux:/mnt/nfs# touch 1.txt
root@linux:/mnt/nfs# echo this is nfs test > 1.txt
root@linux:/mnt/nfs# ls
1.txt
(6)登录NFS服务器,便可查到文件:
root@linux:/tmp# ll
total 72
-rw-r--r-- 1 root root 1.txt
root@linux:/tmp# cat 1.txt
this is nfs test
root@linux:/tmp#
(二)Kubernetes挂载NFS
编写一个yaml文件,需要记住server地址和可供挂载的目录
apiVersion: v1
kind: Pod
metadata:
name: mountnfstest
namespace: kubetest #
spec:
containers:
- name: nginx
image: nginx:1.21.0
ports:
- containerPort: 80
volumeMounts:
- name: logs-volume
mountPath: /var/log/nginx
volumes:
- name: logs-volume
nfs:
server: 30.0.1.90 # NFS服务器地址
path: /tmp/ #可供挂载的目录
-
容器
+关注
关注
0文章
499浏览量
22139 -
Docker
+关注
关注
0文章
492浏览量
12006
发布评论请先 登录
相关推荐
Kubernetes存储体系解析 浅谈Volume概念的由来
![Kubernetes存储体系解析 浅谈Volume概念的由来](https://file.elecfans.com/web1/M00/CA/77/pIYBAF-IC0mALamhAAJhTrfo1qM655.png)
![](https://file1.elecfans.com/web2/M00/8E/8E/wKgZomTIYyKAAT_1AAArgRY3CJw110.png)
从零开始入门 K8s | 应用存储和持久化数据卷:核心知识
从零开始入门 K8s | 应用存储和持久化数据卷:存储快照与拓扑调度
从零开始入门 K8s | 应用存储和持久化数据卷:核心知识
电缆材料PVC和PUR有什么区别
pvc软板有什么特性_pvc老化时间是多久
![<b class='flag-5'>pvc</b>软板有什么特性_<b class='flag-5'>pvc</b>老化时间是多久](https://file.elecfans.com/web1/M00/4E/50/o4YBAFq7MauADkKXAAAcU_27iU0523.jpg)
光伏测试仪PV150/PV200/PV210数据导出手册
![光伏测试仪<b class='flag-5'>PV</b>150/<b class='flag-5'>PV</b>200/<b class='flag-5'>PV</b>210数据导出手册](https://file.elecfans.com//web2/M00/64/34/poYBAGMF6ZGAYy2zAABoXYsn4dQ443.png)
评论