轉自:http://www.cnblogs.com/terrycy/p/5915263.html
1.准備工作
准備三台機器(物理機或者虛擬機均可)用於安裝和測試GlusterFS,其中兩台用作服務器,一台用作客戶端,主機名分別為:
Server1.zhaogang.int 10.0.21.241 Server2.zhaogang.int 10.0.21.242 Clinet.zhaogang.int
關閉iptables和selinux
2.安裝glusterFS服務器
yum install centos-release-gluster -y
yum install glusterfs-server glusterfs glusterfs-fuse -y
啟動glusterFS服務:
/etc/init.d/glusterd start
設置glusterFS服務開機啟動
chkconfig glusterd on
GlusterFS服務器設置
加入可信任存儲池
在server1上運行一下命令:
[root@server1 ~]# gluster peer probe 10.0.21.242
peer probe: success.
查看狀態:
[root@server1 ~]# gluster peer status
Number of Peers: 1
Hostname: 10.0.21.242
Uuid: ab640ab2-76ba-47a5-a868-be5514b70258
State: Peer in Cluster (Connected)
移除節點:
gluster peer detach 10.0.21.242
B.創建GlusterFS邏輯卷(Volume)
在server1和server2分別建立/data/gfsdata目錄:
mkdir -p /data/gfsdata
然后執行以下命令(只需要在其中一台服務器上執行即可,本例使用server1):
[root@server1 ~]# gluster volume create gv0 replica 2 10.0.21.241:/data/gfsdata 10.0.21.242:/data/gfsdata
volume create: gv0: success: please start the volume to access data
這條命令的意思是使用Replicated的方式,建立一個名為gv0的卷(Volume),存儲塊(Brick)為2個,分別為server1:/data/gfsdata和server2:/data/gfsdata
volume create: testglustervol: failed: The brick 192.168.21.19:/data0/gluster1/brick1 is being created in the root partition. It is recommended that you don't use the system's root partition for storage backend. Or use 'force' at the end of the command if you want to override this behavior.
發現報錯了,這是因為我們創建的brick在系統盤,這個在gluster的默認情況下是不允許的,生產環境下也盡可能的與系統盤分開,如果必須這樣請使用force
[root@server1 ~]# gluster volume create gv0 replica 2 10.0.21.241:/data/gfsdata 10.0.21.242:/data/gfsdata force
volume create: gv0: success: please start the volume to access data
啟用GlusterFS邏輯卷:
[root@server1 ~]# gluster volume start gv0
volume start: gv0: success
查看邏輯卷狀態:
[root@server1 ~]# gluster volume info
Volume Name: gv0
Type: Replicate
Volume ID: da0f4439-824b-4606-bc18-4bdbdc93d09d
Status: Started
Number of Bricks: 1 x 2 = 2
Transport-type: tcp
Bricks:
Brick1: 10.0.21.241:/data/gfsdata
Brick2: 10.0.21.242:/data/gfsdata
Options Reconfigured:
performance.readdir-ahead: on
清除glusterfs配置
通過查看/etc/glusterfs/glusterd.vol可以得知glusterfs的工作目錄是在/var/lib/glusterd中
[root@localhost ~]# cat /etc/glusterfs/glusterd.vol
volume management
type mgmt/glusterd
option working-directory /var/lib/glusterd
option transport-type socket,rdma
option transport.socket.keepalive-time 10
option transport.socket.keepalive-interval 2
option transport.socket.read-fail-log off
option ping-timeout 0
option event-threads 1
# option transport.address-family inet6
# option base-port 49152
end-volume
如果需要清除glusterfs配置,將工作目錄刪除后重啟服務即可
[root@localhost ~]# rm -rf /var/lib/glusterd/
[root@localhost ~]# /etc/init.d/glusterd restart
刪除卷
gluster volume stop gv0
gluster volume delete gv0
# 卷擴容(由於副本數設置為2,至少要添加2(4、6、8..)台機器)
gluster peer probe 10.0.21.243 # 加節點
gluster peer probe 10.0.21.244 # 加節點
gluster volume add-brick gv0 10.0.21.243:/data/glusterfs 10.0.21.244:/data/glusterfs # 合並卷
# 收縮卷(收縮卷前gluster需要先移動數據到其他位置)
gluster volume remove-brick gv0 10.0.21.243:/data/glusterfs 10.0.21.244:/data/glusterfs start # 開始遷移
gluster volume remove-brick gv0 10.0.21.243:/data/glusterfs 10.0.21.244:/data/glusterfs status # 查看遷移狀態
gluster volume remove-brick gv0 10.0.21.243:/data/glusterfs 10.0.21.244:/data/glusterfs commit # 遷移完成后提交
# 遷移卷
gluster peer probe 10.0.21.245 # 將10.0.21.246數據遷移到10.0.21.245先將10.0.21.245加入集群
gluster volume replace-brick gv0 10.0.21.246:/data/glusterfs 10.0.21.245:/data/glusterfs start # 開始遷移
gluster volume replace-brick gv0 10.0.21.246:/data/glusterfs 10.0.21.245:/data/glusterfs status # 查看遷移狀態
gluster volume replace-brick gv0 10.0.21.246:/data/glusterfs 10.0.21.245:/data/glusterfs commit # 數據遷移完畢后提交
gluster volume replace-brick gv0 10.0.21.246:/data/glusterfs 10.0.21.245:/data/glusterfs commit -force # 如果機器10.0.21.246出現故障已經不能運行,執行強制提交
gluster volume heal gv0 full # 同步整個卷
本文介紹的是GlusterFS最基礎的用法,感覺它的最大優點就是文件使用哈希散列,而不需要單獨去維護MetaData以避開單點問題,而目錄則是所有節點都一致的。節點信息會在變化過程中自動同步!不過增刪節點,需要做Rebalance數據才會重新分布。
C.在clinet上安裝客戶端軟件:
#yum -y install glusterfs glusterfs-fuse
GlusterFS客戶端連接
在客戶端client.zhaogang.int上使用mount命令,把服務器上新建的GlusterFS邏輯卷gv0掛載到本地目錄/mnt/glusterfs上:
[root@localhost ~]# mkdir /mnt/glusterfs [root@localhost ~]# mount -t glusterfs 10.0.21.241:/gv0 /mnt/glusterfs [root@localhost ~]#
確認掛載結果:
[root@localhost ~]# mount -t fuse.glusterfs 10.0.21.241:/gv0 on /mnt/glusterfs type fuse.glusterfs (rw,default_permissions,allow_other,max_read=131072)
如果希望系統重啟后可以自動掛載,在/etc/fstab文件中加入此行:
10.0.0.241:/data/gfsdata /mnt/glusterfs glusterfs defaults 0 0
客戶端測試連接
client端成功掛載邏輯卷之后,在掛載目錄/mnt/glusterfs建立文件以測試GlusterFS是否正常工作。
[root@localhost glusterfs]# cd /mnt/glusterfs/ [root@localhost glusterfs]# touch file1 file2 file3 [root@localhost glusterfs]# ls -l total 0 -rw-r--r--. 1 root root 0 Aug 29 21:07 file1 -rw-r--r--. 1 root root 0 Aug 29 21:07 file2 -rw-r--r--. 1 root root 0 Aug 29 21:07 file3
因為創建卷gv0的時候使用的是鏡像存儲(Replicated),所以在gv0上寫入的文件應該同時出現在兩個服務器的/data/gfsdata目錄上。
在server1和server2查看/data/gfsdata/目錄,可以看到兩個服務器均出現這三個文件:
[root@server1 ~]# cd /data/gfsdata/ [root@server1 gfsdata]# ls -l total 0 -rw-r--r--. 2 root root 0 Aug 29 21:07 file1 -rw-r--r--. 2 root root 0 Aug 29 21:07 file2 -rw-r--r--. 2 root root 0 Aug 29 21:07 file3 [root@server2 ~]# cd /data/gfsdata/ [root@server2 gfsdata]# ls -l total 0 -rw-r--r--. 2 root root 0 Aug 29 21:07 file1 -rw-r--r--. 2 root root 0 Aug 29 21:07 file2 -rw-r--r--. 2 root root 0 Aug 29 21:07 file3
自此GlusterFS快速安裝過程結束。