部署 Keepalived+LAMP+MHA 群集
一、准备工作
1.项目拓补图
2.项目环境
主机名 | 操作系统 | IP地址 | 担任角色 |
master | CentOS7 | 192.168.1.1 | MySQL-Master,MHA管理端 |
slave1 | 192.168.1.2 | MySQL-Slave1 | |
slave2 | 192.168.1.3 | MySQL-Slave2 | |
web1 | 192.168.1.4 | Apache,PHP,Discuz | |
web2 | 192.168.1.5 | Apache,PHP,Discuz | |
keep1 | 192.168.1.6 | Keepalived-主调度器 | |
keep2 | 192.168.1.7 | Keepalived-备调度器 |
二、部署 MySQL 数据库
- 如果未安装 MySQL 数据库 需看:Centos7安装Mysql数据库 来进行安装。
- MHA 软件包从这个链接下载:https://pan.baidu.com/s/1c39hxF-beUxu9Cl_OaZSuQ
- 提取码:whwo
1.配置免密登陆
- 在 master,slave1,slave2 上分别生成密钥对。
[root@master ~]# ssh-keygen -t rsa #一路回车即可
- 每台节点分别将自己的公钥发送到另外的三台节点。(下面以 master 主机为例)
[root@master ~]# ssh-copy-id 192.168.1.2
[root@master ~]# ssh-copy-id 192.168.1.3
2.安装 MHA 软件包
准备工作:master slave1 slave2 都要做
[root@master ~]# mkdir /root/mha
[root@master ~]# cd /root/mha
[root@master mha]# ls
mha4mysql-manager-0.57-0.el7.noarch.rpm mhapath.tar.gz mha4mysql-node-0.57-0.el7.noarch.rpm
[root@master mha]# tar zxf mhapath.tar.gz
[root@master mha]# cat <<END > /etc/yum.repos.d/CentOS7.repo
[centos]
name=centos7
baseurl=file:///mnt
enabled=1
gpgcheck=0
[mha]
name=mha
baseurl=file:///root/mha/mhapath
enabled=1
gpgcheck=0
END
[root@master mha]# mount /dev/cdrom /mnt/
1)在各节点上安装 mha4mysql-node
[root@master ~]# yum -y install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager --skip-broken --nogpgcheck
[root@master ~]# rpm -ivh /root/mha/mha4mysql-node-0.57-0.el7.noarch.rpm
2)在 master 上安装 manager 节点
[root@master ~]# yum -y install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker perl-CPAN
[root@master ~]# rpm -ivh /root/mha/mha4mysql-manager-0.57-0.el7.noarch.rpm
3.配置主从复制
1)主服务器上配置
[root@master ~]# cat <<END >> /etc/my.cnf
log-bin=mysql-bin-master
server-id=1
END
[root@master ~]# systemctl restart mysqld
[root@master ~]# mysql -uroot -p123456
mysql> grant replication slave on *.* to repl@'192.168.1.%' identified by '123456';
mysql> grant all privileges on *.* to root@'192.168.1.%' identified by '123456';
mysql> flush privileges;
mysql> exit
2)从服务器上配置
两台从节点操作一致
[root@slave1 ~]# cat <<END >> /etc/my.cnf
log-bin=mysql-slave1 #slave2改为2
server-id=2 #slave2改为3
log_slave_updates=1
END
[root@slave1 ~]# systemctl restart mysqld
[root@slave1 ~]# mysql -uroot -p123456
mysql> grant replication slave on *.* to repl@'192.168.1.%' identified by '123456';
mysql> grant all privileges on *.* to root@'192.168.1.%' identified by '123456';
mysql> flush privileges;
mysql> exit
3)建立主从复制
在 slave1 slave2 上操作
[root@slave1 ~]# mysql -uroot -p123456
mysql> change master to
master_host='192.168.1.1',
master_user='repl',
master_password='123456';
mysql> start slave;
mysql> show slave status\G;
4.设置 MySQL 程序及 binglog 程序的软连接
master和两台从节点都要做
[root@master ~]# ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
[root@master ~]# ln -s /usr/local/mysql/bin/mysqlbinlog /usr/local/bin/mysqlbinlog
5.在两台 Slave 设置临时只读和不自动清除中继日志
[root@slave1 ~]# mysql -uroot -p123456 -e 'set global read_only=1'
[root@slave1 ~]# mysql -uroot -p123456 -e 'set global relay_log_purge=0'
6.配置 MHA 工作目录及配置文件
[root@master ~]# mkdir -p /etc/masterha
[root@master ~]# mkdir -p /var/log/masterha/app1
[root@master ~]# vim /etc/masterha/app1.cnf
[server default]
manager_workdir=/var/log/masterha/app1
manager_log=/var/log/masterha/app1/manager.log
master_binlog_dir=/usr/local/mysql/data/
user=root
password=123456
ping_interval=1
remote_workdir=/tmp
repl_user=repl
repl_password=123456
ssh_user=root
[server1]
hostname=192.168.1.1
port=3306
[server2]
hostname=192.168.1.2
port=3306
candidate_master=1
check_repl_delay=0
[server3]
hostname=192.168.1.3
port=3306
7.检查 MHA 的环境是否工作正常
1)检测 SSH 连接是否配置正常
[root@master ~]# masterha_check_ssh --conf=/etc/masterha/app1.cnf
2)在管理节点检查复制配置
[root@master ~]# masterha_check_repl --conf=/etc/masterha/app1.cnf
最后显示如下说明环境没有问题:
MySQL Replication Health is OK.
9.配置 VIP 地址
master 上操作
[root@master ~]# ifconfig ens33:1 192.168.1.188 netmask 255.255.255.0 up
[root@master ~]# ifconfig ens33:1
1)修改 MHA 配置文件,使其支持 VIP
[root@master ~]# vim /etc/masterha/app1.cnf
在 [server default] 项下面添加:
master_ip_failover_script=/usr/bin/master_ip_failover
2)编写 VIP 自动切换脚本
[root@master ~]# vim /usr/bin/master_ip_failover
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';
use Getopt::Long;
my (
$command, $ssh_user, $orig_master_host, $orig_master_ip,
$orig_master_port, $new_master_host, $new_master_ip, $new_master_port
);
my $vip = '192.168.1.188/24';
my $key = '1';
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down";
GetOptions(
'command=s' => \$command,
'ssh_user=s' => \$ssh_user,
'orig_master_host=s' => \$orig_master_host,
'orig_master_ip=s' => \$orig_master_ip,
'orig_master_port=i' => \$orig_master_port,
'new_master_host=s' => \$new_master_host,
'new_master_ip=s' => \$new_master_ip,
'new_master_port=i' => \$new_master_port,
);
exit &main();
sub main {
print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";
if ( $command eq "stop" || $command eq "stopssh" ) {
my $exit_code = 1;
eval {
print "Disabling the VIP on old master: $orig_master_host \n";
&stop_vip();
$exit_code = 0;
};
if ($@) {
warn "Got Error: $@\n";
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "start" ) {
my $exit_code = 10;
eval {
print "Enabling the VIP - $vip on the new master - $new_master_host \n";
&start_vip();
$exit_code = 0;
};
if ($@) {
warn $@;
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "status" ) {
print "Checking the Status of the script.. OK \n";
exit 0;
}
else {
&usage();
exit 1;
}
}
sub start_vip() {
`ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
sub stop_vip() {
`ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
}
sub usage {
print
"Usage: master_ip_failover --command=start|stop|stopssh|status --orig_master_host=host --orig_master_ip=ip --orig_master_port=port --new_master_host=host --new_master_ip=ip --new_master_port=port\n";
}
[root@master ~]# chmod +x /usr/bin/master_ip_failover
再次检测,结果应该和上面检测结果一样才对
[root@master ~]# masterha_check_repl --conf=/etc/masterha/app1.cnf
10.开启监控
[root@master ~]# nohup masterha_manager --conf=/etc/masterha/app1.cnf \
--remove_dead_master_conf --ignore_last_failover < /dev/null > \
/var/log/masterha/app1/manager.log 2>&1 &
[root@master ~]# masterha_check_status --conf=/etc/masterha/app1.cnf
三、部署 Apahce+PHP 架构
挂光盘,并配置yum源
- Apache+PHP 软件包从这个链接下载:https://pan.baidu.com/s/13XBKIFTSrVlZ106nWGNVjw
- 提取码:l9we
1.安装并启动 Apache
[root@web1 ~]# yum -y install gcc gcc-c++ apr apr-devel cyrus-sasl-devel expat-devel libdb-devel openldap-devel apr-util-devel apr-util pcre-devel pcre openssl*
[root@web1 ~]# ls
anaconda-ks.cfg httpd-2.4.25.tar.gz
[root@web1 ~]# tar zxf httpd-2.4.25.tar.gz -C /usr/src/
[root@web1 ~]# cd /usr/src/httpd-2.4.25/
[root@web1 httpd-2.4.25]# ./configure \
--prefix=/usr/local/httpd \
--enable-so \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi \
--enable-ssl \
--enable-mpms-shared=all && make && make install
复制启动脚本
[root@web1~]# cp /usr/local/httpd/bin/apachectl /etc/init.d/apache
[root@web1 ~]# vim /etc/init.d/apache
在 #!/bin/bash 下面添加:
#chkconfig: 2345 11 88
#despriction:httpd apache server
[root@web1 ~]# chkconfig --add apache
[root@web1 ~]# vim /usr/local/httpd/conf/httpd.conf
204 ServerName web1:80 #web2将 web1:80 改为 web2:80
[root@web2 ~]# /etc/init.d/apache start
[root@web2 ~]# netstat -anpt | grep 80
2.安装PHP
[root@web1 ~]# yum -y install php-mcrypt libmcrypt libmcrypt-devel autoconf freetype gd libmcrypt libpng libpng-devel libjpeg libxml2 libxml2-devel zlib curl curl-devel re2c libmcrypt-devel freetype-devel libjpeg-devel bzip2-devel
[root@web1 ~]# ls
anaconda-ks.cfg httpd-2.4.25.tar.gz libmcrypt-2.5.8.tar.gz php-5.5.38.tar.gz
[root@web1 ~]# tar zxf libmcrypt-2.5.8.tar.gz -C /usr/src/
[root@web1 ~]# cd /usr/src/libmcrypt-2.5.8/
[root@web1 libmcrypt-2.5.8]# ./configure --prefix=/usr/local/libmcrypt && make && make install
[root@web1 ~]# tar -zxf php-5.5.38.tar.gz -C /usr/src/
[root@web1 ~]# cd /usr/src/php-5.5.38
[root@web1 php-5.5.38]# ./configure \
--prefix=/usr/local/php5.5 \
--with-mysql=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-openssl \
--enable-fpm \
--enable-sockets \
--enable-sysvshm \
--enable-mbstring \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml --with-mhash \
--with-mcrypt=/usr/local/libmcrypt \
--with-config-file-path=/etc \
--with-config-file-scan-dir=/usr/local/php5.5/etc/ \
--with-bz2 \
--enable-maintainer-zts \
--with-apxs2=/usr/local/httpd/bin/apxs && make && make install
[root@web1 php-5.5.38]# cp php.ini-production /usr/local/php5.5/etc/php.ini
3.配置PHP,测试后端数据库VIP
[root@web1 ~]# vim /usr/local/httpd/conf/httpd.conf
263 DirectoryIndex index.html index.php
401 AddType application/x-http-php .php .phtml
[root@web1 ~]# /etc/init.d/apache restart
1)创建测试页面
[root@web1 ~]# vim /usr/local/httpd/htdocs/index.php
<?php
phpinfo();
?>
[root@web1 ~]# /etc/init.d/apache restart
2)访问验证
4.安装 Discuz 论坛
1)在 Master 上创建一个 Discuz 论坛账号
mysql> grant all on *.* to dtest@'%' identified by '123456';
mysql> flush privileges;
2)在 Web1 上创建测试页面
[root@web1 ~]# vim /usr/local/httpd/htdocs/test.php
<?php
$link=mysql_connect('192.168.1.188','dtest','123456');
if ($link)echo "恭喜你,数据库连接成功!!";
mysql_close();
?>
3)访问测试
4)安装 Discuz 论坛
web1上操作
[root@web1 ~]# ls
[root@web1 ~]# unzip Discuz_X3.3_SC_UTF8.zip -d discuz
[root@web1 ~]# cd discuz/
[root@web1 discuz]# cp -r upload/ /usr/local/httpd/htdocs/discuz
[root@web1 discuz]# vim /usr/local/php5.5/etc/php.ini
202 short_open_tag = 0n
[root@web1 discuz]# /etc/init.d/apache restart
[root@web1 discuz]# chown -R daemon:daemon /usr/local/httpd/htdocs/
5)在浏览器上安装 Discuz 论坛
访问:http://192.168.1.4/discuz/install/index.php
继续下一步,到我如下,按我下图填写
安装完成后点击右下角的访问
因为 Web1 上都已经安装好了,这里直接把 Web1 安装好的 Discuz,传给 Web2
[root@web1 ~]# scp -r /usr/local/httpd/ 192.168.1.5:/usr/local/
[root@web2 ~]# /etc/init.d/apache restart
[root@web2 ~]# chown -R daemon:daemon /usr/local/httpd/htdocs/
访问并测试数据一致性
到 Web1 的论坛中,登陆 zhangsan 账号,可以登陆
这是因为两个 Discuz 后端的数据库都是一个,数据都是在数据库中的,所以其中数据都是一样的。
四、部署 Keepliaved 双机热备
挂光盘,并配置yum源
1.配置主和备调度器
主备操作稍微不同,不同的地方下面我会说
[root@keep1 ~]# yum -y install ipvsadm keepalived
[root@keep1 ~]# cd /etc/keepalived/
[root@keep1 keepalived]# rm -rf keepalived.conf
[root@keep1 keepalived]# vim keepalived.conf
global_defs {
router_id master
}
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.1.211
}
}
virtual_server 192.168.1.211 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 0
protocol TCP
real_server 192.168.1.4 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.1.5 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
备调度器需要做以下修改:
将 router_id master 改为 router_id backup
将 state MASTER 修改为 state BACKUP
将 priority 100 修改为 priority 99
[root@keep1 keepalived]# systemctl start keepalived
3.配置 Web 节点的内核参数
Web1,Web2节点操作一致
[root@web1 ~]# vim /root/vip.sh
#!/bin/sh
VIP=192.168.1.211
case $1 in
start)
ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
/sbin/route add -host $VIP dev lo:0
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
sysctl -p >/dev/null 2>&1
echo "RealServer Start OK"
exit 0
;;
stop)
ifconfig lo:0 down
route del $VIP >/dev/null 2>&1
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
echo "RealServer Stoped OK"
exit 1
;;
*)
echo "Usage: $0 {start|stop}"
;;
esac
[root@web1 ~]# chmod +x /root/vip.sh
[root@web1 ~]# cd /root/
[root@web1 ~]# ./vip.sh start
4.访问验证
通过 VIP 的地址访问 Discuz 论坛:
测试和后端 MySQL 通信:
右上角登陆,登陆刚刚创建的 zhangsan 用户,可以登陆上去
通过查看日志,来查看是否轮询
web1
[root@web1 ~]# tail -f /usr/local/httpd/logs/access_log
web2
[root@web2 ~]# tail -f /usr/local/httpd/logs/access_log
只要刷新页面一次,那个符号应该在 Web1 和 Web2 中变动,表示那个终端有变动
五、故障切换 Keepaliaved 和 MHA
[root@master ~]# systemctl stop mysqld
[root@keep1 ~]# systemctl stop keepalived #如果关闭后VIP还未漂移,关机该机器即可
Keepalived VIP漂移了
查看 Manager 日志
[root@master ~]# tail -n10 /var/log/masterha/app1/manager.log
在 192.168.1.2 主机中查看 MHA VIP 是否漂移
访问测试
转载:https://blog.csdn.net/weixin_46902396/article/details/111738396