Linux基礎學習-使用iSCSI服務部署網絡存儲

使用iSCSI服務部署網絡存儲

iSCSI技術實現了物理硬盤設備與TCP/IP網絡協議的相互結合,使得用戶能夠經過互聯網方便地訪問遠程機房提供的共享存儲資源.下面介紹如何在Linux上部署iSCSI服務端程序,並分別給予Linux系統和Windows系統來訪問遠程的存儲資源.node

實驗環境

主機名稱 操做系統 IP地址
iSCSI服務端 RHEL7 192.168.56.15
iSCSI客戶端 CentOS7 192.168.56.5
windows7客戶端 windows7 192.168.56.100

1.安裝iSCSI服務linux

[root@iSCSI-Server ~]# yum install targetd targetcli -y

//啓動iSCSI服務並加入開機自啓動
[root@iSCSI-Server ~]# systemctl start targetd && systemctl enable targetd
Created symlink from /etc/systemd/system/multi-user.target.wants/targetd.service to /usr/lib/systemd/system/targetd.service.

2.配置iSCSI服務共享資源shell

//咱們要添加sdb這塊磁盤
[root@iSCSI-Server ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   50G  0 disk 
├─sda1   8:1    0  500M  0 part /boot
├─sda2   8:2    0    1G  0 part [SWAP]
└─sda3   8:3    0 48.5G  0 part /
sdb      8:16   0    2T  0 disk 
sr0     11:0    1  3.8G  0 rom  /mnt

[root@iSCSI-Server ~]# targetcli 
Warning: Could not load preferences file /root/.targetcli/prefs.bin.
targetcli shell version 2.1.fb46
Copyright 2011-2013 by Datera, Inc and others.
For help on commands, type 'help'.

/> ls
o- / ............................................................................... [...]
  o- backstores .................................................................... [...]
  | o- block ........................................................ [Storage Objects: 0]
  | o- fileio ....................................................... [Storage Objects: 0]
  | o- pscsi ........................................................ [Storage Objects: 0]
  | o- ramdisk ...................................................... [Storage Objects: 0]
  o- iscsi .................................................................. [Targets: 0]
  o- loopback ............................................................... [Targets: 0]
/> cd /backstores/block 
Created block storage object disk0 using /dev/sdb.
/backstores/block> ls
o- block ............................................................ [Storage Objects: 1]
  o- disk0 .................................... [/dev/sdb (2.0TiB) write-thru deactivated]
    o- alua ............................................................. [ALUA Groups: 1]
      o- default_tg_pt_gp ................................. [ALUA state: Active/optimized]
/backstores/block>

3.建立iSCSI target名稱及配置共享資源數據庫

/backstores/block> cd /
/> cd iscsi 
/iscsi> create 
Created target iqn.2003-01.org.linux-iscsi.iscsi-server.x8664:sn.a5b591427412.
Created TPG 1.
Global pref auto_add_default_portal=true
Created default portal listening on all IPs (0.0.0.0), port 3260.
/iscsi> ls
o- iscsi .................................................................... [Targets: 1]
  o- iqn.2003-01.org.linux-iscsi.iscsi-server.x8664:sn.a5b591427412 ............ [TPGs: 1]
    o- tpg1 ....................................................... [no-gen-acls, no-auth]
      o- acls .................................................................. [ACLs: 0]
      o- luns .................................................................. [LUNs: 0]
      o- portals ............................................................ [Portals: 1]
        o- 0.0.0.0:3260 ............................................................. [OK]
/iscsi> cd iqn.2003-01.org.linux-iscsi.iscsi-server.x8664:sn.a5b591427412
/iscsi/iqn.20....a5b591427412> ls
o- iqn.2003-01.org.linux-iscsi.iscsi-server.x8664:sn.a5b591427412 .............. [TPGs: 1]
  o- tpg1 ......................................................... [no-gen-acls, no-auth]
    o- acls .................................................................... [ACLs: 0]
    o- luns .................................................................... [LUNs: 0]
    o- portals .............................................................. [Portals: 1]
      o- 0.0.0.0:3260 ............................................................... [OK]
/iscsi/iqn.20....a5b591427412> cd tpg1
/iscsi/iqn.20...91427412/tpg1> ls
o- tpg1 ........................................................... [no-gen-acls, no-auth]
  o- acls ...................................................................... [ACLs: 0]
  o- luns ...................................................................... [LUNs: 0]
  o- portals ................................................................ [Portals: 1]
    o- 0.0.0.0:3260 ................................................................. [OK]
/iscsi/iqn.20...91427412/tpg1> cd luns 
/iscsi/iqn.20...412/tpg1/luns> create /backstores/block/disk0 
Created LUN 0.
/iscsi/iqn.20...412/tpg1/luns> ls
o- luns ........................................................................ [LUNs: 1]
  o- lun0 .................................... [block/disk0 (/dev/sdb) (default_tg_pt_gp)]
/iscsi/iqn.20...412/tpg1/luns>

4.設置訪問控制列表(ACL).json

/iscsi/iqn.20...412/tpg1/luns> cd ..
/iscsi/iqn.20...91427412/tpg1> ls
o- tpg1 ........................................................... [no-gen-acls, no-auth]
  o- acls ...................................................................... [ACLs: 0]
  o- luns ...................................................................... [LUNs: 1]
  | o- lun0 .................................. [block/disk0 (/dev/sdb) (default_tg_pt_gp)]
  o- portals ................................................................ [Portals: 1]
    o- 0.0.0.0:3260 ................................................................. [OK]
/iscsi/iqn.20...91427412/tpg1> 
/iscsi/iqn.20...91427412/tpg1> cd acls 
/iscsi/iqn.20...412/tpg1/acls> ls
o- acls ........................................................................ [ACLs: 0]
/iscsi/iqn.20...412/tpg1/acls> create iqn.2003-01.org.linux-iscsi.iscsi-server.x8664:sn.a5b591427412:client
Created Node ACL for iqn.2003-01.org.linux-iscsi.iscsi-server.x8664:sn.a5b591427412:client
Created mapped LUN 0.
/iscsi/iqn.20...412/tpg1/acls>

5.設置iSCSI服務端的監聽IP地址和端口號.vim

/iscsi/iqn.20...412/tpg1/acls> cd ..
/iscsi/iqn.20...91427412/tpg1> ls
o- tpg1 ........................................................... [no-gen-acls, no-auth]
  o- acls ...................................................................... [ACLs: 1]
  | o- iqn.2003-01.org.linux-iscsi.iscsi-server.x8664:sn.a5b591427412:client  [Mapped LUNs: 1]
  |   o- mapped_lun0 ............................................. [lun0 block/disk0 (rw)]
  o- luns ...................................................................... [LUNs: 1]
  | o- lun0 .................................. [block/disk0 (/dev/sdb) (default_tg_pt_gp)]
  o- portals ................................................................ [Portals: 1]
    o- 0.0.0.0:3260 ................................................................. [OK]
/iscsi/iqn.20...91427412/tpg1> cd portals/
/iscsi/iqn.20.../tpg1/portals> ls
o- portals .................................................................. [Portals: 1]
  o- 0.0.0.0:3260 ................................................................... [OK]
/iscsi/iqn.20.../tpg1/portals> create 192.168.56.15
/iscsi/iqn.20.../tpg1/portals> cd /
/> saveconfig 
Last 10 configs saved in /etc/target/backup.
Configuration saved to /etc/target/saveconfig.json
/> exit
Global pref auto_save_on_exit=true
Last 10 configs saved in /etc/target/backup.
Configuration saved to /etc/target/saveconfig.json
[root@iSCSI-Server ~]#

6.配置穩當後檢查配置信息,重啓iSCSI服務端程序並配置防火牆策略.windows

[root@iSCSI-Server ~]# systemctl restart targetd

//防火牆能夠以下配置
[root@iSCSI-Server ~]# firewall-cmd --permanent --add-port=3260/tcp
[root@iSCSI-Server ~]# firewall-cmd --reload

配置Linux客戶端.

iscsiadm是用於管理、查詢、插入、更新或刪除iSCSI數據庫配置文件的命令行工具,用戶須要先使用這個工具掃描發現遠程iSCSI服務端,而後查看找到的服務端上有哪些可用的共享存儲資源.安全

-m discovery參數的目的是掃描並發現可用的存儲資源
-t st參數爲執行掃描操做的類型
-p 192.168.56.15參數爲iSCSI服務端的IP地址服務器

-m node參數爲將客戶端所在主機做爲一臺節點服務器
-T 參數爲要使用的存儲資源
-p 192.168.56.15參數爲對方iSCSI服務器IP的地址
--login或-l參數進行登錄驗證網絡

//安裝iSCSI客戶端服務程序initiator.默認RHEL7已經安裝了.
[root@iSCSI-Client ~]# yum install iscsi-initiator-utils -y

//編輯名稱文件
[root@iS
CSI-Client ~]# vim /etc/iscsi/initiatorname.iscsi 

//重啓客戶端服務程序
[root@iSCSI-Client ~]# systemctl restart iscsid
[root@iSCSI-Client ~]# systemctl enable iscsid
Created symlink from /etc/systemd/system/multi-user.target.wants/iscsid.service to /usr/lib/systemd/system/iscsid.service.

//先發現再登陸
[root@iSCSI-Client ~]# iscsiadm -m discovery -t st -p 192.168.56.15
192.168.56.15:3260,1 iqn.2003-01.org.linux-iscsi.qdlinux.x8664:sn.f32bb7ada923

//使用iscsiadm命令發現遠程服務器上可用的存儲資源後,接下來準備登錄iSCSI服務器
[root@iSCSI-Client ~]# iscsiadm -m node -T iqn.2003-01.org.linux-iscsi.qdlinux.x8664:sn.f32bb7ada923 -p 192.168.56.15 --login

登錄成功後查看一下發現多了一塊磁盤

[root@iSCSI-Client ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   50G  0 disk 
├─sda1   8:1    0  500M  0 part /boot
├─sda2   8:2    0    2G  0 part [SWAP]
└─sda3   8:3    0 47.5G  0 part /
sdb      8:16   0    2T  0 disk 
sdc      8:32   0    2T  0 disk 
sr0     11:0    1  4.2G  0 rom  /mnt

[root@iSCSI-Client ~]# file /dev/sdc
/dev/sdc: block special

//查看設備UUID
[root@iSCSI-Client ~]# blkid
/dev/sda1: UUID="2b924cf3-3741-4489-afda-20ad6c3b8366" TYPE="xfs" 
/dev/sda2: UUID="c8ad89d6-ca56-4120-a4b6-5d9c54976bbb" TYPE="swap" 
/dev/sda3: UUID="780d3cde-84a6-4f98-a800-94b0dc7f57f9" TYPE="xfs" 
/dev/sr0: UUID="2018-05-03-20-55-23-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos" 
/dev/sdc: UUID="28619840-b497-49ca-92c6-c530a850dd40" TYPE="xfs" 

//開機自動掛載,因爲iSCSI基於TCP/IP網絡傳輸數據的
//所以必須在fstab配置文件中加上`_netdev`參數
//表示當系統聯網後再進行掛載操做,以避免系統開機時間過長或開機失敗.
[root@iSCSI-Client ~]# vim /etc/fstab 
UUID=28619840-b497-49ca-92c6-c530a850dd40 /iscsi                  xfs      defaults,_netdev 0 0

[root@iSCSI-Client ~]# mount -a
[root@iSCSI-Client ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        48G  4.6G   43G  10% /
devtmpfs        472M     0  472M   0% /dev
tmpfs           488M     0  488M   0% /dev/shm
tmpfs           488M  8.0M  480M   2% /run
tmpfs           488M     0  488M   0% /sys/fs/cgroup
/dev/sr0        4.2G  4.2G     0 100% /mnt
/dev/sda1       497M  148M  350M  30% /boot
tmpfs            98M     0   98M   0% /run/user/0
/dev/sdc        2.0T   33M  2.0T   1% /iscsi

//若是再也不須要使用iSCSI共享設備資源了,可使用iscsiadm命令的-u參數將其卸載.
[root@iSCSI-Client ~]# iscsiadm -m node -T iqn.2003-01.org.linux-iscsi.qdlinux.x8664:sn.f32bb7ada923 -u
Logging out of session [sid: 1, target: iqn.2003-01.org.linux-iscsi.qdlinux.x8664:sn.f32bb7ada923, portal: 192.168.56.15,3260]
Logout of [sid: 1, target: iqn.2003-01.org.linux-iscsi.qdlinux.x8664:sn.f32bb7ada923, portal: 192.168.56.15,3260] successful.

配置Windows客戶端.

1.運行iSCSI發起程序.

在Windows7操做系統中已經默認安裝了iSCSI客戶端程序,咱們只須要在控制面板中找到系統和安全標籤,而後點擊管理工具,進入到管理工具頁面後點擊iSCSI發起程序.在第一次運行iSCSI發起程序時,系統會提示Mirosoft iSCSI服務端未運行,點擊按鈕便可自動啓動並運行iSCSI發起程序.

2.掃描發現iSCSI服務端上可用的存儲資源.

不管是Windows系統仍是Linux系統,要想使用iSCSI共享存儲資源都必須先進行掃描發現操做.運行iSCSI發起程序後在目標選項卡的目標文本框寫入iSCSI服務器的IP地址,而後點擊快速鏈接按鈕.

在彈出的快速連接提示框中可看到共享的硬盤存儲資源,點擊完成按鈕便可.

相關文章
相關標籤/搜索