设为首页 收藏本站
查看: 1779|回复: 1

[经验分享] DNS实现负载均衡的discuz论坛服务

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-10-20 11:09:56 | 显示全部楼层 |阅读模式
   这篇文字以搭建discuz论坛展开,但是,本意不是这个,这个实验,我采用了NFS的文件系统,DNS服务,php的fpm模式,两个web服务,最后还有一个MariaDB的数据库,采用分离式的LAMP平台,实现数据共享。
拓扑图如下:
%E6%8B%93%E6%89%912.JPG
实验的宗旨:
      我们以任意的客户端发送请求,通过DNS服务器做轮询的负载均衡,分别会访问不同的web主机,后端,我将NFS服务器分别挂载至两台web服务器和php的服务器,php服务器和web服务器之间通过fpm的形式交互,同时,后端的数据可以为这个拓扑中的任意主机使用。
实验流程:
1、搭建web服务器(两台web服务器的配置一致)
安装apr的软件包
1
2
3
4
[iyunv@localhost src]# tar jxf apr-1.5.0.tar.bz2
[iyunv@localhost src]# cd apr-1.5.0
[iyunv@localhost apr-1.5.0]# ./configure –prefix=/usr/local/apr
[iyunv@localhost src]# make && make install



安装apr-util软件包
1
2
3
[iyunv@localhost src]# tar jxf apr-util-1.5.3.tar.bz2
[iyunv@localhost src]# cd apr-util-1.5.3
[iyunv@localhost 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
[iyunv@localhost ~]# yum install pcre-devel
[iyunv@localhost src]# tar jxf httpd-2.4.9.tar.bz2
[iyunv@localhost src]# cd httpd-2.4.9
[iyunv@localhost 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
[iyunv@localhost httpd-2.4.9]#make && make install



把httpd服务的命令加入系统的环境变量中
1
2
3
[iyunv@localhost ~]# vim /etc/profile.d/httpd.sh
export  PATH=/usr/local/apache/bin:$PATH
[iyunv@localhost ~]# source /etc/profile.d/httpd.sh




2、MariaDB数据库服务器搭建

解压二进制包于指定位置:
1
[iyunv@localhost src]# tar zxf mariadb-5.5.36-linux-x86_64.tar.gz -C /usr/local/



为MariaDB添加用户和组,为系统用户
1
2
[iyunv@localhost src]# groupadd -r mysql
[iyunv@localhost src]# useradd -g mysql -r mysql



将另一块磁盘作为数据存放位置,且做逻辑卷挂载
1
2
3
4
5
6
7
8
9
10
[iyunv@localhost src]# 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   Id  System
/dev/sdb1               1        2610    20964793+  8e  Linux LVM



查看新建的磁盘是否被dm服务占用
1
2
[iyunv@localhost src]# dmsetup status
sdb1: 0 41929587 linear



解除占用
1
[iyunv@localhost src]# dmsetup remove sdb1



创建物理卷
1
2
3
[iyunv@localhost src]# pvcreate /dev/sdb1
  dev_is_mpath: failed to get device for 8:17
  Physical volume "/dev/sdb1" successfully created



创建卷组
1
2
[iyunv@localhost src]# vgcreate myvg /dev/sdb1
  Volume group "myvg" successfully created



创建逻辑卷
1
2
[iyunv@localhost src]# lvcreate -L 15G -n mylv myvg
  Logical volume "mylv" created



可以用lvs或lvdisplay命令查看
格式化分区
1
[iyunv@localhost src]# mke2fs -t ext4 -b 1024 -m 3 -L mysql /dev/myvg/mylv



让逻辑卷能自动挂载在/MySQL目录下
1
2
3
4
[iyunv@localhost src]# mkdir /MySQL
[iyunv@localhost src]# vim /etc/fstab
LABEL=mysql             /MySQL                  ext4    defaults        0 0
[iyunv@localhost src]# mount -a



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

创建软链接
1
2
3
[iyunv@localhost src]# cd /usr/local/
[iyunv@localhost 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
[iyunv@localhost local]# cd mysql/support-files/
[iyunv@localhost support-files]# mkdir /etc/mysql
[iyunv@localhost support-files]# cp my-large.cnf /etc/mysql/my.cnf
  
[iyunv@localhost support-files]# cp mysql.server /etc/rc.d/init.d/mysqld
[iyunv@localhost support-files]# chkconfig –add mysqld
[iyunv@localhost support-files]# 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
[iyunv@localhost support-files]# mkdir /MySQL/data
[iyunv@localhost support-files]# vim /etc/mysql/my.cnf
datadir = /MySQL/data
[iyunv@localhost mysql]# pwd
/usr/local/mysql
[iyunv@localhost mysql]# ./scripts/mysql_install_db –user=mysql –datadir=/MySQL/data/



将MariaDB的命令加入到环境变量中
1
2
[iyunv@localhost mysql]# vim /etc/profile.d/mysql.sh
export  PATH=/usr/local/msyql/bin:$PATH



重读配置文件
1
[iyunv@localhost mysql]# source /etc/profile.d/mysql.sh



启动MariaDB数据库服务
1
2
[iyunv@localhost mysql]# service mysqld start
Starting MySQL….                                         [  OK  ]



可以登录了
1
2
3
4
5
6
7
8
9
10
[iyunv@localhost mysql]# 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
[iyunv@localhost ~]# yum install libxml2-devel
[iyunv@localhost ~]# yum install bzip2-devel



编译安装php服务器
1
2
3
4
5
6
7
8
[iyunv@localhost src]# tar jxf php-5.4.26.tar.bz2
[iyunv@localhost src]# cd php-5.4.26
[iyunv@localhost 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
[iyunv@localhost php-5.4.26]#make && make install



注意,由于apache采用的是event模式,所以,php在编译时,要加- -enable-maintainer-zts
提供配置文件
1
[iyunv@localhost php-5.4.26]# cp php.ini-production /etc/php.ini



提供 SysV init脚本,并将其添加至服务列表
1
2
3
[iyunv@localhost php-5.4.26]# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
[iyunv@localhost php-5.4.26]# chmod a+x /etc/rc.d/init.d/php-fpm
[iyunv@localhost php-5.4.26]# chkconfig –add php-fpm



为php-fpm服务提供配置文件
1
2
[iyunv@localhost php]# cd /usr/local/php/etc/
[iyunv@localhost 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
[iyunv@localhost ~]# 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
[iyunv@localhost ~]# service nfs start



将另外一块准备好的磁盘做成逻辑卷:
1
2
3
4
[iyunv@localhost ~]# fdisk /dev/sdb #且要格式成LVM格式
[iyunv@localhost ~]# pvcreate /dev/sdb1
[iyunv@localhost ~]# vgcreate myvg /dev/sdb1
[iyunv@localhost ~]# lvcreate -L 16G -n mylv myvg



格式化逻辑卷,并挂在到/share目录,支持开机自动挂载
1
2
3
4
5
[iyunv@localhost ~]# mke2fs -t ext4 -b 2048 -m 3 -L share /dev/myvg/mylv
[iyunv@localhost ~]# mkdir /share
[iyunv@localhost ~]# vim /etc/fstab
LABEL=share             /share                  ext4    defaults        0 0
[iyunv@localhost ~]# mount -a #挂载之,可以用mount查看



编辑nfs的配置选项,使/share被共享
1
2
[iyunv@localhost ~]# vim /etc/exports
/share          192.168.77.0/24(rw,no_root_squash,no_all_squash,insecure)



注意:网段地址一定不能写错,以下是被我写成192.168.77.0所报的错
1
2
[iyunv@localhost ~]# 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
[iyunv@localhost share]# pwd
/share
[iyunv@localhost share]# unzip Discuz_X2.5_SC_GBK.zip



查看目录的文件
1
2
[iyunv@localhost ~]# ls /share/
Discuz_X2.5_SC_GBK.zip  lost+found  readme  upload  utility




5、把nfs共享的目录分别共享给两台web服务器和php服务器
web1:
1
2
3
4
[iyunv@localhost ~]# mkdir /web/
[iyunv@localhost ~]# mount -t nfs 192.168.77.144:/share  /web/
[iyunv@localhost ~]# ls /web/
Discuz_X2.5_SC_GBK.zip  lost+found  readme  upload  utility



web2:
1
2
3
4
[iyunv@localhost ~]# mkdir /web/
[iyunv@localhost ~]# mount -t nfs 192.168.77.144:/share  /web/
[iyunv@localhost ~]# ls /web/
Discuz_X2.5_SC_GBK.zip  lost+found  readme  upload  utility



php服务器:
1
2
3
4
[iyunv@localhost ~]# mkdir /web/
[iyunv@localhost ~]# mount -t nfs 192.168.77.144:/share  /web/
[iyunv@localhost ~]# ls /web/
Discuz_X2.5_SC_GBK.zip  lost+found  readme  upload  utility




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
[iyunv@localhost ~]# 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
[iyunv@localhost etc]# service php-fpm start



在浏览器先输入ip地址测试
http://192.168.77.152/upload
点击“我同意”,继续安装,报以下错误:
%E5%9B%BE%E7%89%871%281%29.jpg
这是没有权限引起的,然后,在/web/upload目录下,改变这些目录、文件的权限
1
2
3
4
[iyunv@localhost upload]# chmod -R a+w data/
[iyunv@localhost upload]# chmod -R a+w uc_server/
[iyunv@localhost upload]# chmod -R a+w config/
[iyunv@localhost upload]# chmod -R a+w uc_client/data/cache/



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

%E5%9B%BE%E7%89%872%281%29.jpg
点击安装全新的discuz,填入数据库信息,上面已经新建表,授权用户
%E5%9B%BE%E7%89%873%281%29.jpg
安装完成
%E5%9B%BE%E7%89%874%281%29.jpg
最后用浏览器访问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
[iyunv@localhost ~]# 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安装后的是否一致

%E5%9B%BE%E7%89%876%281%29.jpg
%E5%9B%BE%E7%89%877%281%29.jpg
注意:两个web服务器的配置必须要一致,我们可以编译好一个配置文件,然后scp拷贝过去
下面,我们在web2上新建用户,并且发表帖子,然后到web1服务上,查看数据是否同步,是否一致
在web2上注册用户
%E5%9B%BE%E7%89%878%281%29.jpg
发表帖子
%E5%9B%BE%E7%89%879%281%29.jpg
在web1上登录刚刚注册的用户
%E5%9B%BE%E7%89%8710%281%29.jpg
查看在web2上发表的帖子
%E5%9B%BE%E7%89%8711.jpg
可以看出,数据是同步的,是一致的

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

配置主文件,如下所示
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[iyunv@localhost ~]# 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
[iyunv@localhost ~]# 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
[iyunv@localhost ~]# vim /etc/named.rfc1912.zones
$TTL    3600
$ORIGIN  365lsy.com.
@       IN      SOA     ns.365lsy.com.  admin.365lsy.com. (
        2014081601
        2H
        10M
        5D
        10H
)
        IN      NS      ns
ns      IN      A       192.168.77.133
discuz  IN      A       192.168.77.152
discuz  IN      A       192.168.77.154



启动服务:
1
[iyunv@localhost ~]# service named start




测试,将一台windows客户端连进这个网络,更改DNS地址,指向我们上面配置的DNS服务器
我们可以用nslookup命令查看一下
%E5%9B%BE%E7%89%8712.jpg
然后,在一台linux下测验:
1
2
[iyunv@localhost ~]# 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
[iyunv@localhost ~]# 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 SIZE  rcvd: 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
[iyunv@localhost ~]# 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 SIZE  rcvd: 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的服务,多请求时,会发起多个系统调用的函数,可能会被阻塞,导致网站的整体性能。


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-26259-1-1.html 上篇帖子: BackTrack 5 r3初体验 下篇帖子: Web漏洞扫描器-UNISCAN 6.2发布 discuz
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表