环境准备:
server:192.168.58 133
client: 192.168.58.131
[root@node1 ~]# fdisk /dev/sdb
#需要进行分区,但不需要做格式化
设备 Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 83 Linux
/dev/sdb2 2099200 4196351 1048576 83 Linux[root@node1 ~]# partprobe
一、服务端配置
1、关闭防火墙
[root@node1 ~]# systemctl stop firewalld
[root@node1 ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
2、安装target软件
[root@node4 ~]# yum -y install targetcli
Installed:
targetcli.noarch 0:2.1.53-1.el7_9
Dependency Installed:
libnl.x86_64 0:1.1.4-3.el7 python-configshell.noarch 1:1.1.26-1.el7 python-ethtool.x86_64 0:0.8-8.el7
python-kmod.x86_64 0:0.9-4.el7 python-rtslib.noarch 0:2.1.74-1.el7_9 python-urwid.x86_64 0:1.1.1-3.el7
Complete!
3、targetcli命令配置服务端
[root@node4 ~]# targetcli
Warning: Could not load preferences file /root/.targetcli/prefs.bin.
targetcli shell version 2.1.53
Copyright 2011-2013 by Datera, Inc and others.
For help on commands, type 'help'.
#1.创建后端存储 /backstores/block create disk1 /dev/sdb1
/> backstores/block create dev=/dev/sdb1 name=test
Created block storage object test using /dev/sdb1.
#2.创建磁盘组,新的 Target
/> iscsi/ create wwn=iqn.2023-9.com.test:server.
WWN not valid as: iqn, naa, eui
/> iscsi/ create wwn=iqn.2023-09.com.test:server.
Created target iqn.2023-09.com.test: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/ delete wwn=iqn.2023-09.com.test:server.
Deleted Target iqn.2023-09.com.test:server..
/> iscsi/ create wwn=iqn.2023-09.com.test:server
Created target iqn.2023-09.com.test:server.
Created TPG 1.
Global pref auto_add_default_portal=true
Created default portal listening on all IPs (0.0.0.0), port 3260.
3.进行lun关联
/> iscsi/iqn.2023-09.com.test:server/tpg1/luns create /backstores/block/test
Created LUN 0.
#4.设置访问控制,设置客户端声称的名字
/> iscsi/iqn.2023-09.com.test:server/tpg1/acls create iqn.2023-09.com.test:client
Created Node ACL for iqn.2023-09.com.test:client
Created mapped LUN 0.
#5.查看配置信息
/> ls
o- / ..................................................................................................... [...]
o- backstores .......................................................................................... [...]
| o- block .............................................................................. [Storage Objects: 1]
| | o- test ........................................................ [/dev/sdb1 (2.0GiB) write-thru activated]
| | o- alua ............................................................................... [ALUA Groups: 1]
| | o- default_tg_pt_gp ................................................... [ALUA state: Active/optimized]
| o- fileio ............................................................................. [Storage Objects: 0]
| o- pscsi .............................................................................. [Storage Objects: 0]
| o- ramdisk ............................................................................ [Storage Objects: 0]
o- iscsi ........................................................................................ [Targets: 1]
| o- iqn.2023-09.com.test:server ................................................................... [TPGs: 1]
| o- tpg1 ........................................................................... [no-gen-acls, no-auth]
| o- acls ...................................................................................... [ACLs: 1]
| | o- iqn.2023-09.com.test:client ...................................................... [Mapped LUNs: 1]
| | o- mapped_lun0 .............................................................. [lun0 block/test (rw)]
| o- luns ...................................................................................... [LUNs: 1]
| | o- lun0 .................................................. [block/test (/dev/sdb1) (default_tg_pt_gp)]
| o- portals ................................................................................ [Portals: 1]
| o- 0.0.0.0:3260 ................................................................................. [OK]
o- loopback ..................................................................................... [Targets: 0]
/> exit
Global pref auto_save_on_exit=true
Configuration saved to /etc/target/saveconfig.json
5、重启targetcli服务
[root@localhost ~]# systemctl restart target
[root@node4 ~]# systemctl enable target.service
Created symlink from /etc/systemd/system/multi-user.target.wants/target.service to /usr/lib/systemd/system/target.service.
二、客户端配置
1、 安装iscsi-initiator-utils软件
[root@node2 ~]# yum -y install iscsi-initiator-utils
Installed:
iscsi-initiator-utils.x86_64 0:6.2.0.874-22.el7_9
Dependency Installed:
iscsi-initiator-utils-iscsiuio.x86_64 0:6.2.0.874-22.el7_9
Complete!
2、修改/etc/iscsi/initiatorname.iscsi文件中客户端声称的名字
[root@node2 ~]# cd /etc/iscsi/
[root@node2 iscsi]# ls
initiatorname.iscsi iscsid.conf
[root@node2 ~]# cd /etc/iscsi/
[root@node2 iscsi]# ls
initiatorname.iscsi iscsid.conf
[root@node2 iscsi]# cp initiatorname.iscsi initiatorname.iscsi.bak
[root@node2 iscsi]# vim initiatorname.iscsi
[root@node2 iscsi]# cat initiatorname.iscsi
InitiatorName=iqn.2023-09.com.test:client
3、重启iscsid服务,刷新客户端标识
[root@node2 iscsi]# systemctl restart iscsi
iscsid.service iscsi.service iscsiuio.service
iscsid.socket iscsi-shutdown.service iscsiuio.socket
[root@node2 iscsi]# systemctl restart iscsid
4、使用客户端发现命令
[root@node2 iscsi]# iscsiadm -m discovery -t st -p 192.168.58.133
192.168.58.133:3260,1 iqn.2023-09.com.test:server
[root@node2 iscsi]# iscsiadm --mode discoverydb --type sendtargets -p 192.168.58.133
# BEGIN RECORD 6.2.0.874-22
discovery.startup = manual
discovery.type = sendtargets
discovery.sendtargets.address = 192.168.58.133
discovery.sendtargets.port = 3260
discovery.sendtargets.auth.authmethod = None
discovery.sendtargets.auth.username = <empty>
discovery.sendtargets.auth.password = <empty>
discovery.sendtargets.auth.username_in = <empty>
discovery.sendtargets.auth.password_in = <empty>
node.session.auth.chap_algs = MD5
discovery.sendtargets.timeo.login_timeout = 15
discovery.sendtargets.use_discoveryd = No
discovery.sendtargets.discoveryd_poll_inval = 30
discovery.sendtargets.reopen_max = 5
discovery.sendtargets.timeo.auth_timeout = 45
discovery.sendtargets.timeo.active_timeout = 30
discovery.sendtargets.iscsi.MaxRecvDataSegmentLength = 32768
# END RECORD
[root@node2 iscsi]# iscsiadm -m session -P 3 |grep Attached
Attached SCSI devices:
Attached scsi disk sdb State: running
[root@node2 iscsi]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─centos-root00 253:0 0 17G 0 lvm /
└─centos-swap 253:1 0 2G 0 lvm
sdb 8:16 0 2G 0 disk
sr0 11:0 1 4.4G 0 rom
[root@node2 iscsi]# fdisk -l
Disk /dev/sdb: 2146 MB, 2146435072 bytes, 4192256 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 4194304 bytes
5、客户端命令操作
#发现服务端已设置好的 Target
iscsiadm -m discovery -t sendtargets -p 192.168.58.133:3260
#详细信息
iscsiadm -m discoverydb -t sendtargets -p 192.168.58.133:3260
[root@node2 ~]# iscsiadm -m discovery -t st -p 192.168.58.133
192.168.58.133:3260,1 iqn.2023-09.com.test:server
验证客户端是否存储已发现的服务端 Target:
[root@node2 ~]# ll -R /var/lib/iscsi/nodes/
/var/lib/iscsi/nodes/:
total 0
drw------- 3 root root 35 Sep 22 14:10 iqn.2023-09.com.test:server
/var/lib/iscsi/nodes/iqn.2023-09.com.test:server:
total 0
drw------- 2 root root 21 Sep 22 14:10 192.168.58.133,3260,1
/var/lib/iscsi/nodes/iqn.2023-09.com.test:server/192.168.58.133,3260,1:
total 4
-rw------- 1 root root 2100 Sep 22 14:10 default
#登录服务端已设置好的 Target
iscsiadm -m node -T iqn.2023-09.com.test:server -p 192.168.58.133 -l
[root@node2 ~]# iscsiadm -m node -T iqn.2023-09.com.test:server -l
Logging in to [iface: default, target: iqn.2023-09.com.test:server, portal: 192.168.58.133,3260] (multiple)
Login to [iface: default, target: iqn.2023-09.com.test:server, portal: 192.168.58.133,3260] successful.
#验证会话状态及相关的磁盘信息
[root@node2 ~]# iscsiadm -m session -P 3|grep Attached
Attached SCSI devices:
Attached scsi disk sdb State: running
Attached scsi disk sdc State: running
[root@node2 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─centos-root00 253:0 0 17G 0 lvm /
└─centos-swap 253:1 0 2G 0 lvm
sdb 8:16 0 1G 0 disk
sdc 8:32 0 1023M 0 disk
sr0 11:0 1 4.4G 0 rom
此时可以进行分区、格式化、挂载使用sdb sdc
#客户端卸载 iSCSI Target
[root@node2 ~]# iscsiadm -m node -T iqn.2023-09.com.test:server -u
Logging out of session [sid: 2, target: iqn.2023-09.com.test:server, portal: 192.168.58.133,3260]
Logout of [sid: 2, target: iqn.2023-09.com.test:server, portal: 192.168.58.133,3260] successful.
[root@node2 ~]# iscsiadm -m session -P 3
iSCSI Transport Class version 2.0-870
version 6.2.0.874-22
iscsiadm: No active sessions.
#删除已发现的 iSCSI 服务端信息并验证
[root@node2 ~]# iscsiadm -m node -T iqn.2023-09.com.test:server -o delete
[root@node2 ~]# ll /var/lib/iscsi/nodes/
total 0