FastDFS介紹和配置過程

因爲網站使用nfs共享方式保存用戶上傳的圖片,附件等資料,而後經過apache下載的方式供用戶訪問,在網站架構初期,使用這種簡單的方式實現了靜態資源的讀寫分離,但隨着網站數據量的增長,圖片服務器漸漸成爲整個網站的短板,緣次催生了使用fastfds的想法,故而先進行一番簡單的測試!在開始以前仍是先來看看fastfds的介紹信息: html

FastDFS is an open source high performance distributed file system (DFS). It's major functions include: file storing, file syncing and file accessing, and design for high capacity and load balancing. git

FastDFS is an open source high performance distributed file system. It's major functions include: file storing, file syncing and file accessing (file uploading and file downloading), and it can resolve the high capacity and load balancing problem. FastDFS should meet the requirement of the website whose service based on files such as photo sharing site and vidio sharing site. github

FastDFS has two roles: tracker and storage. The tracker takes charge of scheduling and load balancing for file access. The storage store files and it's function is file management including: file storing, file syncing, providing file access interface. It also manage the meta data which are attributes representing as key value pair of the file. For example: width=1024, the key is "width" and the value is "1024". web

The tracker and storage contain one or more servers. The servers in the tracker or storage cluster can be added to or removed from the cluster by any time without affecting the online services. The servers in the tracker cluster are peer to peer.apache

The storarge servers organizing by the file volume/group to obtain high capacity. The storage system contains one or more volumes whose files are independent among these volumes. The capacity of the whole storage system equals to the sum of all volumes' capacity. A file volume contains one or more storage servers whose files are same among these servers. The servers in a file volume backup each other, and all these servers are load balancing. When adding a storage server to a volume, files already existing in this volume are replicated to this new server automatically, and when this replication done, system will switch this server online to providing storage services. When the whole storage capacity is insufficiency, you can add one or more volumes to expand the storage capacity. To do this, you need to add one or more storage servers. 服務器

The identification of a file is composed of two parts: the volume name and the file name. 架構

大意爲:
fastdfs是一個開源的,高性能的的分佈式文件系統,他主要的功能包括:文件存儲,同步和訪問,設計基於高可用和負載均衡,fastfd很是適用於基於文件服務的站點,例如圖片分享和視頻分享網站app

fastfds有兩個角色:跟蹤服務和存儲服務,跟蹤服務控制,調度文件以負載均衡的方式訪問;存儲服務包括:文件存儲,文件同步,提供文件訪問接口,同時以key value的方式管理文件的元數據負載均衡

跟蹤和存儲服務能夠由1臺或者多臺服務器組成,同時能夠動態的添加,刪除跟蹤和存儲服務而不會對在線的服務產生影響,在集羣中,tracker服務是對等的dom

存儲系統由一個或多個卷組成,卷與卷之間的文件是相互獨立的,全部卷的文件容量累加就是整個存儲系統中的文件容量。一個卷能夠由一臺或多臺存儲服務器組成,一個卷下的存儲服務器中的文件都是相同的,卷中的多臺存儲服務器起到了冗餘備份和負載均衡的做用。在卷中增長服務器時,同步已有的文件由系統自動完成,同步完成後,系統自動將新增服務器切換到線上提供服務。當存儲空間不足或即將耗盡時,能夠動態添加捲。只須要增長一臺或多臺服務器,並將它們配置爲一個新的卷,這樣就擴大了存儲系統的容量。

下面幾張圖能夠清楚的說明fastfds的架構和文件上傳和下載流程等:

下面將介紹下fastdfs在rhel上的部署過程
tracker服務器:192.168.123.110/24
storage服務器:192.168.123.20/24

一:編譯安裝

  
  
           
  
  
  1. [root@db1 ~]# wget http://cloud.github.com/downloads/libevent/libevent/libevent-2.0.14  
  2. -stable.tar.gz  
  3. [root@db1 ~]# tar -zxvpf libevent-2.0.14-stable.tar.gz   
  4. [root@db1 ~]# cd libevent-2.0.14-stable  
  5. [root@db1 libevent-2.0.14-stable]# ./configure --prefix=/usr/local/libevent-2.0.14 &&   
  6. make && make install  
  7. [root@db1 ~]# wget http://fastdfs.googlecode.com/files/FastDFS_v3.02.tar.gz  
  8. [root@db1 ~]# tar -zxvf FastDFS_v3.02.tar.gz   
  9. [root@db1 ~]# cd FastDFS  
  10. [root@db1 FastDFS]# grep -A 4 '/usr/local/FastDFS' make.sh   
  11. TARGET_PREFIX=/usr/local/FastDFS  
  12. TARGET_CONF_PATH=/etc/fdfs  
  13. WITH_HTTPD=1 
  14. WITH_LINUX_SERVICE=1 
  15. [root@db1 FastDFS]# ./make.sh C_INCLUDE_PATH=/usr/local/libevent-2.0.14/include    
  16. LIBRARY_PATH=/usr/local/libevent-2.0.14/lib  
  17. [root@db1 FastDFS]# ./make.sh install  
  18. [root@db1 FastDFS]# ls /etc/fdfs/  
  19. client.conf  http.conf  mime.types  storage.conf  tracker.conf 

二:tracker配置文件

  
  
           
  
  
  1. [root@db1 ~]# grep -v '^#' /etc/fdfs/tracker.conf |grep -v '^$'  
  2. disabled=false 
  3. bind_addr=192.168.123.110  
  4. port=22122 
  5. connect_timeout=30 
  6. network_timeout=60 
  7. base_path=/home/data/fastdfs  
  8. max_connections=256 
  9. work_threads=4 
  10. store_lookup=2 
  11. store_group=group2 
  12. store_server=0 
  13. store_path=0 
  14. download_server=0 
  15. reserved_storage_space = 4GB 
  16. log_level=info 
  17. run_by_group=  
  18. run_by_user=  
  19. allow_hosts=*  
  20. sync_log_buff_interval = 10 
  21. check_active_interval = 120 
  22. thread_stack_size = 64KB 
  23. storage_ip_changed_auto_adjust = true 
  24. storage_sync_file_max_delay = 86400 
  25. storage_sync_file_max_time = 300 
  26. use_trunk_file = false   
  27. slot_min_size = 256 
  28. slot_max_size = 16MB 
  29. trunk_file_size = 64MB 
  30. http.disabled=false 
  31. http.server_port=8080 
  32. http.check_alive_interval=30 
  33. http.check_alive_type=tcp 
  34. http.check_alive_uri=/status.html  
  35. http.need_find_content_type=true 
  36.  
  37. [root@db1 ~]# grep -v '^#' /etc/fdfs/http.conf  |grep -v '^$'  
  38. http.default_content_type = application/octet-stream  
  39. http.mime_types_filename=/etc/fdfs/mime.types  
  40. http.anti_steal.check_token=false 
  41. http.anti_steal.token_ttl=900 
  42. http.anti_steal.secret_key=FastDFS1234567890 
  43. http.anti_steal.token_check_fail=/home/data/fastdfs/conf/anti-steal.jpg 

三:啓動tracker服務,須要注意tracker.conf文件最後一行爲#include httpd.conf

  
  
           
  
  
  1. [root@db1 ~]# /usr/local/FastDFS/bin/fdfs_trackerd /etc/fdfs/tracker.conf   
  2. /usr/local/FastDFS/bin/fdfs_trackerd: error while loading shared libraries: libevent-  
  3. 2.0.so.5: cannot open shared object file: No such file or directory  
  4. [root@db1 ~]# echo '/usr/local/libevent-2.0.14/include/' >> /etc/ld.so.conf  
  5. [root@db1 ~]# echo '/usr/local/libevent-2.0.14/lib/' >> /etc/ld.so.conf  
  6. [root@db1 ~]# ldconfig  
  7. [root@db1 ~]# /usr/local/FastDFS/bin/fdfs_trackerd /etc/fdfs/tracker.conf   
  8. [2012-07-04 17:52:25] ERROR - file: tracker_func.c, line: 160, "/home/data/fastdfs"   
  9. can't be accessed, error info: No such file or directory  
  10. [root@db1 ~]# mkdir -p /home/data/fastdfs  
  11. [root@db1 ~]# /usr/local/FastDFS/bin/fdfs_trackerd /etc/fdfs/tracker.conf   
  12. [root@db1 ~]# echo $?  
  13. 22  
  14. [root@db1 ~]# cat  /home/data/fastdfs/logs/trackerd.log   
  15. [2012-07-04 17:52:50] ERROR - file: ../common/fdfs_http_shared.c, line: 128, param   
  16. "http.mime_types_filename" not exist or is empty  
  17. [root@db1 ~]# tail -1 /etc/fdfs/tracker.conf   
  18. #include http.conf  
  19. [root@db1 ~]# /usr/local/FastDFS/bin/fdfs_trackerd /etc/fdfs/tracker.conf   
  20. [root@db1 ~]# echo $?  
  21. 0  
  22. [root@db1 ~]# ps -ef |grep track  
  23. root      3535     1  0 15:47 ?        00:00:00 /usr/local/FastDFS/bin/fdfs_trackerd   
  24. /etc/fdfs/tracker.conf  
  25.  
  26. [root@db1 ~]# netstat -ntpl |grep fdfs  
  27. tcp        0      0 192.168.123.110:22122       0.0.0.0:*                   LISTEN        
  28. 3535/fdfs_trackerd    
  29. tcp        0      0 192.168.123.110:8080        0.0.0.0:*                   LISTEN        
  30. 3535/fdfs_trackerd  

四:storage配置文件

  
  
           
  
  
  1. [root@db2 ~]# grep -v '^#' /etc/fdfs/storage.conf |grep -v '^$'  
  2. disabled=false 
  3. group_name=group1 
  4. bind_addr=192.168.123.20  
  5. client_bind=true 
  6. port=23000 
  7. connect_timeout=30 
  8. network_timeout=60 
  9. heart_beat_interval=30 
  10. stat_report_interval=60 
  11. base_path=/home/data/fastdfs  
  12. max_connections=256 
  13. buff_size = 256KB 
  14. work_threads=4 
  15. disk_rw_separated = true 
  16. disk_rw_direct = false 
  17. disk_reader_threads = 1 
  18. disk_writer_threads = 1 
  19. sync_wait_msec=50 
  20. sync_interval=0 
  21. sync_start_time=00:00  
  22. sync_end_time=23:59  
  23. write_mark_file_freq=500 
  24. store_path_count=1 
  25. store_path0=/home/data/fastdfs  
  26. subdir_count_per_path=256 
  27. tracker_server=192.168.123.110:22122  
  28. log_level=info 
  29. run_by_group=  
  30. run_by_user=  
  31. allow_hosts=*  
  32. file_distribute_path_mode=0 
  33. file_distribute_rotate_count=100 
  34. fsync_after_written_bytes=0 
  35. sync_log_buff_interval=10 
  36. sync_binlog_buff_interval=10 
  37. sync_stat_file_interval=300 
  38. thread_stack_size=512KB 
  39. upload_priority=10 
  40. if_alias_prefix=  
  41. check_file_duplicate=0  
  42. key_namespace=FastDFS 
  43. keep_alive=0 
  44. http.disabled=false 
  45. httphttp.domain_name=  
  46. http.server_port=8888 
  47. http.trunk_size=256KB 
  48. http.need_find_content_type=true 
  49.  
  50. [root@db2 ~]# grep -v '^#' /etc/fdfs/client.conf  |grep -v '^$'  
  51. connect_timeout=30 
  52. network_timeout=60 
  53. base_path=/home/data/fastdfs  
  54. tracker_server=192.168.123.110:22122  
  55. log_level=info 
  56. http.tracker_server_port=8080 
  57.  
  58. [root@db2 ~]#  grep -v '^#' /etc/fdfs/http.conf |grep -v '^$'  
  59. http.default_content_type = application/octet-stream  
  60. http.mime_types_filename=mime.types  
  61. http.anti_steal.check_token=false 
  62. http.anti_steal.token_ttl=900 
  63. http.anti_steal.secret_key=FastDFS1234567890 
  64. http.anti_steal.token_check_fail=/home/data/fastdfs/conf/anti-steal.jpg 

五:啓動storage,須要注意storage.conf文件最後一行爲#include httpd.conf

  
  
           
  
  
  1. [root@db2 ~]# mkdir -p /home/data/fastdfs  
  2. [root@db2 ~]# echo '/usr/local/libevent-2.0.14/include/' >> /etc/ld.so.conf  
  3. [root@db2 ~]# echo '/usr/local/libevent-2.0.14/lib/' >> /etc/ld.so.conf  
  4. [root@db2 ~]# ldconfig  
  5.  
  6. [root@db2 ~]# tail -2 /etc/fdfs/storage.conf   
  7. #use "#include" directive to include HTTP other settings  
  8. #include http.conf  
  9.  
  10. [root@db2 ~]# /usr/local/FastFDS/bin/fdfs_storaged /etc/fdfs/storage.conf   
  11. mkdir data path: 00 ...  
  12. mkdir data path: 01 ...  
  13. mkdir data path: 02 ...  
  14. ———輸出省略————  
  15. data path: /home/data/fastdfs/data, mkdir sub dir done.  
  16.  
  17. [root@db2 ~]# ps -ef |grep fdfs  
  18. root     14451     1  0 16:15 ?        00:00:00 /usr/local/FastFDS/bin/fdfs_storaged   
  19. /etc/fdfs/storage.conf  
  20. root     14468  8238  0 16:16 pts/1    00:00:00 grep fdfs  
  21.  
  22. [root@db2 ~]# netstat -ntpl |grep fdfs  
  23. tcp        0      0 192.168.123.20:8888         0.0.0.0:*                   LISTEN        
  24. 14451/fdfs_storaged   
  25. tcp        0      0 192.168.123.20:23000        0.0.0.0:*                   LISTEN        
  26. 14451/fdfs_storaged  

五:測試文件上傳

 [root@db2 ~]# /usr/local/FastFDS/bin/fdfs_test /etc/fdfs/client.conf upload /etc/passwd
This is FastDFS client test program v3.02
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.csource.org/
for more detail.

[2012-07-30 16:25:59] INFO - base_path=/home/data/fastdfs, connect_timeout=30,

network_timeout=60, tracker_server_count=1, anti_steal_token=0, anti_steal_secret_key length=0 tracker_query_storage_store_list_without_group: 
server 1. group_name=group1, ip_addr=192.168.123.20, port=23000
group_name=group1, ip_addr=192.168.123.20, port=23000
storage_upload_by_filename
group_name=group1, remote_filename=M00/00/00/wKh7FFAWRRfcMOGtAAAHKBGhZhE8065783
source ip address: 192.168.123.20
file timestamp=2012-07-30 16:25:59
file size=1832
file crc32=295790097
file url:
http://192.168.123.110:8080/group1/M00/00/00/wKh7FFAWRRfcMOGtAAAHKBGhZhE8065783
storage_upload_slave_by_filename
group_name=group1, remote_filename=M00/00/00/wKh7FFAWRRfcMOGtAAAHKBGhZhE8065783_big
source ip address: 192.168.123.20
file timestamp=2012-07-30 16:25:59
file size=1832
file crc32=295790097
file url:
http://192.168.123.110:8080/group1/M00/00/00/wKh7FFAWRRfcMOGtAAAHKBGhZhE8065783_big

[root@db2 ~]# /usr/local/FastFDS/bin/fdfs_test /etc/fdfs/client.conf upload 2.jpg
This is FastDFS client test program v3.02
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.csource.org/
for more detail.

[2012-07-30 16:29:22] INFO - base_path=/home/data/fastdfs, connect_timeout=30, network_timeout=60, tracker_server_count=1, anti_steal_token=0, anti_steal_secret_key length=0 tracker_query_storage_store_list_without_group: 
server 1. group_name=group1, ip_addr=192.168.123.20, port=23000
group_name=group1, ip_addr=192.168.123.20, port=23000
storage_upload_by_filename
group_name=group1, remote_filename=M00/00/00/wKh7FFAWReLzPs4sAAEYquSDGwc472.jpg
source ip address: 192.168.123.20
file timestamp=2012-07-30 16:29:22
file size=71850
file crc32=3833797383
file url:
http://192.168.123.110:8080/group1/M00/00/00/wKh7FFAWReLzPs4sAAEYquSDGwc472.jpg
storage_upload_slave_by_filename
group_name=group1, remote_filename=M00/00/00/wKh7FFAWReLzPs4sAAEYquSDGwc472_big.jpg
source ip address: 192.168.123.20
file timestamp=2012-07-30 16:29:22
file size=71850
file crc32=3833797383
file url:
http://192.168.123.110:8080/group1/M00/00/00/wKh7FFAWReLzPs4sAAEYquSDGwc472_big.jpg

 

相關文章
相關標籤/搜索