iscsi共享(磁盘共享)

烈酒焚心 提交于 2020-11-30 20:54:52

前言:

最近整理一些以前的学习笔记。
过去都是存储在本地,此次传到网络留待备用。\


 

iSCSI共享

  • internet scsi 磁盘共享
  • 软件包:
    • 服务端: targetcli(centos7) ; scsi-target-utils(centos6)
    • 客户端: iscsi-initiator-utils
  • 系统服务:
    • 服务端: target
    • 客户端: iscsid
  • 默认端口: 3260
·做iscsi服务需要三个概念:
    ①.backstore(后端存储):后端真正的存储设备(实物);

    ②.target(目标):共享磁盘名(虚拟);
        target共享名的要求:iqn规范
            iqn规范 = iqn.yyyy-mm.主机域名反写:任意字串
                例:iqn.2018-02.com.example:data

    ③.lun(逻辑单元):Logic Unit Number ,绑定、关联存储设备。

 

搭建iscsi共享

一、iscsi服务端:

0.准备后端存储磁盘:

]# lsblk /dev/vdb                                           #后端存储必须是分区、LV或磁盘镜像
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vdb  252:16   0  20G  0 disk

1.安装服务端软件targetcli :

]# yum -y install targetcli

2.启动和设置自启服务:

]# systemctl restart target
]# systemctl enable target

3.配置iscsi

3.0 进入targetcli :

]# targetcli
/> 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]
/> 

3.1 创建backstore后端存储:

/> backstores/block create 后端存储名 /后端存储磁盘路径
/> backstores/block create backend /dev/vdb
Created block storage object backend using /dev/vdb.

3.2 创建target共享名:iqn(在客户端显示的名称)

/> iscsi/ create 共享名(iqn.yyyy-mm.主机域名反写:服务器描述)
/> iscsi/ create iqn.2000-10.com.example:server
Created target iqn.2000-10.com.example:server.
Created TPG 1.
Global pref auto_add_default_portal=true
Created default portal listening on all IPs (0.0.0.0), port 3260.

3.3 创建lun,关联共享名和后端存储设备:

/> iscsi/共享名/tpg1/luns create /backstores/block/后端存储名
/> iscsi/iqn.2000-10.com.example:server/tpg1/luns create /backstores/block/backend 
Created LUN 0.

3.4 创建acl访问口令(知道口令的客户端可以访问共享):

/> iscsi/共享名/tpg1/acls create 客户端iqn名称(iqn.yyyy-mm.客户端主机域名反写:任意字串)
/> iscsi/iqn.2000-10.com.example:server/tpg1/acls create iqn.2000-10.com.example:client
Created Node ACL for iqn.2000-10.com.example:client
Created mapped LUN 0.

3.5 创建门户(portal)的监听ip与端口:

# 默认有一个0.0.0.0:3260的portal
/> iscsi/共享名/tpg1/portals create 服务器ip 端口号(默认3260,可省略)
/> iscsi/iqn.2000-10.com.example:server/tpg1/portals create 192.168.4.11 13260
Created network portal 192.168.4.11:13260.

3.6 配置验证用户名与密码(可省略):

/> ls
o- / ................................................................................... [...]
  o- backstores ........................................................................ [...]
  | o- block ............................................................ [Storage Objects: 1]
  | | o- backend ................................... [/dev/vdb (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.2000-10.com.example:server .............................................. [TPGs: 1]
  |   o- tpg1 ......................................................... [no-gen-acls, no-auth]
  |     o- acls .................................................................... [ACLs: 1]
  |     | o- iqn.2000-10.com.example:client ................................. [Mapped LUNs: 1]
  |     |   o- mapped_lun0 ......................................... [lun0 block/backend (rw)]
  |     o- luns .................................................................... [LUNs: 1]
  |     | o- lun0 .............................. [block/backend (/dev/vdb) (default_tg_pt_gp)]
  |     o- portals .............................................................. [Portals: 2]
  |       o- 0.0.0.0:3260 ............................................................... [OK]
  |       o- 192.168.4.11:13260 ......................................................... [OK]
  o- loopback ................................................................... [Targets: 0]
/> 

3.7 保存并退出:

/> saveconfig
Configuration saved to /etc/target/saveconfig.json
/> exit
Global pref auto_save_on_exit=true
Last 10 configs saved in /etc/target/backup/.
Configuration saved to /etc/target/saveconfig.json

4.配置防火墙

]# firewall-cmd --permanent --add-service=iscsi-target      #开启默认3260端口
]# firewall-cmd --permanent --add-port=13260/tcp            #开启指定端口
]# firewall-cmd --reload                                    #重读防火墙配置

 

二、iscsi客户端:

1.安装客户端软件iscsi-initiator-utils:

]# yum -y install iscsi-initiator-utils

2.启动和设置自启服务:

]# systemctl restart iscsid
]# systemctl enable iscsid

3.配置ISCSI initiator名称:

]# vim /etc/iscsi/initiatorname.iscsi        #iscsi访问服务端口令配置文件
InitiatorName=客户端iqn名称                   #与服务器acl的客户端iqn名称相同
InitiatorName=iqn.2000-10.com.exampl:client
]# systemctl restart iscsid                  #修改配置文件后需重启刷新服务

4.发现服务端ISCSI共享设备:

]# iscsiadm --mode discovery --type sendtargets --portal 服务端ip:端口号(端口可省略,默认3260)
]# iscsiadm --mode discovery --type sendtargets --portal 192.168.4.11
192.168.4.11:3260,1 iqn.2000-10.com.example:server
192.168.4.11:13260,1 iqn.2000-10.com.example:server
或
]# iscsiadm -m discovery -t st -p 服务端ip:端口
]# iscsiadm -m discovery -t st -p 192.168.4.11:13260
192.168.4.11:13260,1 iqn.2000-10.com.example:server
192.168.4.11:13260,1 iqn.2000-10.com.example:server

5.手动加载登入(服务ip上的共享):

]# iscsiadm --mode node --targetname 共享名 --portal 服务端ip:端口号 --login
]# iscsiadm --mode node --targetname iqn.2000-10.com.example:server --portal 192.168.4.11:3260 --login
或
]# iscsiadm -m node -L all

6.自动加载

]# vim /var/lib/iscsi/nodes/iqn名\:服务端名/*/default
    node.conn[0].startup = automatic    #将manual手动改为automatic自动
]# vim /var/lib/iscsi/nodes/iqn.2000-10.com.example\:server/192.168.4.11\,3260\,1/default
    node.conn[0].startup = automatic                #改为自动

7.设置multipath多路径

]# lsblk
NAME                  MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                     8:0    0   20G  0 disk              #这两个是同一网络磁盘
sdb                     8:16   0   20G  0 disk
]# /lib/udev/scsi_id --whitelisted --device=/dev/sda        #获取设备的wwid
360014050e9fae7a04594d99be16e0634
]# /lib/udev/scsi_id --whitelisted --device=/dev/sdb
360014050e9fae7a04594d99be16e0634

]# yum -y install device-mapper-multipath                   #安装多路径软件
]# mpathconf --user_friendly_names n                        #不使用设备自动配置的友好名称
]# vim /etc/multipath.conf                                  #设置多路径设备别名
#末尾添加
multipaths {
    multipath {
        wwid    "360014050e9fae7a04594d99be16e0634"     #设备wwid
        alias   "multipath_iscsi"                       #自定义设备别名
    }
}
]# systemctl start multipathd                               #启动多路径服务

]# lsblk                                                    #确认设备命名
NAME                  MAJ:MIN RM  SIZE RO TYPE  MOUNTPOINT
sda                     8:0    0   20G  0 disk  
└─multipath_iscsi     253:2    0   20G  0 mpath         #自定义设备名
sdb                     8:16   0   20G  0 disk  
└─multipath_iscsi     253:2    0   20G  0 mpath
]# ls -1 /dev/mapper/                                       #查看设备
control
multipath_iscsi

8.本地使用

]# parted /dev/mapper/multipath_iscsi mklabel gpt               #新建分区
]# parted /dev/mapper/multipath_iscsi mkpart primary 1M 100%
]# partprobe /dev/mapper/multipath_iscsi                        #刷新分区表
]# mkfs.ext4 /dev/mapper/multipath_iscsi1                       #格式化
]# mkdir /iscsi                                                 #创建挂载点
]# blkid /dev/mapper/multipath_iscsi1                           #查看uuid
/dev/mapper/multipath_iscsi1: UUID="3eed683f-505a-4be6-b0a9-9621729a8bef" TYPE="ext4" PARTLABEL="primary" PARTUUID="aa1d7978-4012-4dc0-b2cf-6caf714549ce"
]# vim /etc/fstab                                               #配置自动挂载
    UUID="3eed683f-505a-4be6-b0a9-9621729a8bef" /iscsi ext4 _netdev 0 0
]# mount -a                                                     #挂载
]# sync; reboot -f                                              #存盘,再强制重启,避免关机卡死
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!