如何在 Ubuntu 18.04 LTS 上设置 iSCSI 存储服务器
本教程适用于这些操作系统版本
- Ubuntu 20.04(Focal Fossa)
- Ubuntu 18.04(仿生海狸)
在此页
- 要求
- 开始
- 安装 iSCSI 目标
- 配置 iSCSI 目标
- 安装和配置 iSCSI 发起程序
iSCSI 代表(Internet 小型计算机系统接口)是一种存储区域网络 (SAN) 协议,可用于在线存储管理。它是一种基于 IP 的存储网络标准,通过在 TCP/IP 网络上传输 SCSI 命令来提供对存储设备的块级访问。
iSCSI 由两个组件 iSCSI target 和 iSCSI initiator 组成。 iSCSI 目标是 iSCSI 服务器上的一项服务,可提供对共享存储的访问,而 iSCSI 启动器是连接到目标并访问共享存储的 iSCSI 客户端。
在本教程中,我们将向您展示如何在 Ubuntu 18.04 服务器上设置 iSCSI 目标和 iSCSI 启动器。
要求
- 用于 iSCSI 目标的全新 Ubuntu 18.04 服务器和 2 GB 外部硬盘驱动器。
- 用于 iSCSI 启动器的全新 Ubuntu 18.04 服务器。
- 在 iSCSI 目标上配置了静态 IP 地址 192.168.0.103,在 iSCSI 启动器上配置了 192.168.0.102。
- 在两台服务器上都配置了根密码。
入门
在开始之前,您需要使用最新版本更新两台服务器。您可以通过在两个 $上运行以下命令来更新它们
apt-get update -y
apt-get upgrade -y
更新两个服务器后,重新启动它们以应用更改。
安装 iSCSI 目标
首先,您需要在 iSCSI 目标服务器上安装目标框架 (TGT) 包。您可以使用以下命令安装它:
apt-get install tgt -y
安装 TGT 后,通过运行以下命令检查 TGT 的状态:
systemctl status tgt
您应该看到以下输出:
? tgt.service - (i)SCSI target daemon
Loaded: loaded (/lib/systemd/system/tgt.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2019-11-08 07:40:28 UTC; 27s ago
Docs: man:tgtd(8)
Main PID: 2343 (tgtd)
Status: "Starting event loop..."
Tasks: 1
CGroup: /system.slice/tgt.service
??2343 /usr/sbin/tgtd -f
Nov 08 07:40:28 ubuntu systemd[1]: Starting (i)SCSI target daemon...
Nov 08 07:40:28 ubuntu tgtd[2343]: tgtd: iser_ib_init(3431) Failed to initialize RDMA; load kernel modules?
Nov 08 07:40:28 ubuntu tgtd[2343]: tgtd: work_timer_start(146) use timer_fd based scheduler
Nov 08 07:40:28 ubuntu tgtd[2343]: tgtd: bs_init(387) use signalfd notification
Nov 08 07:40:28 ubuntu systemd[1]: Started (i)SCSI target daemon.
完成后,您可以继续下一步。
配置 iSCSI 目标
接下来,您需要在 iSCSI 服务器上创建一个 LUN(逻辑单元号)设备。 LUN是发起者以后连接使用的后端存储设备。
您可以通过在 /etc/tgt/conf.d 目录中创建配置文件来实现:
nano /etc/tgt/conf.d/iscsi.conf
添加以下行:
<target iqn.2019-11.example.com:lun1>
# Provided device as an iSCSI target
backing-store /dev/sdb1
initiator-address 192.168.0.102
incominguser iscsi-user password
outgoinguser iscsi-target secretpass
</target>
完成后保存并关闭文件。然后,重新启动 TGT 服务以应用配置更改:
systemctl restart tgt
下面对每个参数做一个简单的解释:
target :这是特定目标的名称。
backing-store :此选项指定启动器将使用的存储磁盘。
initiator-address :这是启动器的 IP 地址。
incominguser :这是用于保护 LUN 的传入用户名/密码。
outgoinguser :这是用于相互 CHAP 身份验证的传出用户名/密码。
重启TGT服务后,使用以下命令检查iSCSI目标服务器:
tgtadm --mode target --op show
您应该看到 iSCSI 目标可用:
Target 1: iqn.2019-11.example.com:lun1
System information:
Driver: iscsi
State: ready
I_T nexus information:
LUN information:
LUN: 0
Type: controller
SCSI ID: IET 00010000
SCSI SN: beaf10
Size: 0 MB, Block size: 1
Online: Yes
Removable media: No
Prevent removal: No
Readonly: No
SWP: No
Thin-provisioning: No
Backing store type: null
Backing store path: None
Backing store flags:
LUN: 1
Type: disk
SCSI ID: IET 00010001
SCSI SN: beaf11
Size: 2146 MB, Block size: 512
Online: Yes
Removable media: No
Prevent removal: No
Readonly: No
SWP: No
Thin-provisioning: No
Backing store type: rdwr
Backing store path: /dev/sdb1
Backing store flags:
Account information:
iscsi-user
iscsi-target (outgoing)
ACL information:
192.168.0.102
安装和配置 iSCSI 发起程序
接下来,您需要在 iSCSI 启动器服务器上安装 iSCSI 启动器包。您可以使用以下命令安装它:
apt-get install open-iscsi -y
安装完成后,对我们的 iSCSI 目标服务器运行目标发现以找出共享目标。
iscsiadm -m discovery -t st -p 192.168.0.103
您应该在以下输出中看到可用的目标:
192.168.0.103:3260,1 iqn.2019-11.example.com:lun1
上述命令还会生成两个包含 LUN 信息的文件。您可以使用以下命令查看它们:
ls -l /etc/iscsi/nodes/iqn.2019-11.example.com\:lun1/192.168.0.103\,3260\,1/ /etc/iscsi/send_targets/192.168.0.103,3260/
您应该看到以下文件:
/etc/iscsi/nodes/iqn.2019-11.example.com:lun1/192.168.0.103,3260,1/:
total 4
-rw------- 1 root root 1840 Nov 8 13:17 default
/etc/iscsi/send_targets/192.168.0.103,3260/:
total 8
lrwxrwxrwx 1 root root 66 Nov 8 13:17 iqn.2019-11.example.com:lun1,192.168.0.103,3260,1,default -> /etc/iscsi/nodes/iqn.2019-11.example.com:lun1/192.168.0.103,3260,1
-rw------- 1 root root 547 Nov 8 13:17 st_config
接下来,您需要编辑默认文件并定义您在 iSCSI 目标上配置的 CHAP 信息,以便从 iSCSI 启动器访问 iSCSI 目标。
nano /etc/iscsi/nodes/iqn.2019-11.example.com\:lun1/192.168.0.103\,3260\,1/default
添加/更改以下行:
node.session.auth.authmethod = CHAP
node.session.auth.username = iscsi-user
node.session.auth.password = password
node.session.auth.username_in = iscsi-target
node.session.auth.password_in = secretpass
node.startup = automatic
完成后保存并关闭文件。然后,重新启动 iSCSI 发起程序服务以应用配置更改:
systemctl restart open-iscsi
您应该看到以下输出:
* Unmounting iscsi-backed filesystems [ OK ]
* Disconnecting iSCSI targets iscsiadm: No matching sessions found
[ OK ]
* Stopping iSCSI initiator service [ OK ]
* Starting iSCSI initiator service iscsid [ OK ]
* Setting up iSCSI targets
Logging in to [iface: default, target: iqn.2019-11.example.com:lun1, portal: 192.168.0.103,3260] (multiple)
Login to [iface: default, target: iqn.2019-11.example.com:lun1, portal: 192.168.0.103,3260] successful.
[ OK ]
* Mounting network filesystems [ OK ]
您现在可以使用以下命令检查从 iSCSI 目标共享的存储磁盘:
lsblk
您应该会看到存储磁盘现在可供启动器用作 sdb:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 465.8G 0 disk
??sda1 8:1 0 93.1G 0 part /
??sda2 8:2 0 1K 0 part
??sda5 8:5 0 186.3G 0 part /home
??sda6 8:6 0 181.6G 0 part /Data
??sda7 8:7 0 4.8G 0 part [SWAP]
sdb 8:16 0 2G 0 disk
您还可以使用以下命令验证 iSCSI 连接:
tgtadm --mode conn --op show --tid 1
您应该得到以下输出:
Session: 1
Connection: 0
Initiator: iqn.1993-08.org.debian:01:2e1e2383de41
IP Address: 192.168.0.102
接下来,您将需要在此共享设备 (sdb) 上创建一个文件系统并挂载它以使该设备可用。
首先,使用以下命令在共享设备 (sdb) 上创建一个文件系统:
fdisk /dev/sdb
您应该看到以下输出:
Welcome to fdisk (util-linux 2.31.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x06091fe8.
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-4194303, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-4194303, default 4194303):
Created a new partition 1 of type 'Linux' and of size 2 GiB.
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
接下来,使用以下命令格式化此分区:
mkfs.ext4 /dev/sdb1
接下来,使用以下命令将此分区挂载到 /mnt 目录:
mount /dev/sdb1 /mnt
现在,您可以使用以下命令检查挂载的设备:
df -h
您应该看到以下输出:
Filesystem Size Used Avail Use% Mounted on
udev 1.9G 4.0K 1.9G 1% /dev
tmpfs 384M 1.2M 383M 1% /run
/dev/sda1 92G 36G 51G 42% /
none 4.0K 0 4.0K 0% /sys/fs/cgroup
none 5.0M 0 5.0M 0% /run/lock
none 1.9G 54M 1.9G 3% /run/shm
none 100M 48K 100M 1% /run/user
/dev/sda5 184G 96G 79G 55% /home
/dev/sda6 179G 32G 138G 19% /Data
/dev/sdb1 2.0G 3.0M 1.9G 1% /mnt
恭喜!您已成功安装 iSCSI 目标服务器并从 iSCSI 启动器连接它。您现在可以将此共享 iSCSI 设备用作普通附加磁盘。