一、準備工作
1.1 glusterfs的發展史
2011年10月04日,紅帽收購Gluster。
2014年4月30日,紅帽公司以1.75億美元的價格将Ceph的企業級産品提供商Inktank收入囊中,以将Inktank的存儲産品內建在其基于GlusterFS(紅帽在2011年10月份斥資1.36億美元收購Gluster)的存儲産品中。
2018年10月28日,IBM 将以每股 190 美元的現金收購 Red Hat 所有已發行股份,總價值約為 340 億美元(2360.45 億人民币)。
1.2 centos還是red hat?
RedHat一直都提供源代碼的發行方式,CentOS就是将RedHat發行的源代碼重新編譯一次,形成一個可使用的二進制版本。由于LINUX的源代碼是GNU,是以從獲得RedHat的源代碼到編譯成新的二進制,都是合法。隻是RedHat是商标,是以必須在新的發行版裡将RedHat的商标去掉。
RedHat對這種發行版的态度是:“我們其實并不反對這種發行版,真正向我們付費的使用者,他們重視的并不是系統本身,而是我們所提供的商業服務。”是以,CentOS可以得到RedHat的所有功能,甚至是更好的軟體。但CentOS并不向使用者提供商業支援,當然也不負上任何商業責任。
如果你要将你的RedHat轉到CentOS上,因為你不希望為RedHat更新而付費。當然,你必須有豐富UNIX使用經驗,是以,RedHat的商業技術支援對你來說并不重要。但如果你是單純的業務型企業,那麼還是建議你選購RedHat軟體并購買相應服務。這樣可以節省你的IT管理費用,并可得到專業服務。
一句話,選用CentOS還是RedHat,取決于你所在公司是否擁有相應的技術力量。
1.3 本文基于centos7
作業系統:centos7以上
本文基于:
[[email protected] ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
如果使用VMWARE虛拟機,需要将網絡設定為NAT模式,并配好響應的網卡。
(1)編輯網卡
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZuBnLygzN1MjM0ETM1EjMxgTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
(2)自定義網絡擴充卡(制定NAT網卡)
二、單機安裝工作
2.1 設定代理
如果聯網沒有限制,可以不進行這一步。
2.1.1 配置全局代理
修改全局環境變量:
vim /etc/profile
在檔案最後加上:
http_proxy=http://ip:port
https_proxy=https://ip:port
ftp_proxy=http://ip:port
export http_proxy https_proxy ftp_proxy
2.1.2 配置yum代理
修改yum指令配置檔案
vim /etc/yum.conf
在檔案最後加上:
proxy=http://ip:port
注:ip和port處填入自己公司提供的代理IP及端口。
如果僅僅使用yum安裝,那麼不需要配置全局代理,隻需配置yum代理即可。
2.2 修改yum倉庫
確定yum倉庫所在路徑(/etc/yum.repos.d/)下包含以下檔案:
執行:
yum clean all
yum makecache
yum makecache作用:
yum有很多源,大多是網絡上的。makecache建立一個緩存,以後用install時就在緩存中搜尋,提高了速度。
2.3 安裝gluster服務
關閉防火牆:
systemctl stop firewalld.service
systemctl disable firewalld.service
安裝gluster的centos版本:
yum install -y centos-release-gluster
安裝glusterfs相關服務:
yum install -y glusterfs glusterfs-server glusterfs-fuse glusterfs-rdma
啟動gluster服務:
systemctl start glusterd.service
systemctl enable glusterd.service #開機啟動gluster
檢查gluster狀态:
service glusterd status
啟動glusterfsd服務
官網沒有這一步
systemctl start glusterfsd
systemctl enable glusterfsd
systemctl status glusterfsd
檢視gluster版本:
glusterd --version
通過檢視gluster狀态和版本,可以看出在目前單機上glusterfs服務已經安裝完成。
三、搭建glusterfs-server叢集
本叢集擁有兩台伺服器,在分别對兩台伺服器進行完gluster-server的單機安裝後,開始進行叢集安裝。
3.1 配置hosts檔案
對兩台伺服器分别執行:
vim /etc/hosts
在最後一行加上以下内容:
10.161.131.33 shenzhenfs
10.177.150.7 changzhoufs
其中,第一部分為IP,第二部分為主機名/别名。
3.2 添加叢集成員
對兩台伺服器分别執行:
gluster peer probe shenzhenfs
gluster peer probe changzhoufs
檢視gluster信任池:
gluster pool list
檢視gluster叢集狀态:
gluster peer status
四、叢集共享檔案夾
4.1 建立檔案夾
在sz伺服器上執行:
mkdir -p /opt/bpmgluster/szserver
mkdir -p /opt/bpmgluster/szclient
在cz伺服器上執行:
mkdir -p /opt/bpmgluster/czserver
mkdir -p /opt/bpmgluster/czclient (暫時沒用到)
4.2 建立共享卷
GlusterFS卷共有三種基本類型,分别是Distributed(分布式存儲)、Striped(将一個檔案分成多個固定長度的資料,分布存放在所有存儲塊,相當于RAID0)、Replicated(鏡像存儲,相當于RAID1)。
按照官網教程,建立Replicated類型:
gluster volume create bpmshare replica 2 shenzhenfs:/opt/bpmgluster/szserver changzhoufs:/opt/bpmgluster/czserver force
在兩台伺服器上分别啟動volume:
gluster volume start bpmshare
确認gluster已經啟動:
gluster volume info
gluster volume status
4.3 挂載用戶端檔案夾
按照網上做法:
[[email protected] szserver]# mount -t glusterfs shenzhenfs:/bpmshare /opt/bpmgluster/szserver
ERROR: /opt/bpmgluster/szserver is in use as a brick of a gluster volume
錯誤原因:已經被設定為共享卷的檔案夾是不能再被挂載的,大部分部落格裡的答案真心不靠譜。
官網解釋:
For this step, we will use one of the servers to mount the volume.
Typically, you would do this from an external machine, known as a
“client”. Since using this method would require additional packages to
be installed on the client machine, we will use one of the servers as
a simple place to test first, as if it were that “client”.
上面這句話可以用下圖解釋:
簡而言之,本來是需要另一台伺服器當用戶端的,但是為了省事,直接在任意一台server上建立檔案夾當用戶端吧。本文選擇用深圳伺服器的szclient檔案夾模拟用戶端:
mount -t glusterfs shenzhenfs:/bpmshare /opt/bpmgluster/szclient/
df -h
4.4 共享檔案測試
經過以上步驟後可總結如下:
檔案夾szserver與czserver組成鏡像存儲共享卷,檔案夾szclient為用戶端,當在szclient裡面存入檔案時,szserver和czserver内的檔案會與szclient保持同步。
測試一:在szclient檔案夾建立檔案szclient1.txt,發現該檔案同步至szserver和czserver
測試二:在szserver檔案夾建立檔案szserver1.txt,發現該檔案同步至szclient和czserver
測試三:在czserver檔案夾建立檔案czserver1.txt,該檔案沒有同步至szserver和szclient。
測試三:在czserver檔案夾建立檔案czserver1.txt,該檔案沒有同步至szserver和szclient。
測試四:在szserver檔案夾建立檔案szserver2.txt,該檔案同步至szclient和czserver,然後在szserver(szclient)中删除szserver2.txt,szclient(szserver)中的szserver2.txt也随之消失,czclient中仍保留szserver2.txt。
測試五:在szserver(szclient)中建立檔案夾szserver1,同步效果與建立檔案同步效果相同。
五、GlusterFS異地雙活實戰
架構圖如下:
5.1 雙活配置過程
5.1.1 配置IP、主機名
首先,在每台主機上執行
vim /etc/hosts
編輯需要與本機進行檔案同步的主機。
實際過程中,隻需要在用戶端上配置好服務端的IP和主機名(别名)即可。
做完這一步後,執行ping IP/hostname,看一下伺服器之間是否連通。
5.1.2 安裝Gluster用戶端
在實際搭建過程中雖然gluster伺服器均為CentOS系統,但webapp伺服器均為Red Hat系統。使用yum指令安裝GlusterFS:
yum install -y glusterfs glusterfs-server
無法安裝,提示系統中已經存在GlusterFS服務,但執行以下指令卻無法對外提供GlusterFS服務:
[[email protected]]# service glusterfsd start
Redirecting to /bin/systemctl start glusterfsd.service
Failed to start glusterfsd.service: Unit glusterfsd.service failed to load: No such file or directory.
幸運的是,webapp伺服器上隻需安裝Gluster用戶端:
yum install -y glusterfs-fuse glusterfs-rdma
RDMA的全稱為Remote Direct Memory
Access,字面的意思就是遠端直接通路記憶體,顧名思義,就是讓一台計算機能夠直接通路另外一台計算機的記憶體。這種技術無需作業系統參與,能讓應用程式直接讀寫網絡收發的資料,進而極大地減少了記憶體拷貝操作,也避免了不同的軟體子產品對資料的反複處理,對性能的提升作用是毋庸置疑的。
可以安裝成功!
5.1.3 将用戶端挂載到服務端共享卷
由此,在基于紅帽系統的webapp伺服器上安裝好了GlusterFS用戶端,按照異地雙活架構圖,在shenzhen webapp伺服器上執行以下指令,将webapp存放檔案的路徑挂載到gluster伺服器的共享卷bpmshare上:
mount -t glusterfs shenzhenfs:/bpmshare /opt/smart
在changzhou webapp伺服器上執行以下指令:
mount -t glusterfs changzhoufs:/bpmshare /opt/smart
附一、GlusterFS運維問題及解決方案
1. 叢集中的節點命名不正确
問題
hostname處命名期望值是簡潔的changzhoufs,而不是冗長的域名
解決
以下操作確定有超級使用者權限
首先移除節點:
[[email protected] /]# gluster peer detach vlxcndcz01bpmt7.aac.com
peer detach: success
再次檢視信任池,節點已經移除:
配置hosts檔案:
vim /etc/hosts
最後改為如下内容(删掉域名部分):
10.161.131.33 shenzhenfs
10.177.150.7 changzhoufs
重新探測其他節點,與其建立叢集(信任池 trusted pool)
[[email protected] /]# gluster peer probe changzhoufs
peer probe: success.
2. cd指令無法進入檔案夾
問題
[[email protected] opt]$ cd bpmgluster/
-bash: cd: bpmgluster/: Permission denied
解決
ll指令發現該檔案夾沒有讀的權限
[[email protected] opt]$ ll
total 8
drwxr-x--- 2 root root 6 Dec 20 10:33 bpmgluster
drwxr-xr-x. 5 root root 4096 Jul 5 16:30 ds_agent
drwxr-xr-x. 9 root root 4096 Sep 8 13:54 omni
修改該檔案夾的權限
[[email protected] opt]$ sudo chmod 755 bpmgluster/
3. umount目錄指令顯示正忙
問題
沒法解除安裝挂載目錄
[[email protected] glusterdata]# umount -v /opt/glusterdata/
umount: /opt/glusterdata: target is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
解決
fuser + ps指令檢視該目錄被哪個程序占用
[[email protected] glusterdata]# fuser -a /opt/glusterdata
/opt/glusterdata: 7176c
[[email protected] glusterdata]# ps -ef |grep 7176
root 7176 7171 0 Dec19 pts/0 00:00:00 bash
root 23232 7176 0 10:24 pts/0 00:00:00 ps -ef
root 23233 7176 0 10:24 pts/0 00:00:00 grep --color=auto 7176
殺程序
[[email protected] glusterdata]# kill -9 7176
Killed
解除安裝
[[email protected] opt]# umount -v /opt/glusterdata/
umount: /opt/glusterdata (shenzhenfs:/sharevol) unmounted
4. 挂載出錯
在用戶端上挂載需要同步的檔案夾:
mount -t glusterfs shenzhenfs:/bpmshare /opt/smart
之前,要在/etc/hosts上配置好對應的Gluster伺服器的IP及别名,否則可能會導緻挂載占用程序,
df -h
指令無法使用,挂載目錄也無法進入。
10.161.131.33 shenzhenfs
如果執行:
mount -t glusterfs shenzhenfs:/bpmshare /opt/smart
指令後仍然無法同步檔案,那麼可以嘗試先删除掉挂載目錄:
rm -rf /opt/smart/
再建立目錄:
mkdir /opt/smart
最後重新執行挂載指令。
附二、GlusterFS常用指令大全
#服務後的d表示守護程序,直到系統關閉該程序才會結束
#開啟gluster服務
service glusterd start
service glusterd stop
service glusterd status
#或者
systemctl start glusterd
systemctl stop glusterd
systemctl status glusterd
#開啟glusterfs服務
service glusterfsd start
service glusterfsd stop
service glusterfsd status
#或者
systemctl start glusterfsd
systemctl stop glusterfsd
systemctl status glusterfsd
#為trusted pool添加/移除伺服器
gluster peer probe shenzhenfs
gluster peer detach shenzhenfs
#檢視所有節點狀态(不包括本節點)
gluster peer status
#volume指令
gluster volume create bpmshare replica 2 shenzhenfs:/opt/bpmgluster/szserver changzhoufs:/opt/bpmgluster/czserver force
gluster volume start bpmshare
gluster volume list
gluster volume info
gluster volume status
gluster volume stop bpmshare
gluster volume delete bpmshare #注意,删除卷的前提是先停止卷。
#用戶端以glusterfs形式挂載
mount -t glusterfs shenzhenfs:/bpmshare /opt/bpmgluster/szclient/
df -h #檢視挂載情況