基于容器的微服务架构中,分布式持久化方案并没有一个默认的最好方案,这里使用NFS来作为容器持久化方案。
NFS服务需要在服务器及需要挂载的客户端上分别安装配置。
nfs-utils包含服务:
rpcbind : The rpcbind server converts RPC program numbers into universal addresses.
nfs-server : It enables the clients to access NFS shares.
nfs-lock / rpc-statd : NFS file locking. Implement file lock recovery when an NFS server crashes and reboots.
nfs-idmap : It translates user and group ids into names, and to translate user and group names
into ids
nfs相关配置文件:
/etc/exports : It is a main configuration file, controls which file systems are exported to remote hosts and specifies options.
/etc/fstab : This file is used to control what file systems including NFS directories are mounted when the system boots.
/etc/sysconfig/nfs : This file is used to control which ports the required RPC services run on.
/etc/hosts.allow, and /etc/hosts.deny : These files are called TCP wrappers, controls the access to NFS server. It is used by NFS to decide whether or not to accept a connection coming in from another IP address
NFS服务器安装:
Step1:安装及启动
1 yum install nfs-utils libnfsidmap 2 systemctl enable rpcbind 3 systemctl enable nfs-server 4 systemctl start rpcbind 5 systemctl start nfs-server 6 systemctl start rpc-statd 7 systemctl start nfs-idmapd
Step2:创建共享路径,并开放访问
1 mkdir /data1/share
2 chmod 777 /data1/share
3 echo '/data1/share 10.200.xx.xx/24(rw,sync,no_root_squash)' >>/etc/exports
其中,IP地址参数指定了需要挂载的client的IP,用掩码模式则指定IP段;
rw表示挂载路径允许读写权限;
sync表示同步模式,默认情况下是异步模式,由于是共享路径所以采用同步模式;
no_root_squash表示客户端能够以root权限操作挂载点文件系统。
Step3:更新挂载文件信息
exportfs -r
exportfs指令的各个参数说明如下:
exportfs -v : Displays a list of shares files and export options on a server
exportfs -a : Exports all directories listed in /etc/exports
exportfs -u : Unexport one or more directories
exportfs -r : Reexport all directories after modifying /etc/exports
Step4:配置服务器防火墙
firewall-cmd --permanent --zone public --add-service mountd
firewall-cmd --permanent --zone public --add-service rpc-bind
firewall-cmd --permanent --zone public --add-service nfs
firewall-cmd --reload
NFS客户端安装:
Step1:安装及启动
yum -y install nfs-utils libnfsidmap systemctl enable rpcbind systemctl start rpcbind
Step2:确认NFS服务器挂载信息
showmount -e 10.200.xx.xx
如果返回信息如下:
Export list for 10.200.xx.xx: /data1/share 10.200.xx.xx/24
其中,IP段包含了该客户端的IP地址,则该客户端可以挂载到远程NFS服务器。
Step3:创建挂载URL并挂载NFS服务器
1 mkdir /mnt/share
2 mount 10.200.xx.xx:/data1/share /mnt/share
挂载后,通过以下指令查看挂载信息:
mount | grep nfs
正常情况下能够看到挂载到的NFS服务器的挂载信息,也可以通过
df -hT
来查看挂载信息,类型为nfs4的挂载即是到NFS服务器的挂载点。
客户端挂载信息的持久化:
默认情况下,客户端的挂载信息将在服务器reboot后丢失,需要重新挂载。与硬盘挂载相同,可以通过在/etc/fstab中添加NFS挂载信息来持久化挂载:
echo '10.200.xx.xx:/data1/share/ /mnt/share nfs rw,sync,hard,intr 0 0' >>/etc/fstab
而解除挂载,可以直接在NFS客户端使用unmount进行:
unmount /mnt/share