对于绝大多数网站,图片资源是一个非常重要的元素。社区论坛里面图片相册,电子商务线上产品展示图,移动端应用图片分享功能等。都在说互联网正在处于读图时代,各大中小型网站都需要保持良好的图片处理能力。需要支持海量图片资源存储时,更加要搭建高可用负载图片服务集群支撑网站系统,保证网站上图片相关功能准确无误运行,用户对象访问页面体验流畅。
环境:四台Linux主机,一台LVS负载均衡,一台FastDFS_tracker,两台FastDFS_storage,FastDFS_client在FastDFS_tracker上面。
负载:192.168.10.141,安装LVS
图片:192.168.10.173,安装Nginx+FastDFS
图片:192.168.10.74,安装Nginx+FastDFS
调度:192.168.10.252,安装FastDFS
基础架构分析
基础架构主要实现负载均衡、读写分离、分布式文件系统、备份功能、高可用性等五个方面功能。备份功能与高可用性没有添加到下面的架构图,只在后面进行一些分析。图片上传通过分布式文件系统,保存在各个图片服务器上特定目录,保证图片资源同步。图片访问通过负载均衡响应请求,按照调度算法分配到单一图片服务器,接受响应返回图片请求结果。这样就把图片的读写分离开来,并且多个图片服务器组成的文件系统,自备一定容灾能力。当然实现图片服务器的备份功能,容灾能力最强。在负载均衡上面搭建主从备份服务,实现高可用性功能。主负载宕机时,由从负载顶替继续运行负载均衡工作。
负载均衡:使用LVS实现负载均衡,工作于网络层的代理转发,支持三个转发工作方式,提供十种代理调度算法。
读写分离:访问图片时通过负载均衡的代理,上传图片时通过分布式文件系统的调度器,访问与上传不再经过单一服务器。
高可用性:使用LVS+Keepalived实现负载均衡高可用性图片服务器集群,搭建两台LVS负载服务器,安装Keepalived检测当前LVS健康状态,随时准备主从切换。
备份功能:启用图片服务的实时备份功能,该备份服务器只进行图片的写操作,不支持文件系统与客户端进行图片删除、修改、访问。
分布式文件系统:使用FastDFS分布式文件系统,图片通过调度器写到优先级高的存储器,然后同步到其他存储器。
系统安装过程
安装LVS负载
命令行方式安装lvs:
[[email protected] Desktop]# yum -y install ipvsadm*
验证lvs安装是否成功:
[[email protected] Desktop]# ipvsadm IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn
安装Nginx服务
添加Nginx安装源:
[[email protected] Desktop]# rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm Retrieving http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm warning: /var/tmp/rpm-tmp.4MHe6h: Header V4 RSA/SHA1 Signature, key ID 7bd9bf62: NOKEY Preparing... ########################################### [100%] 1:nginx-release-centos ########################################### [100%]
查看Nginx安装源是否添加成功:
[[email protected] Desktop]# yum info nginx Loaded plugins: fastestmirror, refresh-packagekit, security Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.sina.cn nginx | 2.9 kB 00:00 nginx/primary_db | 8.3 kB 00:00 Available Packages Name : nginx Arch : x86_64 Version : 1.8.1 Release : 1.el6.ngx Size : 352 k Repo : nginx Summary : High performance web server URL : http://nginx.org/ License : 2-clause BSD-like license Description : nginx [engine x] is an HTTP and reverse proxy server, as well as : a mail proxy server.
命令行安装Nginx:
[[email protected] Desktop]# yum install nginx
启动Nginx命令:
[[email protected] Desktop]# nginx
浏览器访问出现nginx欢迎信息,即安装成功
安装FastDFS文件系统
前期准备与安装libevent:
[[email protected] Desktop]# mkdir FastNFS [[email protected] Desktop]# cd FastNFS/ [[email protected] FastNFS]# git clone https://github.com/libevent/libevent.git [[email protected] libevent]# rpm -qa libevent [[email protected] libevent]# rpm -qa|grep libevent rpm -e libevent* [[email protected] FastNFS]# cd libevent [[email protected] libevent]# cd cmake [[email protected] cmake]# yum install cmake [[email protected] cmake]# cmake .. [[email protected] cmake]# make [[email protected] cmake]# make install
安装libfastcommon依赖:
[[email protected] FastNFS]# git clone https://github.com/happyfish100/libfastcommon.git [[email protected] FastNFS]# cd libfastcommon [[email protected] libfastcommon]# ./make.sh [[email protected] libfastcommon]# ./make.sh install mkdir -p /usr/lib64 mkdir -p /usr/lib install -m 755 libfastcommon.so /usr/lib64 install -m 755 libfastcommon.so /usr/lib mkdir -p /usr/include/fastcommon install -m 644 common_define.h hash.h chain.h logger.h base64.h shared_func.h pthread_func.h ini_file_reader.h _os_define.h sockopt.h sched_thread.h http_func.h md5.h local_ip_func.h avl_tree.h ioevent.h ioevent_loop.h fast_task_queue.h fast_timer.h process_ctrl.h fast_mblock.h connection_pool.h fast_mpool.h fast_allocator.h fast_buffer.h skiplist.h multi_skiplist.h flat_skiplist.h skiplist_common.h system_info.h fast_blocked_queue.h php7_ext_wrapper.h /usr/include/fastcommon
安装fastdfs分布式文件系统:
[[email protected] FastNFS]# wget https://github.com/happyfish100/fastdfs/archive/V5.05.tar.gz [[email protected] FastNFS]# tar -zxvf V5.05.tar.gz [[email protected] FastNFS]# cd fastdfs-5.05 [[email protected] fastdfs-5.05]# ./make.sh [root[email protected] fastdfs-5.05]# ./make.sh install mkdir -p /usr/bin mkdir -p /etc/fdfs cp -f fdfs_trackerd /usr/bin if [ ! -f /etc/fdfs/tracker.conf.sample ]; then cp -f ../conf/tracker.conf /etc/fdfs/tracker.conf.sample; fi mkdir -p /usr/bin mkdir -p /etc/fdfs cp -f fdfs_storaged /usr/bin if [ ! -f /etc/fdfs/storage.conf.sample ]; then cp -f ../conf/storage.conf /etc/fdfs/storage.conf.sample; fi mkdir -p /usr/bin mkdir -p /etc/fdfs mkdir -p /usr/lib64 cp -f fdfs_monitor fdfs_test fdfs_test1 fdfs_crc32 fdfs_upload_file fdfs_download_file fdfs_delete_file fdfs_file_info fdfs_appender_test fdfs_appender_test1 fdfs_append_file fdfs_upload_appender /usr/bin if [ 0 -eq 1 ]; then cp -f libfdfsclient.a /usr/lib64; fi if [ 1 -eq 1 ]; then cp -f libfdfsclient.so /usr/lib64; fi mkdir -p /usr/include/fastdfs cp -f ../common/fdfs_define.h ../common/fdfs_global.h ../common/mime_file_parser.h ../common/fdfs_http_shared.h ../tracker/tracker_types.h ../tracker/tracker_proto.h ../tracker/fdfs_shared_func.h ../storage/trunk_mgr/trunk_shared.h tracker_client.h storage_client.h storage_client1.h client_func.h client_global.h fdfs_client.h /usr/include/fastdfs if [ ! -f /etc/fdfs/client.conf.sample ]; then cp -f ../conf/client.conf /etc/fdfs/client.conf.sample; fi
验证fastdfs安装是否成功:
[[email protected] fastdfs-5.05]# ll /etc/fdfs/ total 20 -rw-r--r--. 1 root root 1461 Mar 21 03:01 client.conf.sample -rw-r--r--. 1 root root 7829 Mar 21 03:01 storage.conf.sample -rw-r--r--. 1 root root 7102 Mar 21 03:01 tracker.conf.sample
系统配置过程
配置LVS负载
查看lvs配置帮助说明:
[[email protected] nginx]# ipvsadm --help ipvsadm v1.26 2008/5/15 (compiled with popt and IPVS v1.2.1) Usage: ipvsadm -A|E -t|u|f service-address [-s scheduler] [-p [timeout]] [-M netmask] [--pe persistence_engine] ipvsadm -D -t|u|f service-address ipvsadm -C ipvsadm -R ipvsadm -S [-n] ipvsadm -a|e -t|u|f service-address -r server-address [options] ipvsadm -d -t|u|f service-address -r server-address ipvsadm -L|l [options] ipvsadm -Z [-t|u|f service-address] ipvsadm --set tcp tcpfin udp ipvsadm --start-daemon state [--mcast-interface interface] [--syncid sid] ipvsadm --stop-daemon state ipvsadm -h Commands: Either long or short options are allowed. --add-service -A add virtual service with options --edit-service -E edit virtual service with options --delete-service -D delete virtual service --clear -C clear the whole table --restore -R restore rules from stdin --save -S save rules to stdout --add-server -a add real server with options --edit-server -e edit real server with options --delete-server -d delete real server --list -L|-l list the table --zero -Z zero counters in a service or all services --set tcp tcpfin udp set connection timeout values --start-daemon start connection sync daemon --stop-daemon stop connection sync daemon --help -h display this help message Options: --tcp-service -t service-address service-address is host[:port] --udp-service -u service-address service-address is host[:port] --fwmark-service -f fwmark fwmark is an integer greater than zero --ipv6 -6 fwmark entry uses IPv6 --scheduler -s scheduler one of rr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq, the default scheduler is wlc. --pe engine alternate persistence engine may be sip, not set by default. --persistent -p [timeout] persistent service --netmask -M netmask persistent granularity mask --real-server -r server-address server-address is host (and port) --gatewaying -g gatewaying (direct routing) (default) --ipip -i ipip encapsulation (tunneling) --masquerading -m masquerading (NAT) --weight -w weight capacity of real server --u-threshold -x uthreshold upper threshold of connections --l-threshold -y lthreshold lower threshold of connections --mcast-interface interface multicast interface for connection sync --syncid sid syncid for connection sync (default=255) --connection -c output of current IPVS connections --timeout output of timeout (tcp tcpfin udp) --daemon output of daemon information --stats output of statistics information --rate output of rate information --exact expand numbers (display exact values) --thresholds output of thresholds information --persistent-conn output of persistent connection info --nosort disable sorting output of service/server entries --sort does nothing, for backwards compatibility --ops -o one-packet scheduling --numeric -n numeric output of addresses and ports
配置LVS调度器
在LVS调度器执行如下脚本,VIP为对外虚拟IP,注意修改网卡信息:
VIP=192.168.10.251 /sbin/ifconfig eth2:0 $VIP broadcast $VIP netmask 255.255.255.255 up /sbin/route add -host $VIP dev eth2:0 echo 1 > /proc/sys/net/ipv4/ip_forward echo 1 > /proc/sys/net/ipv4/conf/all/send_redirects echo 1 > /proc/sys/net/ipv4/conf/default/send_redirects echo 1 > /proc/sys/net/ipv4/conf/eth2/send_redirects /sbin/ipvsadm -C ipvsadm -A -f 1 -s rr ipvsadm -a -f 1 -r $RIP1:80 -g ipvsadm -a -f 1 -r $RIP2:80 -g
查看ipvsadm配置:
[[email protected] ipvsadm]# ipvsadm -ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn FWM 1 rr -> 192.168.10.98:80 Route 1 0 0 -> 192.168.10.128:80 Route 1 0 0
配置真实服务
在真实图片服务器上执行如下脚本:
VIP=192.168.10.251 ifconfig lo down ifconfig lo up echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up /sbin/route add -host $VIP dev lo:0
配置FastDFS文件系统
参照之前FastDFS配置文章,主要是配置地址、端口、目录等一下标签,其他配置根据需求增加即可。http://blog.csdn.net/a821478424/article/details/51067153
配置Nginx服务
配合负载均衡测试
修改nginx默认页面,使集群展示不同信息,以区分页面来自于不同服务器,验证LVS功能是否成功。
配置图片资源访问路径
编译Nginx默认配置文件:
[[email protected] html]# vi /etc/nginx/conf.d/default.conf
找到下面网站根目录内容:
location / { root /usr/share/nginx/html; index index.html index.htm; }
修改成下面的内容:
location / { #root /usr/share/nginx/html; root /home/yuqing/fastdfs/data/00/00; index index.html index.htm; }
防火墙配置
允许80、23000、22122端口,支持Nginx、FastDFS等服务运行。
功能测试
负载均衡功能
浏览器中访问LVS提供的虚拟IP,返回来自Nginx集群服务默认页面。多刷新几遍url,或者使用不同浏览器访问,发现默认页面并不唯一,证明负载均衡功能正常。
图片集群功能
使用客户端上传图片:
[[email protected] Desktop]$ fdfs_upload_file /etc/fdfs/client.conf ./linux.jpg group1/M00/00/00/wKgKYlcgVEGAfRLhAAAX6oV41fw051.jpg
查看图片是否上传成功:
[[email protected] html]# ll /home/yuqing/fastdfs/data/00/00/ total 32 -rw-r--r--. 1 root root 6122 Apr 26 22:55 wKgKYlcgVEGAfRLhAAAX6oV41fw051.jpg -rw-r--r--. 1 root root 11336 Mar 22 00:59 wKjVh1bw-1KADEceAAAsSKZJhlk53.html -rw-r--r--. 1 root root 11336 Mar 22 01:05 wKjViFbw_LyAIv-oAAAsSKZJhlk82.html
浏览器中访问虚拟IP后面加上图片路径与标识,如:http://192.168.10.251/wKgKYlcgVEGAfRLhAAAX6oV41fw051.jpg。浏览器展示所上传图片,证明图片集群功能正常。
说明
LVS提供多种调度模式与算法,工作于网络四层支持海量请求,配置比起Haproxy、nginx稍微复杂,速度与稳定性各方面性能更加好。FastDFS分布式文件系统,模块划分明确,结构清晰,提供各种语言客户端API开发接口,便于与项目整合。而Hadoop功能更加强大,架构算是复杂,常用于大数据分析。淘宝也有开源分布式图片系统,主要用于电商,且为自身特性定做。所以,选择LVS实现负载功能,选择FastDFS实现分布式图片系统。