w2ewe 发表于 2014-10-20 11:09:56

DNS实现负载均衡的discuz论坛服务

   这篇文字以搭建discuz论坛展开,但是,本意不是这个,这个实验,我采用了NFS的文件系统,DNS服务,php的fpm模式,两个web服务,最后还有一个MariaDB的数据库,采用分离式的LAMP平台,实现数据共享。
拓扑图如下:

实验的宗旨:
      我们以任意的客户端发送请求,通过DNS服务器做轮询的负载均衡,分别会访问不同的web主机,后端,我将NFS服务器分别挂载至两台web服务器和php的服务器,php服务器和web服务器之间通过fpm的形式交互,同时,后端的数据可以为这个拓扑中的任意主机使用。
实验流程:
1、搭建web服务器(两台web服务器的配置一致)
安装apr的软件包

1
2
3
4
# tar jxf apr-1.5.0.tar.bz2
# cd apr-1.5.0
# ./configure –prefix=/usr/local/apr
# make && make install




安装apr-util软件包

1
2
3
# tar jxf apr-util-1.5.3.tar.bz2
# cd apr-util-1.5.3
# ./configure- -prefix=/usr/local/apr-util- -with-apr=/usr/local/apr/




安装httpd软件
先解决关于pcre的问题,安装pcre的开发包

1
2
3
4
5
6
7
8
# yum install pcre-devel
# tar jxf httpd-2.4.9.tar.bz2
# cd httpd-2.4.9
# ./configure--prefix=/usr/local/apache--sysconfdir=/etc/httpd2
--enable-so--enable-ssl--enable-cgi--enable-rewrite--with-zlib--with-pcre
--with-apr=/usr/local/apr--with-apr-util=/usr/local/apr-util/--enable-modules=most
--enable-mpms-shared=all--enable-mpm=event
#make && make install




把httpd服务的命令加入系统的环境变量中

1
2
3
# vim /etc/profile.d/httpd.sh
exportPATH=/usr/local/apache/bin:$PATH
# source /etc/profile.d/httpd.sh





2、MariaDB数据库服务器搭建

解压二进制包于指定位置:

1
# tar zxf mariadb-5.5.36-linux-x86_64.tar.gz -C /usr/local/




为MariaDB添加用户和组,为系统用户

1
2
# groupadd -r mysql
# useradd -g mysql -r mysql




将另一块磁盘作为数据存放位置,且做逻辑卷挂载

1
2
3
4
5
6
7
8
9
10
# fdisk -l /dev/sdb
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xa295ba74

   Device Boot      Start         End      Blocks   IdSystem
/dev/sdb1               1      2610    20964793+8eLinux LVM




查看新建的磁盘是否被dm服务占用

1
2
# dmsetup status
sdb1: 0 41929587 linear




解除占用

1
# dmsetup remove sdb1




创建物理卷

1
2
3
# pvcreate /dev/sdb1
dev_is_mpath: failed to get device for 8:17
Physical volume "/dev/sdb1" successfully created




创建卷组

1
2
# vgcreate myvg /dev/sdb1
Volume group "myvg" successfully created




创建逻辑卷

1
2
# lvcreate -L 15G -n mylv myvg
Logical volume "mylv" created




可以用lvs或lvdisplay命令查看
格式化分区

1
# mke2fs -t ext4 -b 1024 -m 3 -L mysql /dev/myvg/mylv




让逻辑卷能自动挂载在/MySQL目录下

1
2
3
4
# mkdir /MySQL
# vim /etc/fstab
LABEL=mysql             /MySQL                  ext4    defaults      0 0
# mount -a




可以使用mount命令直接检测是否已经挂载成功

创建软链接

1
2
3
# cd /usr/local/
# ln -sv mariadb-5.5.36-linux-x86_64/ mysql
`mysql' -> `mariadb-5.5.36-linux-x86_64/'




为MariaDB提供配置文件和启动脚本

1
2
3
4
5
6
7
8
# cd mysql/support-files/
# mkdir /etc/mysql
# cp my-large.cnf /etc/mysql/my.cnf

# cp mysql.server /etc/rc.d/init.d/mysqld
# chkconfig –add mysqld
# chkconfig –list mysqld
mysqld          0:off 1:off 2:on 3:on 4:on 5:on 6:off




编辑配置文件,初始化MariaDB服务器

1
2
3
4
5
6
# mkdir /MySQL/data
# vim /etc/mysql/my.cnf
datadir = /MySQL/data
# pwd
/usr/local/mysql
# ./scripts/mysql_install_db –user=mysql –datadir=/MySQL/data/




将MariaDB的命令加入到环境变量中

1
2
# vim /etc/profile.d/mysql.sh
exportPATH=/usr/local/msyql/bin:$PATH




重读配置文件

1
# source /etc/profile.d/mysql.sh




启动MariaDB数据库服务

1
2
# service mysqld start
Starting MySQL….                                       




可以登录了

1
2
3
4
5
6
7
8
9
10
# mysql
Welcome to the MariaDB monitor.Commands end with ; or g.
Your MariaDB connection id is 2
Server version: 5.5.36-MariaDB-log MariaDB Server

Copyright (c) 2000, 2014, Oracle, Monty Program Ab and others.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

MariaDB [(none)]>





3、以fpm的形式安装php服务器
解决依赖包

1
2
# yum install libxml2-devel
# yum install bzip2-devel




编译安装php服务器

1
2
3
4
5
6
7
8
# tar jxf php-5.4.26.tar.bz2
# cd php-5.4.26
# ./configure--prefix=/usr/local/php--with-mysql=mysqlnd
--with-mysqli=mysqlnd--with-openssl--enable-mbstring- -with-freetype-dir
--with-jpeg-dir--with-png-dir--with-zlib--with-libxml-dir--enable-xml
--enable-sockets--enable-fpm--with-mcrypt--with-config-file-path=/etc/
--with-config-file-scan-dir=/etc/php.d   --with-bz2--enable-maintainer-zts
#make && make install




注意,由于apache采用的是event模式,所以,php在编译时,要加- -enable-maintainer-zts
提供配置文件

1
# cp php.ini-production /etc/php.ini




提供 SysV init脚本,并将其添加至服务列表

1
2
3
# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
# chmod a+x /etc/rc.d/init.d/php-fpm
# chkconfig –add php-fpm




为php-fpm服务提供配置文件

1
2
# cd /usr/local/php/etc/
# cp php-fpm.conf.default php-fpm.conf




更改php-fpm的配置文件,如下所示

1
2
3
4
5
6
pid = /usr/local/php/var/run/php-fpm.pid
listen = 0.0.0.0:9000      #让其监听所有,如果考虑安全,可以采用iptables
pm.max_children = 30
pm.start_servers = 5
pm.min_spare_servers = 3
pm.max_spare_servers = 8





4、创建NFS共享服务器,提供discuz论坛软件
查看是否已经安装nfs的软件

1
2
3
# rpm -qa | grep nfs-utils
nfs-utils-lib-1.1.5-6.el6.x86_64
nfs-utils-1.2.3-39.el6.x86_64




启动服务:

1
# service nfs start




将另外一块准备好的磁盘做成逻辑卷:

1
2
3
4
# fdisk /dev/sdb #且要格式成LVM格式
# pvcreate /dev/sdb1
# vgcreate myvg /dev/sdb1
# lvcreate -L 16G -n mylv myvg




格式化逻辑卷,并挂在到/share目录,支持开机自动挂载

1
2
3
4
5
# mke2fs -t ext4 -b 2048 -m 3 -L share /dev/myvg/mylv
# mkdir /share
# vim /etc/fstab
LABEL=share             /share                  ext4    defaults      0 0
# mount -a #挂载之,可以用mount查看




编辑nfs的配置选项,使/share被共享

1
2
# vim /etc/exports
/share          192.168.77.0/24(rw,no_root_squash,no_all_squash,insecure)




注意:网段地址一定不能写错,以下是被我写成192.168.77.0所报的错

1
2
# mount -t nfs 192.168.77.144:/share/web/
mount.nfs: access denied by server while mounting 192.168.77.144:/share




下载discuz软件,放置在共享目录下,并且解压

1
2
3
# pwd
/share
# unzip Discuz_X2.5_SC_GBK.zip




查看目录的文件

1
2
# ls /share/
Discuz_X2.5_SC_GBK.ziplost+foundreadmeuploadutility





5、把nfs共享的目录分别共享给两台web服务器和php服务器
web1:

1
2
3
4
# mkdir /web/
# mount -t nfs 192.168.77.144:/share/web/
# ls /web/
Discuz_X2.5_SC_GBK.ziplost+foundreadmeuploadutility




web2:

1
2
3
4
# mkdir /web/
# mount -t nfs 192.168.77.144:/share/web/
# ls /web/
Discuz_X2.5_SC_GBK.ziplost+foundreadmeuploadutility




php服务器:

1
2
3
4
# mkdir /web/
# mount -t nfs 192.168.77.144:/share/web/
# ls /web/
Discuz_X2.5_SC_GBK.ziplost+foundreadmeuploadutility





6、在MariaDB数据库服务器里新建一个库,授权给discuz这个用户

1
2
3
MariaDB [(none)]> CREATE DATABASE discuz;
MariaDB [(none)]> GRANT ALL ON discuz.* TO discuz@'192.168.77.%' IDENTIFIED BY 'discuz';
MariaDB [(none)]> FLUSH PRIVILEGES;





7、配置httpd服务器,以httpd1为例,两个一致
编辑配置文件,开启虚拟主机,与php-fpm进行交互,关闭正向解析
添加内容如下所示

启用两个模块,支持虚拟主机、fcgi

1
2
3
Include /etc/httpd2/extra/httpd-vhosts.conf
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so




添加,解析php的功能

1
2
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps




默认作为主页的,添加index.php

1
DirectoryIndex index.html index.php




然后配置虚拟主机的配置文件

1
2
3
4
5
6
7
8
9
10
11
12
# vim /etc/httpd2/extra/httpd-vhosts.conf

    DocumentRoot "/web"
    ServerName discuz.365lsy.com
    ProxyRequests Off
    ProxyPassMatch ^/(.*.php)$ fcgi://192.168.77.143:9000/web/$1
      
      Options none
      AllowOverride none
      Require all granted







8、实验检测及效果展示
启动httpd服务器,然后再启动php-fpm服务器

1
# service php-fpm start




在浏览器先输入ip地址测试
http://192.168.77.152/upload
点击“我同意”,继续安装,报以下错误:

这是没有权限引起的,然后,在/web/upload目录下,改变这些目录、文件的权限

1
2
3
4
# chmod -R a+w data/
# chmod -R a+w uc_server/
# chmod -R a+w config/
# chmod -R a+w uc_client/data/cache/




以上步骤在NFS服务器上直接操作
刷新后重试


点击安装全新的discuz,填入数据库信息,上面已经新建表,授权用户

安装完成

最后用浏览器访问web服务器
如果只输入ip地址的话,浏览器会弹出"It Works"的字样,这是因为识别index.html作为主页了
解决方法:我们可以把/usr/local/apache/htdocs下面的index.html删除
上面我们输的时候,加了upload这个目录名,如果要直接输入ip地址,更改配置文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
# vim /etc/httpd2/extra/httpd-vhosts.conf

    DocumentRoot "/web/upload"
    ServerName discuz.365lsy.com
    ProxyRequests Off
    ProxyPassMatch ^/(.*.php)$ fcgi://192.168.77.143:9000/web/upload/$1
      
      Options none
      AllowOverride none
      Require all granted






然后,分别访问两个web服务器,查看discuz安装后的是否一致



注意:两个web服务器的配置必须要一致,我们可以编译好一个配置文件,然后scp拷贝过去
下面,我们在web2上新建用户,并且发表帖子,然后到web1服务上,查看数据是否同步,是否一致
在web2上注册用户

发表帖子

在web1上登录刚刚注册的用户

查看在web2上发表的帖子

可以看出,数据是同步的,是一致的

9、搭建DNS服务器,将discuz.365lsy.com分别指向这两台web

配置主文件,如下所示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# vim /etc/named.conf
options {
      directory       "/var/named";
      recursion yes;
};
logging {
      channel default_debug {
                file "data/named.run";
                severity dynamic;
      };
};
zone "." IN {
      type hint;
      file "named.ca";
};
include "/etc/named.rfc1912.zones";




编辑辅助配置文件

1
# vim /etc/named.rfc1912.zones




添加此项

1
2
3
4
zone "365lsy.com" IN {
      type master;
      file "365lsy.com.zone";
};




添加域配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# vim /etc/named.rfc1912.zones
$TTL    3600
$ORIGIN365lsy.com.
@       IN      SOA   ns.365lsy.com.admin.365lsy.com. (
      2014081601
      2H
      10M
      5D
      10H
)
      IN      NS      ns
ns      IN      A       192.168.77.133
discuzIN      A       192.168.77.152
discuzIN      A       192.168.77.154




启动服务:

1
# service named start





测试,将一台windows客户端连进这个网络,更改DNS地址,指向我们上面配置的DNS服务器
我们可以用nslookup命令查看一下

然后,在一台linux下测验:

1
2
# vim /etc/resolv.conf
nameserver 192.168.77.133




可以看出,DNS服务器为本机做了轮询,每次访问为不同的IP地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# dig -t A discuz.365lsy.com

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.17.rc1.el6_4.6 <<>> -t A discuz.365lsy.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 41508
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 1, ADDITIONAL: 1

;; QUESTION SECTION:
;discuz.365lsy.com. IN A

;; ANSWER SECTION:
discuz.365lsy.com. 3600 IN A 192.168.77.154
discuz.365lsy.com. 3600 IN A 192.168.77.152

;; AUTHORITY SECTION:
365lsy.com. 3600 IN NS ns.365lsy.com.

;; ADDITIONAL SECTION:
ns.365lsy.com. 3600 IN A 192.168.77.133

;; Query time: 2 msec
;; SERVER: 192.168.77.133#53(192.168.77.133)
;; WHEN: Mon Aug 11 20:30:11 2014
;; MSG SIZErcvd: 100





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# dig -t A discuz.365lsy.com

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.17.rc1.el6_4.6 <<>> -t A discuz.365lsy.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 7580
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 1, ADDITIONAL: 1

;; QUESTION SECTION:
;discuz.365lsy.com. IN A

;; ANSWER SECTION:
discuz.365lsy.com. 3600 IN A 192.168.77.152
discuz.365lsy.com. 3600 IN A 192.168.77.154

;; AUTHORITY SECTION:
365lsy.com. 3600 IN NS ns.365lsy.com.

;; ADDITIONAL SECTION:
ns.365lsy.com. 3600 IN A 192.168.77.133

;; Query time: 2 msec
;; SERVER: 192.168.77.133#53(192.168.77.133)
;; WHEN: Mon Aug 11 20:30:23 2014
;; MSG SIZErcvd: 100




可以看出,我们的windows客户端已经解析出两个web的地址,访问时,会轮询访问
linux客户端上,结果更加明显,返回的结果:
第一次是
discuz.365lsy.com. 3600 IN A 192.168.77.154
第二次是
discuz.365lsy.com. 3600 IN A 192.168.77.152
是轮询的不同结果,整个流程,在DNS服务器的作用下,就有了负载均衡的作用
实验总结:
    这个实验,在做的时候,都是比较容易的,但是,实验本身存在缺陷,对于大并发的访问情况下,它的MariaDB数据库服务器和NFS服务器就会成为整个架构的瓶颈,特别是NFS服务器,如果中间网络故障,就会使整个架构崩溃,另一种就是NFS是基于RPC的服务,多请求时,会发起多个系统调用的函数,可能会被阻塞,导致网站的整体性能。

42ewe 发表于 2014-10-21 17:59:23

支持一下
页: [1]
查看完整版本: DNS实现负载均衡的discuz论坛服务