设为首页 收藏本站
查看: 1595|回复: 0

[经验分享] 基于Lvs-nat模型的discuz负载均衡实现

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-5-30 09:09:12 | 显示全部楼层 |阅读模式
前言:

    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

时间同步:

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





安装软件:

HostDB

安装二进制mariadb-5.5.46


安装NFS
1
# yum install -y nfs-utils





HostRS2

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




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




HostVS
安装LVS的cli接口程序ipvsadm

1
# yum install -y ipvsadm




配置集群

HostDB
配置mariadb
安全初始化完成后,创建discuz数据库和discuz用户,并授权其可远程操作数据库
1
2
3
4
5
6
7
# 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 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/目录
1
2
3
4
# mkdir /nfshare/discuz
# unzip /Discuz_X3.2_SC_UTF8.zip -d /nfshare/discuz/
# ls /nfshare/discuz/
readme  upload  utility




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




挂载HostDB上的NFS共享存储目录,属主属组修改为apache
1
2
3
4
5
6
7
8
9
10
# 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
1
2
# systemctl start php-fpm.service
# ss -tnl | grep 9000




配置nginx的配置文件
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
26
27
28
29
30
31
32
33
34
35
36
# 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安装页面主机加上最后的/
1
2
3
4
5
6
7
# 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
打开核心转发功能
1
# echo 1 > /proc/sys/net/ipv4/ip_forward




   先使用rr调度算法,期望会话丢失情况出现,若出现会话丢失情况,改用sh(源地址哈希)调度算法再测试,期望会话能保持   
1
2
3
# 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/


运维网声明 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-223688-1-1.html 上篇帖子: 基于lvs-dr模型的discuz负载均衡实现 下篇帖子: 简单介绍,基于ldirectord的高可用lvs-dr调度器 discuz 模型
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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