使用NFS作为Glance存储后端

NFS服务介绍
NFS网络文件系统提供了一种在类UNIX系统上共享文件的方法。目前NFS有3个版本:NFSv2、NFSv3、NFSv4。CentOS7默认使用NFSv4提供服务,优点是提供了有状态的连接,更容易追踪连接状态,增强安全性。NFS监听在TCP 2049端口上。客户端通过挂载的方式将NFS服务器端共享的数据目录挂载到本地目录下。在客户端看来,使用NFS的远端文件就像是在使用本地文件一样,只要具有相应的权限就可以使用各种文件操作命令(如cp、cd、mv和rm等),对共享的文件进行相应的操作。Linux操作系统既可以作为NFS服务器也可以作为NFS客户,这就意味着它可以把文件系统共享给其他系统,也可以挂载从其他系统上共享的文件系统。
为什么需要安装NFS服务?当服务器访问流量过大时,需要多台服务器进行分流,而这多台服务器可以使用NFS服务进行共享。NFS除了可以实现基本的文件系统共享之外,还可以结合远程网络启动,实现无盘工作站(PXE启动系统,所有数据均在服务器的磁盘阵列上)或瘦客户工作站(本地自动系统)。NFS应用场景多为高可用文件共享,多台服务器共享同样的数据,但是它的可扩展性比较差,本身高可用方案不完善。取而代之,数据量比较大的可以采用MFS、TFS、HDFS等分布式文件系统。

NFS服务安装与配置

此处nfs-server节点使用的基础镜像为CentOS7.9,该基础镜像中已经安装了NFS服务,查看当前安装的NFS服务

① nfs-utils:包含NFS服务器端守护进程和NFS客户端相关工具。
② rpcbind:提供RPC的端口映射的守护进程及其相关文档、执行文件等。

[root@nfs-serverr ~]# rpm -qa | grep nfs-utils
nfs-utils-1.3.0-0.61.el7.x86_64
[root@nfs-server ~]# rpm -qa | grep rpcbind
rpcbind-0.2.0-47.el7.x86_64
[root@nfs-server ~]# 

创建一个目录作为NFS的共享目录

[root@nfs-server ~]# mkdir /mnt/test
[root@nfs-server ~]# ll /mnt/
total 0
drwxr-xr-x 2 root root 6 May  7 12:58 test

创建完共享目录后,编辑NFS服务的配置文件/etc/exports

[root@nfs-server ~]# cat /etc/exports
/mnt/test 10.60.3.0/24(rw,no_root_squash,no_all_squash,sync,anonuid=501,anongid=501)
[root@nfs-server ~]# exportfs -r
[root@nfs-server ~]# 

配置文件说明:

● /mnt/test:为共享目录(若没有这个目录,请新建一个)。

● 10.60.3.0/24:可以为一个网段,一个IP,也可以是域名。域名支持通配符,例如,*.qq.com。

● rw:read-write,可读写。

● ro:read-only,只读。

● sync:文件同时写入硬盘和内存。

● async:文件暂存于内存,而不是直接写入内存。

● wdelay:延迟写操作。

● no_root_squash:NFS客户端连接服务端时,如果使用的是root,那么对服务端共享的目录来说,也拥有root权限。显然开启这项是不安全的。

● root_squash:NFS客户端连接服务端时,如果使用的是root,那么对服务端共享的目录来说,拥有匿名用户权限,通常它将使用nobody或nfsnobody身份。

● all_squash:不论NFS客户端连接服务端时使用什么用户,对服务端共享的目录来说,都拥有匿名用户权限。

● anonuid:匿名用户的UID(User Identification,用户身份证明)值,可以在此处自行设定。

● anongid:匿名用户的GID(Group Identification,共享资源系统使用者的群体身份)

NFS服务启动

nfs-server端命令启动NFS的相关服务,并配置开机启动。

root@nfs-server ~]# systemctl start rpcbind
[root@nfs-server ~]# systemctl start nfs
[root@nfs-server ~]# systemctl enable rpcbind
[root@nfs-server ~]# systemctl enable nfs-server
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
[root@controller ~]# 

nfs-server端查看可挂载目录

[root@nfs-server ~]# showmount -e 10.60.3.21
Export list for 10.60.3.21:
/mnt/test 10.60.3.0/24
[root@nfs-server ~]# 

至此,NFS服务的Server端配置完毕,接下来要让Controller节点作为NFS的Client端,配置Glance服务的后端存储使用NFS服务

配置NFS作为Glance后端存储

Controller节点,查看是否安装了NFS服务的客户端

[root@controller ~]#  rpm -qa |grep nfs-utils
nfs-utils-1.3.0-0.61.el7.x86_64

挂载目录

在挂载目录之前,必须要弄清楚一件事情,就是Glance服务的后端存储在哪里,或者说,使用glance image-create命令上传的镜像会被存放到哪里。镜像会被存放到/var/lib/glance/images目录下,关于这个路径,感兴趣的读者可以自行上传镜像测试

知道了Glance的存储路径,就可以挂载该目录到NFS服务了

[root@controller ~]# mount -t nfs 10.60.3.20:/mnt/test /var/lib/glance/images/
[root@controller ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/vda1             100G   11G   90G  11% /
devtmpfs              3.8G     0  3.8G   0% /dev
tmpfs                 3.9G     0  3.9G   0% /dev/shm
tmpfs                 3.9G   17M  3.9G   1% /run
tmpfs                 3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/loop0            4.2G  4.2G     0 100% /opt/centos
/dev/loop1            3.6G  3.6G     0 100% /opt/iaas
tmpfs                 783M     0  783M   0% /run/user/0
10.60.3.20:/mnt/test  100G  967M  100G   1% /var/lib/glance/images
[root@controller ~]# 

修改配置

在做完挂载操作后,此时Glance服务还不能正常使用,若使用glance image-create命令上传镜像的话,会报错,因为此时images目录的用户与用户组不是glance,而是root,需要把images目录的用户与用户组进行修改

[root@controller images]# cd /var/lib/glance/
[root@controller glance]# chown glance:glance images/
[root@controller glance]# ll
total 0
drwxr-xr-x 2 glance glance 6 May  7 13:09 images
[root@controller glance]# 

测试验证

这个时候,Glance服务就可以正常使用了,使用centos镜像进行测试

[root@controller images]# glance image-create --disk-format qcow2 --container-format bare --name centos --progress --file ./CentOS_7.5_x86_64_XD.qcow2 
[=============================>] 100%
+------------------+--------------------------------------+
| Property         | Value                                |
+------------------+--------------------------------------+
| checksum         | 3d3e9c954351a4b6953fd156f0c29f5c     |
| container_format | bare                                 |
| created_at       | 2022-05-07T13:18:54Z                 |
| disk_format      | qcow2                                |
| id               | 2afee0d2-cb11-47e0-890f-c6f49c089a04 |
| min_disk         | 0                                    |
| min_ram          | 0                                    |
| name             | centos                               |
| owner            | bf36698d9afd45c4b46c1933469a9101     |
| protected        | False                                |
| size             | 510459904                            |
| status           | active                               |
| tags             | []                                   |
| updated_at       | 2022-05-07T13:19:52Z                 |
| virtual_size     | None                                 |
| visibility       | shared                               |
+------------------+--------------------------------------+

可以看到上传镜像成功。查看images目录下的文件

[root@controller images]# cd /var/lib/glance/images/
[root@controller images]# ll
total 498496
-rw-r----- 1 glance glance 510459904 May  7 13:19 2afee0d2-cb11-47e0-890f-c6f49c089a04
[root@controller images]# 

然后回到nfs-server节点,查看/mnt/test下的文件

[root@nfs-server ~]# cd /mnt/test/
[root@nfs-server test]# ll
total 498496
-rw-r----- 1 161 161 510459904 May  7 13:19 2afee0d2-cb11-47e0-890f-c6f49c089a04
[root@nfs-server test]# 

文件的ID相同,验证NFS作为Glance镜像服务的后端存储成功。