一、概念
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提供的磁盤。
使用場景:
服務端:
[root@server ~]# 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.
##建立端口
添加火牆策略:
[root@server ~]# firewall-cmd --permanent --add-port=3260/tcp ##tcp協議的端口
success
[root@server ~]# firewall-cmd --reload
success
測試:
客戶端
[root@client ~]# yum install iscsi-initiator-utils.i686 -y
重啓時遇到了這樣的報錯[root@client ~]# systemctl restart iscsid.service
Warning: Unit file of iscsid.service changed on disk, 'systemctl daemon-reload' recommended.
解決方法:
[root@client ~]# systemctl daemon-reload ##就是讓系統重新加載下,此時即可以重啓服務
[root@client ~]# systemctl restart iscsid.service
[root@client ~]# systemctl restart iscsi
要將服務端設置的鑰匙給客戶端
[root@client ~]# cat /etc/iscsi/initiatorname.iscsi
InitiatorName=iqn.2017-12.com.example:westoskey
重啓服務:
[root@client ~]# systemctl restart iscsid.service ##此服務是iscsi的上層服務,有時不重啓此服務,直接重啓
[root@client ~]# systemctl restart iscsi iscsi服務,會報錯
發現目標:
[root@client ~]# iscsiadm -m discovery -t st -p 172.25.71.1 ##發現設備
172.25.71.1:3260,1 iqn.2017-12.com.example:storage1
也可:
[root@client ~]# iscsiadm -m discovery -t sendtargets -p 172.25.71.1
172.25.71.1:3260,1 iqn.2017-12.com.example:storage1
登陸
[root@client ~]# iscsiadm -m node -T iqn.2017-12.com.example:storage1 -p 172.25.71.1:3260 -l
##(可加ip,也可不加)
測試:
[root@client ~]# 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文件中不會發生可能出現的名字飄逸情況,此問題會導致系統不乏正常啓動
[root@client ~]# vim /etc/fstab
[root@client ~]# mount -a
[root@client ~]# 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、卸載
[root@client ~]# umount /mnt/
2、刪除文件中的策略
3、退出登陸
[root@client ~]# iscsiadm -m node -T iqn.2017-12.com.example:storage1 -p 172.25.71.1:3260 -u
在登陸時使用了端口,所以在退出登陸時,也要使用端口,登陸與退出必須成對出現
4、刪除
[root@client ~]# 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格式的分區
[root@server ~]# 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
[root@server ~]# vgcreate iscsi_vg /dev/vdb1
Volume group "iscsi_vg" successfully created
[root@server ~]# 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.
測試:
[root@client ~]# iscsiadm -m discovery -t st -p 172.25.71.1
172.25.71.1:3260,1 iqn.2017-12.com.example:storage1
[root@client ~]# 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
[root@server ~]# pvcreate /dev/vdb2
Physical volume "/dev/vdb2" successfully created
[root@server ~]# vgextend iscsi_vg /dev/vdb2
Volume group "iscsi_vg" successfully extended
[root@server ~]# lvextend -L 4000M /dev/iscsi_vg/iscsi_lv
Extending logical volume iscsi_lv to 3.91 GiB
Logical volume iscsi_lv successfully resized
客戶端:
在企業運維中客戶端是可以實現自動化擴大的,但在此需要手動擴展
在客戶端先退出登陸,刪除後重新進行登陸,
[root@client ~]# 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.
[root@client ~]# iscsiadm -m node -T iqn.2017-12.com.example:storage1 -p 172.25.71.1 -o delete
在此登陸:
查看是否更新了大小