linux系统简单搭建iSCSI

一、常见的存储类型
   目前磁盘存储市场上,存储分类(如下表一)根据服务器类型分为:封闭系统的存储和开放系统的存储,封闭系统主要指大型机,AS400等服务器,开放系统指基于包括Windows、UNIX、Linux等操作系统的服务器;开放系统的存储分为:内置存储和外挂存储;开放系统的外挂存储根据连接的方式分为:直连式存储(Direct-Attached Storage,简称DAS)和网络化存储(Fabric-Attached Storage,简称FAS);开放系统的网络化存储根据传输协议又分为:网络接入存储(Network-Attached Storage,简称NAS)和存储区域网络(Storage Area Network,简称SAN)。
wKiom1X9cnbTY8qcAAC09FFSFmA295.jpg
1、 DAS
   直接连接存储 (DAS:Direct Attached Storage),是指将存储设备通过SCSI接口或FC接口直接连接到一台计算机上。DAS不算是网络存储,因为只有它所挂载的主机才可访问它。也就是说,服务器发生故障时,连接在服务器上的DAS存储设备中的数据暂时不能被存取。
DAS存储在中小企业应用中使用不少,因为廉价成本较低,存储系统被直连到应用的服务器中,在中小企业中,许多的数据应用是必须安
装在直连的DAS存储器上。
2、NAS
   网络连接存储 (NAS:Network Attached Storage),是指将存储设备通过标准的网络拓扑结构(例如以太网),连接到一群计算机上。NAS有文件系统和IP地址,可以类似的理解为网上邻居的共享磁盘。NAS是文件级的存储方法,它的重点在于帮助工作组和部门级机构解决迅速增加存储容量的需求。如今用户采用NAS较多的功能是用来文档共享、图片共享、电影共享等等,而且随着云计算的发展,一些NAS厂商也推出了云存储功能,大大方便了企业和个人用户的使用。
3、SAN
   存储区域网络(SAN:Storage Area Network),目前的SAN存储有2种:一是基于光纤通道的FC SAN;二是基于以太网的IP SAN(也就常说的iSCSI)。 FC SAN通过光纤交换机连接到主机(HBA卡),也就是说可以连接到光纤交换机的主机都可以访问这个存储; iSCSI作为共享于以太网络上的存储则更类似于NAS。
4、三种存储的主要区别
DAS :可作为本机的外挂硬盘,不过现在单块磁盘的空间已经很大,如果几个T的空间,直接在主机里插硬盘就可以实现了,不需要外挂。 
NAS :由于它的文件系统特性,加上以太网网线传输,更像是我的电脑-网上邻居-共享磁盘,访问方式也是类似:\NAS01BACKUPdatabase_name.bak。更多的是作为文件共享、备份、归档所用,比如数据库的历史备份/异地备份文件。 
SAN:FC SAN使用光纤传输,是一个高速的共享存储,数据库的任何东西都可以放在上面,还有就是在做集群时(failover clustering) 作为仲裁盘,合适做虚拟化项目和性能要求高的数据库存储;iSCSI的传输速率要低于FC SAN,目前在我们的环境中还没有直接使用iSCSI做数据库存储,通常是用在类似NAS的地方。
   总结:DAS存储一般应用在中小企业,与计算机采用直连方式,性能与成本最低;NAS存储则通过IP以太网添加到计算机上,性能与成本中等;SAN存储则使用FC光纤接口,性能与成本较高。SAN与NAS的主要区别体现在操作系统在什么位置。如下图所示:
wKioL1X9dNnAXeLHAAFzUhbrGPg252.jpg
二、iSCSI
1、简介
   2003 年 2 月 11 日, IETF( Internet Engineering Task Force,互联网工程任务组)通 过了 iSCSI( Internet SCSI)标准,这项由 IBM、 Cisco 共同发起的技术标准,经过三年 20 个版本的不断完善,终于得到了 IETF 认可。这吸引了很多的厂商参与到相关产品的开发中 来,也推动了更多的用户采用 iSCSI 的解决方案。 
   iSCSI ,全称: Internet Small Computer System Interface,它是通过 TCP/IP 网络传 输 SCSI 指令的协议。 iSCSI 协议参照 SAM-3( SCSI Architecture Model – 3)制订。在 SAM-3 的体系结构, iSCSI 属于传输层协议,在 TCP/IP 模型中属于应用层协议。
2、SCSI 协议栈描述
   iSCSI 是集成了 SCSI 协议和 TCP/IP 协议的新的协议。它是在 SCSI 基础上扩展了网 络功能,也就是可以让 SCSI 命令通过网络传送到远程 SCSI 设备上,而 SCSI 协议只能访 问本地的 SCSI 设备。 
   iSCSI 是传输层之上的协议,使用 TCP 连接建立会话。在 initiator 端的 TCP 端口号随 机选取, target 的端口号默认是 3260。 
   iSCSI 使用客户/服务器模型。客户端称为 initiator,服务器端称为 target。
Initiator:通常指用户主机系统,用户产生 SCSI 请求,并将 SCSI 命令和数据封装到 TCP/IP 包中发送到 IP 网络中。 
Target:通常存在于存储设备上,用于转换 TCP/IP 包中的 SCSI 命令和数据。
wKioL1X9doqyU02NAALo_PDoVrI192.jpg
3、iSCSI 名字规范
   iSCSI 协议中, initiator 和 target 是通过名字进行通信的,因此,每一个 iSCSI 节 点(即 initiator)必须拥有一个 iSCSI 名字。 
iSCSI 协议定义了 3 类名称结构:
 iqn( iSCSI Qualified Name) 格式是:“ iqn” +“年月” +“ .” +“域名的颠倒” +“ :” +“设备的具体 名称”,之所以颠倒域名是为了避免可能的冲突。 
            举例: iqn.2008-07.com.h3c.rd:test
eui( Extend Unique Identifier) eui 来源与 IEEE 中的 EUI,格式是: “ eui” + “ 64bits 的唯一标识( 16 个字母)”。 64bits 中,前 24bits( 6 个字母)是公司的唯一标识,后面 40bits ( 10 个字母)是设备的标识。 
            举例: eui.acde48234667abcd
naa( Network Address Authority) 由于 SAS 协议和 FC 协议都支持 naa, iSCSI 协议定义也支持这种名字结构。naa 的格式: “ naa” +“ 64bits( 16 个字母)或者 128bits( 32 个字母) 的唯一标识”。 
            举例: naa. 52004567BA64678D
                 naa. 62004567BA64678D0123456789ABCDEF
在实际使用过程中, iSCSI 节点名称可以不这么复杂。


三、搭建iSCSI
1、实验环境
wKiom1X9dKiDkH-fAADv6fWgJ2c691.jpg
2、建立所需要的新分区


[root@Director ~]# fdisk /dev/sdc
[root@Director ~]# fdisk -l /dev/sdc[1-5]
Disk /dev/sdc1: 8595 MB, 8595385344 bytes
255 heads, 63 sectors/track, 1044 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: 0x00000000
 
Disk /dev/sdc2: 8595 MB, 8595417600 bytes
255 heads, 63 sectors/track, 1045 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: 0x00000000
3、安装iscsi服务端及配置


[root@Director ~]# yum install -y scsi-target-utils
[root@Director ~]# service tgtd start
正在启动 SCSI target daemon:                              [确定]
[root@Director ~]# ss -tunlp |grep tgtd
tcp    LISTEN     0      128                   :::3260                 :::*      users:(("tgtd",1540,5),("tgtd",1543,5))
tcp    LISTEN     0      128                    *:3260                  *:*      users:(("tgtd",1540,4),("tgtd",1543,4))
[root@Director ~]# tgtadm -L iscsi -o new -m target -t 1 -T iqn.2015-09.com.example.testsan:1
[root@Director ~]# tgtadm -L iscsi -o bind -m target -t 1 -I 192.168.0.0/16
[root@Director ~]# tgtadm -L iscsi -o new -m logicalunit -t 1 -l 1 -b /dev/sdc1
[root@Director ~]# tgtadm -L iscsi -o new -m logicalunit -t 1 -l 2 -b /dev/sdc2
[root@Director ~]# tgtadm -L iscsi -o show -m target
Target 1: iqn.2015-09.com.example.testsan:1
    System information:
        Driver: iscsi
        State: ready
    I_T nexus information:
    LUN information:
        LUN: 0
            Type: controller
            SCSI ID: IET     00010000
            SCSI SN: beaf10
            Size: 0 MB, Block size: 1
            Online: Yes
            Removable media: No
            Prevent removal: No
            Readonly: No
            Backing store type: null
            Backing store path: None
            Backing store flags: 
        LUN: 1
            Type: disk
            SCSI ID: IET     00010001
            SCSI SN: beaf11
            Size: 8595 MB, Block size: 512
            Online: Yes
            Removable media: No
            Prevent removal: No
            Readonly: No
            Backing store type: rdwr
            Backing store path: /dev/sdc1
            Backing store flags: 
        LUN: 2
            Type: disk
            SCSI ID: IET     00010002
            SCSI SN: beaf12
            Size: 8595 MB, Block size: 512
            Online: Yes
            Removable media: No
            Prevent removal: No
            Readonly: No
            Backing store type: rdwr
            Backing store path: /dev/sdc2
            Backing store flags: 
    Account information:
    ACL information:
        192.168.0.0/16
4、node1节点安装iscsi客户端及配置


[root@node1 ~]# yum install -y iscsi-initiator-utils
[root@node1 ~]# echo "InitiatorName=`iscsi-iname -p iqn.2015-09.com.example`">/etc/iscsi/initiatorname.iscsi 
[root@node1 ~]# cat /etc/iscsi/initiatorname.iscsi
InitiatorName=iqn.2015-09.com.example:c6a6b10bea5
[root@node1 ~]# echo "InitiatorAlias=initiatorone">>/etc/iscsi/initiatorname.iscsi
[root@node1 ~]# cat /etc/iscsi/initiatorname.iscsi
InitiatorName=iqn.2015-09.com.example:c6a6b10bea5
InitiatorAlias=initiatorone
启动服务:


[root@node1 ~]# service iscsi start
[root@node1 ~]# service iscsid start
[root@node1 ~]# chkconfig iscsi on
[root@node1 ~]# chkconfig iscsid on
连接服务端:


[root@node1 ~]# iscsiadm -m discovery -t st -p 192.168.1.8
正在启动 iscsid:                                          [确定]
192.168.1.8:3260,1 iqn.2015-09.com.example.testsan:1
[root@node1 ~]# iscsiadm -m node -T iqn.2015-09.com.example.testsan:1 -p 192.168.1.8 -l
Logging in to [iface: default, target: iqn.2015-09.com.example.testsan:1, portal: 192.168.1.8,3260] (multiple)
Login to [iface: default, target: iqn.2015-09.com.example.testsan:1, portal: 192.168.1.8,3260] successful.
5、将共享的磁盘进行分区格式化挂载使用


[root@node1 ~]# fdisk -l /dev/sd*
Disk /dev/sdc: 8595 MB, 8595385344 bytes
64 heads, 32 sectors/track, 8197 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk /dev/sdd: 8595 MB, 8595417600 bytes
64 heads, 32 sectors/track, 8197 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
[root@node1 ~]# fdisk /dev/sdc
[root@node1 ~]# mke2fs -t ext4 /dev/sdc1
[root@node1 ~]# mkdir /mnt/test
[root@node1 ~]# mount /dev/sdc1 /mnt/test
[root@node1 ~]# ls /mnt/test
lost+found
[root@node1 ~]# cp /etc/issue /mnt/test/
[root@node1 ~]# vim /mnt/test/issue
test
CentOS release 6.6 (Final)
Kernel \r on an \m
6、node2节点安装iscsi客户端及配置


[root@node2 ~]# yum install -y iscsi-initiator-utils
[root@node2 ~]# service iscsi start
[root@node2 ~]# service iscsid start
[root@node2 ~]# chkconfig iscsi on
[root@node2 ~]# chkconfig iscsid on
[root@node2 ~]# iscsiadm -m node -T iqn.2015-09.com.example.testsan:1 -p 192.168.1.8 -l
Logging in to [iface: default, target: iqn.2015-09.com.example.testsan:1, portal: 192.168.1.8,3260] (multiple)
Login to [iface: default, target: iqn.2015-09.com.example.testsan:1, portal: 192.168.1.8,3260] successful.
7、将共享的磁盘进行分区格式化挂载使用


[root@node2 ~]# fdisk -l /dev/sd*
Disk /dev/sdc2: 3222 MB, 3222274048 bytes
64 heads, 32 sectors/track, 3073 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk /dev/sdd: 8595 MB, 8595417600 bytes
64 heads, 32 sectors/track, 8197 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
[root@node2 ~]# mount /dev/sdc1 /mnt/
[root@node2 ~]# echo "testtest">>/mnt/issue
[root@node2 ~]# cat /mnt/issue
test
CentOS release 6.6 (Final)
Kernel \r on an \m
 
testtest
8、关于第一块磁盘分区是否能同时在两个节点上同时使用


[root@node1 ~]# umount /mnt/test/
[root@node1 ~]# mount /dev/sdc1 /mnt/test/
[root@node1 ~]# echo "aaaaaaaaaa">>/mnt/test/issue
[root@node1 ~]# cat /mnt/test/issue 
test
CentOS release 6.6 (Final)
Kernel \r on an \m
 
testtest
aaaaaaaaaa
[root@node2 ~]# umount /mnt/
[root@node2 ~]# mount /dev/sdc1 /mnt/
[root@node2 ~]# cat /mnt/issue 
test
CentOS release 6.6 (Final)
Kernel \r on an \m
 
testtest
aaaaaaaaaa
9、各节点删除刚才的配置


[root@node1 ~]# iscsiadm -m node -T iqn.2015-09.com.example.testsan:1 -p 192.168.1.8 -u
Logging out of session [sid: 1, target: iqn.2015-09.com.example.testsan:1, portal: 192.168.1.8,3260]
Logout of [sid: 1, target: iqn.2015-09.com.example.testsan:1, portal: 192.168.1.8,3260] successful.
[root@node1 ~]# iscsiadm -m node -T iqn.2015-09.com.example.testsan:1 -p 192.168.1.8 -o delete
[root@node1 ~]# rm -rf /var/lib/iscsi/*
10、服务端删除刚才的配置信息


[root@Director ~]# tgtadm -L iscsi -o unbind -m target -t 1 -I 192.168.0.0/16 
[root@Director ~]# tgtadm -L iscsi -o delete -m logicalunit -t 1 -l 2
[root@Director ~]# tgtadm -L iscsi -o delete -m logicalunit -t 1 -l 1
[root@Director ~]# tgtadm -L iscsi -o show -m target
Target 1: iqn.2015-09.com.example.testsan:1
    System information:
        Driver: iscsi
        State: ready
    I_T nexus information:
    LUN information:
        LUN: 0
            Type: controller
            SCSI ID: IET     00010000
            SCSI SN: beaf10
            Size: 0 MB, Block size: 1
            Online: Yes
            Removable media: No
            Prevent removal: No
            Readonly: No
            Backing store type: null
            Backing store path: None
            Backing store flags: 
    Account information:
    ACL information:
[root@Director ~]# tgtadm -L iscsi -o delete -m target -t 1 
[root@Director ~]# tgtadm -L iscsi -o show -m target
11、使用配置文件进行配置服务端


[root@Director ~]# vim /etc/tgt/targets.conf
<target iqn.2015-09.com.example.testsan2>
   backing-store /dev/sdc2
   initiator-address 192.168.0.0/16
</target>s
[root@Director ~]# service tgtd reload
Updating SCSI target daemon configuration:                 [确定]
[root@Director ~]# tgtadm -L iscsi -o show -m target
Target 1: iqn.2015-09.com.example.testsan2
    System information:
        Driver: iscsi
        State: ready
    I_T nexus information:
    LUN information:
        LUN: 0
            Type: controller
            SCSI ID: IET     00010000
            SCSI SN: beaf10
            Size: 0 MB, Block size: 1
            Online: Yes
            Removable media: No
            Prevent removal: No
            Readonly: No
            Backing store type: null
            Backing store path: None
            Backing store flags: 
        LUN: 1
            Type: disk
            SCSI ID: IET     00010001
            SCSI SN: beaf11
            Size: 8595 MB, Block size: 512
            Online: Yes
            Removable media: No
            Prevent removal: No
            Readonly: No
            Backing store type: rdwr
            Backing store path: /dev/sdc2
            Backing store flags: 
    Account information:
    ACL information:
        192.168.0.0/16


四、tgtadm和iscsiadm常用选项说明
1、服务端配置管理工具tgtadm的使用
语法:tgtadm --lld [driver] --op [operation] --mode [mode] [OPTION]... 
添加一个新的 target 且其ID为 [id], 名字为 [name]. 
        --lld [driver] --op new --mode target --tid=[id] --targetname [name]
显示所有或某个特定的target:
        --lld [driver] --op show --mode target [--tid=[id]]
向某ID为[id]的设备上添加一个新的LUN,其号码为[lun],且此设备提供给initiator使用。[path]是某“块设备”的路径,此块设备也可以是raid或lvm设备。lun0已经被系统预留。
        --lld [driver] --op new --mode=logicalunit --tid=[id] --lun=[lun] --backing-store [path]
删除ID为[id]的target:
        --lld [driver] --op delete --mode target --tid=[id]
删除target [id]中的LUN [lun]:
        --lld [driver] --op delete --mode=logicalunit --tid=[id] --lun=[lun]
定义某target的基于主机的访问控制列表,其中,[address]表示允许访问此target的initiator客户端的列表:
        --lld [driver] --op bind --mode=target --tid=[id] --initiator-address=[address]
解除target [id]的访问控制列表中[address]的访问控制权限:
        --lld [driver] --op unbind --mode=target --tid=[id] --initiator-address=[address]
例如:
(1)创建一个target:


# tgtadm --lld iscsi --op new --mode target --tid 1 -T iqn.2013-05.com.example:tsan.disk1
(2)显示所有:


# tgtadm --lld iscsi --op show --mode target
(3)显示刚创建的target:


# tgtadm --lld iscsi --op show --mode target --tid 1
(4)创建LUN,号码为1:


# tgtadm --lld iscsi --op new --mode logicalunit --tid 1 --lun 1 -b /dev/sda5
(5)开放给192.168.0.0/24网络中的主机访问:


# tgtadm --lld iscsi --op bind --mode target --tid 1 -I 192.168.0.0/16
其中的-I相当于--initiator-address
(6)Create a new account:


# tgtadm --lld iscsi --op new --mode account --user christina --password 123456
# tgtadm --lld iscsi --op show --mode account
Assign this account to a target:


# tgtadm --lld iscsi --op bind --mode account --tid 1 --user christina
# tgtadm --lld iscsi --op show --mode target
(7)Set up an outgoing account. First, you need to create a new account like the previous example


# tgtadm --lld iscsi --op new --mode account --user clyde --password 123456
# tgtadm --lld iscsi --op show --mode account
# tgtadm --lld iscsi --op bind --mode account --tid 1 --user clyde --outgoing
# tgtadm --lld iscsi --op show --mode target
2、iscsiadm工具的使用
   iscsiadm是个模式化的工具,其模式可通过-m或--mode选项指定,常见的模式有discovery、node、fw、session、host、iface几个,如果没有额外指定其它选项,则discovery和node会显示其相关的所有记录;session用于显示所有的活动会话和连接,fw显示所有的启动固件值,host显示所有的iSCSI主机,iface显示/var/lib/iscsi/ifaces目录中的所有ifaces设定。
语法:iscsiadm -m discovery [ -d debug_level ] [ -P printlevel ] [ -I iface -t type -p ip:port [ -l ] ]
iscsiadm -m node [ -d debug_level ] [ -P printlevel ] [ -L all,manual,automatic ] [ -U all,manual,automatic ] [ [ -T tar-getname -p ip:port -I iface ] [ -l | -u | -R | -s] ] [ [ -o operation ]
-d, --debug=debug_level   显示debug信息,级别为0-8; 
-l, --login 
-t, --type=type  这里可以使用的类型为sendtargets(可简写为st)、slp、fw和 isns,此选项仅用于discovery模式,且目前仅支持st、fw和isns;其中st表示允许每个iSCSI target发送一个可用target列表给initiator; 
-p, --portal=ip[:port]  指定target服务的IP和端口; 
-m, --mode op  可用的mode有discovery, node, fw, host iface 和 session 
-T, --targetname=targetname  用于指定target的名字 
-u, --logout  
-o, --op=OPEARTION:指定针对discoverydb数据库的操作,其仅能为new、delete、update、show和nonpersistent其中之一; 
-I, --interface=[iface]:指定执行操作的iSCSI接口,这些接口定义在/var/lib/iscsi/ifaces中;


五、基于用户认证搭建iSCSI
创建服务端谁所需要的帐号,并实现单方向的认证:
1、在target端创建帐号christina,并为其授予访问某tid的权限:


# tgtadm --lld iscsi --op new --mode account --user mageedu --password 123456
接下来还要将用户与某target进行绑定:


# tgtadm --lld iscsi --op bind --mode account --tid 1 --user test
# tgtadm --lld iscsi --op show --mode account
2、编辑initiator端主配置文件,配置客户端登录target时使用此帐号和密码:
# vim /etc/iscsi/iscsid.conf
取消如下项的注释:


# node.session.auth.authmethod = CHAP
# node.session.auth.username = username
# node.session.auth.password = password
而后,将后两项的用户名密码设置为target端设置的用户名和密码:


node.session.auth.username = example
node.session.auth.password = 123456
哪果此前尚未登录过此target,接下来直接发现并登入即可。否则,则需要按照下面的第三步实现认证的启用。
3、如果initiator端已经登录过此target,此时还需要先注销登录后重启iscsid服务,并在删除此前生成的database后重新发现target,并重新登入,过程如下:


# iscsiadm -m session -r sid -u
# iscsiadm -m node -T iqn.2010-8.com.example.ts:disk1 -p 192.168.1.8:3260 -u
# iscsiadm -m node -T iqn.2010-8.com.example.ts:disk1 -p 192.168.1.8:3260 -o delete
# rm -rf -rf /var/lib/iscsi/send_targets/192.168.1.8,3260
# service iscsid restart
# iscsiadm -m discovery -t sendtargets -p 192.168.1.8
# iscsiadm -m node -T iqn.2010-8.com.example.ts:disk1 -p 192.168.1.8:3260 -l
说明:其中的target名字和target主机地址可能需要按照您的实际情况修改。 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值