基于linux下的iSCSI远程块存储

系统存储分离目的是实现紧急情况下的数据恢复。

iSCSI概念:

SCSI(Internet SCSI)支持从客户端(发起端)
通过IP向远程服务器上的iSCSI存储设备(目标)
发送SCSI命令。iSCSI限定名称用于确定发起端
和目标,并采用 iqn.yyyy-mm.{reverse domain}:label
的格式默认情况下,网络通信是至iSCSI目标上的端口3260/tcp的明文端口。

共享设备:

在服务端:

[root@server ~]# yum install targetcli -y 安装策略共享编辑器

这里写图片描述

[root@server ~]# fdisk /dev/vdb  建立一个2G的分区
[root@server ~]# partprobe  同步分区
[root@server ~]# systemctl start target  开启服务

这里写图片描述

[root@server ~]# targetcli  
Warning: Could not load preferences file /root/.targetcli/prefs.bin.
targetcli shell version 2.1.fb34
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]
/> /backstores/block create westos:storage1 /dev/vdb1创建共享的磁盘
Created block storage object westos:storage1 using /dev/vdb1.
/> /iscsi create iqn.2018-06.com.example:storage1 iqn域名反写,创建共享主机
Created target iqn.2018-06.com.example:storage1.
Created TPG 1.

这里写图片描述

创建密钥:
/> /iscsi/iqn.2018-06.com.example:storage1/tpg1/acls create iqn.2018-06.com.example:westoskey 设置钥匙
Created Node ACL for iqn.2018-06.com.example:westoskey
/> /iscsi/iqn.2018-06.com.example:storage1/tpg1/luns create /backstores/block/westos:storage1 关联设备
Created LUN 0.
Created LUN 0->0 mapping in node ACL iqn.2018-06.com.example:westoskey
/> /iscsi/iqn.2018-06.com.example:storage1/tpg1/portals create 172.25.254.221开端口
Using default IP port 3260
Created network portal 172.25.254.221:3260.
/> 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 ~]# systemctl stop firewalld  关闭防火墙
[root@server ~]# fdisk -l  查看硬件信息
   Device Boot      Start         End      Blocks   Id  System  建立的分区
/dev/vdb1         4194304     8388607     2097152   83  Linux

这里写图片描述

在客户端:

[root@client ~]# yum search iscsi  查找安装包
Loaded plugins: langpacks
================== N/S matched: iscsi ==================
iscsi-initiator-utils.i686 : iSCSI daemon and utility
                           : programs
iscsi-initiator-utils.x86_64 : iSCSI daemon and utility
                             : programs
iscsi-initiator-utils-iscsiuio.x86_64 : Userspace
     ...: configuration daemon required for some iSCSI
     ...: hardware
libiscsi.i686 : iSCSI client library
libiscsi.x86_64 : iSCSI client library

  Name and summary matches only, use "search all" for everything.
[root@client ~]# yum install iscsi-initiator-utils.x86_64  安装软件
Loaded plugins: langpacks
Package iscsi-initiator-utils-6.2.0.873-21.el7.x86_64 already installed and latest version
Nothing to do

这里写图片描述

[root@client ~]# iscsiadm -m discovery -t st -p 172.25.254.221   -m命令执行发生的动作,-t表示发现网络类型,客户端寻找服务器提供的密钥
172.25.254.221:3260,1 iqn.2018-06.com.example:storage1
[root@client ~]# iscsiadm -m node -T iqn.2018-06.com.example:storage1 -p 172.25.254.221 -l  ,-T网络名称,-p主机IP,-l登录,登录端口
Logging in to [iface: default, target: iqn.2018-06.com.example:storage1, portal: 172.25.254.221,3260] (multiple)
iscsiadm: Could not login to [iface: default, target: iqn.2018-06.com.example:storage1, portal: 172.25.254.221,3260].
iscsiadm: initiator reported error (24 - iSCSI login failed due to authorization failure)报错是本地认证与服务端钥匙不匹配
iscsiadm: Could not log into all portals
[root@client ~]# cd /etc/iscsi/

这里写图片描述

[root@client iscsi]# vim initiatorname.iscsi  编辑密钥文件
InitiatorName=iqn.2018-06.com.example:westoskey  更改为服务器提供的密钥

这里写图片描述

[root@client iscsi]# systemctl restart iscsid.service  重启服务
[root@client iscsi]# iscsiadm -m discovery -t st -p 172.25.254.221 客户端寻找找服务器提供的密钥
172.25.254.221:3260,1 iqn.2018-06.com.example:storage1
[root@client iscsi]# iscsiadm -m node -T iqn.2018-06.com.example:storage1 -p 172.25.254.221 -l   登录端口
Logging in to [iface: default, target: iqn.2018-06.com.example:storage1, portal: 172.25.254.221,3260] (multiple)
Login to [iface: default, target: iqn.2018-06.com.example:storage1, portal: 172.25.254.221,3260] successful.

这里写图片描述

[root@client iscsi]# fdisk -l   查看硬件信息出来服务端建立的分区
Disk /dev/sda: 2147 MB, 2147483648 bytes, 4194304 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 iscsi]# fdisk /dev/sda 建立一个2G分区
[root@client iscsi]# partprobe  同步分区

这里写图片描述

[root@client iscsi]# mkfs.xfs /dev/sda1 格式化
meta-data=/dev/sda1              isize=256    agcount=8, agsize=65408 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=523264, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
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@client iscsi]# mount /dev/sda1 /mnt/  挂载
[root@client iscsi]# df  挂载成功
Filesystem         1K-blocks    Used Available Use% Mounted on
/dev/vda1           10473900 3187380   7286520  31% /
devtmpfs              469344       0    469344   0% /dev
tmpfs                 484932      80    484852   1% /dev/shm
tmpfs                 484932   12788    472144   3% /run
tmpfs                 484932       0    484932   0% /sys/fs/cgroup
/dev/mapper/vg0-vo    483670    2355    451824   1% /home
/dev/sda1            2082816   33056   2049760   2% /mnt

这里写图片描述

[root@client iscsi]# vim /etc/fstab 编辑文件开启自启动
/dev/sda1      /mnt   xfs    defaults,_netdev   0   0 加入,_netdev目的是先启动网络服务在挂载设备。

这里写图片描述

[root@client iscsi]# reboot
Connection to 172.25.254.121 closed by remote host.
Connection to 172.25.254.121 closed. 
[kiosk@foundation21 Desktop]$ rht-vmctl poweroff desktop 第一次一般起不来需要强行关闭
Powering off desktop..

这里写图片描述

[kiosk@foundation21 Desktop]$ rht-vmctl start desktop  再次启动虚拟机
Starting desktop.
[kiosk@foundation21 Desktop]$ rht-vmctl view desktop  显示虚拟机,正常登陆
[kiosk@foundation21 Desktop]$ ssh root@172.25.254.121
root@172.25.254.121's password: 
Last login: Sun Jun  3 01:45:41 2018

这里写图片描述

[root@client ~]# df  查看挂载
Filesystem         1K-blocks    Used Available Use% Mounted on
/dev/vda1           10473900 3186724   7287176  31% /
devtmpfs              469344       0    469344   0% /dev
tmpfs                 484932      80    484852   1% /dev/shm
tmpfs                 484932   12776    472156   3% /run
tmpfs                 484932       0    484932   0% /sys/fs/cgroup
/dev/mapper/vg0-vo    483670    2355    451824   1% /home
/dev/sda1            2082816   33056   2049760   2% /mnt

这里写图片描述

设备的删除:

[root@client ~]# yum install tree -y  安装树形显示工具

这里写图片描述

[root@client ~]# tree /var/lib/iscsi/  显示iscsi的信息
/var/lib/iscsi/
├── ifaces
├── isns
├── nodes
│   └── iqn.2018-06.com.example:storage1
│       └── 172.25.254.221,3260,1
│           └── default
├── send_targets
│   └── 172.25.254.221,3260
│       ├── iqn.2018-06.com.example:storage1,172.25.254.221,3260,1,default -> /var/lib/iscsi/nodes/iqn.2018-06.com.example:storage1/172.25.254.221,3260,1
│       └── st_config
├── slp
└── static

10 directories, 2 files

这里写图片描述

[root@client ~]# df  查看挂载
Filesystem         1K-blocks    Used Available Use% Mounted on
/dev/vda1           10473900 3188508   7285392  31% /
devtmpfs              469344       0    469344   0% /dev
tmpfs                 484932      80    484852   1% /dev/shm
tmpfs                 484932   12776    472156   3% /run
tmpfs                 484932       0    484932   0% /sys/fs/cgroup
/dev/mapper/vg0-vo    483670    2355    451824   1% /home
/dev/sda1            2082816   33056   2049760   2% /mnt
[root@client ~]# umount /mnt/  解除挂载
[root@client ~]# vim /etc/fstab 删除开机自启动行

这里写图片描述

[root@client ~]# iscsiadm -m node -T iqn.2018-06.com.example:storage1 -p 172.25.254.221 -u  登出
Logging out of session [sid: 1, target: iqn.2018-06.com.example:storage1, portal: 172.25.254.221,3260]
Logout of [sid: 1, target: iqn.2018-06.com.example:storage1, portal: 172.25.254.221,3260] successful.
[root@client ~]# tree /var/lib/iscsi/  查看iscsi信息还在
/var/lib/iscsi/
├── ifaces
├── isns
├── nodes
│   └── iqn.2018-06.com.example:storage1
│       └── 172.25.254.221,3260,1
│           └── default
├── send_targets
│   └── 172.25.254.221,3260
│       ├── iqn.2018-06.com.example:storage1,172.25.254.221,3260,1,default -> /var/lib/iscsi/nodes/iqn.2018-06.com.example:storage1/172.25.254.221,3260,1
│       └── st_config
├── slp
└── static

10 directories, 2 files

这里写图片描述

[root@client ~]# fdisk -l  看不到硬件信息

Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x00013f3e

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    20970332    10484142+  83  Linux

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x09ee18d7

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048     1026047      512000   8e  Linux LVM

Disk /dev/mapper/vg0-vo: 520 MB, 520093696 bytes, 1015808 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

这里写图片描述

[root@client ~]# systemctl restart iscsi  重启服务之后
[root@client ~]# fdisk -l  又会从文件中加载出来

Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x00013f3e

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    20970332    10484142+  83  Linux

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x09ee18d7

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048     1026047      512000   8e  Linux LVM

Disk /dev/mapper/vg0-vo: 520 MB, 520093696 bytes, 1015808 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/sda: 2147 MB, 2147483648 bytes, 4194304 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: dos
Disk identifier: 0xd38703a8

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            8192     4194303     2093056   83  Linux

这里写图片描述

[root@client ~]# iscsiadm -m node -T iqn.2018-06.com.example:storage1 -p 172.25.254.221 -u登出
Logging out of session [sid: 2, target: iqn.2018-06.com.example:storage1, portal: 172.25.254.221,3260]
Logout of [sid: 2, target: iqn.2018-06.com.example:storage1, portal: 172.25.254.221,3260] successful.
[root@client ~]# iscsiadm -m node -T iqn.2018-06.com.example:storage1 -p 172.25.254.221 -o delete 删除文件
[root@client ~]# tree /var/lib/iscsi/ 树形信息消失
/var/lib/iscsi/
├── ifaces
├── isns
├── nodes
├── send_targets
│   └── 172.25.254.221,3260
│       └── st_config
├── slp
└── static

7 directories, 1 file

这里写图片描述

[root@client ~]# fdisk -l  硬件信息显示设备删除

Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x00013f3e

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    20970332    10484142+  83  Linux

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x09ee18d7

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048     1026047      512000   8e  Linux LVM

Disk /dev/mapper/vg0-vo: 520 MB, 520093696 bytes, 1015808 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

这里写图片描述

[root@client ~]# systemctl restart iscsi 重启服务
[root@client ~]# fdisk -l 硬件信息显示设备已经删除

Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x00013f3e

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    20970332    10484142+  83  Linux

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x09ee18d7

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048     1026047      512000   8e  Linux LVM

Disk /dev/mapper/vg0-vo: 520 MB, 520093696 bytes, 1015808 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

这里写图片描述

猜你喜欢

转载自blog.csdn.net/aaaaaab_/article/details/80572701