title: iscsi(tgt)+librbd使用
1霞幅、前言
因?yàn)橐{(diào)試看下librbd的一個(gè)功能蒜撮。然后準(zhǔn)備在自己的測(cè)試環(huán)境配置下iscsi+librbd叮雳,然后方便調(diào)試温圆,這里記錄下大概過程语淘。
這里是使用的tgt作為iscsi的target诲宇。具體什么是iscsi和iscsi有哪幾種target類型,google下就好惶翻。
1姑蓝、ceph集群準(zhǔn)備
準(zhǔn)備一個(gè)標(biāo)準(zhǔn)三節(jié)點(diǎn)ceph集群:
[root@ceph01 ~]# ceph -v
ceph version 12.2.12 (1436006594665279fe734b4c15d7e08c13ebd777) luminous (stable)
[root@ceph01 ~]#
[root@ceph01 ~]#
[root@ceph01 ~]# ceph osd tree
ID CLASS WEIGHT TYPE NAME STATUS REWEIGHT PRI-AFF
-1 0.08455 root default
-3 0.02917 host ceph01
0 hdd 0.01459 osd.0 up 1.00000 1.00000
1 hdd 0.01459 osd.1 up 1.00000 1.00000
-5 0.02818 host ceph02
2 hdd 0.01459 osd.2 up 1.00000 1.00000
4 hdd 0.01360 osd.4 up 1.00000 1.00000
-7 0.02719 host ceph03
3 hdd 0.01360 osd.3 up 1.00000 1.00000
5 hdd 0.01360 osd.5 up 1.00000 1.00000
2、編譯tgtd
為啥要自己編譯吕粗,不直接使用yum install纺荧?因?yàn)橹苯觵um安裝的tgtd服務(wù)可能不包含rbd的功能模塊,也就不能使用librbd了颅筋。所以要自己編譯宙暇,在編譯的時(shí)候加入rbd的功能到tgtd服務(wù)里面。
2.1议泵、獲取tgtd源碼包
下載地址:https://github.com/fujita/tgt/releases占贫, 下載當(dāng)前最新的版本v1.0.76,并解壓:
[root@ceph01 ~]# tar -zxvf tgt-1.0.76.tar.gz
2.2先口、修改相關(guān)文件
為了把rbd功能編入tgtd服務(wù)里面型奥,還要一些相關(guān)文件才行。
2.2.1池充、修改spec文件的如下內(nèi)容
[root@ceph01 ~]# vim tgt-1.0.76/scripts/tgtd.spec
...
Version: 1.0.76 # 修改版本為你下載的包的版本
...
%install
...
%{__install} -d %{buildroot}/etc/tgt
%{__install} -d %{buildroot}/usr/lib/tgt/backing-store # 增加存放bs_rbd.so庫(kù)的目錄
...
%{__install} -p -m 0600 conf/targets.conf %{buildroot}/etc/tgt
%{__install} -p -m 0600 usr/bs_rbd.so %{buildroot}/usr/lib/tgt/backing-store # 放置usr/bs_rbd.so到/usr/lib/tgt/backing-store目錄下
...
%files
...
/usr/lib/tgt/backing-store/bs_rbd.so # 在該rpm包釋放的文件列表中增加bs_rbd.so文件
...
如果不加上面有關(guān)bs_rbd.so相關(guān)的文件桩引,那制作出來的tgt包就不會(huì)包含bs_rbd.so文件缎讼,最后安裝的tgt包的后端存儲(chǔ)就不會(huì)支持rbd收夸。
2.2.2、修改build-pkg.sh里面相關(guān)信息
下面的幾個(gè)信息血崭,根據(jù)你下載的源碼版本而定:
[root@ceph01 ~]# vim tgt-1.0.76/scripts/build-pkg.sh
...
branch="master"
version="1.0.76"
release="1"
...
2.3卧惜、編譯tgtd
安裝依賴包(注意下面列出的依賴包,在我們編譯出tgtd的rpm包后夹纫,待安裝tgtd服務(wù)的目標(biāo)機(jī)器上也需要安裝這些依賴包)
[root@ceph01 ~]# yum install librbd1-devel librados2-devel -y
[root@ceph01 ~]# yum install libxslt docbook-style-xsl libibverbs-devel librdmacm-devel libaio-devel -y
開始編譯咽瓷,要設(shè)置CEPH_RBD變量為1,表示編譯時(shí)加入rbd的功能模塊
[root@ceph01 ~]# cd tgt-1.0.76
[root@ceph01 tgt-1.0.76]# make CEPH_RBD=1
make -C usr
make[1]: Entering directory `/root/tgt-1.0.76/usr'
cc -c -DUSE_SIGNALFD -DUSE_TIMERFD -DUSE_EVENTFD -D_GNU_SOURCE -I. -g -O2 -fno-strict-aliasing -Wall -Wstrict-prototypes -Werror -fPIC -DTGT_VERSION=\"1.0.76\" -DBSDIR=\"/usr/lib/tgt/backing-store\" iscsi/conn.c -o iscsi/conn.o
cc -c -DUSE_SIGNALFD -DUSE_TIMERFD -DUSE_EVENTFD -D_GNU_SOURCE -I. -g -O2 -fno-strict-aliasing -Wall -Wstrict-prototypes -Werror -fPIC -DTGT_VERSION=\"1.0.76\" -DBSDIR=\"/usr/lib/tgt/backing-store\" iscsi/param.c -o iscsi/param.o
cc -c -DUSE_SIGNALFD -DUSE_TIMERFD -DUSE_EVENTFD -D_GNU_SOURCE -I. -g -O2 -fno-strict-aliasing -Wall -Wstrict-prototypes -Werror -fPIC -DTGT_VERSION=\"1.0.76\" -DBSDIR=\"/usr/lib/tgt/backing-store\" iscsi/session.c -o iscsi/session.o
cc -c -DUSE_SIGNALFD -DUSE_TIMERFD -DUSE_EVENTFD -D_GNU_SOURCE -I. -g -O2 -fno-strict-aliasing -Wall -Wstrict-prototypes -Werror -fPIC -DTGT_VERSION=\"1.0.76\" -DBSDIR=\"/usr/lib/tgt/backing-store\" iscsi/iscsid.c -o iscsi/iscsid.o
cc -c -DUSE_SIGNALFD -DUSE_TIMERFD -DUSE_EVENTFD -D_GNU_SOURCE -I. -g -O2 -fno-strict-aliasing -Wall -Wstrict-prototypes -Werror -fPIC -DTGT_VERSION=\"1.0.76\" -DBSDIR=\"/usr/lib/tgt/backing-store\" iscsi/target.c -o iscsi/target.o
...
上面編譯過程沒有報(bào)錯(cuò)的話舰讹,繼續(xù)制作rpm安裝包茅姜,因?yàn)槭褂胷pm包來安裝,后期管理相關(guān)包時(shí)會(huì)方便很多:
[root@ceph01 tgt-1.0.76]# make rpm
Building version: 1.0.76-1
Creating rpm build dirs under /root/tgt-1.0.76/pkg
Creating tgz scsi-target-utils-1.0.76-1.tgz
Creating rpm
Wrote: /root/tgt-1.0.76/pkg/SRPMS/scsi-target-utils-1.0.76-1.src.rpm
Wrote: /root/tgt-1.0.76/pkg/RPMS/x86_64/scsi-target-utils-1.0.76-1.x86_64.rpm
Wrote: /root/tgt-1.0.76/pkg/RPMS/x86_64/scsi-target-utils-debuginfo-1.0.76-1.x86_64.rpm
Done.
3月匣、安裝配置tgtd
安裝下上面編譯出來的rpm包:
[root@ceph01 tgt-1.0.76]# rpm -ivh /root/tgt-1.0.76/pkg/RPMS/x86_64/scsi-target-utils-1.0.76-1.x86_64.rpm
Preparing... ################################# [100%]
Updating / installing...
1:scsi-target-utils-1.0.76-1 ################################# [100%]
啟動(dòng)tgtd服務(wù):
[root@ceph01 ~]# systemctl restart tgtd
檢查是否支持rbd:
[root@ceph01 ~]# tgtadm --lld iscsi --mode system --op show | grep rbd
rbd (bsoflags sync:direct)
如果沒有rbd相關(guān)信息輸出钻洒,表示不支持rbd奋姿,請(qǐng)檢查相關(guān)編譯過程是否都正確。
準(zhǔn)備一個(gè)rbd:
[root@ceph01 ~]# rbd create img02 --size 10G
配置target:
[root@ceph01 ~]# cat /etc/tgt/conf.d/librbd.conf
<target iqn.2019-04.com.example:cephrbd.img02>
bs-type rbd
backing-store rbd/img02
initiator-address 192.168.0.0/16
</target>
重啟tgtd服務(wù):
systemctl restart tgtd
查看target是否正確啟用:
[root@ceph01 conf.d]# tgtadm --lld iscsi --mode target --op show
Target 1: iqn.2019-04.com.example:cephrbd.img02
System information:
Driver: iscsi
State: ready
I_T nexus information:
LUN information:
LUN: 0
Type: controller
SCSI ID: IET 00010000
SCSI SN: beaf10
Size: 0 MB, Block size: 1
Online: Yes
Removable media: No
Prevent removal: No
Readonly: No
SWP: No
Thin-provisioning: No
Backing store type: null
Backing store path: None
Backing store flags:
LUN: 1
Type: disk
SCSI ID: IET 00010001
SCSI SN: beaf11
Size: 10737 MB, Block size: 512
Online: Yes
Removable media: No
Prevent removal: No
Readonly: No
SWP: No
Thin-provisioning: No
Backing store type: rbd
Backing store path: rbd/img02
Backing store flags:
Account information:
ACL information:
192.168.0.0/16
好了素标,看到一切都正常称诗,然后就可以到客戶端去發(fā)現(xiàn)、登陸和使用這個(gè)rbd設(shè)備了头遭。
4寓免、使用
來到另外一個(gè)節(jié)點(diǎn)client01,把我們剛剛配置的img02這個(gè)rbd設(shè)備掛載到我們的client01節(jié)點(diǎn)本地來使用计维。
4.1袜香、安裝iscsi客戶端軟件
安裝并啟動(dòng)iscsi-initiator-utils服務(wù):
[root@client01 ~]# yum install iscsi-initiator-utils -y
[root@client01 ~]# systemctl start iscsid
4.2、發(fā)現(xiàn)目標(biāo)設(shè)備
有個(gè)小口訣就是先發(fā)現(xiàn)再登陸鲫惶,最后再使用:
[root@client01 ~]# iscsiadm -m discovery -t st -p 192.168.10.20
192.168.10.20:3260,1 iqn.2019-04.com.example:cephrbd.img02
上面-m discovery參數(shù)是掃描iscsi服務(wù)端可用的存儲(chǔ)設(shè)備困鸥,-t st參數(shù)是執(zhí)行掃描操作的類型,-p 192.168.10.20參數(shù)為iscsi服務(wù)端的ip地址剑按。
可以看到上面已經(jīng)發(fā)現(xiàn)了iqn.2019-04.com.example:cephrbd.img02這個(gè)設(shè)備疾就,我們下面就開始登陸:
[root@client01 ~]# iscsiadm -m node -T iqn.2019-04.com.example:cephrbd.img02 -p 192.168.10.20 --login
Logging in to [iface: default, target: iqn.2019-04.com.example:cephrbd.img02, portal: 192.168.10.20,3260] (multiple)
Login to [iface: default, target: iqn.2019-04.com.example:cephrbd.img02, portal: 192.168.10.20,3260] successful.
登陸成功之后,就可以在本地看到這個(gè)設(shè)備了艺蝴,也就是下面的sdd這個(gè)設(shè)備:
[root@client01 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 500M 0 part /boot
└─sda2 8:2 0 19.5G 0 part
├─centos-root 253:0 0 17.5G 0 lvm /
└─centos-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk
sdc 8:32 0 20G 0 disk
sdd 8:48 0 10G 0 disk
sr0 11:0 1 4G 0 rom
好了猬腰,現(xiàn)在就可以像使用本地設(shè)備一樣使用這個(gè)sdd設(shè)備了。