軟件下載
# 已經事先把所需軟件下載好並上傳到/usr/local/src目錄了
https://github.com/happyfish100/libfastcommon/archive/V1.0.43.tar.gz
https://github.com/happyfish100/fastdfs-nginx-module/archive/V1.22.tar.gz
https://github.com/happyfish100/fastdfs/archive/V6.06.tar.gz
https://github.com/happyfish100/fastdfs-client-java/archive/V1.28.tar.gz
https://openresty.org/download/openresty-1.15.8.3.tar.gz
基礎環境設置
安裝依賴組件
yum -y install gcc gcc-c++ libevent
yum -y groupinstall 'Development Tools'
安裝libfastcommon
cd /usr/local/src
tar -zxvf libfastcommon-1.0.43.tar.gz
cd libfastcommon-1.0.43
./make.sh
./make.sh install
# 檢查文件是否存在,確保在/usr/lib路徑下有libfastcommon.so,沒有的話創建超鏈接或者復制一份
ll /usr/lib | grep "libf"
lrwxrwxrwx 1 root root 27 Apr 2 10:07 libfastcommon.so -> /usr/lib64/libfastcommon.so
安裝fastdfs
cd /usr/local/src
tar -zxvf fastdfs-6.06.tar.gz
cd fastdfs-6.06
./make.sh
./make.sh install
# FastDFS的配置文件默認安裝到/etc/fdfs目錄下
# 安裝成功后將fastdfs-6.06/conf下的倆文件拷貝到/etc/fdfs/下
cd conf
cp http.conf mime.types /etc/fdfs/
cd /etc/fdfs/
[root@bogon fdfs]# ll
total 68
-rw-r--r-- 1 root root 1909 Apr 2 10:15 client.conf.sample
-rw-r--r-- 1 root root 965 Apr 2 10:16 http.conf
-rw-r--r-- 1 root root 31172 Apr 2 10:16 mime.types
-rw-r--r-- 1 root root 10246 Apr 2 10:15 storage.conf.sample
-rw-r--r-- 1 root root 620 Apr 2 10:15 storage_ids.conf.sample
-rw-r--r-- 1 root root 9138 Apr 2 10:15 tracker.conf.sample
fdfs_trackerd配置並啟動
# 創建tracker工作目錄,storage存儲目錄(選擇大磁盤空間)等
mkdir -p /opt/{fdfs_tracker,fdfs_storage,fdfs_storage_data}
cd /etc/fdfs/
cp tracker.conf.sample tracker.conf
vim tracker.conf
disabled = false # 配置tracker.conf這個配置文件是否生效,因為在啟動fastdfs服務端進程時需要指定配置文件,所以需要使次配置文件生效。false是生效,true是屏蔽。
bind_addr = # 程序的監聽地址,如果不設定則監聽所有地址,可以設置本地ip地址,注意,不能設置為127.0.0.1,否則storage注冊時會報錯:ERROR - file: storage_func.c, line: 1361, conf file "/etc/fdfs/storage.conf", tracker: "127.0.0.1:22122" is invalid, tracker server ip can't be 127.0.0.1
port = 22122 #tracker監聽的端口
base_path = /opt/fdfs_tracker # tracker保存data和logs的路徑
http.server_port=8080 # http服務端口,保持默認
# 啟動fdfs_trackerd
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start
# 查看/opt/fdfs_tracker目錄,發現目錄下多了data和logs兩個目錄
# 查看端口號,驗證啟動情況
[root@bogon fdfs]# ps -ef | grep fdfs
root 2119 1 0 10:22 ? 00:00:00 /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start
[root@bogon fdfs]# ss -tulnp | grep 22122
tcp LISTEN 0 128 *:22122 *:* users:(("fdfs_trackerd",pid=2119,fd=5))
# 命令行選項
Usage: /usr/bin/fdfs_trackerd <config_file> [start|stop|restart]
# 設置開機自啟動
echo "/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart" | tee -a /etc/rc.d/rc.local
fdfs_storage配置並啟動
與tracker不同的是,storage還需要一個目錄用來存儲數據,所以在上面步驟中另外多建了兩個目錄fdfs_storage_data,fdfs_storage
cd /etc/fdfs/
cp storage.conf.sample storage.conf
vim storage.conf
disabled=false # 啟用這個配置文件
group_name=group1 #組名,根據實際情況修改,文件鏈接中會用到
port=23000 #設置storage的端口號,默認是23000,同一個組的storage端口號必須一致
base_path = /opt/fdfs_storage # #設置storage數據文件和日志目錄,注意,這個目錄最好有大於50G的磁盤空間
store_path_count=1 #存儲路徑個數,需要和store_path個數匹配
store_path0 = /opt/fdfs_storage_data # 實際保存文件的路徑,注意,這個目錄最好有大於50G的磁盤空間
tracker_server = 192.168.75.5:22122 # tracker監聽地址和端口號,要與tracker.conf文件中設置的保持一致
http.server_port=8888 #設置 http 端口號
# 啟動fdfs_storaged
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf start
# 查看端口號,驗證啟動情況
[root@bogon fdfs]# ps -ef | grep "fdfs_storaged"
root 2194 1 7 10:36 ? 00:00:01 /usr/bin/fdfs_storaged /etc/fdfs/storage.conf start
[root@bogon fdfs]# ss -tulnp | grep "fdfs"
tcp LISTEN 0 128 *:23000 *:* users:(("fdfs_storaged",pid=2194,fd=5))
tcp LISTEN 0 128 *:22122 *:* users:(("fdfs_trackerd",pid=2119,fd=5))
# 命令行選項
Usage: /usr/bin/fdfs_trackerd <config_file> [start|stop|restart]
# 設置開機自啟動
echo "/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart" | tee -a /etc/rc.d/rc.local
校驗整合
要確定一下,storage是否注冊到了tracker中去
/usr/bin/fdfs_monitor /etc/fdfs/storage.conf
成功后可以看到:ip_addr = 192.168.75.5 ACTIVE
使用FastDFS自帶工具測試
cd /etc/fdfs/
cp client.conf.sample client.conf
vim client.conf
base_path = /opt/fdfs_tracker # tracker服務器文件路徑
tracker_server = 192.168.75.5:22122 #tracker服務器IP地址和端口號
http.tracker_server_port = 8080 # tracker服務器的http端口號,必須和tracker的設置對應起來
上傳一張圖片1.jpg到Centos服務器上的 /tmp 目錄下,進行測試,命令如下:
/usr/bin/fdfs_test /etc/fdfs/client.conf upload /tmp/1.jpg
This is FastDFS client test program v6.06
Copyright (C) 2008, Happy Fish / YuQing
FastDFS may be copied only under the terms of the GNU General
Public License V3, which may be found in the FastDFS source kit.
Please visit the FastDFS Home Page http://www.fastken.com/
for more detail.
[2020-04-02 10:47:57] DEBUG - base_path=/opt/fdfs_tracker, connect_timeout=5, network_timeout=60, tracker_server_count=1, anti_steal_token=0, anti_steal_secret_key length=0, use_connection_pool=0, g_connection_pool_max_idle_time=3600s, use_storage_id=0, storage server id count: 0
tracker_query_storage_store_list_without_group:
server 1. group_name=, ip_addr=192.168.75.5, port=23000
group_name=group1, ip_addr=192.168.75.5, port=23000
storage_upload_by_filename
group_name=group1, remote_filename=M00/00/00/wKhLBV6FUl6AA0eTACSnIi51C7k037.jpg
source ip address: 192.168.75.5
file timestamp=2020-04-02 10:47:58
file size=2402082
file crc32=779422649
example file url: http://192.168.75.5:8080/group1/M00/00/00/wKhLBV6FUl6AA0eTACSnIi51C7k037.jpg
storage_upload_slave_by_filename
group_name=group1, remote_filename=M00/00/00/wKhLBV6FUl6AA0eTACSnIi51C7k037_big.jpg
source ip address: 192.168.75.5
file timestamp=2020-04-02 10:47:58
file size=2402082
file crc32=779422649
example file url: http://192.168.75.5:8080/group1/M00/00/00/wKhLBV6FUl6AA0eTACSnIi51C7k037_big.jpg
組名:group1
磁盤:M00
目錄:00/00
文件名稱:wKhLBV6FUl6AA0eTACSnIi51C7k037.jpg
注意圖片路徑中的8080端口,這個是tracker的端口
上傳的圖片會被上傳到我們創建的fdfs_storage_data目錄下,會有四個圖片文件:
[root@bogon 00]# pwd
/opt/fdfs_storage_data/data/00/00
[root@bogon 00]# ll
total 4704
-rw-r--r-- 1 root root 2402082 Apr 2 10:47 wKhLBV6FUl6AA0eTACSnIi51C7k037_big.jpg
-rw-r--r-- 1 root root 49 Apr 2 10:47 wKhLBV6FUl6AA0eTACSnIi51C7k037_big.jpg-m
-rw-r--r-- 1 root root 2402082 Apr 2 10:47 wKhLBV6FUl6AA0eTACSnIi51C7k037.jpg
-rw-r--r-- 1 root root 49 Apr 2 10:47 wKhLBV6FUl6AA0eTACSnIi51C7k037.jpg-m
data下有256個1級目錄,每級目錄下又有256個2級子目錄,總共65536個文件,新寫的文件會以hash的方式被路由到其中某個子目錄下,然后將文件數據直接作為一個本地文件存儲到該目錄中。
FastDFS和nginx結合使用
FastDFS通過Tracker服務器,將文件放在Storage服務器存儲,但是同組之間的服務器需要復制文件,有延遲的問題.
假設Tracker服務器將文件上傳到了172.20.132.57,文件ID已經返回客戶端,這時,后台會將這個文件復制到172.20.132.57,如果復制沒有完成,客戶端就用這個ID在172.20.132.57取文件,肯定會出現錯誤。
這個fastdfs-nginx-module可以重定向連接到源服務器取文件,避免客戶端由於復制延遲的問題,出現錯誤。
正是這樣,FastDFS需要結合nginx,所以取消原來對HTTP的直接支持。
在tracker上安裝 nginx
在每個tracker上安裝nginx的主要目的是做負載均衡及實現高可用。如果只有一台tracker服務器可以不配置nginx.
一個tracker對應多個storage,通過nginx對storage負載均衡;
在storage上安裝nginx(openresty)
cd /usr/local/src/
tar -zxvf fastdfs-nginx-module-1.22.tar.gz
cd fastdfs-nginx-module-1.22/src
cp mod_fastdfs.conf /etc/fdfs/
vim /etc/fdfs/mod_fastdfs.conf
base_path=/opt/fdfs_storage # 與storage.conf配置中的保持一致
tracker_server=192.168.75.5:22122 #tracker服務器的IP地址以及端口號
url_have_group_name = true # url中包含group名稱
store_path0=/opt/fdfs_storage_data #與storage.conf中的路徑保持一致
group_count = 1 #設置組的個數
yum -y install pcre pcre-devel openssl openssl-devel zlib zlib-devel
cd /usr/local/src
tar -zxvf openresty-1.15.8.3.tar.gz
cd openresty-1.15.8.3
./configure \
--with-luajit \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_gzip_static_module \
--add-module=/usr/local/src/fastdfs-nginx-module-1.22/src
gmake
gmake install
# 修改配置文件
vim /usr/local/openresty/nginx/conf/nginx.conf
error_log logs/error.log;
pid logs/nginx.pid;
server{
server_name 192.168.75.5;
}
# 啟動
/usr/local/openresty/nginx/sbin/nginx -c /usr/local/openresty/nginx/conf/nginx.conf
# 瀏覽器訪問,出現openresty歡迎頁面
# 設置nginx開機啟動
echo "/usr/local/openresty/nginx/sbin/nginx -c /usr/local/openresty/nginx/conf/nginx.conf" | tee -a /etc/rc.d/rc.local
# 再次修改配置文件,加載fastdfs模塊
vim /usr/local/openresty/nginx/conf/nginx.conf
server{
location /group1/M00/ {
root /opt/fdfs_storage/data;
ngx_fastdfs_module;
}
}
# 重載nginx
/usr/local/openresty/nginx/sbin/nginx -s reload
# 參考上面測試的那一步圖片url地址:http://192.168.75.5:8080/group1/M00/00/00/wKhLBV6FUl6AA0eTACSnIi51C7k037.jpg
使用nginxf訪問的話,實際地址是:http://192.168.75.5/group1/M00/00/00/wKhLBV6FUl6AA0eTACSnIi51C7k037.jpg
需要把tracker使用的8080端口去掉,否則無法訪問
# 進一步完善nginx配置文件
# 這個server設置的是storage nginx
server {
listen 9991;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location ~/group1/M00 {
root /opt/fastdfs_storage/data;
ngx_fastdfs_module;
}
location = /50x.html {
root html;
}
}
# 若訪問不到圖片需要配置這個軟連接
# ln -s /opt/fastdfs_storage_data/data/ /opt/fastdfs_storage_data/data/M00
# 這個server設置的是tracker nginx
upstream fdfs_group1 {
server 127.0.0.1:9991;
}
server {
listen 80;
server_name localhost;
location /group1/M00 {
proxy_pass http://fdfs_group1;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
搭建集群
集群規划(單tracker,雙storage)
虛擬機 IP 說明
tracker 192.168.75.5 tracker 服務器
storage01 192.168.75.6 storage01服務器【group1】
storage02 192.168.75.7 storage02服務器【group2】
軟件清單
fastdfs-6.06.tar.gz
fastdfs-client-java-1.28.tar.gz
fastdfs-nginx-module-1.22.tar.gz
libfastcommon-1.0.43.tar.gz
openresty-1.15.8.3.tar.gz
安裝步驟
1.tracker服務器
# 1. 安裝libfastcommon 模塊
# 2. 編譯安裝 FastDFS
# 3. 修改配置文件tarcker.conf和client.conf(測試上傳)
# vim /etc/fdfs/tracker.conf
store_lookup=0 #采用輪詢策略進行存儲,0:輪詢 1:始終定向到某個group 2:選擇存儲空間最大的進行存儲
# 4. 開機啟動
2.storage服務器
# 1. 安裝libfastcommon 模塊
# 2. 編譯安裝 FastDFS
# 3. 修改配置文件storage.conf
# storage01 配置
# vim /etc/fdfs/storage.conf
group_name=group1
base_path=/home/fastdfs_storage
store_path0=/home/fastdfs_storage
tracker_server=192.168.75.6:22122
http.server_port=8888
# storage02 配置
# vim /etc/fdfs/storage.conf
group_name=group2
base_path=/home/fastdfs_storage
store_path0=/home/fastdfs_storage
tracker_server=192.168.75.7:22122
http.server_port=8888
# 4. 開機啟動
# 5. 安裝nginx和fastdfs-nginx-module模塊
# storage01 配置:
# vim /etc/fdfs/mod_fastdfs.conf
connect_timeout=10
base_path=/home/fastdfs_storage
url_have_group_name=true
store_path0=/home/fastdfs_storage
tracker_server=192.168.75.6:22122
group_name=group1
# storage02 配置:
# vim /etc/fdfs/mod_fastdfs.conf
connect_timeout=10
base_path=/home/fastdfs_storage
url_have_group_name=true
store_path0=/home/fastdfs_storage
tracker_server=192.168.75.7:22122
group_name=group2
# 6. 復制 FastDFS 安裝目錄的部分配置文件到 /etc/fdfs 目錄
cp http.conf mime.types /etc/fdfs/
# 7. 配置nginx
server {
listen 8888;
server_name localhost;
location ~/group([0-9])/M00 {
ngx_fastdfs_module;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
3.測試
# vim /etc/fdfs/client.conf
base_path=/home/fastdfs_tracker
tracker_server=192.168.75.5:22122
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf test.jpg
4. tracker安裝nginx
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#group1
upstream fdfs_group1 {
server 192.168.75.6:8888;
}
#group2
upstream fdfs_group2 {
server 192.168.75.7:8888;
}
server {
listen 8000;
server_name localhost;
location /group1/M00 {
proxy_pass http://fdfs_group1;
}
location /group2/M00 {
proxy_pass http://fdfs_group2;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}