一、搭建iscsi存储服务

安装target

[root@localhost wuqiong]# yum install -y scsi-target-utils
[root@localhost wuqiong]# ls /etc/tgt/targets.conf
/etc/tgt/targets.conf
[root@localhost wuqiong]# /etc/init.d/tgtd start
[root@localhost wuqiong]# systemctl start tgtd.service 
[root@localhost wuqiong]# systemctl status tgtd.service 
[root@localhost wuqiong]# systemctl restart tgtd.service

 新建存储分区: sda4

[root@localhost wuqiong]# fdisk -l 
 Disk /dev/sda: 107.4 GB, 107374182400bytes
 255heads, 63sectors/track, 13054cylinders
 Units =cylinders of 16065* 512=8225280bytes
 Sector size (logical/physical): 512bytes / 512bytes
 I/O size (minimum/optimal): 512bytes / 512bytes
 Disk identifier: 0x000b076e
 
   Device Boot     Start         End     Blocks   Id System
 /dev/sda1   *           1         26     204800  83Linux
 Partition 1does not end on cylinder boundary.
 /dev/sda2              26        287    2097152  82Linux swap / Solaris
[root@localhost wuqiong]# fdisk /dev/sda
 
 WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').
 
 Command (m forhelp): p
 
 Disk /dev/sda: 107.4 GB, 107374182400bytes
 255heads, 63sectors/track, 13054cylinders
 Units =cylinders of 16065* 512=8225280bytes
 Sector size (logical/physical): 512bytes / 512bytes
 I/O size (minimum/optimal): 512bytes / 512bytes
 Disk identifier: 0x000b076e
 
   Device Boot     Start         End     Blocks   Id System
 /dev/sda1   *           1         26     204800  83Linux
 Partition 1does not end on cylinder boundary.
 /dev/sda3             287      13055  102554624  83Linux
 
 Command (m forhelp): n
 Command action
   e   extended
   p   primary partition (1-4)
 ##   选(p)
 Selected partition 4
 First cylinder (1428-2610, default 1428): 
 Using default value 1428
 Last cylinder, +cylinders or +size{K,M,G} (1428-2610, default 2610):
 
 Command (m forhelp): w
 The partition table has been altered!
 Calling ioctl() to re-read partition table.
 WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
 The kernel still uses the old table. The new table will be used at
 the next reboot or after you run partprobe(8) or kpartx(8)
 Syncing disks.
 [root@docker-02 ~]# reboot
 [root@docker-02 ~]# fdisk -l
 
 Disk /dev/sda: 107.4 GB, 107374182400bytes
 255heads, 63sectors/track, 13054cylinders
 Units =cylinders of 16065* 512=8225280bytes
 Sector size (logical/physical): 512bytes / 512bytes
 I/O size (minimum/optimal): 512bytes / 512bytes
 Disk identifier: 0x000b076e
 
   Device Boot     Start         End     Blocks   Id System
 /dev/sda1   *           1         26     204800  83Linux
 Partition 1does not end on cylinder boundary.
 /dev/sda3             287      13055  102554624  83Linux
 /dev/sda4              26        286    2091471  83Linux
 
 Partition table entries are not indisk order

配置sda4共享

[root@docker-02 ~]# vim /etc/tgt/targets.conf   
75#<target iqn.2008-09.com.example:server.target4>
 76#   direct-store /dev/sdb     # Becomes LUN 1
 77#   direct-store /dev/sdc     # Becomes LUN 2
 78#   direct-store /dev/sdd     # Becomes LUN 3
 79#   write-cache off
 80#   vendor_id MyCompany Inc.
 81#</target>

在上面内容后面新增以下内容:

<target iqn.2018-01.cn.docker:target_san1>
         backing-store /dev/sda4
         initiator-address 192.168.80.0/24
         vendor_id docker
         product_id   target1
</target>

 注释:

default-driver iscsi      #此配置文件默认全部注释,使用iscsi驱动 

<tarrget iqn.2018-1.cn.docker:target_san1>  # iscsi正规名字格式:iqn.年-月.主机名倒着写: target端名字

backing-store /dev/sda4     #可以是具体的分区,也可以是DD出来的文件。不能小于5G。(后面的文件系统是GFS,光日志空间就128M)       

initiator-address 192.168.80.0   #指定允许访问的此存储主机

vendor_id “docker”    #vendor是供应厂商编号标识这个设备(字符不要过长)

product_id "TARGET1"     # 产品编号

</target>

启动服务

[root@localhost wuqiong]#systemctl start tgtd service
[root@localhost wuqiong]#tgt-admin --show

二、配置客户端

安装服务

[root@localhost wuqiong]# yum install -y iscsi-initiator-utils
[root@localhost wuqiong]# service iscsid restart
[root@localhost wuqiong]# /etc/init.d/iscsid status
 iscsid (pid  1553) 正在运行...

target存储服务器信息在客户端存储的位置

[root@localhost wuqiong]# yum install -y tree ##安装tree命令,用于查询树
[root@localhost wuqiong]# tree /var/lib/iscsi/ ##发现target后,会在此目录先生成树结构
 /var/lib/iscsi/
 ├── ifaces
 ├── isns
 ├── nodes
 │   └── iqn.2018-01.cn.docker:target_san1
 │       └── 172.17.1.151,3260,1
 │           └── default
 ├── send_targets
 │   └── 172.17.1.151,3260
 │       ├── iqn.2018-01.cn.docker:target_san1,172.17.1.151,3260,1,default -> /var/lib/iscsi/nodes/iqn.2018-01.cn.docker:target_san1/172.17.1.151,3260,1
 │       └── st_config
 ├── slp
 └── static

10directories, 2files

客户端服务启动关系

先启动iscsid,再启动iscsi,iscsi是根据/var/lib/iscsi/中发现的信息,识别设备。

查看发现到新硬盘以及卸载,挂载存储设备

[root@localhost wuqiong]# ll /dev/sdb
 brw-rw---- 1root disk 8, 169月  2916:51 /dev/sdb
 ##卸载
 [root@docker-03 opt]# iscsiadm -m node -T iqn.2018-01.cn.docker:target_san1 -u
 ##登录存储存储设备
 [root@docker-03 opt]# iscsiadm -m node -T iqn.2018-01.cn.docker:target_san1 -l

在192.168.80.0上对识别出来的硬盘,分区格式化,挂载使用。

[root@localhost wuqiong]# iscsiadm -m discovery -t sendtargets -p 172.17.1.151:3260 ##发现存储设备
 [root@localhost wuqiong]# service iscsid restart ##登录存储
 [root@localhost wuqiong]# service iscsi restart
 [root@localhost wuqiong]# ll /dev/sdb
 brw-rw---- 1root disk 8, 169月  2916:51 /dev/sdb
 [root@docker-03 opt]# fdisk /dev/sdb ##划分一个分区当作本地盘使用即可
 WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').
 
 Command (m forhelp): p
 
 Disk /dev/sdb: 2141MB, 2141666304bytes
 66heads, 62sectors/track, 1022cylinders
 Units =cylinders of 4092* 512=2095104bytes
 Sector size (logical/physical): 512bytes / 512bytes
 I/O size (minimum/optimal): 512bytes / 512bytes
 Disk identifier: 0x4c80a2ad
 
   Device Boot     Start         End     Blocks   Id System
 /dev/sdb1               1       1022    2090981  83Linux
 
 Command (m forhelp): n
 Command action
   e   extended
   p   primary partition (1-4)
 p
 Partition number (1-4): 
 Value out of range.
 Partition number (1-4): 
 Value out of range.
 Partition number (1-4): 1
 ##以下回车之后w保存
 [root@localhost wuqiong]# ll /dev/sdb* ##查看分区
 brw-rw---- 1root disk 8, 169月  2917:06 /dev/sdb
 brw-rw---- 1root disk 8, 179月  2917:06 /dev/sdb1
 [root@dlocalhost wuqiong]# mkfs.ext4 /dev/sdb1 ##格式化分区
 [root@localhost wuqiong]# mount /dev/sdb1 /opt/ ##挂载使用
 [root@localhost wuqiong]# cp /etc/passwd /opt/ ##测试文件写入
 [root@localhost wuqiong]# cd /opt/
 [root@localhost wuqiong]# ls
 lost+found passwd

测试: 192.168.80.0挂载上硬盘,数据同步

[root@docker-01 opt]# yum -y install iscsi-initiator-utils
 [root@docker-01 opt]# iscsiadm -m discovery -t sendtargets -p 172.17.1.151:3260
 [root@docker-01 opt]# service iscsid restart
 [root@docker-01 opt]# service iscsi restart
 [root@docker-01 opt]# ls /dev/sdb*
 /dev/sdb /dev/sdb1
 [root@docker-01 opt]# mount /dev/sdb1 /opt/
 [root@docker-01 opt]# ls /opt/ ##可以看到数据已经同步过来了
 lost+found passwd
 添加新数据测试同步:
 [root@localhost wuqiong]# cp /etc/sysconfig/network /opt/
 [root@localhost wuqiong]# ls /opt/
 lost+found network passwd
 172.17.1.152上查看:
 [root@localhost wuqiong]# ls
 lost+found passwd