基於IP的SAN網絡--iscsi

一、簡介

SCSI(Small Computer System Interface)是塊數據傳輸協議,在存儲行業廣泛應用,是存儲設備最基本的標準協議。

iSCSI協議是一種利用IP網絡來傳輸潛伏時間短的SCSI 數據塊的方法,iSCSI使用以太網協議傳送SCSI命令、響應和數據。iSCSI可以用我們已經熟悉和每天都在使用的以太網來構建IP存儲局域網。通過這種方法,iSCSI克服了直接連接存儲的侷限性,使我們可以跨不同服務器共享存儲資源,並可以在不停機狀態下擴充存儲容量。


二、原理

wKioL1NKj5XyuDgtAADY_3P_vVQ845.jpg


iSCSI的工作過程:當iSCSI主機應用程序發出數據讀寫請求後,操作系統會生成一個相應的SCSI命令,該SCSI命令在iSCSI Initiator層被封裝成iSCSI消息包並通過TCP/IP傳送到設備側,設備側的iSCSI Target層會解開iSCSI消息包,得到SCSI命令的內容,然後傳送給SCSI設備執行;設備執行SCSI命令後的響應,在經過設備側iSCSI Target層時被封裝成iSCSI響應PDU,通過TCP/IP網絡傳送給主機的iSCSI Initiator層,iSCS Initiator會從iSCSI響應PDU裏解析出SCSI響應並傳送給操作系統,操作系統再響應給應用程序。要實現iSCSI讀寫,除了使用特定硬設備外,也可透過軟件方式,將服務器仿真爲iSCSI的發起端(Initiator)或目標端(target),利用既有的處理器與普通的以太網絡卡資源實現iSCSI的連接。



三、規劃

172.16.1.50
centos 6.4
target
172.16.1.60
centos 6.4
Initiator


四、搭建iscsi目標段

1. 安裝iscsi-target軟件

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


2. 查看分區

[root@target ~]# lsblk
NAME                         MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0                           11:0    1  4.1G  0 rom  /mnt
sda                            8:0    0   20G  0 disk
├─sda1                         8:1    0  500M  0 part /boot
└─sda2                         8:2    0 19.5G  0 part
  ├─vg_target-lv_root (dm-0) 253:0    0 17.6G  0 lvm  /
  └─vg_target-lv_swap (dm-1) 253:1    0    2G  0 lvm  [SWAP]
sdb                            8:16   0   10G  0 disk
├─sdb1                         8:17   0    4G  0 part
└─sdb2                         8:18   0    5G  0 part


3. 啓動tgtd服務

[root@target ~]# service tgtd start
Starting SCSI target daemon:                               [  OK  ]
[root@target ~]# chkconfig tgtd on


4. 配置

LUN(Logical Unit Number)是邏輯單元號,一般爲數字,用來標識存儲設備。

IQN(iSCSI Qualified Name)格式:

格式

意義

範例

yyyy-mm

年份-月份

2014-04

reversed domain name

把域名名稱反過來寫,通常把公司的域名反過來寫

com.target

identifier

識別字,通常註明這個存儲空間的用途

share-storage


[root@target ~]# tgtadm --lld iscsi --mode target --op new --tid 1 --targetname iqn.2014-04.com.target:share-storage
#新增target device
[root@target ~]# tgtadm --lld iscsi --mode logicalunit --op new --tid 1 --lun 1 --backing-store /dev/sdb1
#將起初新建的分區加入target device
[root@target ~]# tgtadm --lld iscsi --mode target --op bind --tid 1 --initiator-address 172.16.1.0/24
#設置可以訪問存取此target device的initiator節點
[root@target ~]# tgtadm --lld iscsi --mode target --op show
Target 1: iqn.2014-04.com.target:share-storage
    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: 4302 MB, Block size: 512
            Online: Yes
            Removable media: No
            Prevent removal: No
            Readonly: No
            Backing store type: rdwr
            Backing store path: /dev/sdb1
            Backing store flags:
    Account information:
    ACL information:
        172.16.1.0/24

這些配置命令不需要記憶,-h查看一下就知道了


5. 編輯配置文件配置永久生效

[root@target ~]# vim /etc/tgt/targets.conf
<target iqn.2014-04.com.target:share-storage>
    <backing-store /dev/sdb1>
        lun 1
    </backing-store>
    initiator-address 172.16.1.0/24
</target>
#在最後添加這幾行

當然,這裏還有很多其他參數可以寫,如incominguser、outgoinguser、MaxConnections等,具體可以參見配置文件,文件裏寫得很清楚。


6. 在防火牆上放行3260端口

[root@target ~]# iptables -I INPUT 1 -p tcp -m state --state NEW --dport 3260 -j ACCEPT
[root@target ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]


六、linux發起端配置

1. 安裝並啓動軟件

[root@client ~]# yum install iscsi-initiator-utils -y
[root@client ~]# service iscsi start
Starting iscsi:                                            [  OK  ]
[root@client ~]# chkconfig iscsi on


2. 發現設備並登錄

[root@client ~]# iscsiadm -m discovery -t sendtargets -p 172.16.1.50:3260                    
172.16.1.50:3260,1 iqn.2014-04.com.target:share-storage
[root@client ~]# iscsiadm -m node -T iqn.2014-04.com.target:share-storage -p 172.16.1.50:3260 -l
Logging in to [iface: default, target: iqn.2014-04.com.target:share-storage, portal: 172.16.1.50,3260] (multiple)
Login to [iface: default, target: iqn.2014-04.com.target:share-storage, portal: 172.16.1.50,3260] successful.


3. 查看設備

[root@client ~]# lsblk
NAME                         MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0                           11:0    1  4.1G  0 rom  /mnt
sda                            8:0    0   20G  0 disk
├─sda1                         8:1    0  500M  0 part /boot
└─sda2                         8:2    0 19.5G  0 part
  ├─vg_client-lv_root (dm-0) 253:0    0 17.6G  0 lvm  /
  └─vg_client-lv_swap (dm-1) 253:1    0    2G  0 lvm  [SWAP]
sdb                            8:16   0    4G  0 disk
└─sdb1                         8:17   0    1G  0 part   #iscsi設備,我已經分區並格式化了


4. 自動掛載這個磁盤

[root@client ~]# tune2fs -l /dev/sdb1
tune2fs 1.41.12 (17-May-2010)
Filesystem volume name:   <none>
Last mounted on:          <not available>
Filesystem UUID:          4b8cbee1-9e6f-4939-9049-1d18ca61d397
[root@client ~]# vim /etc/fstab
UUID=4b8cbee1-9e6f-4939-9049-1d18ca61d397       /backup         ext4    defaults,_netdev        0 0
#_netdev是針對iscsi設備的特殊mount選項,此掛載選項指示將在網絡啓動後掛載該卷,在關閉網絡前卸載該卷。


5. 刪除該iscsi設備

[root@client ~]# iscsiadm -m node -T iqn.2014-04.com.target:share-storage -p 172.16.1.50:3260 -u
Logging out of session [sid: 2, target: iqn.2014-04.com.target:share-storage, portal: 172.16.1.50,3260]
Logout of [sid: 2, target: iqn.2014-04.com.target:share-storage, portal: 172.16.1.50,3260] successful.
[root@client ~]# iscsiadm -m node -o delete -T iqn.2014-04.com.target:share-storage -p 172.16.1.50:3260


6. 進行讀寫測試

[root@client ~]# cd /backup/
[root@client backup]# ls
ds  dsaf  lost+found
[root@client backup]# dd if=/dev/zero of=/backup/dd bs=1M count=100
100+0 records in
100+0 records out
104857600 bytes (105 MB) copied, 0.896341 s, 117 MB/s
[root@client backup]# ls
dd  ds  dsaf  lost+found


七、使用CHAP認證

1. 目標端

[root@target ~]# vim /etc/tgt/targets.conf
<target iqn.2014-04.com.target:share-storage>
    <backing-store /dev/sdb1>
        lun 1
    </backing-store>
    initiator-address 172.16.1.0/24
    incominguser lion passwd        #發起端登錄的用戶名和密碼
    outgoinguser sven 12345         #連接發起端的用戶名和密碼
</target>

配置完成重啓服務即可,再查看一下。

[root@target ~]# tgtadm --lld iscsi --mode target --op show
Target 1: iqn.2014-04.com.target:share-storage
    System information:
        Driver: iscsi
        State: ready
    I_T nexus information:
        I_T nexus: 1
            Initiator: iqn.2014-04.com.target:share-storage
            Connection: 0
                IP Address: 172.16.1.60
    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: 4302 MB, Block size: 512
            Online: Yes
            Removable media: No
            Prevent removal: No
            Readonly: No
            Backing store type: rdwr
            Backing store path: /dev/sdb1
            Backing store flags:
    Account information:
        lion
        sven (outgoing)
    ACL information:
        172.16.1.0/24


2. 發起端配置

[root@client ~]# vim /etc/iscsi/iscsid.conf
node.session.auth.authmethod = CHAP
node.session.auth.username = lion
node.session.auth.password = passwd
node.session.auth.username_in = sven
node.session.auth.password_in = 12345

配置完成重啓服務。

這樣就實現了基於用戶名和密碼的認證了。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章