lnmp搭建(discuz高可用集群版)
第一节需求
在上一篇lnmp搭建(discuz集群版)中,存在单点故障,本次教程主要是解决单点故障的,即升级高可用架构,架构如下
第二节工具准备
linux==>CentOS Linux release 7.4.1708 (Core)
php==> php-5.4.16
nginx==>nginx-1.12.2
mysql==>mysql-5.7.26
discuz==>Discuz!X3.4
keepalive ==>keepalived-1.3.5-1
第三节实战操作
注意注意!!!!!!
每一台服务器前期准备:
[root@dispatch104 iso]# yum -y install epel-realease
[root@dispatch104 iso]# vim /etc/selinux/config
SELINUX=disabled
[root@dispatch104 iso]# systemctl stop firewalld
[root@dispatch104 iso]# systemctl disable firewalld
[root@dispatch104 iso]#yum -y install iptables-services
[root@dispatch104 iso]# iptables -F
[root@dispatch104 iso]# service iptables save
3.1nginx调度器高可用配置
1)主机103
[root@dispatch103 ~]# yum list | grep keepalived
keepalived.x86_64 1.3.5-1.el7 local
[root@dispatch103 ~]# yum -y install keepalived
[root@dispatch103 ~]# vim /etc/keepalived/keepalived.conf #删掉多余的,仅留下以下
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL_1
#vrrp_skip_check_adv_addr
#vrrp_strict
#vrrp_garp_interval 0
#vrrp_gna_interval 0
}
vrrp_script chk_http_port {
script "/opt/chk_nginx.sh"
interval 1
weight -2
fail 2
rise 1
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.68.250
}
track_script {
chk_http_port #引用上面的vrrp_script定义的脚本名称
}
}
[root@dispatch103 ~]# vim /opt/chk_nginx.sh
#!/bin/bash
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
#/usr/local/nginx/sbin/nginx #生产环境以下注释打开,现在注释只为了更直观看到效果
#sleep 2
#counter=$(ps -C nginx --no-heading|wc -l)
#if [ "${counter}" = "0" ]; then
/usr/bin/systemctl stop keepalived
#fi
fi
[root@dispatch103 ~]# chmod +x /opt/chk_nginx.sh
[root@dispatch103 ~]# systemctl restart keepalived
[root@dispatch103 ~]# systemctl enable keepalived
[root@dispatch103 ~]# ip addr
......
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:d5:49:2d brd ff:ff:ff:ff:ff:ff
inet 192.168.68.103/24 brd 192.168.68.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.68.250/32 scope global ens33 #vip
........
2)主机104
[root@dispatch104 iso]# ls
nginx-1.12.2.tar.gz
[root@dispatch104 iso]# tar -xvf nginx-1.12.2.tar.gz
[root@dispatch104 ~]# yum -y install gcc-c++ zlib-devel openssl-devel pcre-devel
[root@dispatch104 ~]# useradd -s /sbin/nologin nginx
[root@dispatch104 nginx-1.12.2]# make && make install
[root@dispatch104 ~]# ln -s /usr/local/nginx/sbin/nginx /sbin/
[root@dispatch104 ~]# cd /usr/local/nginx/
[root@dispatch104 nginx]# vim conf/nginx.conf
..........
http {
......
upstream myweb {
ip_hash;
server 192.168.68.50:80 weight=1 max_fails=1 fail_timeout=30;
server 192.168.68.51:80 weight=1 max_fails=1 fail_timeout=30;
server 192.168.68.52:80 weight=1 max_fails=1 fail_timeout=30;
server 192.168.68.53:80 weight=1 max_fails=1 fail_timeout=30;
}
server {
location / {
proxy_pass http://myweb;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 10m;
: }
}
.........
}
}
[root@dispatch104 ~]# ss -tunlp | grep :80
tcp LISTEN 0 128 *:80 *:* users:(("nginx",pid=1270,fd=6),("nginx",pid=1269,fd=6))
[root@dispatch104 ~]# echo '/usr/local/nginx/sbin/nginx' >> /etc/rc.local
[root@dispatch104 ~]# chmod +x /etc/rc.local
[root@dispatch104 ~]# yum -y install keepalived
[root@dispatch104 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL_1
#vrrp_skip_check_adv_addr
#vrrp_strict
#vrrp_garp_interval 0
#vrrp_gna_interval 0
}
vrrp_script chk_http_port {
script "/opt/chk_nginx.sh"
interval 1
weight -2
fail 2
rise 1
}
vrrp_instance VI_1 {
state MASTER
interface ens33 #改成自己机器的网卡名称如:eth0等
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.68.250
}
track_script {
chk_http_port #引用上面的vrrp_script定义的脚本名称
}
}
[root@dispatch104 ~]# vim /opt/chk_nginx.sh
#!/bin/bash
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
#/usr/local/nginx/sbin/nginx #生产环境以下注释打开,现在注释只为了更直观看到效果
#sleep 2
#counter=$(ps -C nginx --no-heading|wc -l)
#if [ "${counter}" = "0" ]; then
/usr/bin/systemctl stop keepalived
#fi
fi
[root@dispatch104 ~]# chmod +x /opt/chk_nginx.sh
[root@dispatch104 ~]# systemctl restart keepalived
[root@dispatch104 ~]# systemctl enable keepalived
[root@dispatch104 ~]# ip addr
.......
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:9b:a7:e1 brd ff:ff:ff:ff:ff:ff
inet 192.168.68.104/24 brd 192.168.68.255 scope global ens33
valid_lft forever preferred_lft forever
3.2maxscale高可用配置
1)主机maxscale10
[root@maxscale10 ~]# yum -y install keepalived
[root@maxscale10 ~]# vim /etc/keepalived/keepalived.conf #删掉多余的,仅留下以下
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL_1
#vrrp_skip_check_adv_addr
#vrrp_strict
#vrrp_garp_interval 0
#vrrp_gna_interval 0
}
vrrp_script chk_http_port {
script "/opt/chk_maxscale.sh"
interval 1
weight -2
fail 2
rise 1
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.68.9
}
track_script {
chk_http_port #引用上面的vrrp_script定义的脚本名称
}
}
[root@maxscale10 ~]# vim /opt/chk_maxscale.sh
#!/bin/bash
counter=$(ps -C maxscale --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
#/usr/bin/systemctl startmaxscale #生产环境以下注释打开,现在注释只为了更直观看到效果
#sleep 2
#counter=$(ps -C maxscale --no-heading|wc -l)
#if [ "${counter}" = "0" ]; then
/usr/bin/systemctl stop maxscale
#fi
fi
[root@maxscale10 ~]# chmod +x /opt/chk_maxscale.sh
[root@maxscale10 ~]# systemctl restart keepalived
[root@maxscale10 ~]# systemctl enable keepalived
[root@dispatch103 ~]# ip addr
......
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:d5:49:2d brd ff:ff:ff:ff:ff:ff
inet 192.168.68.103/24 brd 192.168.68.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.68.9/32 scope global ens33
valid_lft forever preferred_lft forever
.........
2)主机maxscale11
[root@maxscale11 ~]# yum -y install keepalived
[root@maxscale11 ~]# vim /etc/keepalived/keepalived.conf #删掉多余的,仅留下以下
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL_1
#vrrp_skip_check_adv_addr
#vrrp_strict
#vrrp_garp_interval 0
#vrrp_gna_interval 0
}
vrrp_script chk_http_port {
script "/opt/chk_maxscale.sh"
interval 1
weight -2
fail 2
rise 1
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.68.9
}
track_script {
chk_http_port #引用上面的vrrp_script定义的脚本名称
}
}
[root@maxscale11 ~]# vim /opt/chk_maxscale.sh
#!/bin/bash
counter=$(ps -C maxscale --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
#/usr/bin/systemctl start maxscale #生产环境以下注释打开,现在注释只为了更直观看到效果
#sleep 2
#counter=$(ps -C maxscale --no-heading|wc -l)
#if [ "${counter}" = "0" ]; then
/usr/bin/systemctl stop maxscale
#fi
fi
[root@maxscale11 ~]# chmod +x /opt/chk_maxscale.sh
[root@maxscale11 ~]# systemctl restart keepalived
[root@maxscale11 ~]# systemctl enable keepalived
[root@maxscale11 ~]# ip addr
......
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:d5:49:2d brd ff:ff:ff:ff:ff:ff
inet 192.168.68.11/24 brd 192.168.68.255 scope global ens33
valid_lft forever preferred_lft forever
[root@maxscale11 iso]# ls
maxscale-2.3.6-1.centos.7.x86_64.rpm
[root@maxscale11 iso]# yum -y install maxscale-2.3.6-1.centos.7.x86_64.rpm
[root@maxscale11 ~]# vim /etc/maxscale.cnf
[maxscale]
threads=auto
[server1]
type=server
address=192.168.68.102
port=3306
#protocol=MariaDBBackend
protocol=MySQLBackend
[server2]
type=server
address=192.168.68.110
port=3306
#protocol=MariaDBBackend
protocol=MySQLBackend
[server3]
type=server
address=192.168.68.110
port=3306
#protocol=MariaDBBackend
protocol=MySQLBackend
[server4]
type=server
address=192.168.68.110
port=3306
#protocol=MariaDBBackend
protocol=MySQLBackend
[MariaDB-Monitor]
type=monitor
#module=mariadbmon
module=mysqlmon
servers=server1, server2, server3, server4
user=scalemon
password=Scalemon.com522527
monitor_interval=2000
detect_stale_master=true
#[Read-Only-Service]
#type=service
#router=readconnroute
#servers=server1
#user=myuser
#password=mypwd
#router_options=slave
[Read-Write-Service]
type=service
router=readwritesplit
servers=server1, server2, server3, server4
user=pliadmin
password=Pliadmin.com522527
max_slave_connections=100%
[MaxAdmin-Service]
type=service
router=cli
#[Read-Only-Listener]
#type=listener
#service=Read-Only-Service
#protocol=MariaDBClient
#port=4008
[Read-Write-Listener]
type=listener
service=Read-Write-Service
protocol=MariaDBClient
port=3306
[MaxAdmin-Listener]
type=listener
service=MaxAdmin-Service
protocol=maxscaled
socket=default
[root@maxscale11 ~]# systemctl restart maxscale
[root@maxscale11 ~]# systemctl enable maxscale
[root@dispatch104 ~]# ss -tunlp | grep maxscale
tcp LISTEN 0 128 127.0.0.1:8989 *:* users:(("maxscale",pid=23315,fd=23))
tcp LISTEN 0 128 :::3306 :::* users:(("maxscale",pid=23315,fd=19))
3.3修改web中discuz数据库配置文件
1)第一个文件
[root@web50 ~]# cd /usr/local/nginx/html/
[root@web50 html]# vim config/config_global.php
// ---------------------------- CONFIG DB ----------------------------- //
$_config['db']['1']['dbhost'] = '192.168.68.9';
$_config['db']['1']['dbuser'] = 'BBSuser';
$_config['db']['1']['dbpw'] = 'Bbsuser.com522527';
$_config['db']['1']['dbcharset'] = 'utf8';
$_config['db']['1']['pconnect'] = '0';
$_config['db']['1']['dbname'] = 'bbsdb';
.......
2)第二个文件
[root@web50 html]# vim uc_server/data/config.inc.php
define('UC_DBHOST', '192.168.68.9');
define('UC_DBUSER', 'BBSuser');
define('UC_DBPW', 'Bbsuser.com522527');
define('UC_DBNAME', 'bbsdb');
define('UC_DBCHARSET', 'utf8');
.......
3)第三个文件
[root@web50 html]# vim config/config_ucenter.php
.......
define('UC_DBHOST', '192.168.68.9');
define('UC_DBUSER', 'BBSuser');
define('UC_DBPW', 'Bbsuser.com522527');
define('UC_DBNAME', 'bbsdb');
define('UC_DBCHARSET', 'utf8');
........
4)同步文件给web51,52,53
[root@web50 html]# yum -y install rsync
[root@web50 html]# for i in 51 52 53
> do
> rsync -ar /usr/local/nginx/html/ 192.168.68.$i:/usr/local/nginx/html/
> done
第四节测试
4.1测试调度器nginx是否高可用
1,浏览器访问http://192.168.250
2,停掉dispatch103的nginx
[root@dispatch103 ~]# nginx -s stop
3,浏览器访问http://192.168.250依然能看到页面
原因:停掉103nginx导致keepalived触发chk_nginx.sh停掉keepalived,vip:192.168.68.250就飘到104去了,达到了高可用,即:
[root@dispatch103 ~]# ip addr
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:d5:49:2d brd ff:ff:ff:ff:ff:ff
inet 192.168.68.103/24 brd 192.168.68.255 scope global ens33
valid_lft forever preferred_lft forever
[root@dispatch104 ~]# ip addr
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:9b:a7:e1 brd ff:ff:ff:ff:ff:ff
inet 192.168.68.104/24 brd 192.168.68.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.68.250/32 scope global ens33
valid_lft forever preferred_lft forever
4.2测试maxscale是否高可用
1,连接数据库
[root@mysql102 ~]# mysql -uBBSuser -pBbsuser.com522527 -P3306 -h192.168.68.9
mysql> #不要退出
2,停掉maxscale10的maxscale
[root@maxscale10 ~]# [root@dispatch103 ~]# systemctl stop maxscale
3,刚才在mysql102并没有因为maxscale10停掉maxscale而卡死,依然能够正常操作数据库
原因:停掉11的maxscale导致keepalived触发chk_maxscale.sh停掉keepalived,vip:192.168.68.9就飘到11去了,达到了高可用,即:
[root@maxscale10 ~]# ip addr
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:d5:49:2d brd ff:ff:ff:ff:ff:ff
inet 192.168.68.10/24 brd 192.168.68.255 scope global ens33
valid_lft forever preferred_lft forever
[root@maxscale11 ~]# ip addr
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:9b:a7:e1 brd ff:ff:ff:ff:ff:ff
inet 192.168.68.11/24 brd 192.168.68.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.68.9/32 scope global ens33
valid_lft forever preferred_lft forever
总结
本次教程主要简单的和大家分享如何搭建一个简单的web集群高可用架构,这个架构也有许多不足:
1,可搭建redis集群或者redis哨兵模式来提高网站速度
2,数据库并不能达到高可用(如主库死掉了就不行了),当然也有解决办法,如可以搭建mha数据库集群即可解决,具体就看公司业务来决定了应该选择用哪一种方案,选择适合架构才是最好的。
温馨提示:
1,nginx调度器启动顺序,先启动nginx,在启动keepalived;maxscale也是差不多的,先启动maxscale,再启动keepalived
2,做实验时,每一台服务器都要关闭防火墙,禁用selinux!!!!
3,mysql的mha集群搭建,redis集群搭建,redis哨兵模式搭建正在赶来,关注我
看官福利:
结束语:
更多精彩内容持续更新中,关注我,有你更精彩。