Linux基础学习-使用iSCSI服务部署网络存储

使用iSCSI服务部署网络存储

iSCSI技术实现了物理硬盘设备与TCP/IP网络协议的相互结合,使得用户可以通过互联网方便地访问远程机房提供的共享存储资源.下面介绍如何在Linux上部署iSCSI服务端程序,并分别给予Linux系统和Windows系统来访问远程的存储资源.

实验环境

主机名称 操作系统 IP地址
iSCSI服务端 RHEL7 192.168.56.15
iSCSI客户端 CentOS7 192.168.56.5
windows7客户端 windows7 192.168.56.100

1.安装iSCSI服务

[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服务共享资源

//我们要添加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).

/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地址和端口号.

/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服务端程序并配置防火墙策略.

[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地址,然后点击快速连接按钮.

在弹出的快速链接提示框中可看到共享的硬盘存储资源,点击完成按钮即可.

posted @ 2018-08-22 22:23  抖音不抖  阅读(1020)  评论(0编辑  收藏  举报