iscsi简介和配置

来源:互联网 发布:淘宝聘用店主 编辑:程序博客网 时间:2024/06/05 09:59

一、iscsi的概念

       iSCSI是一种在Internet协议上,特别是以太网上进行数据块传输的标准,它是一种基于Ip Storage理论的新型存储技术,该技术是将存储行业广泛应用的SCSI接口与IP网络技术相结合,可以在IP网络上构建SAN存储区域网络,简单地说,iSCSI就是在IP网络上运行SCSI协议的一种网络存储技术。

二、iSCSI的工作原理

      要理解iSCSI的工作原理,就必须知道iSCSI的层次结构。根据OSI模型,iSCSI的协议自顶向下一共可以分为三层,从initiator端到target端,数据要进过封装-->IP网络传输-->解封装

 

 

如图是iSCSI的封装:最上层为二层以太网帧,中间为三层TCP/IP封装,最下面为iSCSI的封装


 iSCSI protocal的封装格式


SCSI层:根据客户端发出的请求建立SCSI CDB(命令描述块),并传给iSCSI层。同时接收来自iSCSI层的CDB,并向应用返回数据。

iSCSI层:对SCSI CDB进行封装,以便能够在基于TCP/IP协议的网络上进行传输,完成SCSI到TCP/IP的协议映射。这一层是iSCSI协议的核心层。本章也主要针对这一层的配置和管理进行介绍。

TCP/IP层:对IP报文进行路由和转发,并且提供端到端的透明可靠的传输。

    过程: iSCSI协议定义了在TCP/IP网络发送、接收数据块存储数据的规则和方式。先发送端将SCSI命令和数据封装到TCP/IP包中,然后通过IP网络转发,接收端收到TCP/IP包之后,将其还原为SCSI命令和数据并执行,执行完成后,将返回的SCSI命令和数据再封装到TCP/IP包中,之后再传回发送端。这样就完成了数据传输的整个过程。

     iSCSI的整个数据传输过程在用户看来是完全透明的,用户使用远端的存储设备就像使用本地的硬盘设备一样。不过,这只是理论状态,实际上iSCSI的数据传输速率并不能完全达到本地硬盘的数据传输速率,但差别并不明显。而且这种网络存储模式还有一个优点是安全性高,这对于数据集中存储的iSCSI来说显然非常重要。

三、SCSI的组成

一个简单的iSCSI系统大致由以下部分组成:

  •   iSCSI Initiator或者iSCSI HBA
  •   iSCSI Target
  •   以太网交换机
  •   一台或者多台服务器

一个完整的iSCSI系统的拓扑结构:


                                                            图7-1 完整的iSCSI系统拓扑结构

1.iSCSI的initiator有三种:

       1:HBA卡,采用内键的SCSI指令及TOE引擎的ASIC芯片的适配卡价格最贵,性能最好,消耗CPU很少

如图:


       2:内键的TOE引擎的ASIC芯片适配卡,由于SCSI指令任然以软件方式运行,所以任然会消耗一定的CPU资源,但价格相对便宜

如图


       3:完全由软件驱动的initiator,会消耗大量的CPU资源,性能

2、iSCSITarget

      一个可以用于存储数据的iSCSI磁盘阵列或者具有iSCSI功能的设备都可以被称为“iSCSI Target”,因为大多数操作系统都可以利用一些软件将系统转变为一个“iSCSI Target”。本章重点讲述如何构建一个PC构架的iSCSI存储系统。所谓PC构架就是选择一个普通的、性能优良的、可支持多块磁盘的PC(一般为PC服务器),再选择一款相对成熟稳定的iSCSI Target软件,将iSCSI Target软件安装在PC服务器上,使普通的PC服务器转变成一台iSCSI存储设备,并通过PC服务器的以太网卡对外提供iSCSI数据传输服务。

      目前大多数iSCSI Target软件都是收费的,例如DataCorc  Software的SANmelody,FalconStor Software的iSCSI Server forWindows等,这些都是Windows平台支持的。不过,也有一些Linux平台的开源iSCSI Target软件,例如iSCSI Enterprise Target,后面的内容会重点介绍这个软件。

       利用iSCSI Target软件,可以将服务器的存储空间分配给客户机使用,客户机可以像使用本地硬盘一样使用iSCSI磁盘,包括对其进行分区、格式化及读写等。而且每个客户端都可以向iSCSI磁盘写数据,互不干扰,并且不会破坏存储到服务器中的数据。同时,iSCSITarget软件对用户权限控制非常灵活,支持配置文件。

       我们知道,iSCSI是使用TCP/IP协议进行通信的,因此,将iSCSI两端连接起来,仅仅需要一个以太网络就可以了。由此可知,iSCSI的存储性能和这个以太网络有直接关系,所以最好在iSCSI网络中使用千兆以太网交换机,劣质的网络设备会严重影响存储系统的性能,也就是说,要为每个服务器配备高质量的千兆以太网交换机,并提供两个连接。对于iSCSI Target,应该为每个独立阵列中的两个独立端口配备交换机,最后将交换机连接起来,采用这种配置方式,即使两个交换机中的一个出现了故障,整个iSCSI存储系统仍然能够正常工作,这保证了存储系统的不间断运行。

如图为一个SAN架构图:

 

四、iscsi的安装配置

 1.target安装配置:
 1.准备好提供存储的磁盘(实验环境可以使用分区),注意磁盘或者分区不要格式化
 2.在target端安装服务:

   [root@localhost ~]  #yum -y install scsi-target-utils
   [root@localhost ~]# rpm -ql scsi-target-utils | less
      /etc/sysconfig/tgtd
      /etc/tgt/conf.d/sample.conf
      /etc/tgt/targets.conf
      /etc/tgt/tgtd.conf
      /usr/lib/systemd/system/tgtd.service
      /usr/sbin/tgt-admin(会自动读取tgtd.conf文件中的定义)
      /usr/sbin/tgt-setup-lun
      /usr/sbin/tgtadm(命令行的工具)
      /usr/sbin/tgtd
      /usr/sbin/tgtimg
 3.启动服务
        systemctl start tgtd,
    设置开机自启动
        systemctl enable tgtd 
 4.创建lun
     tgtadm 
三种模式:
  target,logicalunit,account
     1.target --op
        new,delete,show,update,bind,unbind
     2.logicalunit --op 
        new,delete
     3.account --op
        new,delete,bind,unbind 
             --lld,-L 
             --tid,-t 
             --lun,-l
             --backing-store <path>,-b
             --initiator-address <address>,-I 
    
           tid0是保留给当前主机的,自己不要使用
           lun应该是某个target的lun,对应一个分区,lvm,硬盘
  

  • 创建一个target   
[root@localhost ~]# tgtadm --lld iscsi --mode target --op new --targetname iqn.2017-5.com.luxiangyu:teststore.disk1  --tid 1 
[root@localhost ~]# tgtadm --lld iscsi --mode target --op show 
Target 1: iqn.2017-05.com.luxiangyu:teststore.disk1
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
    SWP: No
    Thin-provisioning: No
    Backing store type: null
    Backing store path: None
    Backing store flags: 
    Account information:
    ACL information:

    

    target上的lun也是有限制的



  •  基于target创建多个LUN
[root@localhost ~]# tgtadm --lld iscsi --mode logicalunit --op new --tid 1 --lun 1 --backing-store /dev/sdb 
[root@localhost ~]# tgtadm --lld iscsi --mode target --op show 
Target 1: iqn.2017-05.com.luxiangyu:teststore.disk1
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
SWP: No
Thin-provisioning: No
Backing store type: null
Backing store path: None
Backing store flags: 
LUN: 1
Type: disk
SCSI ID: IET     00010001
SCSI SN: beaf11
Size: 107374 MB, Block size: 512
Online: Yes
Removable media: No
Prevent removal: No
Readonly: No
SWP: No
Thin-provisioning: No
Backing store type: rdwr
Backing store path: /dev/sdb
Backing store flags: 
Account information:
ACL information:
5.绑定ACL
        [root@localhost ~]# tgtadm --lld iscsi --mode target --op bind --tid 1 --initiator-address 172.17.0.0/16 
[root@localhost ~]# tgtadm --lld iscsi --mode target --op show 
        Target 1: iqn.2017-05.com.luxiangyu:teststore.disk1
        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
            SWP: No
            Thin-provisioning: No
            Backing store type: null
            Backing store path: None
            Backing store flags: 
        LUN: 1
            Type: disk
            SCSI ID: IET     00010001
            SCSI SN: beaf11
            Size: 107374 MB, Block size: 512
            Online: Yes
            Removable media: No
            Prevent removal: No
            Readonly: No
            SWP: No
            Thin-provisioning: No
            Backing store type: rdwr
            Backing store path: /dev/sdb
            Backing store flags: 
        Account information:
        ACL information:
             172.17.0.0/16
   也可以解除绑定    
initiator安装配置:

1.安装
  [root@localhost ~]# yum -y install iscsi-initiator-utils
  
    每个initiator也有一个名字,可以利用命令iscsi-iname
 
        2.查看现在的iscsi-iname:
  [root@localhost ~]# iscsi-iname 
          iqn.1994-05.com.redhat:be65954ff6f
利用命令生成:
           [root@localhost ~]# iscsi-iname -p iqn.2017-05.com.luxiangyu
              iqn.2017-05.com.luxiangyu:21bb2a55199
   直接生成并保存至相关的配置文件中:
[root@localhost ~]# echo "InitiatorName=`iscsi-iname -p iqn.2017-05.com.luxiangyu`" > /etc/iscsi/initiatorname.iscsi  
 并且可以添加别名:
  Initiatoralias=node1.luxiangyu 
       3.启动服务
        [root@localhost ~]# systemctl start iscsi
       4.发现target并连接:
iscsiadm 模式化命令:
-m {discovery|node|session|iface} 
  discovery:发现某服务器是否有target输出,以及输出了那些target
  node:管理和某target的关联关系
  sesion:会话管理
  iface:接口管理
iscsiadm -m discovery [ -d debug_level ] [ -I iface -t type -p ip:port ]
     
   [root@localhost ~]# iscsiadm -m discovery -t sendtargets -p 172.17.107.42
     172.17.107.42:3260,1 iqn.2017-05.com.luxiangyu:teststore.disk1
       发现的主机target会被存放在/var/lib/iscsi/send_targets/
         [root@localhost ~]# ls /var/lib/iscsi/send_targets/172.17.107.42,3260/
           iqn.2017-05.com.luxiangyu:teststore.disk1,172.17.107.42,3260,1,default  st_config
        5.登入
         [root@localhost ~]# iscsiadm -m node -T iqn.2017-05.com.luxiangyu:teststore.disk1 -p 172.17.107.42 -l
Logging in to [iface: default, target: iqn.2017-05.com.luxiangyu:teststore.disk1, portal:172.17.107.42,3260] (multiple)Login to [iface: default, target: iqn.2017-05.com.luxiangyu:teststore.disk1, portal: 172.17.107.42,3260] successful.
        
        6.格式化识别的硬盘并测试使用
   查看现在的硬盘数量:(可以发现多了一块硬盘)
[root@localhost ~]# fdisk -l 
Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000a4424
  Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      411647      204800   83  Linux
/dev/sda2          411648     4605951     2097152   82  Linux swap / Solaris
/dev/sda3         4605952   209715199   102554624   83  Linux
Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
         格式化:
           [root@localhost ~]# mke2fs -t ext4 /dev/sdb
           [root@localhost ~]# mount /dev/sdb /opt  
           [root@localhost ~]# cp anaconda-ks.cfg /opt
      

     共享磁盘,但是单机文件系统,同时编辑同一个文件可能会造成系统崩溃
双向认证:
 1.target认证initiator
  [root@localhost iscsi]# vim /etc/iscsi/iscsid.conf
   #node.session.auth.authmethod = CHAP


# To set a CHAP username and password for initiator
# authentication by the target(s), uncomment the following lines:
#node.session.auth.username = username
#node.session.auth.password = password
                 2.initiator验证target
# To set a CHAP username and password for target(s)
# authentication by the initiator, uncomment the following lines:
#node.session.auth.username_in = username_in
#node.session.auth.password_in = password_in
open-iscsi不支持discover时认证,所以关于discovery认证无效。
        另外基于用户名密码的认证必须以基于IP的认证为前提

      基于CHAP中用户名密码登陆:
  1.initiator端登出target
              [root@localhost iscsi]# iscsiadm -m node -T iqn.2017-05.com.luxiangyu:teststore.disk1 -p 172.17.107.42 -u
  2.initiator端删除相关的数据库
      [root@localhost iscsi]# iscsiadm -m node -T iqn.2017-05.com.luxiangyu:teststore.disk1 -p 172.17.107.42 -o delete 
             [root@localhost iscsi]# rm -rf /var/lib/iscsi/send_targets/172.17.107.42,3260/
          3.target端解除相关ip的绑定规则:
[root@localhost ~]# tgtadm --lld iscsi --mode target --op unbind --tid 1 --initiator-address 172.17.0.0/16 
[root@localhost ~]# tgtadm --lld iscsi --mode target --op show 

outgoing 账号指绑定用来让客户端验证服务器端的账号密码
[root@localhost ~]# tgtadm --lld iscsi --mode account --op new --user iscsiuser --password iscsiuser 
[root@localhost ~]# tgtadm --lld iscsi --mode account --op bind --tid 1 --user iscsiuser                                                 [root@localhost ~]# tgtadm --lld iscsi --mode account --op show 
Account list:
iscsiuser
      
     服务可用性:保证服务在重启后根据配置文件自动启动target
initiator端在服务重启后会自动连接target;
target服务重启后会读取/etc/tgt/target.conf 文件中的定义
[root@localhost tgt]# cat targets.conf
  <target iqn.2017-05.com.luxiangyu:teststore.disk1>
<backing-store /dev/sdb>
  vendor_id luxinagyu
  lun 6
</backing-store>
<backing-store /dev/sdc>
  vendor_id luxinagyu
  lun 7
</backing-store>
incominguser iscsiuser iscsiuser 
initiator-address 172.17.0.0/16
</target>