DNS轮询实现双web负载均衡

 

案例描述与实现。由于公司知名度不断提升,网站的访问量也不断提升开放的对产品和经营的论坛访问量也越来越大,一台WEB服务器已经不能满足需求,需要增加一台web服务器,将两台web服务器做成一个Discuz论坛的小集群,使用简单的DNS轮询功能来实现双web的负载均衡,在web1上搭建DNS服务器,NFS服务器,并部署phpmysql,以及Discuz论谈,web2上只需搭建LAMP环境即可,主要提供mysql数据存储服务。利用web1的NFS共享来实现数据间的同步,此时就可以实现访问其中一台web服务器时都可以看到相同的页面。

案例拓扑结构图

 

先配置web1.

配置dns实现解析与轮询功能

 
 
  1. # vim /var/named/zzu.com.db 
  2. $TTL 600 
  3. @          IN SOA  ns.zzu.com. admin.zzu.com ( 
  4.                     2012071501 
  5.                     30M 
  6.                     15M 
  7.                     1W 
  8.                     1D) 
  9.            IN   NS     192.168.0.100 
  10. ns         IN   A      192.168.0.101 
  11. ns         IN   A      192.168.0.100 
  12. www        IN   A      192.168.0.100 
  13. www        IN   A      192.168.0.101 
  14. ~                                      
  15. # vim /var/named/192.168.0.db 
  16. $TTL    600 
  17. @    IN   SOA   ns.zzu.com  admin.zzu.com ( 
  18.          2012071501 
  19.          30M 
  20.          15M 
  21.          1W 
  22.          1D) 
  23.  
  24.        IN   ns  ns.zzu.com. 
  25. 100    IN   PTR ns.zzu.com. 
  26. 102    IN   PTR ns.zzu.com. 
  27. 100    IN   PTR www.zzu.com. 
  28. 101    IN   PTR www.zzu.com. 
  29.  
  30. [root@localhost named]# vim /etc/resolv.conf  #dns指向本服务器 
  31. [root@localhost named]# dig -t A www.zzu.com 
  32.  
  33. <<>> DiG 9.3.6-P1-RedHat-9.3.6-20.P1.el5 <<>> -t A www.zzu.com 
  34. ;; global options:  printcmd 
  35. ;; Got answer: 
  36. ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 31504 
  37. ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 1, ADDITIONAL: 0 
  38.  
  39. ;; QUESTION SECTION: 
  40. ;www.zzu.com.           IN  A 
  41.  
  42. ;; ANSWER SECTION: 
  43. www.zzu.com.        600 IN  A   192.168.0.101 #  注意,下边就轮询到了0.100 
  44. www.zzu.com.        600 IN  A   192.168.0.100 
  45. ;; AUTHORITY SECTION: 
  46. zzu.com.        600 IN  NS  192.168.0.100.zzu.com. 
  47.  
  48. ;; Query time: 5 msec 
  49. ;; SERVER: 192.168.0.100#53(192.168.0.100) 
  50. ;; WHEN: Fri Jul 13 05:37:58 2012 
  51. ;; MSG SIZE  rcvd: 89 
  52.  
  53. [root@localhost named]# dig -t A www.zzu.com 
  54.  
  55. <<>> DiG 9.3.6-P1-RedHat-9.3.6-20.P1.el5 <<>> -t A www.zzu.com 
  56. ;; global options:  printcmd 
  57. ;; Got answer: 
  58. ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 22071 
  59. ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 1, ADDITIONAL: 0 
  60.  
  61. ;; QUESTION SECTION: 
  62. ;www.zzu.com.           IN  A 
  63.  
  64. ;; ANSWER SECTION: 
  65. www.zzu.com.        600 IN  A   192.168.0.100 
  66. www.zzu.com.        600 IN  A   192.168.0.101 
  67.  
  68. ;; AUTHORITY SECTION: 
  69. zzu.com.        600 IN  NS  192.168.0.100.zzu.com. 
  70.  
  71. ;; Query time: 5 msec 
  72. ;; SERVER: 192.168.0.100#53(192.168.0.100) 
  73. ;; WHEN: Fri Jul 13 05:38:02 2012 
  74. ;; MSG SIZE  rcvd: 89 

dns配置完毕 编译安装配置apache 编译过程依赖pcre-devel 编译安装前务必要确保该软件包安装上。

 
 
  1. [root@localhost httpd-2.4.2]# vim /etc/httpd/httpd.conf 
  2. 编译安装apr 
  3. # tar xf apr-1.4.6.tar.bz2  
  4. #  cd apr-1.4.6 
  5. #   ./configure --prefix=/usr/local/apr 
  6. #  make &&make install 
  7. 编译安装apr-util  
  8. #  tar xf apr-util-1.4.1.tar.bz2  
  9. # cd apr-util-1.4.1 
  10. #./configure --prefix=/usr/local/apr-util --with apr=/usr/local/apr 
  11. # make &&make install 
  12.  编译安装httpd  
  13. #  tar xf httpd-2.4.2.tar.bz2  
  14. # cd httpd-2.4.2 
  15. #  ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --enable-so --enable-ssl --enable-cgi -enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util 

 

 
 
  1. # vim /etc/rc.d/init.d/httpd  #为httpd服务提供脚本
  2.  
  3.  #!/bin/bash 
  4. # httpd        Startup script for the Apache HTTP Server 
  5. # chkconfig: - 85 15 
  6. # description: Apache is a World Wide Web server.  It is used to serve \ 
  7. #          HTML files and CGI. 
  8. # processname: httpd 
  9. # config: /etc/httpd/conf/httpd.conf 
  10. # config: /etc/sysconfig/httpd 
  11. # pidfile: /var/run/httpd.pid 
  12.  
  13. # Source function library. 
  14. . /etc/rc.d/init.d/functions 
  15.  
  16. if [ -f /etc/sysconfig/httpd ]; then 
  17.         . /etc/sysconfig/httpd 
  18. fi 
  19.  
  20. # Start httpd in the C locale by default. 
  21. HTTPD_LANG=${HTTPD_LANG-"C"} 
  22.  
  23. # This will prevent initlog from swallowing up a pass-phrase prompt if 
  24. # mod_ssl needs a pass-phrase from the user. 
  25. INITLOG_ARGS="" 
  26.  
  27. # Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server 
  28. # with the thread-based "worker" MPM; BE WARNED that some modules may not 
  29. # work correctly with a thread-based MPM; notably PHP will refuse to start. 
  30.  
  31. # Path to the apachectl script, server binary, and short-form for messages. 
  32. apachectl=/usr/local/apache/bin/apachectl 
  33. httpd=${HTTPD-/usr/local/apache/bin/httpd} 
  34. prog=httpd 
  35. pidfile=${PIDFILE-/var/run/httpd.pid} 
  36. lockfile=${LOCKFILE-/var/lock/subsys/httpd} 
  37. RETVAL=0 
  38.  
  39. start() { 
  40.         echo -n $"Starting $prog: " 
  41.         LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS 
  42.         RETVAL=$? 
  43.         echo 
  44.         [ $RETVAL = 0 ] && touch ${lockfile} 
  45.         return $RETVAL 
  46.  
  47. stop() { 
  48.     echo -n $"Stopping $prog: " 
  49.     killproc -p ${pidfile} -d 10 $httpd 
  50.     RETVAL=$? 
  51.     echo 
  52.     [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile} 
  53. reload() { 
  54.     echo -n $"Reloading $prog: " 
  55.     if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then 
  56.         RETVAL=$? 
  57.         echo $"not reloading due to configuration syntax error" 
  58.         failure $"not reloading $httpd due to configuration syntax error" 
  59.     else 
  60.         killproc -p ${pidfile} $httpd -HUP 
  61.         RETVAL=$? 
  62.     fi 
  63.     echo 
  64.  
  65. # See how we were called. 
  66. case "$1" in 
  67.   start) 
  68.     start 
  69.     ;; 
  70.   stop) 
  71.     stop 
  72.     ;; 
  73.   status) 
  74.         status -p ${pidfile} $httpd 
  75.     RETVAL=$? 
  76.     ;; 
  77.   restart) 
  78.     stop 
  79.     start 
  80.     ;; 
  81.   condrestart) 
  82.     if [ -f ${pidfile} ] ; then 
  83.         stop 
  84.         start 
  85.     fi 
  86.     ;; 
  87.   reload) 
  88.         reload 
  89.     ;; 
  90.   graceful|help|configtest|fullstatus) 
  91.     $apachectl $@ 
  92.     RETVAL=$? 
  93.     ;; 
  94.   *) 
  95.     echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}" 
  96.     exit 1 
  97. esac 
  98.  
  99. exit $RETVAL 
  100.  
  101. # chmod +x /etc/rc.d/init.d/httpd 
  102. # chkconfig --add httpd 
  103. # chkconfig httpd on 
  104. # service httpd start 

 

 
 
  1. 编译安装mysql,下步为提供php动态页面,需要用到mysql 所以这里这需要安装就可以了不需要启动。 
  2.  
  3. [root@localhost ~]# tar xf mysql-5.5.24-linux2.6-i686.tar.gz  -C /usr/local 
  4. [root@localhost ~]# cd /usr/local 
  5. [root@localhost local]# ln -s mysql-5.5.24-linux2.6-i686/ mysql 
  6. [root@localhost local]# cd mysql 
  7. [root@localhost mysql]# chown -R mysql:mysql . 
  8. [root@localhost mysql]#  scripts/mysql_install_db --user=mysql --datadir=/mydata/data  
  9. Installing MySQL system tables... 
  10. OK 
  11. Filling help tables... 
  12. OK 
  13.  
  14. To start mysqld at boot time you have to copy 
  15. support-files/mysql.server to the right place for your system 
  16.  
  17. PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER ! 
  18. To do so, start the server, then issue the following commands: 
  19.  
  20. ./bin/mysqladmin -u root password 'new-password' 
  21. ./bin/mysqladmin -u root -h localhost.localdomain password 'new-password' 
  22.  
  23. Alternatively you can run: 
  24. ./bin/mysql_secure_installation 
  25.  
  26. which will also give you the option of removing the test 
  27. databases and anonymous user created by default.  This is 
  28. strongly recommended for production servers. 
  29.  
  30. See the manual for more instructions. 
  31.  
  32. You can start the MySQL daemon with: 
  33. cd . ; ./bin/mysqld_safe & 
  34.  
  35. You can test the MySQL daemon with mysql-test-run.pl 
  36. cd ./mysql-test ; perl mysql-test-run.pl 
  37.  
  38. Please report any problems with the ./bin/mysqlbug script! 
  39.  
  40. [root@localhost mysql]# chown -R root . 
  41. [root@localhost mysql]# cp support-files/my-large.cnf  /etc/my.cnf 
  42. [root@localhost mysql]# vim /etc/ld.so.conf 
  43. [root@localhost mysql]# ldconfig 
  44.  
  45. 到此为止mysql就配置完毕,此时不需要启动服务。 

编译安装php

 
 
  1. # tar xf php-5.3.14.tar.bz2 
  2. # cd php-5.3.14 
  3. # ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml  --with-apxs2=/usr/local/apache/bin/apxs --with-mcrypt  --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2  
  4.  
  5. # make && make intall 
  6.  
  7. 为php提供配置文件: 
  8. # cp php.ini-production /etc/php.ini 
  9.  
  10. 3、 编辑apache配置文件httpd.conf,以apache支持php 
  11.   
  12.  # vim /etc/httpd/httpd.conf 
  13.  1、添加如下二行 
  14.    AddType application/x-httpd-php  .php 
  15.    AddType application/x-httpd-php-source  .phps 
  16.  
  17.  2、定位至DirectoryIndex index.html  
  18.    修改为: 
  19.     DirectoryIndex  index.php  index.html 
  20.  
  21. 而后重新启动httpd,或让其重新载入配置文件即可测试php是否已经可以正常使用。 

配置Discuz

 
 
  1. 为Discuz运行创建mysql用户 
  2. # mysql -u root  
  3. mysql> CREATE DATABASE discuz;    //创建数据库discuz 
  4. mysql> GRANT ALL ON discuz.* TO discuz@192.168.56.102 IDENTIFIED BY "discuz";   //创建mysql用户discuz并给于对discuz数据库所有权,并设置密码为discuz 
  5. mysql> FLUSH PRIILEGES;     //刷新授权表 
  6. mysql> quit   //退出 
  7.  
  8. 2、安装Discuz 
  9. # unzip Discuz_7.2_FULL_SC_GBK.zip /root 
  10. # mv /root/upload/* /var/www/Dicuz 

详细配置请参考我的推荐博文<快速建站LAMP>,这里就不详细介绍

 

搭建NFS服务器,这个很简单,只需安装nfs-utils即可,配置也很简单

 
 
  1. # vim /etc/exports 
  2.  /var/www/Discuz  192.168.0.101(rw) 
  3. 执行exportfs -a 即可实现共享 

 

web2的配置,在web2只需搭建LNMP环境,但重要的是提供Mysql服务,因为本案例实现的是分离Mysql实现的负载均衡,来自web1,和web2的数据都存储在web2的mysql中,web1中的数据只是提供编译安装php是需要用到mysql。搭建mysql课参考web1中mysql的编译安装。

 
 
  1. [root@localhost ~]# mount -t nfs 192.168.0.100:/var/www/Discuz      /Discuz 
  2. 可以编辑/etc/fstab 设置开机自动挂载。 

此时一个基于dns轮询的双web服务器集群就搭建完毕。用户访问的时候,不管轮询到哪台服务器都可以访问到相同的数据。此处各个阶段的测试图片都没有给大家展示,希望大家在每个阶段都做好测试工作,随时发现错误并改正,这样才有利于下面的工作顺利进行,更能提高效率。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值