iscsi ,Internet小型计算机系统接口,又称为IP-SAN可以简单的理解为是一种基于网络及 scsi
协议下的存储技术,iSCSI
利用了TCP/IP
的port 860
和 3260
作为沟通的渠道。透过两部计算机之间利用iSCSI
的协议来交换SCSI命令,让计算机可以透过高速的局域网集线来把SAN模拟成为本地的储存装置。
ISCSI 分为服务端和客户端两部分
服务端(target)负责提供存储服务
客户端(initiator)使用存储服务
实验场景
主机名 | IP 地址 | 操作系统 |
---|---|---|
iscsi-server | 192.168.169.30 | centos 7.5 |
iscsi-clie | 192.168.169.50 | centos 7.4 |
以下内容在服务端和客户端都进行配置
[root@iscsi-server ~]# cd /etc/yum.repos.d/
[root@iscsi-server yum.repos.d]# curl -o CentOS7-Base-163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
[root@iscsi-server yum.repos.d]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@iscsi-server yum.repos.d]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@iscsi-server yum.repos.d]# yum install -y epel-release //下载epel 源
[root@iscsi-server ~]# setenforce 0 //关闭selinux
[root@iscsi-server ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config //设置开机不启动
[root@iscsi-server ~]# systemctl stop firewalld //关闭防火墙
[root@iscsi-server ~]# systemctl disable firewalld //设置开机不启动
在服务端添加一块硬盘作为共享硬盘
[root@iscsi-server ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 4.7G 0 part /boot
├─sda2 8:2 0 10G 0 part /
└─sda3 8:3 0 5.4G 0 part [SWAP]
sdb 8:16 0 12G 0 disk //此处就是新添加的硬盘,大小根据实际需要而定
targetd
和targetcli
[root@iscsi-server ~]# yum install targetd targetcli -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
2. epel: mirrors.tuna.tsinghua.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package targetcli.noarch 0:2.1.fb46-7.el7 will be installed
---> Package targetd.noarch 0:0.8.6-1.el7 will be installed
--> Finished Dependency Resolution
.....此处下载过程省略
[root@iscsi-server ~]# systemctl start targetd
[root@iscsi-server ~]# systemctl disable targetd //开机自启动
targetcli
命令进行管理[root@iscsi-server ~]# targetcli //直接输入命令
targetcli shell version 2.1.fb46
Copyright 2011-2013 by Datera, Inc and others.
For help on commands, type 'help'.
/> //出现这样的提示符为成功!
/> cd backstores/block
/backstores/block> create disk0 /dev/sdb //ls 可以查看
Created block storage object disk0 using /dev/sdb.
/backstores/block> cd /iscsi
/iscsi> create //ls 可查看
Created target iqn.2003-01.org.linux-iscsi.iscsi-server.x8664:sn.a5ba7f31f39b.
Created TPG 1.
Global pref auto_add_default_portal=true
Created default portal listening on all IPs (0.0.0.0), port 3260.
/iscsi> cd iqn.2003-01.org.linux-iscsi......
/iscsi/iqn.20....a5ba7f31f39b> cd tpg1/luns
/iscsi/iqn.20...39b/tpg1/luns> create /backstores/block/disk0
Created LUN 0.
/iscsi> cd /iscsi/iqn.2003-01.org.linux-iscsi.iscsi-server.x8664:sn.a5ba7f31f39b/
/iscsi/iqn.20....a5ba7f31f39b> cd tpg1/acls
/iscsi/iqn.20...39b/tpg1/acls> create iqn.2003-01.org.linux-iscsi.iscsi-server.x8664:sn.a5ba7f31f39b //注意这里设置成上面一样以iqn. 开头一大串的字符
/iscsi/iqn.20...39b/tpg1/acls> cd ..
/iscsi/iqn.20...7f31f39b/tpg1> pwd //注意当前路径正确
/iscsi/iqn.2003-01.org.linux-iscsi.iscsi-server.x8664:sn.a5ba7f31f39b/tpg1
/iscsi/iqn.20...7f31f39b/tpg1> cd portals/
/iscsi/iqn.20.../tpg1/portals> ls //可以查看以下
o- portals ............................................................................................. [Portals: 1]
o- 0.0.0.0:3260 .............................................................................................. [OK]
~~~删除这个默认的监听地址,并配置为服务端的IP地址
/iscsi/iqn.20.../tpg1/portals> delete 0.0.0.0 3260
/iscsi/iqn.20.../tpg1/portals> create 192.168.169.30
/iscsi/iqn.20.../tpg1/portals> ls
o- portals ............................................................................................. [Portals: 1]
o- 192.168.169.30:3260 ....................................................................................... [OK]
/> saveconfig //这一步很重要、切记
Last 10 configs saved in /etc/target/backup/.
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
[root@iscsi-server ~]# systemctl restart targetd
[root@iscsi-server ~]# systemctl status targetd
● targetd.service - targetd storage array API daemon
Loaded: loaded (/usr/lib/systemd/system/targetd.service; disabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Mon 2019-02-25 18:58:14 CST; 13min ago
Process: 1213 ExecStart=/usr/bin/targetd (code=exited, status=255)
Main PID: 1213 (code=exited, status=255)
Feb 25 18:58:14 localhost.localdomain systemd[1]: Started targetd storage array API daemon.
Feb 25 18:58:14 localhost.localdomain systemd[1]: Starting targetd storage array API daemon...
Feb 25 18:58:14 localhost.localdomain targetd[1213]: CRITICAL:root:password not set in /etc/target/targetd.yaml
Feb 25 18:58:14 localhost.localdomain systemd[1]: targetd.service: main process exited, code=exited, status=255/n/a
Feb 25 18:58:14 localhost.localdomain systemd[1]: Unit targetd.service entered failed state.
Feb 25 18:58:14 localhost.localdomain systemd[1]: targetd.service failed.
[root@iscsi-clie ~]# yum list|grep iscsi
iscsi-initiator-utils.i686 6.2.0.874-10.el7 @base
iscsi-initiator-utils.x86_64 6.2.0.874-10.el7 @base
iscsi-initiator-utils-iscsiuio.x86_64 6.2.0.874-10.el7 @base
[root@iscsi-clie ~]# yum install -y iscsi-initiator-utils
[root@iscsi-clie ~]# systemctl start iscsid
[root@iscsi-clie ~]# vi /etc/iscsi/initiatorname.iscsi
InitiatorName=iqn.2003-01.org.linux-iscsi.localhost.x8664:sn.30202fe62cce //这里必须是服务端创建的名称
[root@iscsi-clie /]# systemctl restart iscsid
[root@iscsi-clie /]# iscsiadm -m discovery -t sendtargets -p 192.168.169.30
192.168.169.30:3260,1 iqn.2003-01.org.linux-iscsi.localhost.x8664:sn.30202fe62cce
登录单个服务器
[root@iscsi-clie /]# iscsiadm --mode node --targetname iqn.2003-01.org.linux-iscsi.localhost.x8664:sn.30202fe62cce --portal 192.168.169.30 -l
登录扫描到的服务器
[root@iscsi-clie /]# iscsiadm -m node -l
[root@iscsi-clie /]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 6G 0 part /boot
├─sda2 8:2 0 9.3G 0 part /
└─sda3 8:3 0 4.7G 0 part [SWAP]
sdb 8:16 0 8G 0 disk
sdc 8:32 0 8G 0 disk
sdd 8:48 0 8G 0 disk
sde 8:64 0 12G 0 disk //已找到共享的磁盘
[root@iscsi-clie /]# fdisk /dev/sde
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x3815b301.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p):
Using default response p
Partition number (1-4, default 1):
First sector (8192-25165823, default 8192):
Using default value 8192
Last sector, +sectors or +size{K,M,G} (8192-25165823, default 25165823):
Using default value 25165823
Partition 1 of type Linux and of size 12 GiB is set
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@iscsi-clie /]# mkfs.xfs /dev/sde1
meta-data=/dev/sde1 isize=512 agcount=4, agsize=786176 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=3144704, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
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@iscsi-clie /]# mkdir soft
[root@iscsi-clie /]# mount /dev/sde1 /soft/ //这只是临时挂载、重启后失效
[root@iscsi-clie /]# blkid
/dev/sr0: UUID="2017-09-06-10-51-00-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"
/dev/sda1: UUID="bdbb6db2-9bef-4b60-a5a5-6bedb9afb944" TYPE="xfs"
/dev/sda2: UUID="baf34060-0010-49e8-8e15-b73acbcc7485" TYPE="xfs"
/dev/sda3: UUID="d1fd294c-5e0a-4f93-9f74-5f4e023e3465" TYPE="swap"
/dev/sde1: UUID=“b86c28be-4152-4321-92b3-575dfcbcf42a" TYPE="xfs" //复制这个UUID,在/etc/fstab 下挂载
[root@iscsi-clie /]# vi /etc/fstab //永久挂载
# /etc/fstab
# Created by anaconda on Tue Jan 22 22:05:24 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=baf34060-0010-49e8-8e15-b73acbcc7485 / xfs defaults 0 0
UUID=bdbb6db2-9bef-4b60-a5a5-6bedb9afb944 /boot xfs defaults 0 0
UUID=d1fd294c-5e0a-4f93-9f74-5f4e023e3465 swap swap defaults 0 0
UUID=b86c28be-4152-4321-92b3-575dfcbcf42a /soft xfs _netdev 0 0 //注意这个位置的区别
[root@iscsi-clie ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 9.4G 1.7G 7.7G 18% /
devtmpfs 983M 0 983M 0% /dev
tmpfs 993M 0 993M 0% /dev/shm
tmpfs 993M 8.6M 984M 1% /run
tmpfs 993M 0 993M 0% /sys/fs/cgroup
/dev/sda1 6.1G 119M 6.0G 2% /boot
/dev/sde1 12G 33M 12G 1% /soft //已经挂载
tmpfs 199M 0 199M 0% /run/user/0