欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

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

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

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

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