iscsi服务器的安装和配置
一、原理
iSCSI( Internet Small Computer System Interface 互联网小型计算机系统接口)
支持从客户端通过ip向远程服务器上的存储设备发送命令,默认端口为3260/tcp。
1. iscsi发起端:即为客户端
2. iscsi目标:服务器提供的远程共享磁盘
3. 目标门户:服务器
4. iqn:发起端和目标之间的全局唯一名称标识
二、安装配置
1、服务器端安装配置
1)安装
[root@server ~]# yum install targetcli -y
2)简单配置服务器
先创建一个新的分区`fdisk /dev/vdb`,然后打开服务`systemctl start target`,然后进行配置
/> 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]
/> /backstores/block create lcl:storage1 /dev/vdb1 ##将新建的想要共享的分区创建为一个块存储
Created block storage object lcl:storage1 using /dev/vdb1.
/> /iscsi create iqn.2017-12.com.example:storage1 ##创建共享名称,iqn表示全局唯一名称标识
Created target iqn.2017-12.com.example:storage1.
Created TPG 1.
/> /iscsi/iqn.2017-12.com.example:storage1/tpg1/acls create iqn.2017-12.com.example:lclkey ##创建加密字符串,acl里面的表示允许拥有这个key的ISCSI客户端主机可以连接服务器端
Created Node ACL for iqn.2017-12.com.example:lclkey
/> /iscsi/iqn.2017-12.com.example:storage1/tpg1/luns create /backstores/block/lcl:storage1 ##创建target块设备的逻辑单元
Created LUN 0.
Created LUN 0->0 mapping in node ACL iqn.2017-12.com.example:lclkey
/> /iscsi/iqn.2017-12.com.example:storage1/tpg1/portals create 172.25.254.170 ##创建ip
Using default IP port 3260
Created network portal 172.25.254.170:3260.
/> ls
o- / ..................................................................... [...]
o- backstores .......................................................... [...]
| o- block .............................................. [Storage Objects: 1]
| | o- lcl:storage1 ................ [/dev/vdb1 (5.0GiB) write-thru activated]
| o- fileio ............................................. [Storage Objects: 0]
| o- pscsi .............................................. [Storage Objects: 0]
| o- ramdisk ............................................ [Storage Objects: 0]
o- iscsi ........................................................ [Targets: 1]
| o- iqn.2017-12.com.example:storage1 .............................. [TPGs: 1]
| o- tpg1 ........................................... [no-gen-acls, no-auth]
| o- acls ...................................................... [ACLs: 1]
| | o- iqn.2017-12.com.example:lclkey ................... [Mapped LUNs: 1]
| | o- mapped_lun0 ...................... [lun0 block/lcl:storage1 (rw)]
| o- luns ...................................................... [LUNs: 1]
| | o- lun0 ............................. [block/lcl:storage1 (/dev/vdb1)]
| o- portals ................................................ [Portals: 1]
| o- 172.25.254.170:3260 .......................................... [OK]
o- loopback ..................................................... [Targets: 0]
/> exit
Global pref auto_save_on_exit=true
Last 10 configs saved in /etc/target/backup.
Configuration saved to /etc/target/saveconfig.json
[root@server ~]# firewall-cmd --permanent --add-port=3260/tcp ##从上面的结果可以看出,使用的端口为3260,协议为tcp
success
[root@server ~]# firewall-cmd --reload
success
3、客户端安装配置
1)安装
[root@client nfs]# yum install iscsi-initiator-utils.x86_64 -y
2)挂载测试
[root@client ~]# systemctl start iscsi
[root@client ~]# vim /etc/iscsi/initiatorname.iscsi
InitiatorName=iqn.2017-12.com.example:lclkey ##服务端设置的acls里面的内容,让客户端可以连接服务端
[root@client ~]# systemctl restart iscsi
[root@client ~]# iscsiadm -m discovery -t st -p 172.25.254.170 ##发现170主机上的设备
##-m表示动作 -t表示类型 -p表示ip
172.25.254.170:3260,1 iqn.2017-12.com.example:storage1
[root@client ~]# iscsiadm -m node -T iqn.2017-12.com.example:storage1 -p 172.25.254.170 -l ##添加,-T表示targetname
Logging in to [iface: default, target: iqn.2017-12.com.example:storage1, portal: 172.25.254.170,3260] (multiple)
Login to [iface: default, target: iqn.2017-12.com.example:storage1, portal: 172.25.254.170,3260] successful.
[root@client ~]# fdisk -l ##会发现多出来一个/dev/sda设备,就是服务器的共享设备
……
Disk /dev/sda: 5368 MB, 5368709120 bytes, 10485760 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
[root@client ~]# fdisk /dev/sda ...
[root@client ~]# partprobe
[root@client ~]# mkfs.xfs /dev/sda1
[root@client ~]# mount /dev/sda1 /mnt
/dev/sda1 2086912 33056 2053856 2% /mnt
4、设置自动挂载
[root@client ~]# blkid
/dev/vda1: UUID="9bf6b9f7-92ad-441b-848e-0257cbb883d1" TYPE="xfs"
/dev/vdb1: UUID="SIaPf4-OdHu-OzAW-NlQG-vZ3D-X8ZO-1FK3Ih" TYPE="LVM2_member"
/dev/mapper/vg0-vo: UUID="12294be2-bdad-4817-b162-038e22313d9f" TYPE="ext4"
/dev/sda1: UUID="07c00245-0c05-4da5-88a7-261efb0fec4f" TYPE="xfs"
[root@client ~]# vim /etc/fstab
UUID="07c00245-0c05-4da5-88a7-261efb0fec4f" /mnt xfs defaults,_netdev 0 0 ##UUID为/dev/sda1的UUID,_netdev表示网络优先启动
**重启后第一次启动可能会有一个bug,导致无法启动系统,将虚拟机强制关机后,再次打开,便可以打开。
5、删除客户端共享磁盘
先删除/etc/fstab里面关于共享磁盘的内容,然后输入`umount /mnt`
[root@client ~]# iscsiadm -m node -T iqn.2017-12.com.example:storage1 -p 172.25.254.170 -u ##退出登陆,-u表示logout
Logging out of session [sid: 3, target: iqn.2017-12.com.example:storage1, portal: 172.25.254.170,3260]
Logout of [sid: 3, target: iqn.2017-12.com.example:storage1, portal: 172.25.254.170,3260] successful.
[root@client ~]# iscsiadm -m node -T iqn.2017-12.com.example:storage1 -p 172.25.254.170 -o delete ##删除
重启服务后,用fdisk -l查看,后面没有/dev/sda1这个共享磁盘
6、将共享磁盘设置为lvm格式,可以扩容等。
先新建一个分区,并且进行格式化
fdisk /dev/vdb ... n ... t...8e
partprobe
mkfs.xfs /dev/vdb1
然后创建逻辑卷
[root@localhost ~]# pvcreate /dev/vdb1
Physical volume "/dev/vdb1" successfully created
[root@localhost ~]# vgcreate iscsi_vg /dev/vdb1
Volume group "iscsi_vg" successfully created
[root@localhost ~]# lvcreate -l 255 -n iscsi_lv0 iscsi_vg
Logical volume "iscsi_lv0" created
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
iscsi_lv0 iscsi_vg -wi-a----- 1020.00m
[root@localhost ~]# mkfs.xfs /dev/iscsi_vg/iscsi_lv0
meta-data=/dev/iscsi_vg/iscsi_lv0 isize=256 agcount=4, agsize=65280 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0
data = bsize=4096 blocks=261120, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal log bsize=4096 blocks=853, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
制作共享磁盘
[root@localhost ~]# targetcli
targetcli shell version 2.1.fb34
Copyright 2011-2013 by Datera, Inc and others.
For help on commands, type 'help'.
/> /backstores/block create lichunliang:storage1 /dev/iscsi_vg/iscsi_lv0
Created block storage object lichunliang:storage1 using /dev/iscsi_vg/iscsi_lv0.
/> /iscsi create iqn.2017-12.com.example:storage1
Created target iqn.2017-12.com.example:storage1.
Created TPG 1.
/> /iscsi/iqn.2017-12.com.example:storage1/tpg1/acls create iqn.2017-12.com.example:lichunliangkey
Created Node ACL for iqn.2017-12.com.example:lichunliangkey
/> /iscsi/iqn.2017-12.com.example:storage1/tpg1/luns create /backstores/block/lichunliang:storage1
Created LUN 0.
Created LUN 0->0 mapping in node ACL iqn.2017-12.com.example:lichunliangkey
/> /iscsi/iqn.2017-12.com.example:storage1/tpg1/portals create 172.25.254.170
Using default IP port 3260
Created network portal 172.25.254.170:3260.
添加可以通过防火墙的端口:
[root@localhost ~]# firewall-cmd --permanent --add-port=3260/tcp
success
[root@localhost ~]# firewall-cmd --reload
success
## 在客户端测试
[root@client ~]# vim /etc/iscsi/initiatorname.iscsi
InitiatorName=iqn.2017-12.com.example:lichunliangkey
[root@client ~]# systemctl restart iscsi
[root@client ~]# iscsiadm -m discovery -t st -p 172.25.254.170
[root@client ~]# iscsiadm -m node -T iqn.2017-12.com.example:storage1 -p 172.25.254.170 -l
此时,fdisk -l命令会多出来一个服务器端共享出来的磁盘
服务器端进行扩容
重新添加一个新的磁盘
fdisk /dev/vdb ... 8e
pvcreate /dev/vdb2
vgextend iscsi_vg /dev/vdb2
lvextend -L 1500M /dev/iscsi_vg/iscsi_lv0