概念:
- Linux自带的多路径软件叫做multipath,指服务器通过多条物理路径连接到块存储设备。
- 当因为主机HBA卡、线缆、交换机或者存储设备的RAID控制器故障等原因造成一条物理路径失效时,服务器可以将通过此物理路径的I/O转移到其他正常的物理路径上面,应用程序不会觉察到这种改变,从而提高系统的可用性。硬件方面需要服务器有2块或以上的HBA接口卡,网络上有两个或以上的交换机,块存储设备有两个或以上的冗余控制器,各个物理路径之间没有任何硬件相互依赖。多路径冗余I/O也可以实现I/O的负载均衡,提高系统性能,但主要还是一种容错机制。
一、服务端安装iscsi
1、安装iscsi
[root@localhost ~]# yum -y install targetcli
2、配置iscsi
[root@localhost ~]# targetcli
Warning: Could not load preferences file /root/.targetcli/prefs.bin.
targetcli shell version 2.1.fb41
Copyright 2011-2013 by Datera, Inc and others.
For help on commands, type 'help'.
# 步骤1:建立一个块存储,(名称yzl:storage1可自定义),/dev/sdb为新建的分区名称。
/> /backstores/block create yzl:storage1 /dev/sdb
Created block storage object yzl:storage1 using /dev/sdb.
# 步骤2:配置ISCSITarget命名,注意:命名在同一子网内确保是唯一的,命名格式为:iqn.yyyy-mm.<主机名反写>:自定义名称(自定义名称内不能有下划线)
/> /iscsi create iqn.2020-04.com.example:localhost
Created target iqn.2020-04.com.example:localhost.
Created TPG 1.
Global pref auto_add_default_portal=true
Created default portal listening on all IPs (0.0.0.0), port 3260.
# 步骤3:创建ACL允许ISCSI客户机连接,注意:iqn.2020-04.com.example:localhost为客户机ISCSI名称。
/> /iscsi/iqn.2020-04.com.example:localhost/tpg1/acls create iqn.2020-04.com.example:客户机主机名
Created Node ACL for iqn.2020-04.com.example:localhost
# 步骤4:创建lun(target块设备的逻辑单元)
/> /iscsi/iqn.2020-04.com.example:localhost/tpg1/luns create /backstores/block/yzl:storage1
Created LUN 0.
Created LUN 0->0 mapping in node ACL iqn.2020-04.com.example:localhost
# 步骤5:创建ip与端口,注:192.18.13.30为服务器IP;不指定端口默认为3260,此步骤似乎可以省略。。。后续还有设置验证用户名和密码,以及查看配置信息,都可省略。
# 退出
/> exit
Global pref auto_save_on_exit=true
Last 10 configs saved in /etc/target/backup.
Configuration saved to /etc/target/saveconfig.json
二、客户端配置iscsi
1、安装并加入认证
[root@yzl ~]# yum -y install iscsi-initiator-utils
[root@yzl ~]# vim /etc/iscsi/initiatorname.iscsi
InitiatorName=iqn.2020-04.com.example:localhost
2、发现服务器端iscsi
[root@yzl ~]# iscsiadm -m discovery -t st -p 192.168.40.193
192.168.40.193:3260,1 iqn.2020-04.com.example:localhost
[root@node2 multipath]# iscsiadm -m discovery -t st -p 172.16.100.56
172.16.100.56:3260,1 iqn.2020-04.com.example:node1
3、登录
[root@yzl ~]# iscsiadm -m node -T iqn.2020-04.com.example:localhost -p 192.168.40.193 -l
Logging in to [iface: default, target: iqn.2020-04.com.example:localhost, portal: 192.168.40.193,3260] (multiple)
Login to [iface: default, target: iqn.2020-04.com.example:localhost, portal: 192.168.40.193,3260] successful.
4、连接iscsi设备
[root@yzl ~]# iscsiadm -m node --login
5、查看系统磁盘信息
[root@yzl ~]# fdisk -l # 或者lsblk
Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000ca9a5
...
...
Disk /dev/sdb: 8589 MB, 8589934592 bytes
64 heads, 32 sectors/track, 8192 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 4194304 bytes
Disk identifier: 0x00000000
三、multipath安装及配置(客户端)
1、安装multipath
[root@yzl ~]# yum -y install device-mapper-multipath
[root@yzl ~]# modprobe dm-multipath # 命令初始化DM
[root@yzl ~]# modprobe dm-round-robin # 命令初始化DM
[root@yzl ~]# lsmod | grep dm_multipath
dm_multipath 17724 1 dm_round_robin
dm_mod 84337 9 dm_multipath,dm_mirror,dm_log
[root@yzl ~]# rpm -ql device-mapper-multipath # 查看生成文件
[root@yzl ~]# cp /usr/share/doc/device-mapper-multipath-0.4.9/multipath.conf /etc/
# 也可以通过下面命令生成配置文件
[root@yzl ~]# mpathconf --enable
2、查看wwid
# 注意scsi_id命令的路径(/usr/lib/udev/scsi_id 或者 /lib/udev/scsi_id )
[root@yzl ~]# /usr/lib/udev/scsi_id --whitelisted --device=/dev/sdb
-bash: /usr/lib/udev/scsi_id: 没有那个文件或目录
[root@yzl ~]# /lib/udev/scsi_id --whitelisted --device=/dev/sdb
360014050939ee9c6b434831840535128
3、配置multipath
# vim修改配置文件如下:
[root@yzl ~]# sed -n "/^\s*[^#\t].*$/p" /etc/multipath.conf # ed匹配非注释行和非空白行
defaults {
user_friendly_names yes
}
multipaths {
multipath {
wwid 360014050939ee9c6b434831840535128
alias yzlpath1
path_grouping_policy multibus
path_checker readsector0
path_selector "round-robin 0"
failback manual
rr_weight priorities
no_path_retry 5
}
}
[root@yzl ~]# ls /dev/mapper/
control vg_yzl-lv_root vg_yzl-lv_swap
[root@yzl ~]# service multipathd restart # 重启iscsid服务
ux_socket_connect: No such file or directory
正在关闭multipathd 端口监控程序: [失败]
正在启动守护进程multipathd: [确定]
[root@yzl ~]# ls /dev/mapper/
control vg_yzl-lv_root vg_yzl-lv_swap yzlpath1 # 发现yzlpath1
4、格式化挂载
[root@yzl ~]# mkfs.xfs /dev/mapper/yzlpath1
meta-data=/dev/mapper/yzlpath1 isize=256 agcount=8, agsize=262144 blks
= sectsz=512 attr=2, projid32bit=0
data = bsize=4096 blocks=2097152, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@yzl ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_yzl-lv_root
18G 11G 6.0G 64% /
tmpfs 931M 76K 931M 1% /dev/shm
/dev/sda1 485M 53M 407M 12% /boot
/dev/sr0 3.8G 3.8G 0 100% /media/Asianux
[root@yzl ~]# mkdir /iscsi
[root@yzl ~]# mount /dev/mapper/yzlpath1 /iscsi/
[root@yzl ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_yzl-lv_root
18G 11G 6.0G 64% /
tmpfs 931M 76K 931M 1% /dev/shm
/dev/sda1 485M 53M 407M 12% /boot
/dev/sr0 3.8G 3.8G 0 100% /media/Asianux
/dev/mapper/yzlpath1 8.0G 33M 8.0G 1% /iscsi
5、查看状态
[root@yzl ~]# multipath -rr
Apr 26 23:44:27 | multipath.conf line 67, invalid keyword: path_checker
reload: yzlpath1 (360014050939ee9c6b434831840535128) undef LIO-ORG,yzl:storage1
size=8.0G features='0' hwhandler='0' wp=undef
`-+- policy='round-robin 0' prio=1 status=undef
`- 3:0:0:0 sdb 8:16 active ready running
[root@yzl ~]# multipath -ll
Apr 26 23:44:30 | multipath.conf line 67, invalid keyword: path_checker
yzlpath1 (360014050939ee9c6b434831840535128) dm-2 LIO-ORG,yzl:storage1
size=8.0G features='1 queue_if_no_path' hwhandler='0' wp=rw
`-+- policy='round-robin 0' prio=1 status=active
`- 3:0:0:0 sdb 8:16 active ready running
四、multipath配置简介
可用的节关键字如下:
- defaults:全局属性的默认设置。
- blacklist:黑名单,multipath会忽略黑名单中的设备。
- blacklist_exceptions:免除黑名单,加入黑名单内,但包含在这里的设备不会被忽略。
- multipaths:多路径相关配置。
- devices:存储设备相关配置。
配置文件:
uid_attribute:用udev的哪个属性唯一标识一个设备,默认值为ID_SERIAL
path_grouping_policy ###路径分组策略
- failover ###一条路径一个组(默认值)
- multibus ###所有路径在一个组
- group_by_serial ###根据序列号分组
- group_by_prio ###根据优先级分组
- group_by_node_name ###根据名字分组
path_selector ###I/O操作算法
- service-time 0 ###选择IO服务时间最短的路径(默认值)
- round-robin 0 ###多个路径间不断循环
- queue-length 0 ###选择当前处理IO数最少的路径
failback ###回复失败管理
- immediate ###立刻回复到活跃的优先级最高的组
- manual ###不操作,需自行处理
- followover ###第一个路径变为活动状态时应执行自动故障回复。 这可以防止节点在另一个节点请求故障转移时自动失败。
no_path_retry ###多少次后禁止排队
- fail ###失败后就立刻失败,没有排队
- queue ###一直进行排队,直到修复
五、常见问题:
1、ssh连接不上
[root@yzl ~]# ssh 192.168.40.193
[email protected]'s password:
Permission denied, please try again.
- 可能原因之一:selinux限制。
- ssh远程主机上的selinux设置为禁用或者宽松模式,或者放行ssh端口即可解决。
2、发现服务器失败
[root@yzl ~]# iscsiadm -m discovery -t st -p 192.168.40.193
iscsiadm: cannot make connection to 192.168.40.193: No route to host
iscsiadm: cannot make connection to 192.168.40.193: No route to host
iscsiadm: cannot make connection to 192.168.40.193: No route to host
iscsiadm: cannot make connection to 192.168.40.193: No route to host
iscsiadm: cannot make connection to 192.168.40.193: No route to host
iscsiadm: cannot make connection to 192.168.40.193: No route to host
iscsiadm: connection login retries (reopen_max) 5 exceeded
iscsiadm: Could not perform SendTargets discovery: encountered connection failure
- 可能原因之一:服务器防火墙设限。
- 解决办法:关闭防火墙
systemctl stop firewalld
- 解决办法:关闭防火墙
3、补充:sed匹配非注释行和非空白行
sed -n "/^\s*[^# \t].*$/p" "$file"