centos7配置iscsi

时间:2022-10-06 17:27:39

什么是ISCSI

iscsi--internet small computer system interface互联小型计算机系统接口,将数据包封装在TCP/IP协议中传输,使用普通网线和网络设备即可实现,相对于FC-SAN存储来说更廉价。

ISCSI系统结构

iscsi一般分为服务器端和客户端,服务器端负责管理本地磁盘或磁盘陈列,客户端则是用来发现并连接服务器端获得访问iscsi共享盘。

centos7中服务端和客户端所需要的安装包

centos7 软件包
服务端 targetcli, targetd,scsi-target-utils
客户端 iscsi-initiator-utils

实验环境

角色 主机名 IP
服务端 master 192.168.138.200
客户端 slave 192.168.138.201

服务器端配置

  • 准备一块磁盘并分好区,先不用格式化
  • 运行targetcli命令,给要要共享的磁盘定义名字
# targetcli
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 slave /dev/sdb
Created block storage object slave using /dev/sdb.
  • 创建iqn

    命名在同一子网内确保是唯一的,命名格式为:iqn.yyyy-mm.<主机名反写>:自定义名称
/backstores/block> cd /iscsi
/iscsi> create iqn.2018-02.com.frank.master:slave
Created target iqn.2018-02.com.frank.master:slave.
Created TPG 1.
Default portal not created, TPGs within a target cannot share ip:port.
  • 创建acl允许ISCSI客户机连接
/iscsi> cd iqn.2018-02.com.frank.master:slave/tpg1/acls
/iscsi/iqn.20...ave/tpg1/acls> create iqn.2018-02.com.frank.master:slave
Created Node ACL for iqn.2018-02.com.frank.master:slave
/iscsi/iqn.20...ave/tpg1/acls> ls
o- acls .................................................... [ACLs: 1]
o- iqn.2018-02.com.frank.master:slave ............. [Mapped LUNs: 0]
  • 创建lun(target块设备的逻辑单元) 把块设备和iqn绑定
/iscsi> cd iqn.2018-02.com.frank.master:slave/tpg1/luns
/iscsi/iqn.20...ave/tpg1/luns> create /backstores/block/slave
Created LUN 0.
Created LUN 0->0 mapping in node ACL iqn.2018-02.com.frank.master:slave
/iscsi/iqn.20...ave/tpg1/luns> ls
o- luns .................................................... [LUNs: 1]
o- lun0 ................ [block/slave (/dev/sdb) (default_tg_pt_gp)]
  • 设置监听地址和端口
/iscsi/iqn.20.../tpg1/portals> create 192.168.138.200 ip_port=3260
Using default IP port 3260
Created network portal 192.168.138.200:3260.

这里要注意的是,使用ls查看一下是不是已经有0.0.0.0 3260这个条目,如果有删除掉再创建,不然会报错,还有一点的是如果 tgtd服务正在开启的情况下也无法创建,必须先停掉tgtd这个服务。

  • 设置登录验证
/iscsi/iqn.20....master:slave> set auth userid=test
Parameter userid is now 'test'.
/iscsi/iqn.20....master:slave> set auth password=test
Parameter password is now 'test'.

到这里服务器的配置就算完成了,最后看一下配置完成的状态

/> ls
o- / ........................................................................... [...]
o- backstores ................................................................ [...]
| o- block .................................................... [Storage Objects: 1]
| | o- slave ............................. [/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.2018-02.com.frank.master:slave .................................. [TPGs: 1]
| o- tpg1 ................................................. [no-gen-acls, no-auth]
| o- acls ............................................................ [ACLs: 1]
| | o- iqn.2018-02.com.frank.master:slave ..................... [Mapped LUNs: 1]
| | o- mapped_lun0 ................................... [lun0 block/slave (rw)]
| o- luns ............................................................ [LUNs: 1]
| | o- lun0 ........................ [block/slave (/dev/sdb) (default_tg_pt_gp)]
| o- portals ...................................................... [Portals: 1]
| o- 192.168.138.200:3260 ............................................... [OK]
o- loopback ........................................................... [Targets: 0]
/>
  • 启动服务端
# systemctl start tgtd.service

客户端配置

  • 发现设备
# iscsiadm -m discovery -t sendtargets -p 192.168.138.200
iscsiadm: This command will remove the record [iface: default,
target: iqn.2018-04.com.frank.www:iscsi1, portal: 192.168.138.200,3260],
but a session is using it. Logout session then rerun command to remove record.
192.168.138.200:3260,1 iqn.2018-02.com.frank.master:slave
  • 连接设备
# iscsiadm -m node -T iqn.2018-02.com.frank.master:slave -p 192.168.138.200 --login
Logging in to [iface: default, target: iqn.2018-02.com.frank.master:slave,
portal: 192.168.138.200,3260] (multiple)
Login to [iface: default, target: iqn.2018-02.com.frank.master:slave,
portal: 192.168.138.200,3260] successful.
  • 查看设备
fdsik -l

WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.

Disk /dev/sdc: 21.5 GB, 21474836480 bytes, 41943040 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
Disk label type: gpt
# Start End Size Type Name # fdisk /dev/sdc Command (m for help): n
Partition number (1-128, default 1):
First sector (34-41943006, default 8192):
Last sector, +sectors or +size{K,M,G,T,P} (8192-41943006, default 41943006): +10G
Created partition 1 Command (m for help): n
Partition number (2-128, default 2):
First sector (34-41943006, default 20979712):
Last sector, +sectors or +size{K,M,G,T,P} (20979712-41943006, default 41943006):
Created partition 2 Command (m for help): w
The partition table has been altered! Calling ioctl() to re-read partition table.
Syncing disks.
[root@slave ~]# fdisk /dev/sdc
WARNING: fdisk GPT support is currently new, and therefore in an
experimental phase. Use at your own discretion.
Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them.
Be careful before using the write command. Command (m for help): p Disk /dev/sdc: 21.5 GB, 21474836480 bytes, 41943040 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
Disk label type: gpt # Start End Size Type Name
1 8192 20979711 10G Linux filesyste
2 20979712 41943006 10G Linux filesyste
  • 格式化分区并挂载
# mkfs.xfs /dev/sdc1
meta-data=/dev/sdc1 isize=512 agcount=4, agsize=610351 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=2441402, 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@slave ~]# mkfs.xfs /dev/sdc2
meta-data=/dev/sdc2 isize=512 agcount=4, agsize=610291 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=2441162, 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
# mount /dev/sdc1 /iscsi/
# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/cl-root xfs 37G 11G 27G 28% /
devtmpfs devtmpfs 712M 0 712M 0% /dev
tmpfs tmpfs 723M 12K 723M 1% /dev/shm
tmpfs tmpfs 723M 72M 651M 10% /run
tmpfs tmpfs 723M 0 723M 0% /sys/fs/cgroup
/dev/sda1 xfs 1014M 184M 831M 19% /boot
tmpfs tmpfs 145M 0 145M 0% /run/user/0
/dev/sdc1 xfs 9.4G 33M 9.3G 1% /iscsi