FastDFS分布式文件系统的安装(集群)

xiaoxiao2021-02-28  74

图片太多了,暂时不上传了。

 

目录

一、 FastDFS集群规划 5 二、 FastDFS的安装 6 1、 编译和安装所需的依赖包 6 2、 安装libfastcommon 7 3、 安装FastDFS 8 三、 配置FastDFS跟踪器Tracker 9 1、 复制一份FastDFS跟踪器样例配置文件,并重命名为tracker.conf。 9 2、 编辑跟踪器配置文件。 9 3、 创建基础数据目录,与上面配置的基础目录base_path路径相同。 10 4、 编辑防火墙文件,打开跟踪器端口,默认端口为22122。 10 5、 启动Tracker跟踪器。 10 6、 关闭Tracker跟踪器。 11 7、 设置FastDFS Tracker跟踪器开机启动。 11 四、 配置FastDFS存储器Storage 11 1、 复制FastDFS存储器样例配置文件,并重命名。 12 2、 编辑存储器样例配置文件(以group1中的storage节点的storage.conf为例)。 12 3、 创建基础数据目录,与上面配置的基础目录base_path路径相同。 13 4、 编辑防火墙文件,打开存储器端口,默认端口为23000。 13 5、 启动Storage存储器。 13 6、 查看集群信息。 14 7、 停止Storage存储器。 14 8、 设置FastDFS Storage存储器开机启动。 14 五、 文件上传测试 15 1、 修改任一台Tracker跟踪器的client.conf配置文件。 15 2、 执行如下文件上传测试命令。 15 六、 在每个Storage存储服务器节点上安装nginx 16 1、 fastdfs-nginx-module作用介绍。 16 2、 上传fastdfs-nginx-module-master.zip到/usr/local/src目录。16 3、 解压、重命名文件。 16 4、 修改fastdfs-nginx-module中的config配置文件。 16 5、 上传nginx-1.13.4.tar.gz到/usr/local/src目录。 16 6、 安装编译Nginx所需的依赖包,安装过程需要输入yes 或y。 16 7、 编译安装Nginx,添加fastdfs-nginx-module模块。 17 8、 复制fastdfs-nginx-module中的配置文件到/etc/fdfs目录,并修改配置。 17 9、 复制FastDFS的两个配置文件到/etc/fdfs目录。 17 10、 在/fastdfs/storage文件存储目录下创建软连接,将其链接到实际存放数据的目录。 17 11、 配置nginx。 17 12、 防火墙中打开nginx的8888端口。 19 13、 启动nginx。 20 14、 设置nginx开机启动。 20 15、 通过浏览器访问测试时上传的文件。 23 七、 在每个Tracker跟踪服务器节点上安装nginx 24 1、 安装编译nginx所需的依赖包。 24 2、 上传ngx_cache_purge-2.3.tar.gz到/usr/local/src目录,解压文件。24 3、 上传nginx-1.13.4.tar.gz到/usr/local/src目录。 24 4、 编译安装nginx,添加fastdfs-nginx-module模块。 24 5、 修改nginx配置文件,设置负载均衡、缓存服务。 24 6、 按以上nginx配置文件的要求,创建对应的缓存目录。 28 7、 系统防火墙打开对应的端口。 28 8、 启动nginx。 28 9、 设置nginx开机启动。 28 10、 文件访问测试。 32 八、 Keepalived简要介绍 32 九、 方案规划 33 十、 安装nginx 33 1、 安装编译nginx所需的依赖包。 33 2、 上传nginx-1.13.4.tar.gz到/usr/local/src目录。 34 3、 编译安装nginx。 34 4、 配置nginx。 34 5、 系统防火墙打开88端口。 35 6、 测试nginx是否安装成功。 35 7、 启动nginx。 35 8、 设置nginx开机启动。 35 9、 分别访问两个nginx。 35 十一、 安装Keepalived 35 1、 上传keepalived-1.3.6.tar.gz到/usr/local/src目录。 35 2、 解压安装。 36 3、 将 keepalived 安装成 Linux 系统服务。 36 4、 修改Keepalived配置文件。 36 5、 创建nginx状态检测脚本。 38 6、 启动keepalived。 38 7、 keepalived+nginx的高可用测试。 38 十二、 使用Keepalived + Nginx组成的高可用负载均衡集群做两个Tracker节点中Nginx的负载均衡41 1、 在keepalived+nginx实现高可用负载均衡集群中配置tracker节点中nginx的负载均衡反向代理。41 2、 重启192.168.137.138和192.168.137.139中的nginx。 43 3、 访问FastDFS集群中的文件。 43 十三、 FastDFS集群在系统中的使用 43

 

 

  一、 FastDFS集群规划 两个tracker(跟踪器),四个storage(存储服务器)。 跟踪服务器1:192.168. 137.141 kp-dfs-tracker-1 跟踪服务器2:192.168. 137.142 kp -dfs-tracker-2 存储服务器1:192.168. 137.143 kp -dfs-storage-group1-1 存储服务器2:192.168. 137.144 kp -dfs-storage-group1-2 存储服务器3:192.168. 137.145 kp -dfs-storage-group2-1 存储服务器4:192.168. 137.146 kp -dfs-storage-group2-2 操作系统:CentOS-6.6-x86_64-bin-DVD1.iso 操作用户:root 数据目录:/fastdfs (注:数据目录按你的数据盘挂载路径而定) 安装包: fastdfs-5.11.tar.gz(Version 5.11) https://github.com/happyfish100/fastdfs libfastcommon-master.zip(Version 1.36) https://github.com/happyfish100/libfastcommon fastdfs-nginx-module-master.zip(Version 1.20) https://github.com/happyfish100/fastdfs-nginx-module nginx-1.13.4.tar.gz(Version 1.13.4) http://nginx.org/en/download.html fastdfs-client-java-master.zip(Version 1.26) https://github.com/happyfish100/fastdfs-client-java ngx_cache_purge-2.3.tar.gz(Version 2.3) http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz https://github.com/FRiCKLE/ngx_cache_purge keepalived-1.3.6.tar.gz(Version 1.3.6) http://www.keepalived.org/download.html 源码地址:https://github.com/happyfish100/  http://sourceforge.net/projects/fastdfs/files/ FastDFS论坛:http://bbs.chinaunix.net/forum-240-1.html 官方文档配置说明:http://bbs.chinaunix.net/thread-1941456-1-1.html   FastDFS文件系统集群的最终结构图如下。   二、 FastDFS的安装 所有跟踪服务器和存储服务器均执行如下安装操作。 1、 编译和安装所需的依赖包 安装过程需要输入y。 # yum install make cmake gcc gcc-c++       2、 安装libfastcommon  (1)上传libfastcommon-master.zip到/usr/local/src目录。  (2)解压 # cd /usr/local/src/ # unzip libfastcommon-master.zip # cd libfastcommon-master     (3)编译、安装 # ./make.sh # ./make.sh install     libfastcommon默认安装到了/usr/lib64/libfastcommon.so和/usr/lib/ libfastcommon.so。 (4)因为FastDFS主程序设置的lib目录是/usr/local/lib,所以需要创建软链接。 # cd /usr/lib64 # ls | grep libfastcommon # ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so  3、 安装FastDFS (1)上传fastdfs-5.11.tar.gz源码包到/usr/local/src目录。 (2)解压 # cd /usr/local/src/ # tar -zxvf fastdfs-5.11.tar.gz # mv fastdfs-5.11 fastdfs # cd fastdfs (3)编译安装 编译前要确保已经成功安装了libfastcommon。 # ./make.sh # ./make.sh install     采用默认方式安装,安装后的相应文件与目录: A、服务脚本在: /etc/init.d/fdfs_storaged /etc/init.d/fdfs_tracker B、配置文件在(样例配置文件): /etc/fdfs/client.conf.sample /etc/fdfs/storage.conf.sample /etc/fdfs/tracker.conf.sample C、命令工具在/usr/bin/目录下的: fdfs_appender_test fdfs_appender_test1 fdfs_append_file fdfs_crc32 fdfs_delete_file fdfs_download_file fdfs_file_info fdfs_monitor fdfs_storaged fdfs_test fdfs_test1 fdfs_trackerd fdfs_upload_appender fdfs_upload_file 查看FastDFS的相关工具命令。 # cd /usr/bin/ # ls | grep fdfs   三、 配置FastDFS跟踪器Tracker 跟踪服务器1:192.168. 137.141 跟踪服务器2:192.168. 137.142 在跟踪服务器上操作。 1、 复制一份FastDFS跟踪器样例配置文件,并重命名为tracker.conf。 # cd /etc/fdfs/ # cp tracker.conf.sample tracker.conf   2、 编辑跟踪器配置文件。 # vi /etc/fdfs/tracker.conf 修改的内容如下,其它参数保留默认配置: disabled=false #启用配置文件 port=22122 #tracker的端口号,一般采用22122这个默认端口 base_path=/fastdfs/tracker #tracker的数据文件和日志目录   3、 创建基础数据目录,与上面配置的基础目录base_path路径相同。 # mkdir -p /fastdfs/tracker 4、 编辑防火墙文件,打开跟踪器端口,默认端口为22122。 # vi /etc/sysconfig/iptables 在文件中添加如下端口行: ## FastDFS Tracker Port -A INPUT -m state --state NEW -m tcp -p tcp --dport 22122 -j ACCEPT 重启防火墙服务: # service iptables restart 5、 启动Tracker跟踪器。 # /etc/init.d/fdfs_trackerd start   初次启动成功,会在/fastdfs/tracker目录下创建data、logs两个目录。 查看FastDFS Tracker跟踪器是否已成功启动。 # ps -ef | grep fdfs   通过netstat命令查看端口监听情况,确认tracker正常启动。 # netstat -unltp|grep fdfs   通过以下命令查看tracker的启动日志。 # tail -100f  /fastdfs/tracker/logs/trackerd.log 6、 关闭Tracker跟踪器。 # /etc/init.d/fdfs_trackerd stop   7、 设置FastDFS Tracker跟踪器开机启动。 编辑rc.local文件。 # vi /etc/rc.d/rc.local 添加以下内容: ## FastDFS Tracker /etc/init.d/fdfs_trackerd start 四、 配置FastDFS存储器Storage 第一组group1 存储服务器1:192.168. 137.143 存储服务器2:192.168. 137.144 第二组group2 存储服务器3:192.168. 137.145 存储服务器4:192.168. 137.146 1、 复制FastDFS存储器样例配置文件,并重命名。 # cd /etc/fdfs/ # cp storage.conf.sample storage.conf   2、 编辑存储器样例配置文件(以group1中的storage节点的storage.conf为例)。 # vi /etc/fdfs/storage.conf 修改的内容如下: disabled=false #启用配置文件 group_name=group1 #组名(第一组为group1,第二组为group2) port=23000 #storage的端口号,同一个组的storage端口号必须相同 base_path=/fastdfs/storage #设置storage的日志目录 store_path0=/fastdfs/storage #存储路径 store_path_count=1 #存储路径个数,需要和store_path个数匹配 tracker_server=192.168.137.141:22122 #tracker服务器的IP地址和端口 tracker_server=192.168.137.142:22122 #多个tracker直接添加多条配置 http.server_port=8888 #设置http端口号       3、 创建基础数据目录,与上面配置的基础目录base_path路径相同。 # mkdir -p /fastdfs/storage 4、 编辑防火墙文件,打开存储器端口,默认端口为23000。 # vi /etc/sysconfig/iptables 在文件中添加如下端口行: ## FastDFS Storage Port -A INPUT -m state --state NEW -m tcp -p tcp --dport 23000 -j ACCEPT 重启防火墙服务: # service iptables restart 5、 启动Storage存储器。 # /etc/init.d/fdfs_storaged start 初次启动成功,会在/fastdfs/storage目录下创建data、logs两个目录。 查看FastDFS Storage存储器是否已成功启动。 # ps -ef | grep fdfs   通过netstat命令查看端口监听情况,确认storage正常启动: # netstat -unltp|grep fdfs   6、 查看集群信息。 所有storage存储器节点都启动之后,查看storage服务器是否已经登记到tracker服务器,tracker与storage是否整合成功,可以在任一个storage存储器节点上使用如下命令查看集群信息。 # /usr/bin/fdfs_monitor /etc/fdfs/storage.conf     如上图所示,看到存储节点状态为ACTIVE,说明storage服务器已经成功登记到了tracker服务器。 OFFLINE状态为存储节点离线。 使用命令监听存储节点日志。 # tail -f /fastdfs/storage/logs/storaged.log 可以看到存储节点链接到跟踪器,并提示哪一个为leader跟踪器,同时也会看到同一组中的其他节点加入进来的日志信息。   7、 停止Storage存储器。 # /etc/init.d/fdfs_storaged stop 8、 设置FastDFS Storage存储器开机启动。 编辑rc.local文件。 # vi /etc/rc.d/rc.local 添加以下内容: ## FastDFS Storage /etc/init.d/fdfs_storaged start 五、 文件上传测试 1、 修改任一台Tracker跟踪器的client.conf配置文件。    两台Tracker跟踪器,在任一台Tracker跟踪器上测试,修改Tracker跟踪服务器中的客户端配置文件。 # cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf # vi /etc/fdfs/client.conf base_path=/fastdfs/tracker tracker_server=192.168.1.131:22122 tracker_server=192.168.1.132:22122 2、 执行如下文件上传测试命令。 # /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /usr/local/src/fastdfs-5.11.tar.gz 返回文件ID号:group2/M00/00/00/wKiJkVmeN8SAX9KqAAUkKwe5sE4.tar.gz group2说明文件上传到了第二组storaged存储节点中。 能返回文件ID号,说明文件上传成功,每次上传返回的文件ID号不同。   文件名由group、存储目录、两级子目录、fileid、文件后缀名拼接而成,如下图:    /fastdfs/storage/data目录下有256个1级目录,每级目录下有256个2级子目录,总共65536个 文件,新写的文件会以hash的方式被路由到其中某个子目录下,然后将文件数据直接作为一个本地 文件存储到该目录中。 # cd /fastdfs/storage # ls # cd data # ls # cd 00 # ls # cd 00 # ls   六、 在每个Storage存储服务器节点上安装nginx 存储服务器1:192.168. 137.143 存储服务器2:192.168. 137.144 存储服务器3:192.168. 137.145 存储服务器4:192.168. 137.146 所有存储服务器均执行如下安装操作。 1、 fastdfs-nginx-module作用介绍。 FastDFS通过Tracker服务器,将文件存储在Storage服务器,但是同组存储服务器之间需要进入文件复制,有同步延迟的问题。 假设Tracker服务器将文件上传到了192.168.137.145,上传成功后文件ID已经返回给客户端。此时FastDFS存储集群机制会将这个文件同步到同组存储192.168.137. 146,在文件还没有复制完成的情况下,客户端如果用这个文件ID在192.168.137.146上取文件,就会出现文件无法访问的错误。 fastdfs-nginx-module可以重定向文件连接到源服务器取文件,避免客户端由于复制延迟导致的文件无法访问错误。 2、 上传fastdfs-nginx-module-master.zip到/usr/local/src目录。 3、 解压、重命名文件。 # cd /usr/local/src/ # unzip fastdfs-nginx-module-master.zip # mv fastdfs-nginx-module-master fastdfs-nginx-module 4、 修改fastdfs-nginx-module中的config配置文件。 # vi /usr/local/src/fastdfs-nginx-module/src/config 检查CORE_INCS配置是否如下所示,如果不一样,则修改。 CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"   5、 上传nginx-1.13.4.tar.gz到/usr/local/src目录。 6、 安装编译Nginx所需的依赖包,安装过程需要输入yes 或y。 # yum install gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel   7、 编译安装Nginx,添加fastdfs-nginx-module模块。 # cd /usr/local/src/ # tar -zxvf nginx-1.13.4.tar.gz # cd nginx-1.13.4 # ./configure  --prefix=/usr/local/nginx  --add-module=/usr/local/src/fastdfs-nginx-module/src # make && make install 8、 复制fastdfs-nginx-module中的配置文件到/etc/fdfs目录,并修改配置。 # cp /usr/local/src/fastdfs-nginx-module/src/mod_fastdfs.conf  /etc/fdfs/ # vi /etc/fdfs/mod_fastdfs.conf 修改以下配置: connect_timeout=10 #连接超时时间 base_path=/tmp #缓存目录 tracker_server=192.168.137.141:22122 #tracker服务器的IP地址和端口 tracker_server=192.168.137.142:22122 #多个tracker直接添加多条配置 storage_server_port=23000#storage server端口号 group_name=group1 #组名(第一组为group1,第二组为group2) url_have_group_name = true #文件URL是否带上group组名 store_path0=/fastdfs/storage #存储路径 group_count = 2 #group组的个数 [group1] #第一组storage存储节点配置 group_name=group1 #组名 storage_server_port=23000 #storage server端口号  store_path_count=1 #存储路径个数,需要和store_path个数匹配 store_path0=/fastdfs/storage #存储路径 [group2] #第二组storage存储节点配置 group_name=group2 #组名 storage_server_port=23000 #storage server端口号 store_path_count=1 #存储路径个数,需要和store_path个数匹配 store_path0=/fastdfs/storage #存储路径 9、 复制FastDFS的两个配置文件到/etc/fdfs目录。 # cd /usr/local/src/fastdfs/conf # cp http.conf  mime.types  /etc/fdfs/ 10、 在/fastdfs/storage文件存储目录下创建软连接,将其链接到实际存放数据的目录。 # ln -s /fastdfs/storage/data/ /fastdfs/storage/data/M00 11、 配置nginx。 # cd /usr/local/nginx # cd conf # vi nginx.conf 简洁版nginx配置样例: user  root; worker_processes  1; events { worker_connections  1024; } http { include  mime.types; default_type  application/octet-stream; sendfile        on; keepalive_timeout  65; server { listen 8888; server_name  localhost; location ~/group([0-9])/M00 {       #alias /fastdfs/storage/data; ngx_fastdfs_module;     } error_page   500 502 503 504  /50x.html; location = /50x.html { root   html;       }     } }   注意: (1)、listen 8888端口值要与文件/etc/fdfs/storage.conf中的http.server_port=8888相对应,因为http.server_port默认为8888,如果想改成80,则要对应修改过来。 (2)、storage对应有多个group分组的情况下,访问路径带组名称,如/group1/M00/00/00/xxx, 对应的nginx配置为: location ~/group([0-9])/M00 { ngx_fastdfs_module; } (3)、如果下载时发现总是报错误404,将nginx.conf第一行user nobody修改为user root后重新启动nginx服务。 12、 防火墙中打开nginx的8888端口。 # vi /etc/sysconfig/iptables 添加以下内容: ## Nginx Port -A INPUT -m state --state NEW -m tcp -p tcp --dport 8888 -j ACCEPT 重启防火墙服务:  # service iptables restart 13、 启动nginx。 启动每一个storage存储节点上的nginx。 # /usr/local/nginx/sbin/nginx   重启nginx命令: /usr/local/nginx/sbin/nginx -s reload 停止nginx命令: /usr/local/nginx/sbin/nginx -s stop 14、 设置nginx开机启动。 使用chkconfig管理nginx服务。在/etc/init.d/目录下创建nginx脚本文件。 # vi /etc/init.d/nginx 在nginx脚本文件中添加如下内容: #!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig:   - 85 15 # description:  NGINX is an HTTP(S) server, HTTP(S) reverse \ #               proxy and IMAP/POP3 proxy server # processname: nginx # config:      /etc/nginx/nginx.conf # config:      /etc/sysconfig/nginx # pidfile:     /var/run/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/local/nginx/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx make_dirs() {    # make required directories    user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`    if [ -n "$user" ]; then       if [ -z "`grep $user /etc/passwd`" ]; then          useradd -M -s /bin/nologin $user       fi       options=`$nginx -V 2>&1 | grep 'configure arguments:'`       for opt in $options; do           if [ `echo $opt | grep '.*-temp-path'` ]; then               value=`echo $opt | cut -d "=" -f 2`               if [ ! -d "$value" ]; then                   # echo "creating" $value                   mkdir -p $value && chown -R $user $value               fi           fi        done     fi } start() {     [ -x $nginx ] || exit 5     [ -f $NGINX_CONF_FILE ] || exit 6     make_dirs     echo -n $"Starting $prog: "     daemon $nginx -c $NGINX_CONF_FILE     retval=$?     echo     [ $retval -eq 0 ] && touch $lockfile     return $retval } stop() {     echo -n $"Stopping $prog: "     killproc $prog -QUIT     retval=$?     echo     [ $retval -eq 0 ] && rm -f $lockfile     return $retval } restart() {     configtest || return $?     stop     sleep 1     start } reload() {     configtest || return $?     echo -n $"Reloading $prog: "     killproc $nginx -HUP     RETVAL=$?     echo } force_reload() {     restart } configtest() {   $nginx -t -c $NGINX_CONF_FILE } rh_status() {     status $prog } rh_status_q() {     rh_status >/dev/null 2>&1 } case "$1" in     start)         rh_status_q && exit 0         $1         ;;     stop)         rh_status_q || exit 0         $1         ;;     restart|configtest)         $1         ;;     reload)         rh_status_q || exit 7         $1         ;;     force-reload)         force_reload         ;;     status)         rh_status         ;;     condrestart|try-restart)         rh_status_q || exit 0             ;;     *)         echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"         exit 2 esac 注意以下内容必须与安装设置路径一致: nginx执行程序的路径。 nginx="/usr/local/nginx/sbin/nginx" nginx配置文件的路径。 NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" 脚本来自于nginx官方网站。https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/ 保存脚本文件后设置文件的执行权限: # chmod a+x /etc/init.d/nginx 先将nginx服务加入chkconfig管理列表: # chkconfig --add /etc/init.d/nginx 使用service对nginx进行启动操作。 # service nginx start 停止nginx命令。 # service nginx stop 重启nginx命令。 # service nginx restart 设置nginx开机启动: # chkconfig nginx on 15、 通过浏览器访问测试时上传的文件。 4个storage存储节点都可以访问到上传的文件。 http://192.168.137.143:8888/group2/M00/00/00/wKiJkVmeN8SAX9KqAAUkKwe5sE4.tar.gz http://192.168.137.144:8888/group2/M00/00/00/wKiJkVmeN8SAX9KqAAUkKwe5sE4.tar.gz http://192.168.137.145:8888/group2/M00/00/00/wKiJkVmeN8SAX9KqAAUkKwe5sE4.tar.gz http://192.168.137.146:8888/group2/M00/00/00/wKiJkVmeN8SAX9KqAAUkKwe5sE4.tar.gz 七、 在每个Tracker跟踪服务器节点上安装nginx 跟踪服务器1:192.168. 137.141 跟踪服务器2:192.168. 137.142 所有跟踪服务器均执行如下安装操作。 在tracker跟踪服务器节点上安装nginx,主要为了提供http访问的反向代理、负载均衡、缓存服务。 1、 安装编译nginx所需的依赖包。 安装过程需要输入yes 或y。 # yum install gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel   2、 上传ngx_cache_purge-2.3.tar.gz到/usr/local/src目录,解压文件。 # cd /usr/local/src/ # tar -zxvf ngx_cache_purge-2.3.tar.gz 3、 上传nginx-1.13.4.tar.gz到/usr/local/src目录。 4、 编译安装nginx,添加fastdfs-nginx-module模块。 # cd /usr/local/src/ # tar -zxvf nginx-1.13.4.tar.gz # cd nginx-1.13.4 # ./configure --prefix=/usr/local/nginx --add-module=/usr/local/src/ngx_cache_purge-2.3 # make && make install 5、 修改nginx配置文件,设置负载均衡、缓存服务。 # vi /usr/local/nginx/conf/nginx.conf 修改添加如下配置。 user  root; worker_processes  1; #error_log  logs/error.log; #error_log  logs/error.log  notice; #error_log  logs/error.log  info; #pid        logs/nginx.pid; events { worker_connections  1024; use epoll; } http { include  mime.types; default_type  application/octet-stream;     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '     #                  '$status $body_bytes_sent "$http_referer" '     #                  '"$http_user_agent" "$http_x_forwarded_for"';     #access_log  logs/access.log  main; sendfile        on; tcp_nopush     on;     #keepalive_timeout  0; keepalive_timeout  65; #gzip  on; #设置缓存 server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 300m; proxy_redirect off; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 16k; proxy_buffers 4 64k; proxy_busy_buffers_size 128k; proxy_temp_file_write_size 128k; #设置缓存存储路径、存储方式、分配内存大小、磁盘最大空间、缓存期限 proxy_cache_path /fastdfs/cache/nginx/proxy_cache levels=1:2 keys_zone=http-cache:200m max_size=1g inactive=30d; proxy_temp_path /fastdfs/cache/nginx/proxy_cache/tmp; #设置group1的服务器 upstream fdfs_group1 { server 192.168.137.143:8888 weight=1 max_fails=2 fail_timeout=30s; server 192.168.137.144:8888 weight=1 max_fails=2 fail_timeout=30s; } #设置group2的服务器 upstream fdfs_group2 { server 192.168.137.145:8888 weight=1 max_fails=2 fail_timeout=30s; server 192.168.137.146:8888 weight=1 max_fails=2 fail_timeout=30s;     } server { listen 8000; server_name  localhost;         #charset koi8-r;         #access_log  logs/host.access.log  main; #设置group1的负载均衡参数 location /group1/M00 { proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_cache http-cache; proxy_cache_valid 200 304 12h; proxy_cache_key $uri$is_args$args; proxy_pass http://fdfs_group1; expires 30d;     } #设置group2的负载均衡参数 location /group2/M00 { proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_cache http-cache; proxy_cache_valid 200 304 12h; proxy_cache_key $uri$is_args$args; proxy_pass http://fdfs_group2; expires 30d;     } #设置清除缓存的访问权限,allow充许清除缓存的访问网段 location ~/purge(/.*) { allow 127.0.0.1; allow 192.168.137.0/32; deny all; proxy_cache_purge http-cache $1$is_args$args;     } #error_page  404              /404.html;     # redirect server error pages to the static page /50x.html     # error_page   500 502 503 504  /50x.html; location = /50x.html { root   html;         }         # proxy the PHP scripts to Apache listening on 127.0.0.1:80         #         #location ~ \.php$ {         #    proxy_pass   http://127.0.0.1;         #}         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000         #         #location ~ \.php$ {         #    root           html;         #    fastcgi_pass   127.0.0.1:9000;         #    fastcgi_index  index.php;         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;         #    include        fastcgi_params;         #}         # deny access to .htaccess files, if Apache's document root         # concurs with nginx's one         #         #location ~ /\.ht {         #    deny  all;         #}     }     # another virtual host using mix of IP-, name-, and port-based configuration     #     #server {     #    listen       8000;     #    listen       somename:8080;     #    server_name  somename  alias  another.alias;     #    location / {     #        root   html;     #        index  index.html index.htm;     #    }     #}     # HTTPS server     #     #server {     #    listen       443 ssl;     #    server_name  localhost;     #    ssl_certificate      cert.pem;     #    ssl_certificate_key  cert.key;     #    ssl_session_cache    shared:SSL:1m;     #    ssl_session_timeout  5m;     #    ssl_ciphers  HIGH:!aNULL:!MD5;     #    ssl_prefer_server_ciphers  on;     #    location / {     #        root   html;     #        index  index.html index.htm;     #    }     #} } 6、 按以上nginx配置文件的要求,创建对应的缓存目录。 # mkdir -p /fastdfs/cache/nginx/proxy_cache # mkdir -p /fastdfs/cache/nginx/proxy_cache/tmp 7、 系统防火墙打开对应的端口。 # vi /etc/sysconfig/iptables ## Nginx -A INPUT -m state --state NEW -m tcp -p tcp --dport 8000 -j ACCEPT # service iptables restart 8、 启动nginx。 启动每一个tracker跟踪节点上的nginx。 # /usr/local/nginx/sbin/nginx 重启nginx命令: /usr/local/nginx/sbin/nginx -s reload 停止nginx命令: /usr/local/nginx/sbin/nginx -s stop 9、 设置nginx开机启动。 使用chkconfig管理nginx服务。在/etc/init.d/目录下创建nginx脚本文件。 # vi /etc/init.d/nginx 在nginx脚本文件中添加如下内容: #!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig:   - 85 15 # description:  NGINX is an HTTP(S) server, HTTP(S) reverse \ #               proxy and IMAP/POP3 proxy server # processname: nginx # config:      /etc/nginx/nginx.conf # config:      /etc/sysconfig/nginx # pidfile:     /var/run/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/local/nginx/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx make_dirs() {    # make required directories    user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`    if [ -n "$user" ]; then       if [ -z "`grep $user /etc/passwd`" ]; then          useradd -M -s /bin/nologin $user       fi       options=`$nginx -V 2>&1 | grep 'configure arguments:'`       for opt in $options; do           if [ `echo $opt | grep '.*-temp-path'` ]; then               value=`echo $opt | cut -d "=" -f 2`               if [ ! -d "$value" ]; then                   # echo "creating" $value                   mkdir -p $value && chown -R $user $value               fi           fi        done     fi } start() {     [ -x $nginx ] || exit 5     [ -f $NGINX_CONF_FILE ] || exit 6     make_dirs     echo -n $"Starting $prog: "     daemon $nginx -c $NGINX_CONF_FILE     retval=$?     echo     [ $retval -eq 0 ] && touch $lockfile     return $retval } stop() {     echo -n $"Stopping $prog: "     killproc $prog -QUIT     retval=$?     echo     [ $retval -eq 0 ] && rm -f $lockfile     return $retval } restart() {     configtest || return $?     stop     sleep 1     start } reload() {     configtest || return $?     echo -n $"Reloading $prog: "     killproc $nginx -HUP     RETVAL=$?     echo } force_reload() {     restart } configtest() {   $nginx -t -c $NGINX_CONF_FILE } rh_status() {     status $prog } rh_status_q() {     rh_status >/dev/null 2>&1 } case "$1" in     start)         rh_status_q && exit 0         $1         ;;     stop)         rh_status_q || exit 0         $1         ;;     restart|configtest)         $1         ;;     reload)         rh_status_q || exit 7         $1         ;;     force-reload)         force_reload         ;;     status)         rh_status         ;;     condrestart|try-restart)         rh_status_q || exit 0             ;;     *)         echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"         exit 2 esac 注意以下内容必须与安装设置路径一致: nginx执行程序的路径。 nginx="/usr/local/nginx/sbin/nginx" nginx配置文件的路径。 NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" 脚本来自于nginx官方网站。https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/ 保存脚本文件后设置文件的执行权限: # chmod a+x /etc/init.d/nginx 先将nginx服务加入chkconfig管理列表: # chkconfig --add /etc/init.d/nginx 使用service对nginx进行启动操作。 # service nginx start 停止nginx命令。 # service nginx stop 重启nginx命令。 # service nginx restart 设置nginx开机启动: # chkconfig nginx on 10、 文件访问测试。 前面通过访问storage存储器节点中的nginx来访问文件。 http://192.168.137.143:8888/group2/M00/00/00/wKiJkVmeN8SAX9KqAAUkKwe5sE4.tar.gz http://192.168.137.144:8888/group2/M00/00/00/wKiJkVmeN8SAX9KqAAUkKwe5sE4.tar.gz http://192.168.137.145:8888/group2/M00/00/00/wKiJkVmeN8SAX9KqAAUkKwe5sE4.tar.gz http://192.168.137.146:8888/group2/M00/00/00/wKiJkVmeN8SAX9KqAAUkKwe5sE4.tar.gz 现在可以通过tracker跟踪器节点中的nginx来访问文件。 通过tracker1跟踪器节点中的nginx来访问文件。 http://192.168.137.141:8000/group2/M00/00/00/wKiJkVmeN8SAX9KqAAUkKwe5sE4.tar.gz 通过tracker2跟踪器节点中的nginx来访问文件。 http://192.168.137.142:8000/group2/M00/00/00/wKiJkVmeN8SAX9KqAAUkKwe5sE4.tar.gz 由上面的文件访问效果可以看到,每一个tracker中的nginx都单独对后端的storage组做了负载均衡,但整套FastDFS集群如果想对外提供统一的文件访问地址,还需要对两个tracker中的nginx进行HA集群。 八、 Keepalived简要介绍 Keepalived 是一种高性能的服务器高可用或热备解决方案,Keepalived可以用来防止服务器单点故障的发生,通过配合Nginx可以实现web前端服务的高可用。 Keepalived以VRRP协议为实现基础,用VRRP协议来实现高可用性(HA)。VRRP(Virtual Router Redundancy Protocol)协议是用于实现路由器冗余的协议,VRRP协议将两台或多台路由器设备虚拟成一个设备,对外提供虚拟路由器IP(一个或多个),而在路由器组内部,如果实际拥有这个对外IP的路由器如果工作正常的话就是MASTER,或者是通过算法选举产生,MASTER实现针对虚拟路由器IP的各种网络功能,如ARP请求,ICMP,以及数据的转发等;其他设备不拥有该虚拟IP,状态是BACKUP,除了接收MASTER的VRRP状态通告信息外,不执行对外的网络功能。当主机失效时,BACKUP将接管原先MASTER的网络功能。 VRRP协议使用多播数据来传输VRRP数据,VRRP数据使用特殊的虚拟源MAC地址发送数据而不是自身网卡的MAC地址,VRRP运行时只有MASTER路由器定时发送VRRP通告信息,表示MASTER工作正常以及虚拟路由器IP(组),BACKUP只接收VRRP数据,不发送数据,如果一定时间内没有接收到MASTER的通告信息,各BACKUP将宣告自己成为MASTER,发送通告信息,重新进行MASTER选举状态。     九、 方案规划 VIP虚拟IP IP 主机名 nginx端口 默认主从 192.168.137.140 192.168.137.138kp-proxy-01 88MASTER 192.168.137.139kp-proxy-02 88BACKUP keepalived-1.3.6.tar.gz http://www.keepalived.org/download.html nginx-1.13.4.tar.gz(Version 1.13.4) http://nginx.org/en/download.html 十、 安装nginx 1、 安装编译nginx所需的依赖包。 # yum install gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel 2、 上传nginx-1.13.4.tar.gz到/usr/local/src目录。 3、 编译安装nginx。 # cd /usr/local/src/ # tar -zxvf nginx-1.13.4.tar.gz # cd nginx-1.13.4 # ./configure --prefix=/usr/local/nginx # make && make install 4、 配置nginx。 # vi /usr/local/nginx/conf/nginx.conf user root; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 88; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } 修改nginx欢迎首页内容(用于后面测试,用于区分两个节点的nginx): # vi /usr/local/nginx/html/index.html 192.168.137.138 中的标题加 1 <h1>Welcome to nginx! 1</h1> 192.168.137.139 中的标题加 2 <h1>Welcome to nginx! 2</h1> 5、 系统防火墙打开88端口。 # vi /etc/sysconfig/iptables ## Nginx -A INPUT -m state --state NEW -m tcp -p tcp --dport 88 -j ACCEPT 重启防火墙服务。 # service iptables restart 6、 测试nginx是否安装成功。 # /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful 7、 启动nginx。 # /usr/local/nginx/sbin/nginx 重启 Nginx # /usr/local/nginx/sbin/nginx -s reload 8、 设置nginx开机启动。 # vi /etc/rc.local 添加如下内容: /usr/local/nginx/sbin/nginx 9、 分别访问两个nginx。      十一、 安装Keepalived 1、 上传keepalived-1.3.6.tar.gz到/usr/local/src目录。 2、 解压安装。 # cd /usr/local/src # tar -zxvf keepalived-1.3.6.tar.gz # cd keepalived-1.3.6 # ./configure --prefix=/usr/local/keepalived # make && make install 3、 将 keepalived 安装成 Linux 系统服务。 因为没有使用 keepalived 的默认路径安装(默认是/usr/local) ,安装完成之后,需要做一些工作 复制默认配置文件到默认路径 # mkdir /etc/keepalived # cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ 复制 keepalived 服务脚本到默认的地址 # cp /usr/local/src/keepalived-1.3.6/keepalived/etc/init.d/keepalived /etc/init.d/ # cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ # ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/ # ln -s /usr/local/keepalived/sbin/keepalived /sbin/ 设置 keepalived 服务开机启动 # chkconfig keepalived on 4、 修改Keepalived配置文件。 (1)MASTER节点配置文件(192.168.137.138)。 # vi /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { ## keepalived自带的邮件提醒需要开启sendmail服务。建议用独立的监控或第三方SMTP router_id kp-proxy-01##标识本节点的字条串,通常为hostname } ## keepalived会定时执行脚本并对脚本执行的结果进行分析,动态调整vrrp_instance的优先级。如果脚本执行结果为0,并且weight配置的值大于0,则优先级相应的增加。如果脚本执行结果非0,并且weight配置的值小于0,则优先级相应的减少。其他情况,维持原本配置的优先级,即配置文件中priority对应的值。 vrrp_script chk_nginx {     script "/etc/keepalived/nginx_check.sh"##检测nginx状态的脚本路径     interval 2##检测时间间隔     weight -20##如果条件成立,权重-20 } ## 定义虚拟路由,VI_1 为虚拟路由的标示符,自己定义名称 vrrp_instance VI_1 { state MASTER ##主节点为MASTER,对应的备份节点为BACKUP interface eth0##绑定虚拟IP的网络接口,与本机IP地址所在的网络接口相同,我的是eth1 virtual_router_id 51##虚拟路由的ID号,两个节点设置必须一样,可选IP最后一段使用, 相同的VRID为一个组,他将决定多播的MAC地址 mcast_src_ip 192.168.137.138## 本机IP地址 priority 100 ##节点优先级,值范围0-254,MASTER要比BACKUP高 nopreempt ## 优先级高的设置nopreempt解决异常恢复后再次抢占的问题 advert_int 1 ##组播信息发送间隔,两个节点设置必须一样,默认1s ##设置验证信息,两个节点必须一致 authentication { auth_type PASS auth_pass 123456## 两个节点相互验证密码,真实生产,按需求对应该过来 } ##将track_script块加入instance 配置块 track_script { chk_nginx ## 执行Nginx监控的服务 } ##虚拟IP池, 两个节点设置必须一样 virtual_ipaddress {     192.168.137.140##虚拟ip,可以定义多个     } } (2)BACKUP节点配置文件(192.168.137.139)。 # vi /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { router_id kp-proxy-02 } vrrp_scriptchk_nginx { script "/etc/keepalived/nginx_check.sh" interval 2 weight -20 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 mcast_src_ip 192.168.137.139 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 123456     } track_script { chk_nginx } virtual_ipaddress {         192.168.137.140 } } 5、 创建nginx状态检测脚本。 编辑/etc/keepalived/nginx_check.sh(已在keepalived.conf中配置)。 脚本效果:如果nginx停止运行,尝试启动,如果无法启动则杀死本机的keepalived进程,keepalied 将虚拟ip绑定到BACKUP机器上。内容如下: # vi /etc/keepalived/nginx_check.sh #!/bin/bash A=`ps -C nginx –no-header |wc -l` if [ $A -eq 0 ];then /usr/local/nginx/sbin/nginx sleep 2 if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then killall keepalived fi fi 保存后,给脚本赋执行权限。 # chmod +x /etc/keepalived/nginx_check.sh 6、 启动keepalived。 # service keepalived start 7、 keepalived+nginx的高可用测试。 (1)关闭192.168.137.138中的nginx,keepalived会将它重新启动。 # /usr/local/nginx/sbin/nginx -s stop (2)测试停止192.168.137.138中的keepalived,查看VIP(虚拟IP地址192.168.137.140)路由访问情况。 停止keepalived之前,使用命令查看VIP情况。 # ip add   可以看到VIP地址192.168.137.140。 浏览器访问http://192.168.137.140:88/,访问到的是192.168.137.138的nginx1。   在Windows下使用cmd命令查看此时VIP对应的MAC地址。 arp -a 192.168.137.138 arp -a 192.168.137.139 arp -a 192.168.137.140   虚拟VIP地址192.168.137.140对应的MAC地址与192.168.137.138的MAC地址一样。此时访问虚拟 VIP的请求会转发到192.168.137.138上面。 停止192.168.137.138中的keepalived,使用命令查看VIP情况。 # service keepalived stop # ip add   keepalived停止后,该节点的网络接口中的VIP(虚拟IP地址192.168.137.140)地址将消失。 浏览器访问http://192.168.137.140:88/,访问到的是192.168.137.139的nginx2。   在Windows下使用cmd命令查看此时VIP对应的MAC地址。 arp -a 192.168.137.138 arp -a 192.168.137.139 arp -a 192.168.137.140   虚拟VIP地址192.168.137.140对应的MAC地址与192.168.137.139的MAC地址一样。此时访问虚拟 VIP的请求会转发到192.168.137.139上面。说明此时VIP已经漂移到物理主机192.168.137.139上 了。 (3)重新启动192.168.137.138中的keepalived,VIP又会切回到192.168.137.138中来。 # service keepalived start 使用命令查看VIP情况。 # ip add   keepalived重新启动后,可以看到VIP(虚拟IP地址192.168.137.140)地址,网络接口上又会创建 出VIP 192.168.137.140。 浏览器访问http://192.168.137.140:88/,访问到的是192.168.137.138的nginx1。    keepalived服务管理命令: 停止:service keepalived stop 启动:service keepalived start 重启:service keepalived restart 查看状态:service keepalived status其他参考资料: keepalived之vrrp_script总结:http://my.oschina.net/hncscwc/blog/158746 keepalived双机热备实现故障时发送邮件通知:http://www.2cto.com/os/201407/317795.html 基于keepalived 实现VIP转移,lvs,nginx的高可用:http://www.tuicool.com/articles/eu26Vz 十二、 使用Keepalived + Nginx组成的高可用负载均衡集群做两个Tracker节点中Nginx的负载均衡   1、 在keepalived+nginx实现高可用负载均衡集群中配置tracker节点中nginx的负载均衡反向代理。 (192.168.137.138和192.168.137.139中的nginx执行相同的配置)。 # vi /usr/local/nginx/conf/nginx.conf user  root; worker_processes  1; #error_log  logs/error.log; #error_log  logs/error.log  notice; #error_log  logs/error.log  info; #pid        logs/nginx.pid; events { worker_connections  1024; } http { include mime.types; default_type  application/octet-stream;     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '     #                  '$status $body_bytes_sent "$http_referer" '     #                  '"$http_user_agent" "$http_x_forwarded_for"';     #access_log  logs/access.log  main; sendfile        on;     #tcp_nopush     on;     #keepalive_timeout  0; keepalive_timeout  65; #gzip  on;     ## FastDFS Tracker Proxy upstream fastdfs_tracker { server 192.168.137.141:8000 weight=1 max_fails=2 fail_timeout=30s; server 192.168.137.142:8000 weight=1 max_fails=2 fail_timeout=30s;     } server { listen       88; server_name  localhost;         #charset koi8-r;         #access_log  logs/host.access.log  main; location / { root   html; index  index.html index.htm;         }         #error_page  404              /404.html;         # redirect server error pages to the static page /50x.html error_page   500 502 503 504  /50x.html; location = /50x.html { root   html; }       ## FastDFS Proxy location /dfs { root   html; index  index.html index.htm; proxy_pass  http://fastdfs_tracker/; proxy_set_header Host  $http_host; proxy_set_header Cookie $http_cookie; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; client_max_body_size  300m;       }     } } 2、 重启192.168.137.138和192.168.137.139中的nginx。 # /usr/local/nginx/sbin/nginx -s reload 3、 访问FastDFS集群中的文件。 通过keepalived+nginx组成的高可用负载集群的VIP(192.168.137.140)来访问FastDFS集群中的文件。

http://192.168.137.140:88/dfs/group2/M00/00/00/wKiJkVmeN8SAX9KqAAUkKwe5sE4.tar.gz

 

十三、 FastDFS集群在系统中的使用 ......

 

 

 

 

转载请注明原文地址: https://www.6miu.com/read-48114.html

最新回复(0)