您的位置:首页 > 其它

基于Lvs-nat模型的discuz负载均衡实现

2016-05-27 21:41 513 查看
前言:

lvs-nat模型需要一台机器作为前端VS(Virtual Server)主机,和两台作为后端的RS(Real Server)才能够实现负载均衡效果
因为需要负载均衡应用Discuz,同一客户端请求如果被调度到不同的RS上,无法保障用户访问会话持久不丢失,和数据的一致性,所以还需要考虑会话保持和数据共享的问题。

会话保持的解决方案有三种:源地址哈希、会话集群、会话服务器,这里选择源地址;即将来自同一客户端的请求调度至同一台服务器,只需要将lvs集群的调度算法配置为SH(Source Hash)即可,比较简单,但实际上这种方案存在弊端,如果初始调度的RS宕机,将请求调度至其他RS时,就无法获取到之前的会话了,此处我们暂时不考虑这一情况!随后我会提供相关博客。

数据共享的解决方案在前面一篇文章(rsync+inotify)中提到过,此处我们选择NFS,由于其搭建起来比较方便,性能还不错。

环境需求:

HostVS
OS:CentOS-7-x86_64

hostname:ws1
eno16777736:10.0.0.61/8(VIP)
eno33554984:172.18.64.1/16(DIP)
gateway:10.0.0.254

HostRS1
OS:CentOS-7-x86_64
hostname:ws2 eno16777736:172.18.64.2/16 (RIP1) gateway:172.18.64.254
HostRS2 OS:CentOS-7-x86_64
hostname:ws3 eno16777736:172.18.64.3/16 (RIP2) gateway:172.18.64.254
HostDB

OS:CentOS-7-x86_64
hostname:ws4 eno16777736:172.18.64.4/16 gateway:172.18.64.254

时间同步:

# ntpdate cn.pool.ntp.org
# hwclock --sysohc

安装软件:

HostDB

安装二进制mariadb-5.5.46
详细配置请入传送门:http://wscto.blog.51cto.com/11249394/1783131

安装NFS
# yum install -y nfs-utils


HostRS2

安装nginx,注意nginx属于epel源
# yum install-y nginx php-fpm php-mbstring php-mysql nfs-utils mariadb


HostRS1
安装nginx,注意nginx属于epel源
# yum install-y nginx php-fpm php-mbstring php-mysql nfs-utils mariadb


HostVS
安装LVS的cli接口程序ipvsadm

# yum install -y ipvsadm


配置集群

HostDB
配置mariadb
安全初始化完成后,创建discuz数据库和discuz用户,并授权其可远程操作数据库
# mysql_secure_installation
# mysql -u root -p

> create database discuz;
> create user 'discuz'@'lodalhost' identified by '123456';
> grant all privileges on discuz.* to 'discuz'@'%' identified by '123456';
> flush privileges;


配置NFS
# mkdir /nfshare/
# ls -ld /nfshare/
drwxr-xr-x 2 root root 6 May 9 17:01 /nfshare/
# echo "/nfshare/ 10.0.0.62(rw,no_root_squash,async) 10.0.0.63(rw,no_root_squash,async)" > /etc/exports
# systemctl start rpcbind
Starting rpcbind:                                          [  OK  ]
# systemctl start nfs-server
Starting NFS services:                                     [  OK  ]
Starting NFS quotas:                                       [  OK  ]
Starting NFS mountd:                                       [  OK  ]
Starting NFS daemon:                                       [  OK  ]
Starting RPC idmapd:                                       [  OK  ]
# systemctl enable rpcbind
# systemctl enable nfs-server
# chkconfig rpcbind --list
rpcbind            0:off    1:off    2:on    3:on    4:on    5:on    6:off
# chkconfig nfs --list
nfs                0:off    1:off    2:on    3:on    4:on    5:on    6:off
# showmount -e 127.0.0.1
Export list for 127.0.0.1:
/nfshare/ 10.0.0.62,10.0.0.63
注意:rpcbind和nfs两个服务启动顺序不能更换,否则会出问题

解压discuz的程序包至/nfsshare/目录
# mkdir /nfshare/discuz
# unzip /Discuz_X3.2_SC_UTF8.zip -d /nfshare/discuz/
# ls /nfshare/discuz/
readme  upload  utility


HostR2
测试链接HostDB上的mariadb
# mysql -h 172.18.64.4 -u discuz -p


挂载HostDB上的NFS共享存储目录,属主属组修改为apache
# showmount -e 172.18.64.4
Export list for 172.18.64.4:
/nfshare/ 10.0.0.62,10.0.0.63
# mkdir /htdocs
# ls -ld /htdocs/
drwxr-xr-x 2 root root 6 May 9 17:05 /htdocs/
# mount -t nfs 172.18.64.4:/nfshare /htdocs
# ls /htdocs/
discuz
# chown -R apache:apache /htdocs/discuz/


启动php-fpm
# systemctl start php-fpm.service
# ss -tnl | grep 9000


配置nginx的配置文件
# vim /etc/nginx/nginx.conf
server {
listen     80;
server_name  ws3

# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;

location / {
root    /htdocs/discuz/upload;
index index.html index.htm index.php;
}

location ~ \.php$ {
root  /htdocs/discuz/upload;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;

include /etc/nginx/fastcgi.conf;
}

error_page 404 /404.html;
location = /40x.html {
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}

# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# nginx
# ss -tnl | grep 80
LISTEN     0      128          *:80                       *:*


测试访问discuz安装页面主机加上最后的/
# curl -I http://172.18.64.3/install/ HTTP/1.1 200 OK
Server: nginx/1.6.3
Date: Mon, 09 May 2016 10:23:50 GMT
Content-Type: text/html
Connection: keep-alive
X-Powered-By: PHP/5.4.16


HostRS1
配置同上

HostVS
打开核心转发功能
# echo 1 > /proc/sys/net/ipv4/ip_forward


先使用rr调度算法,期望会话丢失情况出现,若出现会话丢失情况,改用sh(源地址哈希)调度算法再测试,期望会话能保持
# ipvsadm -A -t 10.0.0.61:80 -s rr
# ipvsadm -a -t 10.0.0.61:80 -r 172.18.64.2:80 -m
# ipvsadm -a -t 10.0.0.61:80 -r 172.18.64.3:80 -m


测试:
浏览器访问http://10.0.0.61/install/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息