基於CentOS 8伺服器來搭建FastDFS高可用叢集環境
阿新 • • 發佈:2020-10-20
伺服器版本 我們在伺服器的命令列輸入如下命令來檢視伺服器的核心版本。 [root@localhost lib]# cat /etc/redhat-release CentOS Linux release 8.1.1911 (Core) 可以看到,叢集中每臺伺服器的核心版本為:release 8.1.1911 (Core)。 伺服器規劃 這裡,我們總共規劃了6臺伺服器,分別為:2臺tracker伺服器,4臺storage伺服器,其中2臺storage伺服器為group1,兩臺storage伺服器為group2。具體如下所示。 tracker伺服器 tranker1:192.168.175.101 tracker2:192.168.175.102 storage伺服器 storage1:192.168.175.103 group1 storage2:192.168.175.104 group1 storage3:192.168.175.105 group2 storage4:192.168.175.106 group2 環境準備 下載FastDFS 在每臺伺服器上執行如下命令下載FastDFS。 [root@localhost source]# wget https://github.com/happyfish100/fastdfs/archive/V6.06.tar.gz [root@localhost source]# wget https://github.com/happyfish100/fastdfs-nginx-module/archive/V1.22.tar.gz [root@localhost source]# wget https://github.com/happyfish100/libfastcommon/archive/V1.0.43.tar.gz 安裝環境依賴 在每臺伺服器上執行如下命令安裝FastDFS所依賴的環境。 [root@localhost dest]# yum install gcc gcc-c++ [root@localhost dest]# yum install libtool zlib zlib-devel openssl openssl-devel [root@localhost dest]# yum -y install pcre pcre-devel libevent libevent-devel perl unzip net-tools wget 安裝FastDFS 安裝libfastcommon 在每臺伺服器上依次執行如下命令。 (1)解壓libfastcommon的壓縮包 [root@localhost source]# tar -zxvf V1.0.43.tar.gz (2)編譯並安裝編譯並安裝 [root@localhost source]# cd libfastcommon-1.0.43/ [root@localhost libfastcommon-1.0.43]# ./make.sh && ./make.sh install (3)檢查執行的結果,看安裝是否成功 [root@localhost libfastcommon-1.0.43]# ls /usr/lib64|grep libfastcommon libfastcommon.so [root@localhost libfastcommon-1.0.43]# ls /usr/lib|grep libfastcommon libfastcommon.so 安裝fastdfs 在每臺伺服器上依次執行如下命令。 (1)解壓fastdfs [root@localhost source]# tar -zxvf V6.06.tar.gz (2)安裝fastdfs [root@localhost source]# cd fastdfs-6.06/ [root@localhost fastdfs-6.06]# ./make.sh && ./make.sh install (3)檢查fastdfs是否安裝成功 [root@localhost fastdfs-6.06]# ls /usr/bin|grep fdfs fdfs_appender_test fdfs_appender_test1 fdfs_append_file fdfs_crc32 fdfs_delete_file fdfs_download_file fdfs_file_info fdfs_monitor fdfs_regenerate_filename fdfs_storaged fdfs_test fdfs_test1 fdfs_trackerd fdfs_upload_appender fdfs_upload_file 安裝部署tracker服務 複製tracker的配置檔案 在兩臺tracker伺服器上,依次執行如下命令。 [root@localhost fastdfs-6.06]# cd /etc/fdfs/ [root@localhost fdfs]# cp client.conf.sample client.conf [root@localhost fdfs]# cp tracker.conf.sample tracker.conf 注意:無須生成storage.conf檔案,這兩臺tracker不做為storage。 安裝Nginx 在兩臺tracker伺服器上,依次執行如下命令。 注意:tracker上不需要安裝fastdfs-nginx-module (1)解壓Nginx [root@localhost source]# tar -zxvf nginx-1.17.8.tar.gz (2)nginx配置,http_stub_status_module 模組 [root@localhost fdfs]# cd /usr/local/source/nginx-1.17.8/ [root@localhost nginx-1.17.8]# ./configure --prefix=/usr/local/soft/nginx --with-http_stub_status_module (3)編譯安裝Nginx [root@localhost nginx-1.17.8]# make && make install (4)檢查安裝是否成功 [root@localhost nginx-1.17.8]# ls /usr/local/soft/ | grep nginx nginx (5)檢視指定的編譯引數是否起作用 [root@localhost fdfs]# /usr/local/soft/nginx/sbin/nginx -V nginx version: nginx/1.17.8 built by gcc 8.3.1 20190507 (Red Hat 8.3.1-4) (GCC) configure arguments: --prefix=/usr/local/soft/nginx --with-http_stub_status_module 配置並啟動FastDFS 在兩臺tracker上,配置並啟動FastDFS。 (1)建立tracker服務所需的目錄 [root@localhost fdfs]# mkdir /data/fastdfs [root@localhost fdfs]# mkdir /data/fastdfs/tracker [root@localhost fdfs]# chmod 777 /data/fastdfs/tracker (2)配置tracker服務,修改 tracker.conf 檔案 [root@localhost fdfs]# vi /etc/fdfs/tracker.conf 只修改base_path一項的值為我們在上面所建立的目錄即可 base_path = /data/fastdfs/tracker (3)啟動 tracker 服務 [root@localhost fdfs]# /etc/init.d/fdfs_trackerd start (4)檢查tracker服務啟動是否成功 [root@localhost fdfs]# ps auxfww | grep fdfs root 15067 0.0 0.0 12320 964 pts/0 S+ 15:14 0:00 | | \_ grep --color=auto fdfs root 15026 0.0 0.1 90160 5940 ? Sl 15:13 0:00 /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf 說明:能看到 fdfs_trackerd,表示tracker服務啟動成功。 (5)檢查tracker服務是否已繫結埠 22122 [root@localhost dest]# netstat -anp | grep 22122 tcp 0 0 0.0.0.0:22122 0.0.0.0:* LISTEN 15026/fdfs_trackerd 說明: 22122埠是在/etc/fdfs/tracker.conf中定義的,如下所示: # the tracker server port port = 22122 配置client.conf 兩臺tracker上,配置client.conf,配置fastdfs的客戶端使用的配置檔案。 (1)配置client.conf [root@localhost fdfs]# vi /etc/fdfs/client.conf 以下兩項配置用到的tracker目錄和伺服器地址埠 base_path = /data/fastdfs/tracker tracker_server = 192.168.175.101:22122 tracker_server = 192.168.175.102:22122 說明:兩臺tracker上的client.conf配置相同 (2)從客戶端的配置可以看到:客戶端只需要瞭解tracker_server的資訊。tracker server作用也正是負載均衡和排程 (3)Storage server作用是檔案儲存,客戶端上傳的檔案最終儲存在 Storage 服務上 測試檔案 用client.conf上傳檔案測試。 (1)從tacker上傳一個檔案 [root@0268c2dc2bf6 ~]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /root/a.txt group1/M00/00/00/rBEABF5aTEeAXHF4AAAABHf4XZU792.txt 注意返回的是group1,我們可以group1下面的兩臺機器均找到此txt檔案: storage1上 [root@d5d19e99e782 docker_tmp]# ls /data/fastdfs/storage/data/00/00 rBEABF5aTEeAXHF4AAAABHf4XZU792.txt storage2上 [root@f201111d0698 docker_tmp]# ls /data/fastdfs/storage/data/00/00 rBEABF5aTEeAXHF4AAAABHf4XZU792.txt (2)指定group上傳檔案 如果想指定上傳到某個group怎麼辦?例如:指定上傳到group2 [root@0268c2dc2bf6 ~]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /root/a.txt 192.168.175.105:23000 group2/M00/00/00/rBEABl5aUAqAXLCZAAAABHf4XZU043.txt 說明:指定group2中任一臺的ip和埠即可。 (3)檢視效果 storage3上 [root@494ac47d63f8 fdfs]# ls /data/fastdfs/storage/data/00/00 rBEABl5aUAqAXLCZAAAABHf4XZU043.txt storage4上 [root@59fa1efff362 fdfs]# ls /data/fastdfs/storage/data/00/00 rBEABl5aUAqAXLCZAAAABHf4XZU043.txt 安裝部署storage服務 生成預設配置檔案 四臺storage上:生成啟動fastdfs預設的配置檔案。 [root@localhost fastdfs-6.06]# cd /etc/fdfs/ [root@localhost fdfs]# cp storage.conf.sample storage.conf [root@localhost fdfs]# cp client.conf.sample client.conf 說明:不需要生成tracker.conf,因為storage上不再執行tracker服務 安裝Nginx 四臺storage上:安裝nginx及fastdfs-nginx-module (1)解壓nginx [root@localhost source]# tar -zxvf nginx-1.17.8.tar.gz (2)解壓fastdfs-nginx-module [root@localhost source]# tar -zxvf V1.22.tar.gz (3)修改config檔案,把/usr/local 替換成 /usr [root@localhost source]# cd fastdfs-nginx-module-1.22/ [root@localhost fastdfs-nginx-module-1.22]# cd src [root@localhost src]# vi config (4)Nginx配置,新增fastdfs-nginx-module和http_stub_status_module 模組 [root@localhost fdfs]# cd /usr/local/source/nginx-1.17.8/ [root@localhost nginx-1.17.8]# ./configure --prefix=/usr/local/soft/nginx --with-http_stub_status_module --add-module=/usr/local/source/fastdfs-nginx-module-1.22/src/ (5)編譯安裝nginx [root@localhost nginx-1.17.8]# make && make install (6)檢查安裝是否成功 [root@localhost nginx-1.17.8]# ls /usr/local/soft/ | grep nginx nginx (7)檢視指定的編譯引數是否起作用 [root@localhost fdfs]# /usr/local/soft/nginx/sbin/nginx -V nginx version: nginx/1.17.8 built by gcc 8.3.1 20190507 (Red Hat 8.3.1-4) (GCC) configure arguments: --prefix=/usr/local/soft/nginx --with-http_stub_status_module --add-module=/usr/local/source/fastdfs-nginx-module-1.22/src/ 配置並啟動storage服務 四臺storage上:配置並啟動storage服務 (1)建立storage服務所需的目錄 [root@localhost fdfs]# mkdir /data/fastdfs/storage [root@localhost fdfs]# chmod 777 /data/fastdfs/storage/ (2)配置storage服務 編輯storage的配置檔案: [root@localhost fdfs]# vi /etc/fdfs/storage.conf 各配置項包括: group_name = group1 #配置base_path為上面所建立的storage目錄 base_path = /data/fastdfs/storage #store_path :儲存所在的目錄,可以設定多個,注意從0開始 store_path0 = /data/fastdfs/storage #tracker_server的ip和埠 tracker_server = 192.168.175.101:22122 tracker_server = 192.168.175.102:22122 #指定http服務的埠 http.server_port = 80 配置的不同之處: 192.168.175.103 group_name = group1 192.168.175.104 group_name = group1 192.168.175.105 group_name = group2 192.168.175.106 group_name = group2 (3)啟動storage服務 [root@localhost fdfs]# /etc/init.d/fdfs_storaged start 正在啟動 fdfs_storaged (via systemctl): [ 確定 ] (4)檢查storage服務啟動是否成功 [root@localhost fdfs]# ps auxfww | grep fdfs root 15630 0.0 0.0 12320 972 pts/0 S+ 15:46 0:00 | | \_ grep --color=auto fdfs root 15026 0.0 0.1 155696 6964 ? Sl 15:13 0:00 /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf root 15573 2.7 1.7 150736 66292 ? Sl 15:45 0:02 /usr/bin/fdfs_storaged /etc/fdfs/storage.conf 說明:看到fdfs_storaged,表示storage服務啟動成功 (5)檢查storage服務是否已繫結到埠:23000 [root@localhost fdfs]# netstat -anp | grep 23000 tcp 0 0 0.0.0.0:23000 0.0.0.0:* LISTEN 15573/fdfs_storaged 說明:23000 埠是在配置檔案 /etc/fdfs/storage.conf中定義的,如下: # the storage server port port = 23000 配置fastdfs-nginx-module 四臺儲存伺服器上:配置fastdfs-nginx-module (1)生成配置檔案 [root@localhost nginx-1.17.8]# cp /usr/local/source/fastdfs-nginx-module-1.22/src/mod_fastdfs.conf /etc/fdfs/ (2)編輯配置檔案 [root@localhost nginx-1.17.8]# vi /etc/fdfs/mod_fastdfs.conf 配置以下幾項 group_name=group1 connect_timeout=10 tracker_server=192.168.175.101:22122 tracker_server=192.168.175.102:22122 url_have_group_name = true store_path0=/data/fastdfs/storage group_count = 2 [group1] group_name=group1 storage_server_port=23000 store_path_count=1 store_path0=/data/fastdfs/storage [group2] group_name=group2 storage_server_port=23000 store_path_count=1 store_path0=/data/fastdfs/storage 說明: 最上面的group_name:當機器屬於group1這組時,值為group1;當機器屬於group2這組時,值為group2。 說明:url_have_group_name = true。 注意:這一項不要漏掉,會導至nginx不正常工作 (3)複製另兩個web訪問用到配置檔案到fdfs配置目錄下: [root@d5d19e99e782 /]# cp /usr/local/source/fastdfs-6.06/conf/http.conf /etc/fdfs/ [root@d5d19e99e782 /]# cp /usr/local/source/fastdfs-6.06/conf/mime.types /etc/fdfs/ 配置Nginx 四臺儲存伺服器上:配置nginx 編輯nginx的配置檔案: [root@localhost conf]# vi /usr/local/soft/nginx/conf/nginx.conf 在server listen 80 的這個server配置下面, 增加一個location location ~/group([0-9]) { root /data/fastdfs/storage/data; ngx_fastdfs_module; } 啟動nginx (1)啟動Nginx [root@localhost storage]# /usr/local/soft/nginx/sbin/nginx (2)檢查nginx是否已成功啟動 [root@localhost storage]# ps auxfww | grep nginx root 24590 0.0 0.0 12320 980 pts/0 S+ 16:44 0:00 | | \_ grep --color=auto nginx root 24568 0.0 0.0 41044 428 ? Ss 16:44 0:00 \_ nginx: master process /usr/local/soft/nginx/sbin/nginx nobody 24569 0.0 0.1 74516 4940 ? S 16:44 0:00 \_ nginx: worker process 配置tracker服務 配置tracker服務 說明:這一步等待四臺storage server配置完成後再進行。使用n=Nginx做upstream負載均衡的原因:可以通過一個地址訪問後端的多個group (1)檔案上傳完成後,從瀏覽器訪問各個storage的Nginx即可: 例如: http://192.168.175.103/group1/M00/00/00/rBEABF5aTRiAEuHwAAAABHf4XZU322.txt http://192.168.175.104/group1/M00/00/00/rBEABF5aTRiAEuHwAAAABHf4XZU322.txt http://192.168.175.105/group2/M00/00/00/rBEABl5aUAqAXLCZAAAABHf4XZU043.txt http://192.168.175.106/group2/M00/00/00/rBEABl5aUAqAXLCZAAAABHf4XZU043.txt 說明:各臺storage server的ip地址後面跟著上傳時所返回的地址。注意:只能訪問各臺機器所在的group, 如果想通過統一的ip地址進行訪問 需要在Nginx中通過upstream訪問到後端的機器 此Nginx應執行在tracker上 (2)配置nginx.conf [root@0268c2dc2bf6 ~]# vi /usr/local/soft/nginx/conf/nginx.conf 內容: 新增 upstream到後端的storage。 upstream fdfs_group1 { server 192.168.175.103:80 weight=1 max_fails=2 fail_timeout=30s; server 192.168.175.104:80 weight=1 max_fails=2 fail_timeout=30s; } upstream fdfs_group2 { server 192.168.175.105:80 weight=1 max_fails=2 fail_timeout=30s; server 192.168.175.106:80 weight=1 max_fails=2 fail_timeout=30s; } 針對帶有group的url進行處理 location /group1 { proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_pass http://fdfs_group1; expires 30d; } location /group2 { proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_pass http://fdfs_group2; expires 30d; } (3)重啟測試 [root@0268c2dc2bf6 ~]# /usr/local/soft/nginx/sbin/nginx -s stop [root@0268c2dc2bf6 ~]# /usr/local/soft/nginx/sbin/nginx 在瀏覽器中訪問: http://192.168.175.101/group1/M00/00/00/rBEABF5aTRiAEuHwAAAABHf4XZU322.txt http://192.168.175.101/group2/M00/00/00/rBEABl5aUAqAXLCZAAAABHf4XZU043.txt