当我们的系统需要大量的磁盘容量,但是身边却没有足够的存储设备,此时,我们可以使用通过网络的scsi磁盘,即Internet scsi(iscsi)。iscsi主要是通过TCP/IP的技术,将存储设备端通过iscsi target功能,做成可以提供磁盘的服务器端,再通过iscsi initiator(iscsi初始化用户)功能,做成能够挂载使用iscsi target的客户端,这样就能够通过iscsi协议来进行磁盘的应用了。
iscsi架构主要将存储设备与使用的主机分为两个部分,分别是:
iscsi target:就是存储设备端,存放磁盘设备。目的在于提供其他主机使用的磁盘。
iscsi initiator:就是能够使用target的客户端,通常是服务器。也就是说,想要连接到iscsi target的服务器,也必须要安装iscsi initiator的相关功能后才能使用iscsi target提供的磁盘。
第一步:首先准备好iscsi共享的磁盘,iscsi可以通过(1)大型文件(2)单一分区(3)单一设备来进行共享。
(1)建立一个500M大小的文件dd if=/dev/zero of=/iscsi bs=1M count=500
(2)创建一个新分区
(3)准备一个空磁盘
[root@localhost iscsi]# dd if=/dev/zero of=/data/scsi bs=1M count=500 #创建500M的空间,创建在/iscsi ---dd 创建 从/dev下 of是存储位置 bs为单位体积 500个
[root@localhost iscsi]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 7.9G 0 rom /mnt
nvme0n1 259:0 0 40G 0 disk
├─nvme0n1p1 259:1 0 300M 0 part /boot
├─nvme0n1p2 259:2 0 2G 0 part [SWAP]
└─nvme0n1p3 259:3 0 37.7G 0 part /
nvme0n2 259:4 0 1G 0 disk
├─nvme0n2p1 259:5 0 300M 0 part
│ └─rhce_vg-rhce_lv1 253:0 0 300M 0 lvm
├─nvme0n2p2 259:6 0 300M 0 part
│ └─rhce_vg-rhce_lv2 253:1 0 300M 0 lvm
└─nvme0n2p3 259:7 0 300M 0 part
├─rhce_vg-rhce_lv1 253:0 0 300M 0 lvm
└─rhce_vg-rhce_lv2 253:1 0 300M 0 lvm
nvme0n3 259:8 0 1G 0 disk
第二步:安装软件
[root@localhost ~]# yum install targetcli -y
第三步:配置服务器
targetcli是用于管理iscsi服务端存储资源的专用配置命令,它能够提供类似于fdisk命令的交互式配置功能,将iscsi共享资源的配置内容抽象成“目录”的形式,我们只需要将各类配置信息填入到相应的“目录”中即可。
[root@localhost ~]# targetcli
/> ls /
o- / ..................................................... [...]
o- backstores .......................................... [...]可用于iscsi的服务存储类型有四种
| o- block .............................. [Storage Objects: 0]块设备,磁盘驱动器,磁盘分区,逻辑卷,以及服务器上定义的任何b类型的设备文件
| o- fileio ............................. [Storage Objects: 0]在服务器上生成的一个指定大小的文件
| o- pscsi .............................. [Storage Objects: 0]物理scsi,通常不用
| o- ramdisk ............................ [Storage Objects: 0]内存盘,其中存储的数据在服务器重启后将全部丢失
o- iscsi ........................................ [Targets: 0]
o- loopback ..................................... [Targets: 0]
第三步:给三个可共享存储定义逻辑单元编号
[root@localhost /]# targetcli
targetcli shell version 2.1.51
Copyright 2011-2013 by Datera, Inc and others.
For help on commands, type 'help'.
/>
/> cd /backstores/block
/backstores/block> create dev=/dev/rhce_vg/rhce_lv1 name=lun0
Created block storage object lun0 using /dev/rhce_vg/rhce_lv1
/backstores/block> create dev=/dev/rhce_vg/rhce_lv2 name=lun1
Created block storage object lun1 using /dev/rhce_vg/rhce_lv1.
/backstores/block> cd /backstores/fileio/
/backstores/fileio> create file_or_dev=/data/scsi lun2 #位置在/data/iscsi 大小为500M
Created fileio lun3 with size 524288000
#查看配置
/> ls
o- / ................................................................................................ [...]
o- backstores ..................................................................................... [...]
| o- block ......................................................................... [Storage Objects: 2]
| | o- lun0 ................................................ [/dev/nvme0n3 (1.0GiB) write-thru activated]
| | | o- alua .......................................................................... [ALUA Groups: 1]
| | | o- default_tg_pt_gp .............................................. [ALUA state: Active/optimized]
| | o- lun1 ..................................... [/dev/rhce_vg/rhce_lv1 (300.0MiB) write-thru activated]
| | o- alua .......................................................................... [ALUA Groups: 1]
| | o- default_tg_pt_gp .............................................. [ALUA state: Active/optimized]
| o- fileio ........................................................................ [Storage Objects: 1]
| | o- lun2 ................................................ [/data/scsi (500.0MiB) write-back activated]
| | o- alua .......................................................................... [ALUA Groups: 1]
| | o- default_tg_pt_gp .............................................. [ALUA state: Active/optimized]
| o- pscsi ......................................................................... [Storage Objects: 0]
| o- ramdisk ....................................................................... [Storage Objects: 0]
o- iscsi ................................................................................... [Targets: 1]
| o- iqn.2022-01.com.rhce:server .............................................................. [TPGs: 1]
| o- tpg1 ......................................................................... [gen-acls, no-auth]
| o- acls ................................................................................. [ACLs: 0]
| o- luns ................................................................................. [LUNs: 3]
| | o- lun0 .......................................... [block/lun0 (/dev/nvme0n3) (default_tg_pt_gp)]
| | o- lun1 ................................. [block/lun1 (/dev/rhce_vg/rhce_lv1) (default_tg_pt_gp)]
| | o- lun2 ........................................... [fileio/lun2 (/data/scsi) (default_tg_pt_gp)]
| o- portals ........................................................................... [Portals: 1]
| o- 0.0.0.0:3260 ............................................................................ [OK]
o- loopback ................................................................................ [Targets: 0]
/>
第四步:设置共享的iscsi target名称及配置共享资源
iscsi target名称可由系统自动生成,这是一串用于描述共享资源的唯一字符串。
iscsi有一套自己共享target名称的定义,基本上iscsi共享出来的target文件名都是以iqn开头,意思是“ISCSI Qualified Name(iscsi合格名称)”。iscsi的target名字示例如下:iqn.yyyy-mm.网络名的反转写法:这个共享的target名称。
/> cd /iscsi
/iscsi> ls
o- iscsi .................................................................................................. [Targets: 0]
/iscsi> create wwn= iqn.2022-01.com.rhce:server #创建iscsi共享文件
Created target iqn.2022-01.com.rhce:server
Created TPG 1.
Global pref auto_add_default_portal=true
Created default portal listening on all IPs (0.0.0.0), port 3260.
/iscsi> cd /iscsi/ iqn.2022-01.com.rhce:server/tpg1/luns
/iscsi/iqn.20...50b/tpg1/luns> create /backstores/block/lun0 #创建/block/lun0的共享
/iscsi/iqn.20...50b/tpg1/luns> create /backstores/block/lun1 #创建/block/lun1的共享
/iscsi/iqn.20...50b/tpg1/luns> create /backstores/fileio/lun2 #创建/block/lun2的共享
//查看ip地址和端口
/> cd /iscsi/iqn.2022-01.com.rhce:server/tpg1/portals/
/iscsi/iqn.20.../tpg1/portals> ls
o- portals ................................................................................... [Portals: 1]
o- 0.0.0.0:3260 .................................................................................... [OK]
//设置关闭账号密码验证访问控制和自动生成acl节点
/> cd /iscsi/iqn.2022-01.com.rhce:server/tpg1/portals/
/iscsi/iqn.20...50b/tpg1>set attribute authentication=0
Parameter authentication is now '0'. #关闭账号密码验证访问控制
/iscsi/iqn.20...50b/tpg1>set attribute generate_node_acls=1
Parameter generate_node_acls is now '1'. #自动生成acl节点
/>saveconfig #保存配置
/>exit #退出
第五步:启动服务
[root@localhost rhce_vg]# systemctl stop firewalld
[root@localhost rhce_vg]# getenforce
Permissive
[root@localhost rhce_vg]# systemctl restart iscsi iscsid
软件安装:
[root@localhost ~]# yum install iscsi-initiator-utils -y
//iscsiadm 客户扫描是否由可以用的iscsi服务
-m: mode discovery, discoverydb, node, fw, host iface or session
-p: ip:port
-T: -T, --targetname=targetname 使用tagetname iqn.xxxx.xx
-t: --type
-t: sendtargets, This option is only valid for discovery mode
-u: logout 退出
-U: 全部退出
-l: login 登录
-L: alllogin 全部登录
-I: interface 接口
discovery: 扫描并发现
node: 把自己节点登录server
iscsi客户端查看和登录
[root@localhost /]# systemctl restart iscsi iscsid #启动服务
[root@localhost /]# iscsiadm -m discovery -t sendtargets -p 192.168.58.66 #iscsi磁盘发现
192.168.58.66:3260,1 iqn.2022-01.com.rhce:server
[root@localhost /]# iscsiadm -m node -p 192.168.58.66:3260 -l #磁盘登录
[root@localhost /]# lsblk #本地查看
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 1G 1 disk
sdb 8:16 0 300M 1 disk
sdc 8:32 0 500M 1 disk
sr0 11:0 1 7.9G 0 rom /mnt
nvme0n1 259:0 0 40G 0 disk
├─nvme0n1p1 259:1 0 300M 0 part /boot
├─nvme0n1p2 259:2 0 2G 0 part [SWAP]
└─nvme0n1p3 259:3 0 37.7G 0 part /
[root@localhost /]#
/iscsi/iqn.20...ver/tpg1/acls> ls
o- acls ......................................................................................... [ACLs: 0]
/iscsi/iqn.20...ver/tpg1/acls> create iqn.2022-01.com.rhce:client
Created Node ACL for iqn.2022-01.com.rhce:client
Created mapped LUN 2.
Created mapped LUN 1.
Created mapped LUN 0.
/iscsi/iqn.20...ver/tpg1/acls> cd /
/> saveconfig
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@localhost rhce_vg]# systemctl restart iscsi iscsid
//此时重新启动新的虚拟机 --- 尝试挂载服务端的iscsi
[root@localhost ~]# iscsiadm -m discovery -t sendtargets -p 192.168.58.66
192.168.58.66:3260,1 iqn.2022-01.com.rhce:server
//此时发现无法挂载服务器的iscsi共享磁盘
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 7.9G 0 rom /run/media/root/RHEL-8-2-0-BaseOS-x86_64
nvme0n1 259:0 0 40G 0 disk
├─nvme0n1p1 259:1 0 1G 0 part /boot
└─nvme0n1p2 259:2 0 39G 0 part
├─rhel-root 253:0 0 37G 0 lvm /
└─rhel-swap 253:1 0 2G 0 lvm [SWAP]
[root@localhost ~]# vim /etc/iscsi/initiatorname.iscsi
#!!!此时要注意InitiatorName为唯一的编码连接!!!
#InitiatorName=iqn.1994-05.com.redhat:32f1b84b4a52
InitiatorName=iqn.2021-12.com.rhce:client
//重启服务
[root@localhost ~]# systemctl restart iscsi iscsid
//录入ip
[root@localhost ~]# iscsiadm -m discovery -t sendtargets -p 192.168.58.66
192.168.58.66:3260,1 iqn.2022-01.com.rhce:server
//登录
[root@localhost ~]# iscsiadm -m node -l
//查看挂载情况
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 1G 0 disk
sdb 8:16 0 300M 0 disk
sdc 8:32 0 500M 0 disk
sr0 11:0 1 7.9G 0 rom /run/media/root/RHEL-8-2-0-BaseOS-x86_64
nvme0n1 259:0 0 40G 0 disk
├─nvme0n1p1 259:1 0 1G 0 part /boot
└─nvme0n1p2 259:2 0 39G 0 part
├─rhel-root 253:0 0 37G 0 lvm /
└─rhel-swap 253:1 0 2G 0 lvm [SWAP]
[root@localhost iscsi]# targetcli
targetcli shell version 2.1.51
Copyright 2011-2013 by Datera, Inc and others.
For help on commands, type 'help'.
/> cd iscsi/iqn.2022-01.com.rhce:server/tpg1/acls/
/iscsi/iqn.20...ver/tpg1/acls> ls
o- acls ......................................................................................... [ACLs: 1]
o- iqn.2022-01.com.rhce:client ......................................................... [Mapped LUNs: 3]
o- mapped_lun0 ................................................................. [lun0 block/lun0 (rw)]
o- mapped_lun1 ................................................................. [lun1 block/lun1 (rw)]
o- mapped_lun2 ................................................................ [lun2 fileio/lun2 (rw)]
/iscsi/iqn.20...ver/tpg1/acls> ls
o- acls ......................................................................................... [ACLs: 1]
o- iqn.2022-01.com.rhce:client ......................................................... [Mapped LUNs: 3]
o- mapped_lun0 ................................................................. [lun0 block/lun0 (rw)]
o- mapped_lun1 ................................................................. [lun1 block/lun1 (rw)]
o- mapped_lun2 ................................................................ [lun2 fileio/lun2 (rw)]
/iscsi/iqn.20...ver/tpg1/acls> cd iqn.2022-01.com.rhce:client/ #cd到具体client下设置acl
/iscsi/iqn.20...m.rhce:client> set auth userid=srb #设置用户为srb :set auth userid=srb
Parameter userid is now 'srb'.
/iscsi/iqn.20...m.rhce:client> set auth password=123456 ##设置用户密码为123456
Parameter password is now '123456'.
/iscsi/iqn.20...m.rhce:client> cd /
/> saveconfig
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@lxb ~]# systemctl restart iscsi iscsid #退出并重启服务
[root@localhost ~]# cd /etc/iscsi/
[root@localhost iscsi]# ll
total 20
-rw-r--r--. 1 root root 95 Dec 30 09:43 initiatorname.iscsi #主要是登录的物理认证
-rw-------. 1 root root 13187 Aug 10 01:10 iscsid.conf #登录的加载认证(用户,密码)
[root@localhost iscsi]# vim iscsid.conf
#此时需要修改iscsid.conf 的配置
//启用认证
node.session.auth.authmethod = CHAP
//用户名
node.session.auth.username = srb
//密码
node.session.auth.password = 123456
//此时重新扫描等登录iscsi
[root@localhost iscsi]# iscsiadm -m node -p 192.168.58.66:3260 -l
Logging in to [iface: default, target: iqn.2022-01.org.linux-iscsi.lxb.x8664:sn.2c4dc2594940, portal: 192.168.58.66,3260]
Login to [iface: default, target: iqn.2022-01.org.linux-iscsi.lxb.x8664:sn.2c4dc2594940, portal: 192.168.58.66,3260] successful.
//查看磁盘挂载
[root@localhost iscsi]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 5G 0 disk
sdb 8:16 0 2G 0 disk
sdc 8:32 0 500M 0 disk
sdd 8:48 0 5G 0 disk
sr0 11:0 1 7.9G 0 rom /run/media/root/RHEL-8-2-0-BaseOS-x86_64
nvme0n1 259:0 0 40G 0 disk
├─nvme0n1p1 259:1 0 1G 0 part /boot
└─nvme0n1p2 259:2 0 39G 0 part
├─rhel-root 253:0 0 37G 0 lvm /
└─rhel-swap 253:1 0 2G 0 lvm [SWAP]
//取消iscsi登录
[root@localhost iscsi]# iscsiadm -m node -p 192.168.220.129:3260 -u