ISCSI服务

关于作者

周李杰,最后一批90后,现居湖北武汉

孤独患者,懒癌中期,无脑幻想者。但却人畜无害,无需远离。

兴趣众多,然无一精通。正努力成为一个有趣的人。

1
2
> print("👆以上是不完整的我") //请我吃大餐将会了解更多
>

[toc]

ISCSI技术概述

硬盘的接口类型

  • IDE是一种成熟稳定、价格便宜的并行传输接口。
  • SATA是一种传输速度更快、数据校验更完整的串行传输接口。
  • SCSI是一种用于计算机和硬盘、光驱等设备之间系统级接口的通用标准,具有系统资源占用率低、转速高、传输速度快等优点。

IBM公司开始研发ISCSI技术

​ 这是一种将SCSI接口与以太网技术相结合的新型存储技术,可以用来在网络中传输SCSI接口的命令和数据。

iscsi = internet + scsi硬盘接口

​ 这样,不仅克服了传统SCSI接口设备的物理局限性,实现了跨区域的存储资源共享,还可以在不停机的状态下扩展存储容量。

配置ISCSI服务端

添加硬盘

首先我们提前将虚拟机加入一块硬盘

开机启动

1
2
3
4
5
6
7
8
9
[root@server222 ~]# 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-root 253:0 0 17G 0 lvm /
└─centos-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk
sr0 11:0 1 4.2G 0 rom /mnt/cdrom

yum安装ISCSI服务端程序

1
2
3
4
[root@server222 ~]# yum install -y targetd targetcli
[root@server222 ~]# systemctl restart targetd
[root@server222 ~]# systemctl enable targetd
Created symlink from /etc/systemd/system/multi-user.target.wants/targetd.service to /usr/lib/systemd/system/targetd.service.

配置ISCSI服务端共享资源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
[root@server222 ~]# 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
/backstores/block> create disk1 /dev/sdb
Created block storage object disk1 using /dev/sdb.
/backstores/block> cd /
/> ls
o- / ..................................................................... [...]
o- backstores .......................................................... [...]
| o- block .............................................. [Storage Objects: 1]
| | o- disk1 ..................... [/dev/sdb (20.0GiB) write-thru deactivated]
| | 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: 0]
o- loopback ..................................................... [Targets: 0]
/>

创建ISCSI target名称以及配置共享资源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/> cd iscsi
/iscsi> .
/iscsi> create
Created target iqn.2003-01.org.linux-iscsi.server222.x8664:sn.c0e1179441ab.
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 iqn.2003-01.org.linux-iscsi.server222.x8664:sn.c0e1179441ab/
/iscsi/iqn.20....c0e1179441ab> ls
o- iqn.2003-01.org.linux-iscsi.server222.x8664:sn.c0e1179441ab ....... [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....c0e1179441ab> cd tpg1/luns
/iscsi/iqn.20...1ab/tpg1/luns> create /backstores/block/disk1
Created LUN 0.

设置访问控制列表(ACL)

1
2
3
4
5
6
7
8
9
10
11
12
/iscsi/iqn.20...1ab/tpg1/luns> cd ..
/iscsi/iqn.20...179441ab/tpg1> ls
o- tpg1 ................................................. [no-gen-acls, no-auth]
o- acls ............................................................ [ACLs: 0]
o- luns ............................................................ [LUNs: 1]
| o- lun0 ........................ [block/disk1 (/dev/sdb) (default_tg_pt_gp)]
o- portals ...................................................... [Portals: 1]
o- 0.0.0.0:3260 ....................................................... [OK]
/iscsi/iqn.20...179441ab/tpg1> cd acls
/iscsi/iqn.20...1ab/tpg1/acls> create iqn.2003-01.org.linux-iscsi.server222.x8664:sn.c0e1179441ab:client
Created Node ACL for iqn.2003-01.org.linux-iscsi.server222.x8664:sn.c0e1179441ab:client
Created mapped LUN 0.

设置ISCSI服务端的监听IP地址和端口号

1
2
3
4
5
6
7
8
9
/iscsi/iqn.20...1ab/tpg1/acls> cd ..
/iscsi/iqn.20...179441ab/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.176.222
Using default IP port 3260
Created network portal 192.168.176.222:3260.
/iscsi/iqn.20.../tpg1/portals>

此处如果报错 请执行下面一步骤

1
2
3
4
5
6
7
> /iscsi/iqn.20.../tpg1/portals> create 192.168.176.222
> Using default IP port 3260
> Could not create NetworkPortal in configFS
>
> /iscsi/iqn.20.../tpg1/portals> delete 0.0.0.0 3260
> Deleted network portal 0.0.0.0:3260
>

检查配置信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
iscsi/iqn.20.../tpg1/portals> ls /
o- / ..................................................................... [...]
o- backstores .......................................................... [...]
| o- block .............................................. [Storage Objects: 1]
| | o- disk1 ....................... [/dev/sdb (20.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.2003-01.org.linux-iscsi.server222.x8664:sn.c0e1179441ab ... [TPGs: 1]
| o- tpg1 ........................................... [no-gen-acls, no-auth]
| o- acls ...................................................... [ACLs: 1]
| | o- iqn.2003-01.org.linux-iscsi.server222.x8664:sn.c0e1179441ab:client [Mapped LUNs: 1]
| | o- mapped_lun0 ............................. [lun0 block/disk1 (rw)]
| o- luns ...................................................... [LUNs: 1]
| | o- lun0 .................. [block/disk1 (/dev/sdb) (default_tg_pt_gp)]
| o- portals ................................................ [Portals: 1]
| o- 192.168.176.222:3260 ......................................... [OK]
o- loopback ..................................................... [Targets: 0]
/iscsi/iqn.20.../tpg1/portals> exit
Global pref auto_save_on_exit=true
Last 10 configs saved in /etc/target/backup.
Configuration saved to /etc/target/saveconfig.json

配置防火墙以及重启服务

1
2
3
4
5
6
7
8
[root@server222 ~]# systemctl restart targetd
[root@server222 ~]# systemctl enable targetd

[root@server222 ~]# systemctl stop firewalld
[root@server222 ~]# vim /etc/selinux/config
[root@server222 ~]# setenforce 0
[root@server222 ~]# getenforce
Permissive

配置ISCSI客户端

客户端地址:192.168.176.111

安装ISCSI客户端服务程序initiator

在rhel 7 系统中,ISCSI-initator-utils已经被默认安装了

1
[root@client111 ~]# yum install iscsi-initiator-utils

编辑initiatorname配置文件

1
2
3
4
5
6
7
[root@client111 ~]# vim /etc/iscsi/initiatorname.iscsi
[root@client111 ~]# cat /etc/iscsi/initiatorname.iscsi
InitiatorName=iqn.2003-01.org.linux-iscsi.server222.x8664:sn.c0e1179441ab:client

[root@client111 ~]# systemctl restart iscsid
[root@client111 ~]# systemctl enable iscsid
Created symlink from /etc/systemd/system/multi-user.target.wants/iscsid.service to /usr/lib/systemd/system/iscsid.service.

登录iSCSI服务端

iscsiadm是用于管理、查询、插入、更新或删除iSCSI数据库配置文件的命令行工具,用户需要先使用这个工具扫描发现远程iSCSI服务端,然后查看找到的服务端上有哪些可用的共享存储资源。

-m discovery参数的目的是扫描并发现可用的存储资源,

-t st参数为执行扫描操作的类型,

-p 192.168.176.222 参数为iSCSI服务端的IP地址

1
2
[root@client111 ~]# iscsiadm -m discovery -t st -p 192.168.176.222
192.168.176.222:3260,1 iqn.2003-01.org.linux-iscsi.server222.x8664:sn.c0e1179441ab

-m node参数为将客户端所在主机作为一台节点服务器,

-T iqn.2003-01.org.linux-iscsi.linuxprobe.x8664:sn.d497c356ad80参数为要使用的存储资源 (直接复制前面命令中扫描发现的结果,以免错误)

-p 192.168.176.222参数依然为服务端iSCSI服务端的IP地址。

–login或-l参数进行登录验证。

1
2
3
[root@client111 ~]# iscsiadm -m node -T iqn.2003-01.org.linux-iscsi.server222.x8664:sn.c0e1179441ab -p 192.168.176.222 --login
Logging in to [iface: default, target: iqn.2003-01.org.linux-iscsi.server222.x8664:sn.c0e1179441ab, portal: 192.168.176.222,3260] (multiple)
Login to [iface: default, target: iqn.2003-01.org.linux-iscsi.server222.x8664:sn.c0e1179441ab, portal: 192.168.176.222,3260] successful.

此时我们lsbak看一下

1
2
3
4
5
6
7
8
9
[root@client111 ~]# 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-root 253:0 0 17G 0 lvm /
└─centos-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk
sr0 11:0 1 4.2G 0 rom /mnt/cdrom

发现此处多了一个/dev/sdb的设备文件

1
2
[root@client111 ~]# file /dev/sdb
/dev/sdb: block special

挂载设备

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[root@client111 ~]# file /dev/sdb
/dev/sdb: block special
[root@client111 ~]# mkfs.xfs /dev/sdb
meta-data=/dev/sdb isize=512 agcount=4, agsize=1310720 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=5242880, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@client111 ~]# mkdir /iscsi
[root@client111 ~]# mount /dev/sdb /iscsi/
[root@client111 ~]# df -Th
文件系统 类型 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root xfs 17G 4.4G 13G 26% /
devtmpfs devtmpfs 977M 0 977M 0% /dev
tmpfs tmpfs 993M 0 993M 0% /dev/shm
tmpfs tmpfs 993M 9.0M 984M 1% /run
tmpfs tmpfs 993M 0 993M 0% /sys/fs/cgroup
/dev/sr0 iso9660 4.3G 4.3G 0 100% /mnt/cdrom
/dev/sda1 xfs 1014M 161M 854M 16% /boot
tmpfs tmpfs 199M 12K 199M 1% /run/user/42
tmpfs tmpfs 199M 0 199M 0% /run/user/0
/dev/sdb xfs 20G 33M 20G 1% /iscsi

永久挂载

1
2
3
4
5
6
[root@client111 ~]# blkid | grep /dev/sdb
/dev/sdb: UUID="cc4c5965-a701-4534-85ad-60df267cf5d7" TYPE="xfs"
[root@client111 ~]# vim /etc/fstab
[root@client111 ~]# tail -n 1 /etc/fstab
UUID="cc4c5965-a701-4534-85ad-60df267cf5d7" /iscsi xfs defaults,_netdev 0 0
[root@client111 ~]# mount -a

卸载ISCSI共享资源设备

可以用iscsiadm命令的-u参数将其设备卸载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@client111 ~]# umount /iscsi/
[root@client111 ~]# df -Th
文件系统 类型 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root xfs 17G 4.4G 13G 26% /
devtmpfs devtmpfs 977M 0 977M 0% /dev
tmpfs tmpfs 993M 0 993M 0% /dev/shm
tmpfs tmpfs 993M 9.0M 984M 1% /run
tmpfs tmpfs 993M 0 993M 0% /sys/fs/cgroup
/dev/sr0 iso9660 4.3G 4.3G 0 100% /mnt/cdrom
/dev/sda1 xfs 1014M 161M 854M 16% /boot
tmpfs tmpfs 199M 12K 199M 1% /run/user/42
tmpfs tmpfs 199M 0 199M 0% /run/user/0
[root@client111 ~]# iscsiadm -m node -T iqn.2003-01.org.linux-iscsi.server222.x8664:sn.c0e1179441ab -u
Logging out of session [sid: 1, target: iqn.2003-01.org.linux-iscsi.server222.x8664:sn.c0e1179441ab, portal: 192.168.176.222,3260]
Logout of [sid: 1, target: iqn.2003-01.org.linux-iscsi.server222.x8664:sn.c0e1179441ab, portal: 192.168.176.222,3260] successful.

练习

1.简述iSCSI存储技术在生产环境中的作用。

2.在Linux系统中,iSCSI服务端和iSCSI客户端所使用的服务程序分别叫什么?

3.在使用targetcli命令配置iSCSI服务端配置文件时,acls与portals参数目录中分别存放什么内容?

4.iSCSI协议占用了服务器哪个协议和端口号?

5.用户在填写fstab设备挂载配置文件时,一般会把远程存储资源的UUID(而非设备的名称)填写到配置文件中。这是为什么?

打赏
  • © 2019-2020 Li Jie
  • Powered by Hexo Theme Ayer
    • PV:
    • UV:

喜欢就打赏吧~

支付宝
微信