Kubernetes 对 Pod 进行调度时,以当时集群中各节点的可用资源作为主要依据,自动选择某一个可用的节点,并将 Pod 分配到该节点上。在这种情况下,Pod 中容器数据的持久化如果存储在所在节点的磁盘上,就会产生不可预知的问题,例如,当 Pod 出现故障,Kubernetes 重新调度之后,Pod 所在的新节点上,并不存在上一次 Pod 运行时所在节点上的数据。
为了使 Pod 在任何节点上都能够使用同一份持久化存储数据,我们需要使用网络存储的解决方案为 Pod 提供 数据卷。常用的网络存储方案有:NFS/cephfs/glusterfs。
本文介绍一种使用 centos 搭建 nfs 服务器的方法。此方法仅用于测试目的,请根据您生产环境的实际情况,选择合适的 NFS 服务。
TIP
本章节中所有命令都以 root 身份执行
yum install -y nfs-utils
vim /etc/exports
,创建 exports 文件,文件内容如下:
/root/nfs_root/ *(insecure,rw,sync,no_root_squash)
# 创建共享目录,如果要使用自己的目录,请替换本文档中所有的 /root/nfs_root/
mkdir /root/nfs_root
systemctl enable rpcbind
systemctl enable nfs-server
systemctl start rpcbind
systemctl start nfs-server
exportfs -r
exportfs
# 输出结果如下所示
/root/nfs_root /root/nfs_root
TIP
本章节中所有命令都以 root 身份执行
执行以下命令安装 nfs 客户端所需的软件包
yum install -y nfs-utils
执行以下命令检查 nfs 服务器端是否有设置共享目录
# showmount -e $(nfs服务器的IP)
showmount -e 172.17.216.82
# 输出结果如下所示
Export list for 172.17.216.82:
/root/nfs_root *
执行以下命令挂载 nfs 服务器上的共享目录到本机路径 /root/nfsmount
mkdir /root/nfsmount
# mount -t nfs $(nfs服务器的IP):/root/nfs_root /root/nfsmount
mount -t nfs 172.17.216.82:/root/nfs_root /root/nfsmount
# 写入一个测试文件
echo "hello nfs server" > /root/nfsmount/test.txt
在 nfs 服务器上执行以下命令,验证文件写入成功
cat /root/nfs_root/test.txt
原文:https://www.cnblogs.com/lgj8/p/12186452.html