1. 程式人生 > >FastDFS集群tracker實現負載均衡

FastDFS集群tracker實現負載均衡

fastdfs

簡單介紹:


FastDFS作者余慶,是一套開源的分布式文件系統。


功能:文件存儲、同步、上傳、下載,提供負載均衡功能,適合用於圖片、視頻的存放。


角色:


tracker


作用:任務調度接受客戶端的訪問,檢索存儲節點,為客戶端提供一臺可用的storage。


storage


作用:接受Tracker的調度,響應客戶端請求,將客戶端發來的文件存儲至相應存儲位置,讀取請求的文件,發送給客戶端。


以下使用三臺虛擬機搭建實驗環境:


三臺機器充當存儲節點,其中兩臺配置tracker做主備關系同時充當storage角色,當一臺tracker down機後另外一臺可以提供正常的訪問連接。


tracker01: 192.168.56.101

tracker02: 192.168.56.102

storage03:192.168.56.103


準備工作:


OS: CentoOS6.8

在所有節點上安裝依賴庫:

yum install -y gcc libevent libevent-doc libevent-devel libevent-headers make


所需軟件包:

FastDFS_v4.06.tar.gz

fastdfs-nginx-module_v1.15.tar.gz (配置web訪問時需要nginx支持此模塊)

開始:第一步

[[email protected] ~]# tar -xf FastDFS_v4.06.tar.gz
[[email protected]
/* */ ~]# cd FastDFS [[email protected] FastDFS]# ll total 124 drwxrwxr-x. 3 nginx nginx 4096 Feb 15 2013 client drwxrwxr-x. 2 nginx nginx 4096 Feb 15 2013 common drwxrwxr-x. 2 nginx nginx 4096 Feb 15 2013 conf -rw-rw-r--. 1 nginx nginx 35067 Jul 28 2008 COPYING-3_0.txt -rw-rw-r--. 1 nginx nginx 28437 Jan 24 2013 HISTORY drwxrwxr-x. 2 nginx nginx 4096 Feb 15 2013 init.d -rw-rw-r--. 1 nginx nginx 7757 Dec 23 2011 INSTALL -rwxrwxr-x. 1 nginx nginx 5133 Dec 27 2012 make.sh drwxrwxr-x. 2 nginx nginx 4096 Feb 15 2013 php_client -rw-rw-r--. 1 nginx nginx 2380 Jul 28 2008 README -rwxrwxr-x. 1 nginx nginx 1768 Apr 12 2010 restart.sh -rwxrwxr-x. 1 nginx nginx 1680 Apr 10 2010 stop.sh drwxrwxr-x. 4 nginx nginx 4096 Feb 15 2013 storage drwxrwxr-x. 2 nginx nginx 4096 Feb 15 2013 test drwxrwxr-x. 2 nginx nginx 4096 Feb 15 2013 tracker [[email protected]
/* */ FastDFS]# ./make.sh && ./make.sh install [[email protected] FastDFS]# cp conf/storage_ids.conf /etc/fdfs/ [[email protected] FastDFS]# cp init.d/fdfs_trackerd /etc/init.d #拷貝tracker啟動文件 [[email protected] FastDFS]# cp init.d/fdfs_storaged /etc/init.d #拷貝storage啟動文件

建議:編譯前請先閱讀INSTALL file

第二步:創建數據目錄

mkdir -p /data/fastdfs/tracker #創建tracker文件存放路徑
mkdir -p /data/fastdfs/storage #創建storage 文件存放路徑
mkdir -p /data/fastdfs/client  #創建client 文件存放路徑


第三步:修改配置文件

[[email protected] fdfs]# ll
total 64
-rw-r--r--. 1 root root  1500 Jun 26 14:36 client.conf
-rw-r--r--. 1 root root   858 Jun 23 15:42 http.conf
-rw-r--r--. 1 root root 31172 Jun 23 15:42 mime.types
-rw-r--r--. 1 root root  3898 Jun 26 15:35 mod_fastdfs.conf
-rw-r--r--. 1 root root  7496 Jun 26 15:22 storage.conf
-rw-r--r--. 1 root root   171 Jun 26 15:45 storage_ids.conf
-rw-r--r--. 1 root root  6624 Jun 26 16:04 tracker.conf
#以上目錄處mod_fastdfs.conf文件外如缺少其他文件請檢查 libevent 是否安裝

[[email protected] fdfs]# grep -vE "^#|^$" tracker.conf

disabled=false
bind_addr=
port=22122
connect_timeout=30
network_timeout=60
base_path=/data/fastdfs/tracker
max_connections=4096
work_threads=4
store_lookup=0
store_server=0
store_path=0
download_server=0
reserved_storage_space = 10%
log_level=info
run_by_group=
run_by_user=
allow_hosts=*
sync_log_buff_interval = 10
check_active_interval = 120
thread_stack_size = 64KB
storage_ip_changed_auto_adjust = true
storage_sync_file_max_delay = 86400
storage_sync_file_max_time = 300
use_trunk_file = false
slot_min_size = 256
slot_max_size = 16MB
trunk_file_size = 64MB
trunk_create_file_advance = false
trunk_create_file_time_base = 02:00
trunk_create_file_interval = 86400
trunk_create_file_space_threshold = 20G
trunk_init_check_occupying = false
trunk_init_reload_from_binlog = false
use_storage_id = false
storage_ids_filename = storage_ids.conf
id_type_in_filename = ip
store_slave_file_use_link = false
rotate_error_log = false
error_log_rotate_time=00:00
rotate_error_log_size = 0
use_connection_pool = false
connection_pool_max_idle_time = 3600
http.server_port=8080
http.check_alive_interval=30
http.check_alive_type=tcp
http.check_alive_uri=/status.html

#以上為配置文件內容,各參數的含義請自行了解。

cat /etc/fdfs/storage_ids.conf

[[email protected] fdfs]# cat storage_ids.conf
# <id>  <group_name>  <ip_or_hostname>
 100001   group1  192.168.56.101
 100002   group1  192.168.56.102
 100003   group1  192.168.56.103


[[email protected] fdfs]# grep -vE ‘^#|^$‘ client.conf

connect_timeout=30
network_timeout=60
base_path=/data/fastdfs/client
tracker_server=192.168.56.101:22122
tracker_server=192.168.56.102:22122
log_level=info
use_connection_pool = false
connection_pool_max_idle_time = 3600
load_fdfs_parameters_from_tracker=false
use_storage_id = false
storage_ids_filename = storage_ids.conf
http.tracker_server_port=8080



請在所有tracker主機修改/etc/fdfs/tracker.conf文件


請在所有storage主機修改/etc/fdfs/storage.conf文件

cat /etc/fdfs/storage.conf

[[email protected] fdfs]# grep -vE "^#|^$" storage.conf
disabled=false
group_name=group1
bind_addr=
client_bind=true
port=23000
connect_timeout=30
network_timeout=60
heart_beat_interval=30
stat_report_interval=60
base_path=/data/fastdfs/storage
max_connections=256
buff_size = 256KB
work_threads=4
disk_rw_separated = true
disk_reader_threads = 1
disk_writer_threads = 1
sync_wait_msec=50
sync_interval=0
sync_start_time=00:00
sync_end_time=23:59
write_mark_file_freq=500
store_path_count=1
store_path0=/data/fastdfs/storage
subdir_count_per_path=256
tracker_server=192.168.56.101:22122
tracker_server=192.168.56.102:22122
log_level=info
run_by_group=
run_by_user=
allow_hosts=*
file_distribute_path_mode=0
file_distribute_rotate_count=100
fsync_after_written_bytes=0
sync_log_buff_interval=10
sync_binlog_buff_interval=10
sync_stat_file_interval=300
thread_stack_size=512KB
upload_priority=10
if_alias_prefix=
check_file_duplicate=0
file_signature_method=hash
key_namespace=FastDFS
keep_alive=0
use_access_log = false
rotate_access_log = false
access_log_rotate_time=00:00
rotate_error_log = false
error_log_rotate_time=00:00
rotate_access_log_size = 0
rotate_error_log_size = 0
file_sync_skip_invalid_record=false
use_connection_pool = false
connection_pool_max_idle_time = 3600
http.domain_name=
http.server_port=8888


第四步:啟動tracker and storage

[[email protected] fdfs]# /etc/init.d/fdfs_trackerd start
[[email protected] fdfs]# /etc/init.d/fdfs_storaged start
[[email protected] fdfs]# ps -ef|grep fdfs
root      3157     1  0 11:55 ?        00:00:00 /usr/local/bin/fdfs_trackerd /etc/fdfs/tracker.conf
root      3188     1  0 11:56 ?        00:00:00 /usr/local/bin/fdfs_storaged /etc/fdfs/storage.conf
root      3217  2434  0 11:57 pts/0    00:00:00 grep fdfs
[[email protected] fdfs]#

註意:如果啟動失敗請查看日誌/data/fastdfs/tracker/logs/trackerd.log


至此:tracker和storage啟動完畢。


查看集群狀態:


[[email protected] ~]# /usr/local/bin/fdfs_monitor /etc/fdfs/storage.conf


[2017-06-27 12:02:31] DEBUG - base_path=/data/fastdfs/storage, connect_timeout=30, network_timeout=60, tracker_server_count=2, 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

server_count=2, server_index=0

tracker server is 192.168.56.101:22122

group count: 1

Group 1:
group name = group1
disk total space = 8447 MB
disk free space = 4924 MB
trunk free space = 0 MB
storage server count = 3
active server count = 1
storage server port = 23000
storage HTTP port = 8888
store path count = 1
subdir count per path = 256
current write server index = 0
current trunk file id = 0

Storage 1:
id = 192.168.56.101
ip_addr = 192.168.56.101 (Master) WAIT_SYNC
http domain =
version = 4.06
join time = 2017-06-26 15:22:46
up time = 2017-06-27 11:49:39
total storage = 0 MB
free storage = 0 MB
upload priority = 10
store_path_count = 1
subdir_count_per_path = 256
storage_port = 23000
storage_http_port = 8888
current_write_path = 0
source storage id=
if_trunk_server= 0
total_upload_count = 0
success_upload_count = 0
total_append_count = 0
success_append_count = 0
total_modify_count = 0
success_modify_count = 0
total_truncate_count = 0
success_truncate_count = 0
total_set_meta_count = 0
success_set_meta_count = 0
total_delete_count = 0
success_delete_count = 0
total_download_count = 0
success_download_count = 0
total_get_meta_count = 0
success_get_meta_count = 0
total_create_link_count = 0
success_create_link_count = 0
total_delete_link_count = 0
success_delete_link_count = 0
total_upload_bytes = 0
success_upload_bytes = 0
total_append_bytes = 0
success_append_bytes = 0
total_modify_bytes = 0
success_modify_bytes = 0
stotal_download_bytes = 0
success_download_bytes = 0
total_sync_in_bytes = 0
success_sync_in_bytes = 0
total_sync_out_bytes = 0
success_sync_out_bytes = 0
total_file_open_count = 0
success_file_open_count = 0
total_file_read_count = 0
success_file_read_count = 0
total_file_write_count = 0
success_file_write_count = 0
last_heart_beat_time = 1970-01-01 08:00:00
last_source_update = 1970-01-01 08:00:00
last_sync_update = 1970-01-01 08:00:00
last_synced_timestamp = 1970-01-01 08:00:00 (never synced)
Storage 2:
id = 192.168.56.102
ip_addr = 192.168.56.102 (Minion02) ACTIVE
http domain =
version = 4.06
join time = 2017-06-26 15:12:53
up time = 2017-06-27 11:50:08
total storage = 8447 MB
free storage = 4945 MB
upload priority = 10
store_path_count = 1
subdir_count_per_path = 256
storage_port = 23000
storage_http_port = 8888
current_write_path = 0
source storage id=
if_trunk_server= 0
total_upload_count = 6
success_upload_count = 6
total_append_count = 0
success_append_count = 0
total_modify_count = 0
success_modify_count = 0
total_truncate_count = 0
success_truncate_count = 0
total_set_meta_count = 6
success_set_meta_count = 6
total_delete_count = 0
success_delete_count = 0
total_download_count = 0
success_download_count = 0
total_get_meta_count = 0
success_get_meta_count = 0
total_create_link_count = 0
success_create_link_count = 0
total_delete_link_count = 0
success_delete_link_count = 0
total_upload_bytes = 63284
success_upload_bytes = 63284
total_append_bytes = 0
success_append_bytes = 0
total_modify_bytes = 0
success_modify_bytes = 0
stotal_download_bytes = 0
success_download_bytes = 0
total_sync_in_bytes = 120
success_sync_in_bytes = 120
total_sync_out_bytes = 240
success_sync_out_bytes = 240
total_file_open_count = 10
success_file_open_count = 10
total_file_read_count = 0
success_file_read_count = 0
total_file_write_count = 10
success_file_write_count = 10
last_heart_beat_time = 2017-06-27 12:02:14
last_source_update = 2017-06-27 12:01:01
last_sync_update = 2017-06-26 15:55:03
last_synced_timestamp = 1970-01-01 08:00:00 (never synced)
Storage 3:
id = 192.168.56.103
ip_addr = 192.168.56.103 (Minion03) WAIT_SYNC
http domain =
version = 4.06
join time = 2017-06-26 15:14:03
up time = 2017-06-27 11:50:25
total storage = 8447 MB
free storage = 4924 MB
upload priority = 10
store_path_count = 1
subdir_count_per_path = 256
storage_port = 23000
storage_http_port = 8888
current_write_path = 0
source storage id= 192.168.56.102
if_trunk_server= 0
total_upload_count = 2
success_upload_count = 2
total_append_count = 0
success_append_count = 0
total_modify_count = 0
success_modify_count = 0
total_truncate_count = 0
success_truncate_count = 0
total_set_meta_count = 2
success_set_meta_count = 2
total_delete_count = 0
success_delete_count = 0
total_download_count = 0
success_download_count = 0
total_get_meta_count = 0
success_get_meta_count = 0
total_create_link_count = 0
success_create_link_count = 0
total_delete_link_count = 0
success_delete_link_count = 0
total_upload_bytes = 22
success_upload_bytes = 22
total_append_bytes = 0
success_append_bytes = 0
total_modify_bytes = 0
success_modify_bytes = 0
stotal_download_bytes = 0
success_download_bytes = 0
total_sync_in_bytes = 995
success_sync_in_bytes = 240
total_sync_out_bytes = 120
success_sync_out_bytes = 120
total_file_open_count = 10
success_file_open_count = 10
total_file_read_count = 0
success_file_read_count = 0
total_file_write_count = 10
success_file_write_count = 10
last_heart_beat_time = 2017-06-27 12:02:10
last_source_update = 2017-06-26 15:54:52
last_sync_update = 2017-06-26 16:04:33
last_synced_timestamp = 1970-01-01 08:00:00 (never synced)



測試:


  1. 上傳文件至FastDFS,上傳多個文件至storage查看上傳位置是否有變化

  2. 停掉tracker01 查看是否切換至tracker02



上傳文件:

[[email protected] ~]# /usr/local/bin/fdfs_test /etc/fdfs/client.conf upload install.log



至此:end



如有問題歡迎留言探討。。。。3Q






本文出自 “蝸牛的嘲諷” 博客,請務必保留此出處http://imork.blog.51cto.com/11642843/1942342

FastDFS集群tracker實現負載均衡