CacheFiles是NFS缓存功能的文件系统之一,还有一个是CacheFS。关于FS-Cache、CacheFiles、CacheFS三者之间的区别,可以查看:https://people.redhat.com/dhowells/fscache/FS-Cache.pdf
一、环境
redhat or centos
二、安装
在使用CacheFiles之前,我们需要先使用yum update更新。若不想全部更新,可以选择性更新 selinux-policy-targeted和selinux-policy 到最新版本。原因是因为一个早在2002年就有的bug,详情可以查看:https://bugzilla.redhat.com/show_bug.cgi?id=841425https://bugzilla.redhat.com/show_bug.cgi?id=841425
yum -y update selinux-policy-targeted selinux-policy yum -y install cachefilesd
三、配置
vim /etc/cachefilesd.conf
###############################################################################
#
# Copyright (C) 2006,2010 Red Hat, Inc. All Rights Reserved.
# Written by David Howells (dhowells@redhat.com)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
#
###############################################################################
dir /var/cache/fscache # 自定义缓存目录
tag mycache # 标签,用于区分多个缓存
brun 10% # 剩余空间大于这个设置值时,自动进行缓存
bcull 7% # 剩余空间大于这个设置值时,开始自动删除使用频率最低的缓存
bstop 3% # 剩余空间小于这个设置值时,缓存功能关闭
# 开头的参数则是按照文件数来计算%, 其原理与根据block数(剩余空间)计算是一样的。
frun 10%
fcull 7%
fstop 3%
# Assuming you're using SELinux with the default security policy included in
# this package
secctx system_u:system_r:cachefiles_kernel_t:s0
我这里使用默认配置,关于配置文件更详细的参数说明可以查阅:https://www.cyberciti.biz/files/linux-kernel/Documentation/filesystems/caching/cachefiles.txt
四、启动cachefiles服务
systemctl restart cachefilesd
systemctl status cachefilesd
五、怎么使用缓存功能
1、nfs服务端(192.169.1.1)
在nfs服务端共享一个/mnt
目录,重启nfs服务
$ echo "/mnt *(rw,sync) >> /etc/export"
$ systemctl restart rpcbind
$ systemctl restart nfs
2、在nfs客户端(192.168.1.2)
直接mount服务端共享的目录到本地的/mnt
目录,必须使用-o fsc
参数选项
$ mount -t nfs -o fsc,sync,proto=tcp 192.168.1.1:/mnt /mnt
六、如何验证缓存有没有成功
1、在服务端
使用dd
命令生成一个2G大的文件
$ dd if=/dev/zero of=/mnt/test bs=1G count=2
$ du -sh /mnt/test
2.0G /mnttest
2、在客户端
在客户端/mnt/
目录下,我们会看到刚在服务端dd
产生的2G文件test
,现在我们用time
命令来查看两次copy
该文件所需时间
$ du -sh /mnt/test
2.0G /mnt/test
# 在copy文件前,我们看看cachefiles缓存目录的大小
$ du -sh /var/cache/fscache
52K /var/cache/fscache
$ time cp /mnt/test /tmp
real 0m20.294s
user 0m0.009s
sys 0m3.358s
# 再观察cachefiles缓存目录大小
$ du -sh /var/cache/fscache
2.1G /var/cache/fscache
$ time cp /mnt/test /var
real 0m7.803s
user 0m0.003s
sys 0m1.909s
一目了然,第二次copy文件时间仅花了7秒,比第一次20秒快了将近3倍,而我们的缓存目录大小也变了。所以我们的缓存成功了,这样一来,我们可以避免第二次操作同一文件时走网络,从而减少网络IO。