NFS
网络文件系统是linux系统和网络附加存储文件管理器常用的网络文件系统允许多个客户端通过共享文件访问,他可以用于提供对二进制目录的访问,也可以用于允许用户在统一工作组中从不同的客户端访问文件
1.在服务端
需要下载nfs服务,并指定客户的访问方式
[root@client138 ~]# yum install nfs-utils -y ##安装NFS服务
Loaded plugins: langpacks
Package 1:nfs-utils-1.3.0-0.el7.x86_64 already installed and latest version
Nothing to do
[root@client138 ~]# systemctl start nfs-server ##打开并设置开机启动
[root@client138 ~]# systemctl enable nfs-server
ln -s '/usr/lib/systemd/system/nfs-server.service' '/etc/systemd/system/nfs.target.wants/nfs-server.service'
[root@client138 ~]# systemctl stop firewalld ##关闭防火墙
[root@client138 ~]# systemctl disable firewalld
rm '/etc/systemd/system/basic.target.wants/firewalld.service'
rm '/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service'
新建磁盘分区vdb3
[root@server138 ~]# cat /proc/partitions
major minor #blocks name253 0 10485760 vda
253 1 10484142 vda1
253 16 10485760 vdb
253 17 512000 vdb1
253 18 512000 vdb2
253 19 204800 vdb3
252 0 507904 dm-0[root@server138 ~]# mkdir /shares ##新建根目录
[root@server138 ~]# mkfs.xfs /dev/vdb3 ##给磁盘铺设文件系统
meta-data=/dev/vdb3 isize=256 agcount=4, agsize=12800 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0
data = bsize=4096 blocks=51200, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal log bsize=4096 blocks=853, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@server138 ~]# mount /dev/vdb3 /shares ##挂载vdb3到根目录中
[root@server138 ~]# cd /shares/
[root@server138 shares]# touch file{1..5} ##新建一些文件做测试
[root@server138 shares]# vim /etc/exports ##进行NFS服务的配置
[root@server138 shares]# cat /etc/exports
/shares *(sync) ##标示将/shares所有分享出去
[root@server138 shares]# exportfs -rv
exporting *:/shares
2.客户端
保证和服务端可以连接,可以用ping测试,将共享设备挂载在/mnt下
[root@server88 ~]# mount 172.25.254.138:/shares /mnt ##挂载
[root@server88 ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 6941292 3532608 67% /
devtmpfs 469332 0 469332 0% /dev
tmpfs 484920 84 484836 1% /dev/shm
tmpfs 484920 12760 472160 3% /run
tmpfs 484920 0 484920 0% /sys/fs/cgroup
/dev/loop0 201472 10368 191104 6% /mnt
[root@server88 ~]# cd /mnt
[root@server88 mnt]# ls ##成功获得分享内容
file1 file2 file3 file4 file5
NFS自动挂载
不用的时候还继续挂载,浪费资源
在客户端:
yum install autofs -y
systemctl start autofs
[root@client130 ~]# cd /net/172.25.254.80/shares/
[root@client130 shares]# ls
file1 file2 file3 file4 file5
[root@client130 shares]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 10G 3.1G 7.0G 31% /
devtmpfs 906M 0 906M 0% /dev
tmpfs 921M 84K 921M 1% /dev/shm
tmpfs 921M 17M 904M 2% /run
tmpfs 921M 0 921M 0% /sys/fs/cgroup
172.25.254.80:/shares 1014M 32M 982M 4% /net/172.25.254.80/shares
退出目录过一会就会自动卸载(300s)
修改卸载时间
vim /etc/sysconfig/autofs
13 TIMEOUT=5
systemctl restart autofs
[root@client130 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 10G 3.1G 7.0G 31% /
devtmpfs 906M 0 906M 0% /dev
tmpfs 921M 84K 921M 1% /dev/shm
tmpfs 921M 17M 904M 2% /run
tmpfs 921M 0 921M 0% /sys/fs/cgroup
172.25.254.80:/shares 1014M 32M 982M 4% /net/172.25.254.80/shares
[root@client130 ~]# df -h ##等待5秒后自动卸载
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 10G 3.1G 7.0G 31% /
devtmpfs 906M 0 906M 0% /dev
tmpfs 921M 84K 921M 1% /dev/shm
tmpfs 921M 17M 904M 2% /run
tmpfs 921M 0 921M 0% /sys/fs/cgroup
修改默认挂载目录
默认挂载在/net/172.25.254.ip/docs目录下
现在挂载到/opt/nfs/sharedir
[root@client130 ~]# vim /etc/auto.master #里面写的是最终挂载点的上层目录
/opt/nfs /etc/auto.nfs ##文件名任意,需要自己创建
[root@client130 ~]# vim /etc/auto.nfs
[root@client130 ~]# cat /etc/auto.nfs
sharedir -ro 172.25.254.80:/shares
systemctl restart autofs
[root@client130 ~]# cd /opt/nfs/sharedir ##自动挂载到/opt/nfs/sharedir下
[root@client130 sharedir]# ls
file1 file2 file3 file4 file5
CIFS
nfs更优秀的地方在于,可以在用的时候挂载,不用时自动卸载
磁盘加密
新建了一个vdb3分区进行空分区加密
[root@server138 ~]# cryptsetup luksFormat /dev/vdb3 ##进行加密操作
WARNING!
========
This will overwrite data on /dev/vdb3 irrevocably.Are you sure? (Type uppercase yes): YES ##这里必须输入大写的YES
Enter passphrase:
Verify passphrase:
将/dev/vdb3分配给rsy,可以在自动建立文件
[root@server138 ~]# cryptsetup open /dev/vdb3 rsy
Enter passphrase for /dev/vdb3:
[root@server138 ~]# ll /dev/mapper/rsy
lrwxrwxrwx 1 root root 7 Jan 22 03:03 /dev/mapper/rsy -> ../dm-1
[root@server138 ~]# mkfs.xfs /dev/mapper/rsy
meta-data=/dev/mapper/rsy isize=256 agcount=4, agsize=31872 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0
data = bsize=4096 blocks=127488, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal log bsize=4096 blocks=853, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@server138 ~]# mount /dev/mapper/rsy /mnt ##可以成功挂载
[root@server138 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 10G 3.2G 6.9G 32% /
devtmpfs 459M 0 459M 0% /dev
tmpfs 474M 80K 474M 1% /dev/shm
tmpfs 474M 13M 462M 3% /run
tmpfs 474M 0 474M 0% /sys/fs/cgroup
/dev/vdb2 495M 26M 470M 6% /mnt
/dev/mapper/vg0-vo 473M 2.4M 442M 1% /home
[root@server138 ~]# umount /mnt
[root@server138 ~]# cryptsetup close rsy ##关闭加密,关闭以后,/dev/mapper/rsy文件消失
[root@server138 ~]# mount /dev/vdb3 /mnt ##原始设备无法挂载,因为加密过
mount: unknown filesystem type 'crypto_LUKS'
[root@server138 ~]# cryptsetup open /dev/vdb3 rsy ##重新进行设置
Enter passphrase for /dev/vdb3:
[root@server138 ~]# mount /dev/mapper/rsy /mnt ##又可以重新挂载
磁盘阵列
磁盘阵列(Redundant Arrays of Independent Disks,RAID),全称独立磁盘冗余阵列。
磁盘阵列是由很多廉价的磁盘,组合成一个容量巨大的磁盘组,利用个别磁盘提供数据所产生加成效果提升整个磁盘系统效能。利用这项技术,将数据切割成许多区段,分别存放在各个硬盘上。
利用同位检查(ParityCheck)的观念,在数组中任意一个硬盘故障时,仍可读出数据,在数据重构时,将数据经计算后重新置入新硬盘中。
相同的数据存储在多个硬盘的不同的地方的方法。通过把数据放在多个硬盘上(冗余),输入输出操作能以平衡的方式交叠,改良性能。因为多个硬盘增加了平均故障间隔时间(MTBF),储存冗余数据也增加了容错。
首先,新建三块分区设置磁盘阵列
[root@localhost ~]# fdisk /dev/vdb
Command (m for help): t
Partition number (1-3, default 3): 1
Hex code (type L to list all codes): fd ##Linux raid autodetect
Changed type of partition 'Linux' to 'Linux raid autodetect'Device Boot Start End Blocks Id System
/dev/vdb1 2048 2099199 1048576 fd Linux raid autodetect ##新建三块分区并设置标签
/dev/vdb2 2099200 4196351 1048576 fd Linux raid autodetect
/dev/vdb3 4196352 6293503 1048576 fd Linux raid autodetect
[root@localhost ~]# partprobe ##同步新建分区
mdadm命令:
-C | -a | -l | -n | -x |
建立 | 目录不存在时自动建立 | ride工作方式 | 使用磁盘数目 | 备用磁盘数目 |
[root@localhost ~]# mdadm -C /dev/md0 -a yes -l 1 -n 2 -x 1 /dev/vdb{1..3} ##设置磁盘阵列工作方式
mdadm: Note: this array has metadata at the start and
may not be suitable as a boot device. If you plan to
store '/boot' on this device please ensure that
your boot-loader understands md/v1.x metadata, or use
--metadata=0.90
Continue creating array? yes
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.
[root@localhost ~]# mkfs.xfs /dev/md0 ##铺设文件系统
[root@localhost ~]# mount /dev/md0 /mnt ##挂载
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 10G 3.1G 7.0G 31% /
devtmpfs 459M 0 459M 0% /dev
tmpfs 474M 84K 474M 1% /dev/shm
tmpfs 474M 13M 462M 3% /run
tmpfs 474M 0 474M 0% /sys/fs/cgroup
/dev/md0 1021M 33M 988M 4% /mnt
测试:
mdadm -D /dev/md0 ##查看此时的阵列磁盘信息
mdadm -f /dev/md0 /dev/vdb1 ##破坏某个磁盘分区
mdadm -r /dev/md0 /dev/vdb1 ##删除
mdadm -a /dev/md0 /dev/vdb1 ##恢复
mdadm -S /dev/md0 ##结束磁盘服务
[root@localhost ~]# mdadm -D /dev/md0
/dev/md0:
Version : 1.2
Creation Time : Tue Jan 22 04:12:11 2019
Raid Level : raid1
Array Size : 1048000 (1023.61 MiB 1073.15 MB)
Used Dev Size : 1048000 (1023.61 MiB 1073.15 MB)
Raid Devices : 2
Total Devices : 3
Persistence : Superblock is persistentUpdate Time : Tue Jan 22 04:13:14 2019
State : clean
Active Devices : 2
Working Devices : 3
Failed Devices : 0
Spare Devices : 1Name : localhost:0 (local to host localhost)
UUID : f95cf836:cebe7786:1eae96d1:dc2fcf8f
Events : 17Number Major Minor RaidDevice State
0 253 17 0 active sync /dev/vdb1 ##标示1,2分区在工作
1 253 18 1 active sync /dev/vdb22 253 19 - spare /dev/vdb3 ##标示3分区空闲
[root@localhost ~]# watch -n 1 cat /proc/mdstat
这是一个监视窗口,可以看到三块阵列磁盘的工作情况
作为测试,可以破坏/dev/vdb1分区,发现vdb3接管vdb1的任务,变为2,3分区一起工作
[root@localhost ~]# mdadm -f /dev/md0 /dev/vdb1
mdadm: set /dev/vdb1 faulty in /dev/md0watch窗口中:
Personalities : [raid1]
md0 : active raid1 vdb3[2] vdb2[1] vdb1[0](F)
1048000 blocks super 1.2 [2/2] [UU][root@localhost ~]# mdadm -D /dev/md0
Number Major Minor RaidDevice State
2 253 19 0 active sync /dev/vdb3
1 253 18 1 active sync /dev/vdb20 253 17 - faulty /dev/vdb1
[root@localhost ~]# mdadm -r /dev/md0 /dev/vdb1 ##删除
mdadm: hot removed /dev/vdb1 from /dev/md0
[root@localhost ~]# mdadm -a /dev/md0 /dev/vdb1 ##恢复
mdadm: added /dev/vdb1[root@localhost ~]# umount /mnt
[root@localhost ~]# mdadm -S /dev/md0 ##结束磁盘服务
mdadm: stopped /dev/md0