基於lvs-dr模型的discuz負載均衡實現

前言:

lvs-dr模型與之前的lvs-nat模型情況基本差不多,只是網絡拓撲結構不同。


HostVS

OS:CentOS-7-x86_64

hostname:ws1

eno16777736: 10.0.0.60/8(DIP)

eno167777336:0: 10.0.0.61/32(VIP)

gateway:10.0.0.254


HostRS1

OS:CentOS-7-x86_64

hostname:ws2

eno16777736:10.0.0.101/8 (RIP1)

lo:0 10.0.0.61/32 (VIP)

gateway:10.0.0.254


HostRS2

OS:CentOS-7-x86_64

hostname:ws3

eno16777736:10.0.0.102/8(RIP2)

lo:0 10.0.0.61/32(VIP)

gateway:10.0.0.254


HostDB

OS:CentOS-7-x86_64

hostname:ws4

eno16777736: 10.0.0.202/8

gateway:10.0.0.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.101(rw,no_root_squash,async) 10.0.0.102(rw,no_root_squash,async)" > /etc/exports
# /etc/init.d/rpcbind start
Starting rpcbind:                        [  OK  ]
# /etc/init.d/nfs start
Starting NFS services:                      [  OK  ]
Starting NFS quotas:                      [  OK  ]
Starting NFS mountd:                        [  OK  ]
Starting NFS daemon:                       [  OK  ]
Starting RPC idmapd:                      [  OK  ]
# chkconfig rpcbind on
# chkconfig nfs on
# 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.101,10.0.0.102

exportfs -avr  檢查設置是否正確

注意:rpcbind和nfs兩個服務啓動順序不能更換,否則會出問題


解壓discuz的程序包至/nfsshare/目錄

# mkdir /nfshare/discuz
# unzip /Discuz_X3.2_SC_UTF8.zip -d /nfshare/discuz/
# ls /nfshare/discuz/
readme  upload  utility


HostRS1


給RS配置VIP之前先要禁止RS響應VIP的ARP請求

echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce


在本地迴環接口別名lo:0上配置VIP,並增添路由

ifconfig lo:0 10.0.0.61 broadcast 10.0.0.61 netmask 255.255.255.255 
route add -host 10.0.0.61 dev lo:0


測試鏈接HostDB上的mariadb

# mysql -h 10.0.0.202 -u discuz -p


掛載HostDB上的NFS共享存儲目錄,屬主屬組修改爲apache

# showmount -e 172.18.64.202
Export list for 172.18.64.202:
/nfshare/ 10.0.0.101,10.0.0.102
# mkdir /htdocs
# ls -ld /htdocs/
drwxr-xr-x 2 root root 6 May 9 17:05 /htdocs/
# mount -t nfs 172.18.64.202:/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  ws2

        # 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://10.0.0.101/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


HostRS2 

配置同上


HostVS

配置VIP地址,並打開核心轉發功能。

# ifconfig eno16777736:0 10.0.0.61 broadcast 10.0.0.61 netmask 255.255.255.255 up
# 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 10.0.0.101:80 -g
# ipvsadm -a -t 10.0.0.61:80 -r 10.0.0.102:80 -g
# ipvsadm -Ln


測試驗證:




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章