随着互联网的快速发展,我们的web站点访问量和数据流量的快速增长,对于我们服务器的处理能里的要求也越来越高,这样的情况下,单一的服务器根本无法承受,这样的话我们丢弃掉原有的设备,做硬件升级,会造成成本的浪费,如果再过一段时间,升级过后的硬件有负载不起了,怎么办呢?没关系,我们有负载均衡的技术,就不用担心了!
负载均衡通俗点说,就是一堆的计算机,或设备,同时为用户提供一个相同的服务,下面就来说说怎么实现的!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1,首先用户发送请求访问bbs.andy.com,
2,当前端Nginx负载均衡服务器(上图中的SVR1)收到用户的请求之后,Nginx负载均衡器,会根据此前配置好的调度算法,代用户请求后端的应用程序服务器,
3,应用程序服务器(也就是上图中的SVR2,3,4) 收到前端Nginx负载均衡器的请求的时候,它并不知道是Nginx负载均衡器是代用户请求的,对于后端的应用程序服务器而言前端Nginx负载均衡器就是一个用户,那么它收到请求之后,将对应请求的处理结果,再返回给前端Nginx负载均衡器,
4,当前端的Nginx负载均衡器,收到后端应用程序服务器返回的响应内容之后再讲结果返回给用户,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
应该是这样工作的吧,个人的理解,呵呵,说的不好,还望大家伙指点,下面来说说LNMP分布式架构是怎么工作的,也就是上图啦,待会的配置也是根据上图的架构来配置的,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
LNMP分布式架构的工作原理,根据上图来,
1,用户请求bbs.andy.com
2,前端Nginx负载均衡器收到用户请求,根据此前设定的调度算法,假如说此时前端Nginx负载均衡器,根据调度算法的结果,应该访问svr2,这台应用程序服务器,那么svr2这台服务器根据用户请求的内容的不同,而进行不同的处理机制,根据上图的架构一般有两种可能,
当用户请求的内容是一个静态的html页面的话,那么svr2,就直接将请求的结果响应给前端Nginx负载均衡器,
当用户请求的内容是一个动态的页面内容(在上图中也就是PHP页面了)那么svr2会去找svr4上面的FastCGI程序来帮忙解析动态PHP页面,如果此次页面请求中需要访问数据库的话,fastcgi会通过MysqL的接口访问数据库的,然后将其请求的结果,返回给svr2,
当svr2收到返回的请求结果后,svr2再将结果返回给前端的Nginx负载均衡器,当前端的Nginx负载均衡器收到请求的结果后,再将其返回给用户,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
下面来配置下在Nginx负载均衡的情况下,后端的LNMP应用服务器分布式架构,下面再简单介绍下上图中每台服务器的角色,已经IP地址,等信息,
服务器 角色 IP地址 DNS能解析的域名
SVR1 Nginx负载均衡器 192.168.0.52/172.16.0.1 bbs.andy.com解析到192.168.0.52
SVR2 Nginx 172.16.0.2
SVR3 Nginx/NFS 172.16.0.3
SVR4 PHP(FastCGI)/MysqL 172.16.0.4
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SVR1负责接受用户请求,根据调度算法,负载到SVR2 SVR3上面去,
SVR3上面有个NFS服务,是提供共享存储的,这里主要是共享网站的源码
SVR4 PHP(FastCGI)提供SVR2 SVR3的动态PHP页面的请求,
SVR4 MysqL数据库提供SVR4上的PHP(FastCGI)的数据请求等,
一,安装配置SVR4
二,安装配置SVR3
三,安装配置SVR2
四,安装配置SVR1
注:配置过程中需要用到的源码包,自己在官方下载,所有包下载至系统的/usr/src目录
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1.安装基本开发库,等依赖软件包
- #yum-ygroupinstall"DevelopmentLibraries""DevelopmentTools""XSoftwareDevelopment"
- #cd/usr/src
- #tarxzvfMysqL-5.0.56.tar.gz
- #cdMysqL-5.0.56
- #./configure--prefix=/usr/local/MysqL--sysconfdir=/usr/local/MysqL/etc--with-ssl\
- --localstatedir=/usr/local/MysqL/database--enable-assembler--with-readline\
- --with-extra-charsets=complex--enable-thread-safe-client--with-big-tables\
- --with-embedded-server--enable-local-infile--with-plugins=innobase
- #make&&makeinstall
2.5 为MysqL提供配置文件,服务启动脚本,用户,并初始化MysqL
3,安装配置PHP(FastCGI)
3.1 编译安装libiconv 编码转换库
- #cd/usr/src
- #tarxzvflibiconv-1.13.1.tar.gz
- #cdlibiconv-1.13.1
- #./configure--prefix=/usr/local
- #make&&makeinstall
3.2 编译安装libmcryp加密算法扩展库
- #cd/usr/src
- #tarxjvflibmcrypt-2.5.8.tar.bz2
- #cdlibmcrypt-2.5.8
- #./configure
- #make
- #makeinstall
- #ldconfig
- #cdlibltdl/
- #./configure--enable-ltdl-install
- #make&& makeinstall
3.3 编译安装mhash加密算法扩展库
- #cd/usr/src
- #tarxjvfmhash-0.9.9.9.tar.bz2
- #cdmhash-0.9.9.9
- #./configure
- #make&& makeinstall
- #ln-s/usr/local/lib/libmcrypt.la/usr/lib/libmcrypt.la
- #ln-s/usr/local/lib/libmcrypt.so/usr/lib/libmcrypt.so
- #ln-s/usr/local/lib/libmcrypt.so.4/usr/lib/libmcrypt.so.4
- #ln-s/usr/local/lib/libmcrypt.so.4.4.8/usr/lib/libmcrypt.so.4.4.8
- #ln-s/usr/local/lib/libmhash.a/usr/lib/libmhash.a
- #ln-s/usr/local/lib/libmhash.la/usr/lib/libmhash.la
- #ln-s/usr/local/lib/libmhash.so/usr/lib/libmhash.so
- #ln-s/usr/local/lib/libmhash.so.2/usr/lib/libmhash.so.2
- #ln-s/usr/local/lib/libmhash.so.2.0.1/usr/lib/libmhash.so.2.0.1
3.4 编译安装mcrypt 加密算法工具
- #cd/usr/src
- #tarxzvfmcrypt-2.6.8.tar.gz
- #cdmcrypt-2.6.8
- #./configure
- #make&&makeinstall
3.5 编译安装PHP(FastCGI) 这里使用的是5.4的版本,5.4的版本不需要打fpm补丁就支持FastCGI
3.5.1 编译安装PHP
- #cd/usr/src
- #tarxjvfPHP-5.4.4.tar.bz2
- #cdPHP-5.4.4
- #./configure--prefix=/usr/local/PHP--with-MysqL=/usr/local/MysqL/--with-openssl\
- --enable-fpm--enable-sockets--enable-sysvshm--with-MysqLi=/usr/local/MysqL/bin/MysqL_config\
- --enable-mbstring--with-freetype-dir--with-jpeg-dir--with-png-dir--with-zlib-dir\
- --with-libxml-dir=/usr/--enable-xml--with-mhash--with-mcrypt--with-config-file-path=/etc\
- --with-config-file-scan-dir=/etc/PHP.d--with-bz2--with-curl--with-ldap--with-iconv-dir
- #makeZEND_EXTRA_LIBS='-liconv'
- #makeinstall
- #cpPHP.ini-production/etc/PHP.ini
- #cpsapi/fpm/init.d.PHP-fpm/etc/rc.d/init.d/PHP-fpm
- #chmod+x/etc/rc.d/init.d/PHP-fpm
- #chkconfig--addPHP-fpm
- #chkconfigPHP-fpmon
- #cp/usr/local/PHP/etc/PHP-fpm.conf.default/usr/local/PHP/etc/PHP-fpm.conf
- #vim/usr/local/PHP/etc/PHP-fpm.conf修改内容如下
- pid=/usr/local/PHP/var/run/PHP-fpm.pid
- listen = 172.16.0.4:9000
- pm.max_children=50
- pm.start_servers=5
- pm.min_spare_servers=2
- pm.max_spare_servers=8
- #servicePHP-fpmstart
- #yumgroupinstall"DevelopmentLibraries""DevelopmentTools"
- #yum-yinstallpcre-devel
- #useradd-s/sbin/nologinNginx
- #cd/usr/src
- #tarxzvfNginx-1.2.2.tar.gz
- #cdNginx-1.2.2
- #./configure--prefix=/usr/local/Nginx--pid-path=/var/run/Nginx/Nginx.pid\
- --lock-path=/var/lock/Nginx.lock--user=Nginx--group=Nginx--with-http_ssl_module\
- --with-http_flv_module--with-http_stub_status_module--with-http_gzip_static_module\
- --http-client-body-temp-path=/var/tmp/Nginx/client/--http-proxy-temp-path=/var/tmp/Nginx/proxy\
- --http-fastcgi-temp-path=/var/tmp/Nginx/fcgi--http-uwsgi-temp-path=/var/tmp/Nginx/uwsgi\
- --http-scgi-temp-path=/var/tmp/Nginx/scgi--with-pcre
- #make&& makeinstall
2.2 为Nginx提供服务启动脚本
- #vim/etc/rc.d/init.d/Nginx内容如下
- #!/bin/sh
- #
- #Nginx-thisscriptstartsandstopstheNginxdaemon
- #
- #chkconfig:-8515
- #description:NginxisanHTTP(S)server,HTTP(S)reverse\
- #proxyandIMAP/POP3proxyserver
- #processname:Nginx
- #config:/etc/Nginx/Nginx.conf
- #config:/etc/sysconfig/Nginx
- #pidfile:/var/run/Nginx.pid
- #Sourcefunctionlibrary.
- ./etc/rc.d/init.d/functions
- #Sourcenetworkingconfiguration.
- ./etc/sysconfig/network
- #Checkthatnetworkingisup.
- ["$NETWORKING"="no"]&&exit0
- 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(){
- #makerequireddirectories
- user=`Nginx-V2>&1|grep"configurearguments:"|sed's/[^*]*--user=\([^]*\).*/\1/g'-`
- options=`$Nginx-V2>&1|grep'configurearguments:'`
- foroptin$options;do
- if[`echo$opt|grep'.*-temp-path'`];then
- value=`echo$opt|cut-d"="-f2`
- if[!-d"$value"];then
- #echo"creating"$value
- mkdir-p$value&&chown-R$user$value
- fi
- fi
- done
- }
- start(){
- [-x$Nginx]||exit5
- [-f$Nginx_CONF_FILE]||exit6
- make_dirs
- echo-n$"Starting$prog:"
- daemon$Nginx-c$Nginx_CONF_FILE
- retval=$?
- echo
- [$retval-eq0]&&touch$lockfile
- return$retval
- }
- stop(){
- echo-n$"Stopping$prog:"
- killproc$prog-QUIT
- retval=$?
- echo
- [$retval-eq0]&&rm-f$lockfile
- return$retval
- }
- restart(){
- configtest||return$?
- stop
- sleep1
- 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/null2>&1
- }
- case"$1"in
- start)
- rh_status_q&&exit0
- $1
- ;;
- stop)
- rh_status_q||exit0
- $1
- ;;
- restart|configtest)
- $1
- ;;
- reload)
- rh_status_q||exit7
- $1
- ;;
- force-reload)
- force_reload
- ;;
- status)
- rh_status
- ;;
- condrestart|try-restart)
- rh_status_q||exit0
- ;;
- *)
- echo$"Usage:$0{start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
- exit2
- esac
- #chmoda+x/etc/init.d/Nginx
- #chkconfig--addNginx
- #chkconfigNginxon
- #vim/usr/local/Nginx/conf/Nginx.conf内容如下
- userNginx;
- worker_processes10;
- error_loglogs/error.logcrit;
- pidlogs/Nginx.pid;
- events
- {
- useepoll;
- worker_connections51000;
- }
- http{
- includemime.types;
- default_typeapplication/octet-stream;
- client_header_buffer_size32k;
- large_client_header_buffers432k;
- client_max_body_size10m;
- sendfileon;
- tcp_nopushon;
- keepalive_timeout60;
- tcp_nodelayon;
- fastcgi_connect_timeout300;
- fastcgi_send_timeout300;
- fastcgi_read_timeout300;
- fastcgi_buffer_size64k;
- fastcgi_buffers464k;
- fastcgi_busy_buffers_size128k;
- fastcgi_temp_file_write_size128k;
- gzipon;
- gzip_min_length1k;
- gzip_buffers416k;
- gzip_http_version1.0;
- gzip_comp_level2;
- gzip_typestext/plainapplication/x-javascripttext/ccsapplication/xml;
- gzip_varyon;
- server{
- listen80;
- server_name172.16.0.3;
- indexindex.htmlindex.htmindex.PHP;
- root/web/bbs;
- location~.*\.(PHP|PHP5)?$
- {
- fastcgi_pass172.16.0.4:9000;
- fastcgi_indexindex.PHP;
- fastcgi_paramSCRIPT_FILENAME/web/bbs$fastcgi_script_name;
- includefastcgi.conf;
- }
- access_loglogs/bbs.log;
- }
- }
2.4 创建网站根目录以及测试文件
2.5 启动Nginx服务
- #serviceNginxrestart
3,配置NFS
- #vim/etc/exports
- /web172.16.0.*(rw,no_root_squash,sync)
- #chkconfigportmapon
- #chkconfignfson
- #serviceportmaprestart
- #servicenfsrestart
三,安装配置SVR2
1.安装基本开发库,及依赖软件包
- #yumgroupinstall"DevelopmentLibraries""DevelopmentTools"
- #yum-yinstallpcre-devel
- #useradd-s/sbin/nologinNginx
- #cd/usr/src
- #tarxzvfNginx-1.2.2.tar.gz
- #cdNginx-1.2.2
- #./configure--prefix=/usr/local/Nginx--pid-path=/var/run/Nginx/Nginx.pid\
- --lock-path=/var/lock/Nginx.lock--user=Nginx--group=Nginx--with-http_ssl_module\
- --with-http_flv_module--with-http_stub_status_module--with-http_gzip_static_module\
- --http-client-body-temp-path=/var/tmp/Nginx/client/--http-proxy-temp-path=/var/tmp/Nginx/proxy\
- --http-fastcgi-temp-path=/var/tmp/Nginx/fcgi--http-uwsgi-temp-path=/var/tmp/Nginx/uwsgi\
- --http-scgi-temp-path=/var/tmp/Nginx/scgi--with-pcre
- #make&& makeinstall
- 见 二.2.2 的配置内容
- #vim/usr/local/Nginx/conf/Nginx.conf内容如下
- userNginx;
- worker_processes10;
- error_loglogs/error.logcrit;
- pidlogs/Nginx.pid;
- events
- {
- useepoll;
- worker_connections51000;
- }
- http{
- includemime.types;
- default_typeapplication/octet-stream;
- client_header_buffer_size32k;
- large_client_header_buffers432k;
- client_max_body_size10m;
- sendfileon;
- tcp_nopushon;
- keepalive_timeout60;
- tcp_nodelayon;
- fastcgi_connect_timeout300;
- fastcgi_send_timeout300;
- fastcgi_read_timeout300;
- fastcgi_buffer_size64k;
- fastcgi_buffers464k;
- fastcgi_busy_buffers_size128k;
- fastcgi_temp_file_write_size128k;
- gzipon;
- gzip_min_length1k;
- gzip_buffers416k;
- gzip_http_version1.0;
- gzip_comp_level2;
- gzip_typestext/plainapplication/x-javascripttext/ccsapplication/xml;
- gzip_varyon;
- server{
- listen80;
- server_name172.16.0.2;
- indexindex.htmlindex.htmindex.PHP;
- root/web/bbs;
- location~.*\.(PHP|PHP5)?$
- {
- fastcgi_pass172.16.0.4:9000;
- fastcgi_indexindex.PHP;
- fastcgi_paramSCRIPT_FILENAME/web/bbs$fastcgi_script_name;
- includefastcgi.conf;
- }
- access_loglogs/bbs.log;
- }
- }
3.重启Nginx服务
4.使用NFS 共享的网站目录,以达到共享存储的目的
- #mkdir /web
- #mount-tnfs172.16.0.3:/web/web
- #echo "mount-tnfs172.16.0.3:/web/web" >> /etc/rc.local
四.安装配置SVR1
1. 安装基本开发库,以及依赖的软件包2.安装配置Nginx
- useradd-s/sbin/nologinNginx
- #cd/usr/src
- #tarxzvfNginx-1.2.2.tar.gz
- #cdNginx-1.2.2
- #./configure--prefix=/usr/local/Nginx--pid-path=/var/run/Nginx/Nginx.pid\
- --lock-path=/var/lock/Nginx.lock--user=Nginx--group=Nginx--with-http_ssl_module\
- --with-http_flv_module--with-http_stub_status_module--with-http_gzip_static_module\
- --http-client-body-temp-path=/var/tmp/Nginx/client/--http-proxy-temp-path=/var/tmp/Nginx/proxy\
- --http-fastcgi-temp-path=/var/tmp/Nginx/fcgi--http-uwsgi-temp-path=/var/tmp/Nginx/uwsgi\
- --http-scgi-temp-path=/var/tmp/Nginx/scgi--with-pcre
- #make&&makeinstall
3.为Nginx提供服务启动脚本
- 见二.2.2的配置内容
- #vim/usr/local/Nginx/conf/Nginx.conf
- userNginx;
- worker_processes10;
- error_loglogs/error.logcrit;
- pidlogs/Nginx.pid;
- events
- {
- useepoll;
- worker_connections51000;
- }
- http{
- includemime.types;
- default_typeapplication/octet-stream;
- keepalive_timeout60;
- tcp_nodelayon;
- #指定负载均衡的方式
- upstreambbs.andy.com{
- server172.16.0.2:80;
- server172.16.0.3:80;
- ip_hash;
- }
- server{
- listen80;
- server_namebbs.andy.com;
- indexindex.htmlindex.htmindex.PHP;
- location/{
- proxy_passhttp://bbs.andy.com;
- proxy_next_upstreamhttp_502http_504errortimeoutinvalid_header;
- proxy_set_headerHost$host;
- proxy_set_headerX-Real-IP$remote_addr;
- proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;
- proxy_connect_timeout600;
- proxy_read_timeout600;
- proxy_send_timeout600;
- proxy_buffer_size8k;
- proxy_temp_file_write_size64k;
- }
- access_loglogs/bbs.log;
- }
- }
- #serviceNginxrestart
五,收尾配置,需要在每台服务器上执行以下命令
- #chkconfigiptablesoff
- #serviceiptablesstop
- #setenforce0
- #echo"setenforce0">>/etc/rc.local
2.需要在SVR4上挂载nfs共享
- #mkdir/web
- #mount-tnfs172.16.0.3:/web/web
- #echo"mount-tnfs172.16.0.3:/web/web">>/etc/rc.local
OK.到这里配置就结束了。