iscsi多路径配置方式以及使用udev规则为target创建固定名字的软链接

ISCSI多路径应用

如果存储服务器到交换机只有一条线路的时候,那么一条线路出线故障,整个就没法使用了,所以多线路可以解决这个问题,避免单点故障
在这里插入图片描述
在这里插入图片描述
通过VMware模拟多路径,使用下面的拓扑
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5szkkkcp-1572333812868)(en-resource://database/3355:0)]
实验环境: yunzu63,yunzu64配置双网卡 。
服务端:yunzu63.cn IP:192.168.1.63 ens32 ,br ; IP:192.168.2.63 ens35 , vmnet4
客户端:yunzu64.cn IP:192.168.1.64 ens32 ,br ; IP:192.168.2.64 ens35 , vmnet4

将yunzu63配置成ip san,将yunzu63上的sdb1分区,通过ip san 共享出去。
服务器端: target yunzu63
客户端: initiator yunzu64

配置网络环境:

yunzu63 添加一块网卡:
在这里插入图片描述
[root@yunzu63 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens35
生成ens35配置文件,配置IP ,192.168.2.63
[root@yunzu63 Desktop]# ifdown ens35;ifup ens35
yubzu64同样操作:
[root@yunzu64 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens35
生成ens35配置文件,IP ,192.168.2.64
[root@yunzu64 ~]# ifdown ens35;ifup ens35

安装tgtd服务器:

[root@yunzu63 Desktop]# yum install -y scsi-target-utils

准备一个磁盘: sda4

[root@yunzu63 ~]# fdisk /dev/sda #划分出sda4 分区
WARNING: DOS-compatible mode is deprecated. It’s strongly recommended to switch off the mode (command ‘c’) and change display units to sectors (command ‘u’).

Command (m for help): p

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: 0x000b8b35
Device Boot Start End Blocks Id System
/dev/sda1 * 1 26 204800 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 26 1301 10240000 83 Linux
/dev/sda3 1301 1428 1024000 82 Linux swap / Solaris

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Selected partition 4
First cylinder (1428-2610, default 1428):
Using default value 1428
Last cylinder, +cylinders or +size{K,M,G} (1428-2610, default 2610): +1G

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@yunzu63 ~]#reboot

配置target ,把sda4分区共享出去

修改配置文件
vim /etc/tgt/targets.conf #写入以下内容
在参考这段内容并在段内容后,追加以下内容:
#

启动服务

[root@yunzu63 Desktop]#systemctl restart tgtd
[root@yunzu63 Desktop]# netstat -antup | grep 3260
tcp 0 0 0.0.0.0:3260 0.0.0.0:* LISTEN 3130/tgtd
tcp 0 0 :::3260 ::? LISTEN 3130/tgtd

查看状态tgt-admin --show

[root@yunzu63 Desktop]# tgt-admin --show

开机启动

[root@yunzu63 Desktop]# chkconfig tgtd on

配置客户端: yunzu64

[root@yunzu64 ~]# rpm -ivh /mnt/Packages/iscsi-initiator-utils-6.2.0.872-34.el6.x86_64.rpm

需要先发现target存储,再启动客户端服务,才有效

[root@yunzu64 ~]# iscsiadm -m discovery -t sendtargets -p 192.168.1.63:3260
Starting iscsid: [ OK ] #发现时,会自动把iscsid服务启动
192.168.1.63:3260,1 iqn.2015-01.cn.yunzu.www:target_san1

[root@yunzu64 ~]#systemctl restart scsi # 根据/var/lib/iscsi/ 中发现的信息,识别设备
Stopping iscsi: [ OK ]
Starting iscsi: [ OK ]

开机自动启动:

[root@yunzu64 ~]# systemctl enable iscsi
[root@yunzu64 ~]# systemctl enable iscsid

查看发现到新硬盘:

[root@yunzu64 ~]# ls /dev/sdb

在yunzu63服务端查看链接状态

[root@yunzu63 ~]# netstat -antup | grep 3260
tcp 0 0 0.0.0.0:3260 0.0.0.0:* LISTEN 1999/tgtd
tcp 0 0 192.168.1.63:3260 192.168.1.64:46553 ESTABLISHED
1999/tgtd
tcp 0 0 :::3260 ::? LISTEN 1999/tgtd

客户端从另一条线路发现设备

[root@yunzu64 ~]# iscsiadm -m discovery -t sendtargets -p 192.168.2.63
[root@yunzu64 ~]# rpm -ivh /mnt/Packages/tree-1.5.3-2.el6.x86_64.rpm
[root@yunzu64 ~]# tree /var/lib/iscsi/

识别出硬盘并查看:

[root@yunzu64 ~]# /etc/init.d/iscsi restart
Stopping iscsi: [ OK ]
Starting iscsi: [ OK ]
[root@yunzu64 ~]# ls /dev/sd*
/dev/sda /dev/sda1 /dev/sda2 /dev/sda3 /dev/sdb /dev/sdc

挂载硬盘测试数据:

[root@yunzu64 ~]# fdisk /dev/sdb #使用sdb中所有空间,创建sdb1分区

Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel

Building a new DOS disklabel with disk identifier 0x337c0db1.

Changes will remain in memory only, until you decide to write them.

After that, of course, the previous content won’t be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It’s strongly recommended to switch off the mode (command ‘c’) and change display units to sectors (command ‘u’).

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-1019, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1019, default 1019):
Using default value 1019

Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@yunzu64 ~]# mkfs.ext4 /dev/sdb1
[root@yunzu64 ~]# mount /dev/sdb1 /opt/
[root@yunzu64 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 9.7G 3.7G 5.5G 41% /
tmpfs 569M 0 569M 0% /dev/shm
/dev/sda1 194M 28M 157M 15% /boot
/dev/sr0 3.4G 3.4G 0 100% /mnt
/dev/sdb1 5.0G 138M 4.6G 3% /opt
注:可以看到sdb1可以正常使用
[root@yunzu64 ~]# ls /dev/sdc* #查看不到sdc1
/dev/sdc
[root@yunzu64 ~]# /etc/init.d/iscsi restart #重启iscsi服务,重新挂载存储
Stopping iscsi: [ OK ]
Starting iscsi: [ OK ]
[root@yunzu64 ~]# ls /dev/sdc*
/dev/sdc /dev/sdc1
[root@yunzu64 ~]# mkdir /tmp/sdc1
[root@yunzu64 ~]# mount /dev/sdc1 /tmp/sdc1/
[root@yunzu64 ~]# mount /dev/sdb1 /opt 挂载报错了吧?
在这里插入图片描述
[root@yunzu64 ~]# blkid /dev/sdc1 /dev/sdb1 # 对比一下,发现UUID一样的
解决方法:
[root@yunzu64 ~]#mount -t xfs -o nouuid /dev/sdb1 /opt
[root@yunzu64 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 9.7G 3.7G 5.5G 41% /
tmpfs 569M 0 569M 0% /dev/shm
/dev/sda1 194M 28M 157M 15% /boot
/dev/sr0 3.4G 3.4G 0 100% /mnt
/dev/sdc1 5.0G 138M 4.6G 3% /tmp/sdc1
/dev/sdb1 5.0G 138M 4.6G 3% /opt

在存储客户端yunzu64上配置多路径

多路径软件Device Mapper Multipath(DM-Multipath)可以将服务器节点和存储阵列之间的多条I/O链路配置为一个单独的设备。这些I/O链路是由不同的线缆、交换机、控制器组成的SAN物理链路。Multipath将这些链路聚合在一起,生成一个单独的新的设备。
DM-Multipath概览:
(1)数据冗余
DM-Multipath可以实现在active/passive模式下的灾难转移。在active/passive模式下,只有一半的链路在工作,如果链路上的某一部分(线缆、交换机、控制器)出现故障,DM-Multipath就会切换到另一半链路上。
(2)提高性能
DM-Multipath也可以配置为active/active模式,从而I/O任务以round-robin的方式分布到所有的链路上去。通过配置,DM-Multipath还可以检测链路上的负载情况,动态地进行负载均衡。
安装多路经软件
[root@yunzu64 ~]# yum install device-mapper-multipath -y
使用默认配置文件启动
[root@yunzu64 ~]# cp /usr/share/doc/device-mapper-multipath-0.4.9/multipath.conf /etc/
[root@yunzu64 ~]# systemctl restart multipathd
[root@yunzu64 ~]# multipath -ll #没有输出消息。 表示出错了。

解决:把正在使用中的sdb1和sdbc1卸载了
[root@yunzu64 ~]# umount /opt/
[root@yunzu64 ~]# umount /tmp/sdc1/

[root@yunzu64 ~]# /etc/init.d/iscsi restart
[root@yunzu64 ~]# systemctl restart multipathd

[root@yunzu64 ~]# multipath -ll
3600000000e00000000010001(远程存储设备id)dm-2 yunzu(厂商) ,target1(产品ID)
size=5.0G features=‘0’ hwhandler=‘0’ wp=rw
|-± policy=‘service-time 0’ prio=1 status=active #默认使用active的链路
| - 34:0:0:1 sdb 8:16 active ready running-± policy=‘service-time 0’ prio=1 status=enabled #有效的链路,但不是活动链路
#ll /dev/mapper/mpatha*
lrwxrwxrwx 1 root root 7 1月 13 09:02 /dev/mapper/mpatha -> …/dm-2
lrwxrwxrwx 1 root root 7 1月 13 09:02 /dev/mapper/mpathap1 -> …/dm-3

修改配置文件,启动高可用负载均衡模式,并自定义多路经设备文件的名字:

vim /etc/multipath.conf
注销掉下面三行内容
在这里插入图片描述
请在此模版下写内容:
在这里插入图片描述
在74行以下插入以下内容:
multipaths {
multipath {
wwid “360000000000000000e00000000010001” #填写硬盘产品关键信息
alias webdata #自定义名字
path_grouping_policy multibus
path_selector “round-robin 0”
failback manual
rr_weight priorities
no_path_retry 5
}
}

重启服务multipathd,测试负载均衡:

[root@yunzu64 ~]# systemctl restart multipathd
[root@yunzu64 ~]# systemctl restart iscsi #需要重新实别一下存储
再次查看:
[root@yunzu64 ~]# multipath -ll

测试

[root@yunzu64 ~]# ls /dev/mapper/webdata*
webdata webdata1 # webdata对应硬盘sdb, webdata1对应硬盘中的第一个分区
[root@yunzu64 ~]# mount /dev/mapper/webdata1 /opt
[root@yunzu64 ~]# cp /etc/hosts /opt
[root@yunzu64 ~]# ls /opt
lost+found passwd hosts
模拟故障: 断开一条链路。测试链路高可用,关闭其中一个网卡,多路经驱动需要约1分钟的时间,去识别链路故障
[root@yunzu63 ~]# ifdown ens32
查看链路状态:
[root@yunzu64 ~]# multipath -ll #多路经驱动需要约1分钟的时间,去识别链路故障
打开链路,过一段时间再看:
[root@yunzu63 ~]# ifup ens35
[root@yunzu64 ~]# multipath -ll

使用udev规则为target创建固定名字的软链接

udev概述: udev 是Linux kernel 2.6系列的设备管理器。它主要的功能是管理/dev目录底下的设备节点。udev会根据用户添加/删除硬件的行为,自处理/dev目录下所有设备文件。

主配置文件:/etc/udev/udev.conf
设备文件名字规则目录:

[root@yunzu64 ~]# ls /lib/udev/rules.d/10-dm.rules
命名规律: 开头为数字, 结尾是 .rules
运行机制和注意事项:
1、udev按照规则文件名的数字顺序来查询全部规则文件,然后为匹配规则的设备,创建其设备文件或文件链接。
2、通常情况下,建议让自己想要的规则文件最先被解析。比如,创建一个名为
/etc/udev/rules.d/10-myrule.rules的文件,并把你的规则写入该文件,这样udev就会在解析系统默认的规则文件之前解析到你的文件。
3、在规则文件里,除了以“#”开头的行(注释),所有的非空行都被视为一条规则,但是一条规则不能扩展到多行。
4、规则都是由多个键值对(key-valuepairs)组成,并由逗号隔开,键值对可以分为条件匹配键值对(以下简称“匹配键”)和赋值键值对(以下简称“赋值键”),一条规则可以有多条匹配键和多条赋值键。
5、匹配键是匹配一个设备属性的条件,当一个设备的属性匹配了该规则里所有的匹配键,就认为这条规则生效,然后按照赋值键的内容,执行该规则的赋值。
说明:如果有一个设备被内核实别sdb1,则该条件生效,执行后面的赋值:在/dev下产生一个名为my_
disk的设备文件,并把设备文件的权限设为0666。
详细参数说明

udev规则的匹配键
ACTION: 事件 (uevent) 的行为,例如:add( 添加设备 )、remove( 删除设备 )。
KERNEL: 内核设备名称,例如:sda, cdrom。
DEVPATH:设备的 devpath 路径。
SUBSYSTEM: 设备的子系统名称,例如:sda 的子系统为 block。
BUS: 设备在 devpath 里的总线名称,例如:usb。
DRIVER: 设备在 devpath 里的设备驱动名称,例如:ide-cdrom。
ID: 设备在 devpath 里的识别号。
SYSFS{filename}: 设备的 devpath 路径下,设备的属性文件“filename”里的内容。
ENV{key}: 环境变量。在一条规则中,可以设定最多五条环境变量的 匹配键。
PROGRAM:调用外部命令。
RESULT: 外部命令 PROGRAM 的返回结果。
udev重要的赋值键
NAME:在 /dev下产生的设备文件名。只有第一次对某个设备的 NAME 的赋值行为生效,之后匹配的规则再对该设备的 NAME 赋值行为将被忽略。如果没有任何规则对设备的 NAME 赋值,udev
将使用内核设备名称来产生设备文件。
SYMLINK:为 /dev/下的设备文件产生符号链接。由于 udev 只能为某个设备产生一个设备文件,所以为了不覆盖系统默认的 udev 规则所产生的文件,推荐使用符号链接。
OWNER, GROUP, MODE:为设备设定权限。
ENV{key}:导入一个环境变量
udev规则操作符
“==”:比较键、值,若等于,则该条件满足;
“!=”: 比较键、值,若不等于,则该条件满足;
“=”: 对一个键赋值;
“+=”:为一个表示多个条目的键赋值。
“:=”:对一个键赋值,并拒绝之后所有对该键的改动。目的是防止后面的规则文件对该键赋值。

在应用服务器yunzu64上使用udev规则为每个target创建固定名字的软链接

对iscsi多路径共享的设备设备,让内核识别成:my_disk
在进行udev绑定的时候,可以是一个未分区的磁盘,也可以是磁盘的一个分区。
查看共享设备磁盘的UUID:
[root@yunzu64 rules.d]# /usr/lib/udev/scsi_id -g -u /dev/sdb
或执行:/lib/udev/scsi_id --whitelisted --replace-whitespace /dev/sdb
[root@yunzu64 rules.d]# vim /etc/udev/rules.d/10-sda.rules #写入以下内容
KERNEL==“sd1”,SUBSYSTEM==“block”,PROGRAM="/lib/udev/scsi_id --whitelisted --replace-whitespace /dev/$name",RESULT==“360000000000000000e00000000010001”, SYMLINK+=“my-disk1”, MODE=“0660”
参数:
#KERNEL是匹配键, SUBSYSTEM:设备的子系统名称,例如:sda 的子系统为block。udevadm info -a -p
/sys/block
#PROGRAM:调用外部命令;
#RESULT: 外部命令 PROGRAM 的返回结果。
#SYMLINK:为/dev/下的设备文件产生符号链接。由于udev 只能为某个设备产生一个设备文件,所以为了不覆盖系统默认的udev 规则所产生的文件,推荐使用符号链接。
#MODE:为设备设定权限

通知内核磁盘信息的变化
[root@yunzu62]# /sbin/partprobe /dev/sdb1 或者 systemctl restart systemd-udev-trigger
#/sbin/partprobe /dev/sd* 通常使用该命令让新的规则文件立即生效
#udev 的日志一般没有标准输出,所有的udev 相关信息都按照配置文件(udev.conf)的参数设置,由/var/log/messages记录。

查看:
[root@yunzu64 rules.d]# ll /dev/my_disk
lrwxrwxrwx 1 root root 4 1月 24 14:19 /dev/my-disk1 -> sdb1
[root@yunzu64 rules.d]# mount /dev/my_disk /opt/
#如果无法挂载使用,请卸载multipath再测试。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值