keepalived_lvs-dr實現discuz負載均衡和高可用

前言:

    前一篇文章中由於前端調度器由於只有一臺,如果損壞了整個系統將無法對外提供服務,因此它成爲了系統的SPOF,需要對其做高可用,此時暫不考慮後端存儲故障的情況。經過兩個小時的配置,終於將此配置出來,下面將配置流程寫下來

    由於我的電腦使用的是虛擬機,所以在配置DR模型的時候,需要使用兩塊網卡,一塊網卡用於橋接外網,另外一塊使用NAT模式進行實驗測試!!!(此處無需關注鏈接外網的網卡,本地測試NAT模式很實用) 


高可用模型:

   主備模型:即一臺主機位MASTER,另一臺主機位BACKUP,只有當MASTER主機宕機時,BACKUP主機取而代之成爲MASTER纔會開始工作,因此在單主模型中,始終有一臺主機不工作,會造成資源浪費。

   雙主模型:兩個單主模型疊加,兩臺主機互爲主備,在前段DNS解析時加上雙A記錄,即可實現兩臺主機同時工作,而且實現了高可用,且兼備負載均衡。


    此處我們使用主備模型,兩模型之間切換過於簡單,此處不描述主主模型博客!


拓撲圖:

wKiom1dKq5axoRFnAAGplJ1KS4Q513.png


各個主機的IP地址

Host VS1

    eno16777736 10.0.0.201/8

    gateway: 10.0.0.254

    虛擬IP:10.0.0.111/8


Host VS2

    eno16777736 10.0.0.203/8

    gateway: 10.0.0.254

    虛擬IP:10.0.0.111/8


Host RS1

    eno16777736 10.0.0.101/8 (RIP1)

    lo:0 10.0.0.111/32 (VIP)

    gateway: 10.0.0.254

 

Host RS2

    eno16777736 10.0.0.102/8 (RIP2)

    lo:0 10.0.0.111/32 (VIP)

    gateway: 10.0.0.254 


Host DB

    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


HostRS1

安裝nginx,注意nginx屬於epel源

# yum install-y nginx php-fpm php-mbstring php-mysql nfs-utils mariadb



HostRS2

安裝nginx,注意nginx屬於epel源

# yum install-y nginx php-fpm php-mbstring php-mysql nfs-utils mariadb


Host VS1

安裝keepalived是爲了實現高可用,ipvsadm用來查看狀態 http用來提供sorry server

# yum install -y keepalived ipvsadm httpd


Host VS2

安裝keepalived是爲了實現高可用,ipvsadm用來查看狀態 http用來提供sorry server

# yum install -y keepalived ipvsadm httpd


HostDB

完成安全初始化後,創建discuz數據庫和discuz用戶,並授權其可以遠程操作數據庫

# mysql_secure_installation
# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.46-MariaDB-log MariaDB Server

Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> create database discuz;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> create user 'discuz'@'localhost' identified by 'magedu';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> grant all privileges on discuz.* to 'discuz'@'%' identified by 'magedu';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)


配置NFS

# mkdir /nfshare/
# ls -dl /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
# systemctl start rpcbind   <--如果是CentOS-6要使用/etc/init.d/rpcbind start
Starting rpcbind:                                          [  OK  ]
[root@34400575 ~]# /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  ]
# systemctl enable rpcbind  <--如果是6 chkconfig rpcbind on
# systemctl enable rpcbind  <--如果是6 chkconfig nfs on
[root@34400575 ~]# chkconfig rpcbind --list
rpcbind            0:off    1:off    2:on    3:on    4:on    5:on    6:off
[root@34400575 ~]# chkconfig nfs --list
nfs                0:off    1:off    2:on    3:on    4:on    5:on    6:off
[root@34400575 ~]# showmount -e 127.0.0.1
Export list for 127.0.0.1:
/nfshare 172.18.71.102,172.18.71.101    此步驟如果出現問題,請查看《lvs-dr負載均衡Discuz》


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

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


Host RS1

我們如果要爲RS配置VIP,爲了不產生IP衝突,我們要先禁止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


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

# ip addr add 10.0.0.111/32 broadcast 10.0.0.111 dev lo labal lo:0
# route add -host 10.0.0.111 lo:0

注意:CentOS-7最小安裝,默認沒有route命令,需要安裝net-tools(不用google了,遇到這個問題的都特麼是外國人。沒錯,很坑爹)


測試鏈接HostDB上的mariadb

# mysql -h 10.0.0.202 -u discuz -p


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

# showmount -e 10.0.0.202
Export list for 10.0.0.202:
/nfshare 10.0.0.102,10.0.0.101
[root@b9cf468b ~]# mkdir /htdocs
[root@b9cf468b ~]# ls -ld /htdocs/
drwxr-xr-x 2 root root 6 May 9 17:05 /htdocs/
[root@b9cf468b ~]# mount -t nfs 10.0.0.202:/nfshare /htdocs
[root@b9cf468b ~]# ls /htdocs/
discuz
[root@b9cf468b ~]# chown -R apache:apache /htdocs/discuz/


啓動php-fpm

# systemctl start php-fpm.service


修改nginx的配置文件

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;


    include /etc/nginx/conf.d/*.conf;

server {
	listen  80;
	server_name www.rs1.com;
	

	# 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
客戶端瀏覽器訪問http://10.0.0.101/install/,在這裏就將discuz安裝完成


Host RS2

與Host RS1的配置相同,此處不再複製



Host VS1

修改keepalived配置文件

! Configuration File for keepalived

global_defs {
   notification_email {
       root@localhost  
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_mcast_group4 224.0.71.18
}

vrrp_instance VI_1 {
    state MASTER
    interface eno16777736
    virtual_router_id 52
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
    	10.0.0.111/32 dev eno16777736 label eno16777736:0    
    }
}

virtual_server 10.0.0.111 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    protocol TCP
    sorry_server 127.0.0.1 80

    real_server 10.0.0.101 80 {
        weight 1
  	HTTP_GET {
            url {
              path /install/
	      status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

    real_server 10.0.0.102 80 {
        weight 1
  	HTTP_GET {
            url {
              path /
	      status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
  
}
啓動哈個服務


Host VS2

修改keepalived配置文件

! Configuration File for keepalived

global_defs {
   notification_email {
       root@localhost  
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_mcast_group4 224.0.71.18
}

vrrp_instance VI_1 {
    state BACKUP
    interface eno16777736
    virtual_router_id 52
    priority 98
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
    	10.0.0.111/32 dev eno16777736 label eno16777736:0    
    }
}

virtual_server 10.0.0.111 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    protocol TCP
    sorry_server 127.0.0.1 80

    real_server 10.0.0.101 80 {
        weight 2
  	HTTP_GET {
            url {
              path /
	      status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

    real_server 10.0.0.102 80 {
        weight 1
  	HTTP_GET {
            url {
              path /
	      status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
  
}

至此,地址已經可以飄起來了,如果遇到什麼麻煩請聯繫我QQ:2137626


爲HostVS1 和HostVS2 提供Sorry頁面,並啓動服務

# echo "服務維護中,請稍後訪問." > /var/www/html/index.html
[root@8a028eb8 ~]#sed -i "s@^#ServerName.*@ServerName admin.ws.com@" /etc/httpd/conf/httpd.conf
[root@8a028eb8 ~]# /etc/init.d/httpd start
Starting httpd:                                            [  OK  ]


測試驗證

通過客戶端瀏覽器強制刷新10.0.0.111在HostVS1和2上都可以通過ipvsadm查看,兩個後端RS主機輪詢的很開心

ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.0.0.111:http rr
  -> 10.0.0.101:http              Route   1      3          6         
  -> 10.0.0.102:http              Route   1      3          0


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