下文给大家带来FastDFS集群tracker是怎么样实现负载均衡的,希望能够给大家在实际运用中带来一定的帮助,负载均衡涉及的东西比较多,理论也不多,网上有很多书籍,今天我们就用云搜网在行业内累计的经验来做一个解答。
功能:文件存储、同步、上传、下载,提供负载均衡功能,适合用于图片、视频的存放。
角色:
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支持此模块)
开始:第一步
[root@Master ~]# tar -xf FastDFS_v4.06.tar.gz [root@Master ~]# cd FastDFS [root@Master 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 [root@Master FastDFS]# ./make.sh && ./make.sh install [root@Master FastDFS]# cp conf/storage_ids.conf /etc/fdfs/ [root@Master FastDFS]# cp init.d/fdfs_trackerd /etc/init.d #拷贝tracker启动文件 [root@Master 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 文件存放路径
第三步:修改配置文件
[root@Master 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 是否安装 [root@Master 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
[root@Master 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
[root@Master 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
[root@Master 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
[root@Master fdfs]# /etc/init.d/fdfs_trackerd start [root@Master fdfs]# /etc/init.d/fdfs_storaged start [root@Master 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 [root@Master fdfs]#
注意:如果启动失败请查看日志/data/fastdfs/tracker/logs/trackerd.log
至此:tracker和storage启动完毕。
查看集群状态:
[root@Master ~]# /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)
测试:
-
上传文件至FastDFS,上传多个文件至storage查看上传位置是否有变化
-
停掉tracker01 查看是否切换至tracker02
上传文件:
[root@Master ~]# /usr/local/bin/fdfs_test /etc/fdfs/client.conf upload install.log
看了以上关于FastDFS集群tracker是怎么样实现负载均衡的,如果大家还有什么地方需要了解的可以在云搜网行业资讯里查找自己感兴趣的或者找我们的专业技术工程师解答的,云搜网技术工程师在行业内拥有十几年的经验了。