一、介绍
iSCSI (Internet Small Computer System Interface),互联网小型计算机系统接口;iSCSI 是SCSI接口 与以太网技术相结合的新型存储技术,属于ip san的一种,可以用来在网络中传输 SCSI 接口的命令和数据。这样,不仅克服了传统 SCSI 接口设备的物理局限性,实现了跨区域的存储资源共享,还可以在不停机的状态下扩展存储容量。
iSCSI 存储技术非常便捷,在访问存储资源的形式上发生了很大变化,摆脱了物理环境的限制,同时还可以把存储资源分给多个服务器共同使用,但是,iSCSI 存储技术受到了网速的制约。以往,硬盘设备直接通过主板上的总线进行数据传输,现在则需要让互联网作为据传输的载体和通道,因此传输速率和稳定性是 iSCSI 技术的瓶颈。
既然要通过以太网来传输硬盘设备上的数据,那么数据是通过网卡传入到计算机中的
么?这就有必要向大家介绍 iSCSI-HBA 卡了。与一般的网卡不同(连接网络总线和内存,供计算机上网使用),iSCSI-HBA 卡连接的则是 SCSI 接口或 FC(光纤通道)总线和内存,专门用于在主机之间交换存储数据,其使用的协议也与一般网卡有本质的不同。运行Linux系统的服务器会基于iSCSI协议把硬盘设备命令与数据打包成标准的TCP/IP数据包,然后通过以太网传输到目标存储设备,而当目标存储设备接收到这些数据包后,还需要基于iSCSI 协议把 TCP/IP 数据包解压成硬盘设备命令与数据。
二、部署iSCSI
iSCSI 分为服务端(target)与客户端(initiator)。iSCSI 服务端即用于存放硬盘存储资源的服务器,能够为用户提供可用的存储资源。iSCSI 客户端则是用户使用的软件,用于访问远程服务端的存储源。
本次实验实验vmware虚机来完成实现,系统是Centos7.9
1. 使用两块磁盘做软件raid1,来充当iSCSI的存储资源(可选)
2. 配置raid,raid模式为raid1
2.1. 下载raid配置工具mdadm
# yum -y install mdadm
2.2. 查看需要配置raid的磁盘
采用sdb ,sdc 配置raid1
[root@192 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 200M 0 part /boot
└─sda2 8:2 0 19.8G 0 part /
sdb 8:16 0 20G 0 disk
sdc 8:32 0 20G 0 disk
sr0 11:0 1 4.4G 0 rom
2.3. 配置raid
raid名称为md0
[root@192 ~]# mdadm --create --verbose /dev/md0 --level=1 --raid-devices=2 --metadata=0.90 /dev/sd[bc]
mdadm: size set to 20971456K
mdadm: array /dev/md0 started.
[root@192 ~]# mdadm -Q /dev/md0
/dev/md0: 20.00GiB raid1 2 devices, 0 spares. Use mdadm --detail for more detail.
[root@192 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 200M 0 part /boot
└─sda2 8:2 0 19.8G 0 part /
sdb 8:16 0 20G 0 disk
└─md0 9:0 0 20G 0 raid1
sdc 8:32 0 20G 0 disk
└─md0 9:0 0 20G 0 raid1
sr0 11:0 1 4.4G 0 rom
3. 服务端安装配置
3.1. 安装服务端软件
# yum -y install targetd targetcli
- targetd 守护进程
- targetcli 配置工具
3.2. 启动targetd
# systemctl enable targetd --now
3.3. 配置iSCSI 服务端存储资源
targetcli 是用于管理 iSCSI 服务端存储资源的专用配置命令,是一个交互命令。/backstores/block 是iSCSI 服务端配置共享设备的位置。我们需要把刚刚创建的RAID 1虚拟 磁盘阵列 md0 文件加入到配置共享设备的“资源池”中,并将该文件重新命名为 disk0
# 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'.
/> 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]
/>
3.3.1. 创建共享存储资源
/backstores/block 是iSCSI 服务端配置共享设备的位置
/> ls backstores/
o- backstores ................................................................................................................ [...]
o- block .................................................................................................... [Storage Objects: 0]
o- fileio ................................................................................................... [Storage Objects: 0]
o- pscsi .................................................................................................... [Storage Objects: 0]
o- ramdisk .................................................................................................. [Storage Objects: 0]
/> cd backstores/block
/backstores/block> create disk0 /dev/md0
Created block storage object disk0 using /dev/md0.
/backstores/block> ls
o- block ...................................................................................................... [Storage Objects: 1]
o- disk0 ............................................................................. [/dev/md0 (20.0GiB) write-thru deactivated]
o- alua ....................................................................................................... [ALUA Groups: 1]
o- default_tg_pt_gp ........................................................................... [ALUA state: Active/optimized]
3.3.2. 创建 iSCSI target 名称及配置共享资源
iSCSI target 名称是由系统自动生成的,这是一串用于描述共享资源的唯一字符串。稍后用户在扫描 iSCSI 服务端时即可看到这个字符串,因此我们不需要记住它。系统在生成这个 target 名称后,还会在/iscsi 参数目录中创建一个与其字符串同名的新“目录”用来存放共享资源。我们需要把前面加入到 iSCSI 共享资源池中的硬盘设备添加到这个新目录中,这样用户在登录 iSCSI 服务端后,即可默认使用这硬盘设备提供的共享存储资源了。
/> cd /iscsi
/iscsi> create
Created target iqn.2003-01.org.linux-iscsi.192.x8664:sn.0aaeb17c0459.
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.192.x8664:sn.0aaeb17c0459 ............................................................... [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.192.x8664:sn.0aaeb17c0459/
/iscsi/iqn.20....0aaeb17c0459> cd tpg1/luns
/iscsi/iqn.20...459/tpg1/luns> create /backstores/block/disk0
Created LUN 0.
/iscsi/iqn.20...459/tpg1/luns> ls
o- luns .................................................................................................................. [LUNs: 1]
o- lun0 .............................................................................. [block/disk0 (/dev/md0) (default_tg_pt_gp)]
3.3.3. 设置访问控制列表
iSCSI 协议是通过客户端名称进行验证的,也就是说,用户在访问存储共享资源时不需要输入密码,只要 iSCSI 客户端的名称与服务端中设置的访问控制列表中某一名称条目一致即可,因此需要在 iSCSI 服务端的配置文件中写入一串能够验证用户信息的名称。acls 参数目录用于存放能够访问 iSCSI 服务端共享存储资源的客户端名称。
create iqn.2003-01.org.linux-iscsi.192.x8664:sn.0aaeb17c0459
/iscsi/iqn.20...b17c0459/tpg1> ls
o- tpg1 ..................................................................................................... [no-gen-acls, no-auth]
o- acls ................................................................................................................ [ACLs: 0]
o- luns ................................................................................................................ [LUNs: 1]
| o- lun0 ............................................................................ [block/disk0 (/dev/md0) (default_tg_pt_gp)]
o- portals .......................................................................................................... [Portals: 1]
o- 0.0.0.0:3260 ........................................................................................................... [OK]
/iscsi/iqn.20...b17c0459/tpg1> cd acls
/iscsi/iqn.20...459/tpg1/acls> ls
o- acls .................................................................................................................. [ACLs: 0]
/iscsi/iqn.20...459/tpg1/acls> pwd
/iscsi/iqn.2003-01.org.linux-iscsi.192.x8664:sn.0aaeb17c0459/tpg1/acls
/iscsi/iqn.20...459/tpg1/acls> create iqn.2003-01.org.linux-iscsi.192.x8664:sn.0aaeb17c0459
Created Node ACL for iqn.2003-01.org.linux-iscsi.192.x8664:sn.0aaeb17c0459
Created mapped LUN 0.
/iscsi/iqn.20...459/tpg1/acls> ls
o- acls .................................................................................................................. [ACLs: 1]
o- iqn.2003-01.org.linux-iscsi.192.x8664:sn.0aaeb17c0459 ........................................................ [Mapped LUNs: 1]
o- mapped_lun0 ......................................................................................... [lun0 block/disk0 (rw)]
3.3.3. 指定ip作为将向外提供 iSCSI 共享存储资源
默认是绑定主机上的所有IP,我们需要指定ip(两张网卡的话可以配置bond)
/iscsi/iqn.20...b17c0459/tpg1> ls
o- tpg1 ..................................................................................................... [no-gen-acls, no-auth]
o- acls ................................................................................................................ [ACLs: 1]
| o- iqn.2003-01.org.linux-iscsi.192.x8664:sn.0aaeb17c0459 ...................................................... [Mapped LUNs: 1]
| o- mapped_lun0 ....................................................................................... [lun0 block/disk0 (rw)]
o- luns ................................................................................................................ [LUNs: 1]
| o- lun0 ............................................................................ [block/disk0 (/dev/md0) (default_tg_pt_gp)]
o- portals .......................................................................................................... [Portals: 1]
o- 0.0.0.0:3260 ........................................................................................................... [OK]
/iscsi/iqn.20...b17c0459/tpg1> cd portals/
/iscsi/iqn.20.../tpg1/portals> ls
o- portals ............................................................................................................ [Portals: 1]
o- 0.0.0.0:3260 ............................................................................................................. [OK]
将默认地址删掉,指定ip提供服务,iSCSI 使用3260/tcp作为访问端口,需要确保端口已经被开放
/iscsi/iqn.20.../tpg1/portals> ls
o- portals ............................................................................................................ [Portals: 1]
o- 0.0.0.0:3260 ............................................................................................................. [OK]
/iscsi/iqn.20.../tpg1/portals> delete 0.0.0.0 3260
Deleted network portal 0.0.0.0:3260
/iscsi/iqn.20.../tpg1/portals> create 192.168.92.148
Using default IP port 3260
Created network portal 192.168.92.148:3260.
/iscsi/iqn.20.../tpg1/portals> ls
o- portals ............................................................................................................ [Portals: 1]
o- 192.168.92.148:3260 ...................................................................................................... [OK]
3.3.4. 保存配置,并重启 targetd
在targetcli界面输入exit保存配置
/iscsi/iqn.20.../tpg1/portals> exit
Global pref auto_save_on_exit=true
Configuration saved to /etc/target/saveconfig.json
[root@192 ~]# systemctl restart targetd
[root@192 ~]# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 192.168.92.148:3260 0.0.0.0:* LISTEN -
4. 配置客户端
4.1. 配置linux客户端(使用ubuntu作为客户端)
iSCSI 客户端服务程序 initiator,Centos7.9 下安装: # yum install iscsi-initiator-utils
ubuntu默认已安装
1) 修改客户端iSCSI 唯一标识
iSCSI 协议是通过客户端的名称来进行验证,而该名称也是 iSCSI 客户端的唯一标识,而且必须与服务端配置文件中访问控制列表中的信息一致,否则客户端在尝试访问存储共享设备时,系统会弹出验证失败的保存信息。
编辑 iSCSI 客户端中的 initiator 名称文件,把服务端的访问控制列表名称填写进
来,然后重启客户端 iscsid 服务程序并将其加入到开机启动项中:
root@openstack:~# cat /etc/iscsi/initiatorname.iscsi
## DO NOT EDIT OR REMOVE THIS FILE!
## If you remove this file, the iSCSI daemon will not start.
## If you change the InitiatorName, existing access control lists
## may reject this initiator. The InitiatorName must be unique
## for each iSCSI initiator. Do NOT duplicate iSCSI InitiatorNames.
InitiatorName=iqn.2003-01.org.linux-iscsi.192.x8664:sn.0aaeb17c0459
root@openstack:~# systemctl enable iscsid --now
Synchronizing state of iscsid.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable iscsid
2) 使用iSCSI服务端的存储资源
root@openstack:~# iscsiadm -m discovery -t st -p 192.168.92.148:3260
192.168.92.148:3260,1 iqn.2003-01.org.linux-iscsi.192.x8664:sn.0aaeb17c0459
- -m discovery 扫描并发现可用的存储资源
- -t st 指定 SCSI 目标发现
- -p 192.168.92.148 指定 iSCSI 发现的网络地址
3 ) 登录 iSCSI 服务端
登录完,网络磁盘会自动以裸盘的方式挂载到服务器上
root@openstack:~# iscsiadm -m node -T iqn.2003-01.org.linux-iscsi.192.x8664:sn.0aaeb17c0459 --login
Logging in to [iface: default, target: iqn.2003-01.org.linux-iscsi.192.x8664:sn.0aaeb17c0459, portal: 192.168.92.148,3260] (multiple)
Login to [iface: default, target: iqn.2003-01.org.linux-iscsi.192.x8664:sn.0aaeb17c0459, portal: 192.168.92.148,3260] successful.
- -m node 参数为将客户端所在主机作为一台节点服务器,
- -T iqn.2003-01.org.linux-iscsi.192.x8664:sn.0aaeb17c0459 参数为要使用的存储资源
- -p 192.168.10.10 参数依然为对方 iSCSI 服务端的 IP 地址。
- –login 或-l 参数进行登录验证
4 ) 查看是否添加了新盘
sdb就是新添加的磁盘
root@openstack:~# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
loop0 7:0 0 62M 1 loop /snap/core20/1611
loop1 7:1 0 67.8M 1 loop /snap/lxd/22753
loop2 7:2 0 38.8M 1 loop /snap/snapd/21759
sda 8:0 0 60G 0 disk
├─sda1 8:1 0 1M 0 part
├─sda2 8:2 0 2G 0 part /boot
└─sda3 8:3 0 58G 0 part
└─ubuntu--vg-ubuntu--lv 253:0 0 29G 0 lvm /
sdb 8:16 0 20G 0 disk
sr0 11:0 1 1.3G 0 rom
5) 将新盘格式化并挂载
root@openstack:~# mkdir /data
root@openstack:~# mkfs.ext4 /dev/sdb
mke2fs 1.45.5 (07-Jan-2020)
Creating filesystem with 5242864 4k blocks and 1310720 inodes
Filesystem UUID: c1dafed3-6196-4673-adb3-e2dde7d7fa75
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
root@openstack:~# mount /dev/sdb /data
root@openstack:~# df -hT /dev/sdb
Filesystem Type Size Used Avail Use% Mounted on
/dev/sdb ext4 20G 24K 19G 1% /data
root@openstack:~# touch /data/text
6)永久挂载
/dev/sdb 是一块网络存储设备,而 iSCSI 协议是基于TCP/IP 网络传输数据的,因此必须在/etc/fstab 配置文件中添加上_netdev 参数,表示当系统联网后再进行挂载操作,以免系统开机时间过长或开机失败:
root@openstack:~# blkid /dev/sdb
/dev/sdb: UUID="c1dafed3-6196-4673-adb3-e2dde7d7fa75" TYPE="ext4"
root@openstack:~# cat /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>
# / was on /dev/ubuntu-vg/ubuntu-lv during curtin installation
/dev/disk/by-id/dm-uuid-LVM-G4zbpeVKQ2xZhG0haLE99zL2sMniWCmID1HEfn0D2UF0jweearkJII2cLLNKol63 / ext4 defaults 0 1
# /boot was on /dev/sda2 during curtin installation
/dev/disk/by-uuid/3944bf57-8043-4de8-b3e7-2a0b40a2d11f /boot ext4 defaults 0 1
/swap.img none swap sw 0 0
UUID="c1dafed3-6196-4673-adb3-e2dde7d7fa75" /data ext4 defaults,_netdev 0 0