iscsi的基本设定
一、概念
SAN:可以通过某些特殊的接口或信道(SCSI或eSATA)来提供局域网内的所有机器进行磁盘访问,SAN提供的是磁盘给主机用,而NAS提供的是网络协议的文件系统(NFS或SMB)
iSCSI主要是通过TCP/IP的技术,将存储设备通过iSCSI target功能,做成可以提供磁盘的服务器端,在通过iSCSI initiator功能,做成能够挂载使用的iSCSI target的客户端,如此便可以通过iSCSI协议来进行磁盘的应用了。
iSCSI target就是存储设备端,存放磁盘或RAID的设备
iSCSI initiator:想要连接iSCSI target服务器,也就是要安装iSCSI initiator的相关功能后才能使用iSCSI target提供的磁盘。
使用场景:
服务端:
[[email protected] ~]# yum install targetcli -y
由于iSCSI要共享的硬件设备,在此要做出来一个分区,供共享使用:
/> /backstores/block create westos:storage1 /dev/vdb1
Created block storage object westos:storage1 using /dev/vdb1.
##将/dev/vdb1做成共享的分区
/> /iscsi create iqn.2017-12.com.example:storage1
Created target iqn.2017-12.com.example:storage1.
Created TPG 1.
##本机域名为server.example.com,反写域名是iSCSI的*的共享定义
/> /iscsi/iqn.2017-12.com.example:storage1/tpg1/acls create iqn.2017-12.com.example:westoskey
Created Node ACL for iqn.2017-12.com.example:westoskey
##构建一把钥匙,只有有这把钥匙的主机才可对此共享的分区进行使用
/> /iscsi/iqn.2017-12.com.example:storage1/tpg1/luns create /backstores/block/westos:storage1
Created LUN 0.
Created LUN 0->0 mapping in node ACL iqn.2017-12.com.example:westoskey
##将这把钥匙与共享的分区进行连接
/> /iscsi/iqn.2017-12.com.example:storage1/tpg1/portals create 172.25.71.1
Using default IP port 3260
Created network portal 172.25.71.1:3260.
##建立端口
添加火墙策略:
[[email protected] ~]# firewall-cmd --permanent --add-port=3260/tcp ##tcp协议的端口
success
[[email protected] ~]# firewall-cmd --reload
success
测试:
客户端
[[email protected] ~]# yum install iscsi-initiator-utils.i686 -y
重启时遇到了这样的报错[[email protected] ~]# systemctl restart iscsid.service
Warning: Unit file of iscsid.service changed on disk, 'systemctl daemon-reload' recommended.
解决方法:
[[email protected] ~]# systemctl daemon-reload ##就是让系统重新加载下,此时即可以重启服务
[[email protected] ~]# systemctl restart iscsid.service
[[email protected] ~]# systemctl restart iscsi
要将服务端设置的钥匙给客户端
[[email protected] ~]# cat /etc/iscsi/initiatorname.iscsi
InitiatorName=iqn.2017-12.com.example:westoskey
重启服务:
[[email protected] ~]# systemctl restart iscsid.service ##此服务是iscsi的上层服务,有时不重启此服务,直接重启
[[email protected] ~]# systemctl restart iscsi iscsi服务,会报错
发现目标:
[[email protected] ~]# iscsiadm -m discovery -t st -p 172.25.71.1 ##发现设备
172.25.71.1:3260,1 iqn.2017-12.com.example:storage1
也可:
[[email protected] ~]# iscsiadm -m discovery -t sendtargets -p 172.25.71.1
172.25.71.1:3260,1 iqn.2017-12.com.example:storage1
登陆
[[email protected] ~]# iscsiadm -m node -T iqn.2017-12.com.example:storage1 -p 172.25.71.1:3260 -l
##(可加ip,也可不加)
测试:
[[email protected] ~]# cat /proc/partitions
major minor #blocks name
253 0 10485760 vda
253 1 10484142 vda1
253 16 10485760 vdb
253 17 512000 vdb1
252 0 507904 dm-0
8 0 2097152 sda ##此为服务端共享的设备
实现客户端开机自动挂载:
先给共享的分区建立文件系统,然后实现自动挂载
在写文件策略时,共享的分区最好写该分区的uid,这样在fstab文件中不会发生可能出现的名字飘逸情况,此问题会导致系统不乏正常启动
[[email protected] ~]# vim /etc/fstab
[[email protected] ~]# mount -a
[[email protected] ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3233544 7240356 31% /
devtmpfs 481120 0 481120 0% /dev
tmpfs 496708 80 496628 1% /dev/shm
tmpfs 496708 13080 483628 3% /run
tmpfs 496708 0 496708 0% /sys/fs/cgroup
/dev/mapper/vg0-vo 483670 2350 451829 1% /home
/dev/sda1 2082816 33056 2049760 2% /mnt
退出:
1、卸载
[[email protected] ~]# umount /mnt/
2、删除文件中的策略
3、退出登陆
[[email protected] ~]# iscsiadm -m node -T iqn.2017-12.com.example:storage1 -p 172.25.71.1:3260 -u
在登陆时使用了端口,所以在退出登陆时,也要使用端口,登陆与退出必须成对出现
4、删除
[[email protected] ~]# iscsiadm -m node -T iqn.2017-12.com.example:storage1 -p 172.25.71.1 -o delete
测试:
使用tree此命令来查看
尽量把此文件中的东西删除,否则在下次登陆时,会出现报错
服务端此分区的删除:
先将targetcli中将所有的策略清空。
/> clearconfig confirm=true
All configuration cleared
可以在服务端将分区建立成lvm模式的,在客户端有需求时,随时可以在服务端进行lvm扩展,以满足客户端的需求。
服务端:
做出一块lvm格式的分区
[[email protected] ~]# pvcreate /dev/vdb1
WARNING: dos signature detected on /dev/vdb1 at offset 510. Wipe it? [y/n] y
Wiping dos signature on /dev/vdb1.
Physical volume "/dev/vdb1" successfully created
[[email protected] ~]# vgcreate iscsi_vg /dev/vdb1
Volume group "iscsi_vg" successfully created
[[email protected] ~]# lvcreate -l 500 -n iscsi_lv iscsi_vg
Logical volume "iscsi_lv" created
在targetcli中加入策略
/> /backstores/block create westos:storage1 /dev/iscsi_vg/iscsi_lv
Created block storage object westos:storage1 using /dev/iscsi_vg/iscsi_lv.
/> /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.westoskey
Created Node ACL for iqn.2017-12.com.example.westoskey
/> /iscsi/iqn.2017-12.com.example:storage1/tpg1/luns create /backstores/block/westos:storage1
Created LUN 0.
Created LUN 0->0 mapping in node ACL iqn.2017-12.com.example.westoskey
/> /iscsi/iqn.2017-12.com.example:storage1/tpg1/portals create 172.25.71.1
Using default IP port 3260
Created network portal 172.25.71.1:3260.
测试:
[[email protected] ~]# iscsiadm -m discovery -t st -p 172.25.71.1
172.25.71.1:3260,1 iqn.2017-12.com.example:storage1
[[email protected] ~]# iscsiadm -m node -T iqn.2017-12.com.example:storage1 -p 172.25.71.1 -l
Logging in to [iface: default, target: iqn.2017-12.com.example:storage1, portal: 172.25.71.1,3260] (multiple)
Login to [iface: default, target: iqn.2017-12.com.example:storage1, portal: 172.25.71.1,3260] successful.
若客户端有更大的需求时,可以直接将服务端的lvm扩大即可,理论讲,可以无限扩大的,但实际上,最大为18eb,因为文件系统的gpt格式下,最大为18eb
扩展lvm
[[email protected] ~]# pvcreate /dev/vdb2
Physical volume "/dev/vdb2" successfully created
[[email protected] ~]# vgextend iscsi_vg /dev/vdb2
Volume group "iscsi_vg" successfully extended
[[email protected] ~]# lvextend -L 4000M /dev/iscsi_vg/iscsi_lv
Extending logical volume iscsi_lv to 3.91 GiB
Logical volume iscsi_lv successfully resized
客户端:
在企业运维中客户端是可以实现自动化扩大的,但在此需要手动扩展
在客户端先退出登陆,删除后重新进行登陆,
[[email protected] ~]# iscsiadm -m node -T iqn.2017-12.com.example:storage1 -p 172.25.71.1 -u
Logging out of session [sid: 8, target: iqn.2017-12.com.example:storage1, portal: 172.25.71.1,3260]
Logout of [sid: 8, target: iqn.2017-12.com.example:storage1, portal: 172.25.71.1,3260] successful.
[[email protected] ~]# iscsiadm -m node -T iqn.2017-12.com.example:storage1 -p 172.25.71.1 -o delete
在此登陆:
查看是否更新了大小