目录
第一章实验架构
1.1.实验图谱架构
1.2.实验前环境部署
第二章实验步骤
2.1.在ha01,ha02上部署keeplived,lvs-dr
2.2.Slave01,slave02安装nginx
2.3.LVS负载均衡
2.4.搭建动态网页
2.5.nginx反向代理
2.6.部署NFS
2.7.安装mysql
2.8.安装mha
2.9.主从复制
开启mha
systemctl stop firewalld
setenforce 0
主机改名
hostnamectl set-hostname ha01 192.168.233.100
hostnamectl set-hostname ha02 192.168.233.111
hostnamectl set-hostname master01 192.168.233.199
hostnamectl set-hostname slave01 192.168.233.69
hostnamectl set-hostname slave02 192.168.233.186
yum -y install keepalived ipvsadm
vim /etc/sysctl.conf #编辑内核参数,添加内容
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
#保存退出后查看配置
#查看内核参数是否配置完成
sysctl -p
LVS_01和LVS_02安装ipvsadm并启动服务
yum install ipvsadm -y #yum安装ipvsadm服务,即LVS负载均衡的真实服务名
systemctl start ipvsadm.service #开启ipvsadm服务,此步骤启动服务报错
systemctl status ipvsadm.service>1.txt
cat 1.txt
#将ipvsadm服务启动报错提示导给1.txt文件并查看。内容如下截图
ipvsadm-save>/etc/sysconfig/ipvsadm
#报错信息提示是没有/etc/sysconfig/ipvsadm文件,解决方法是将ipvsadm保存到/etc/sysconfig/ipvsadm文件下。
systemctl restart ipvsadm.service #重新启动ipvsadm成功
02也是一样操作
yum install epel-release -y
yum install nginx -y
systemctl start nginx
#安装epel扩展源,然后安装nginx并启动
#修改nginx主页内容,nginx01修改为nginx01 test,nginx02修改nginx02 test
echo nginx01 test >/usr/share/nginx/html/index.html
echo nginx02 test >/usr/share/nginx/html/index.html
#分别在俩台主机测试能否互相访问到定义的静态内容
curl 192.168.233.69
#2台nginx添加虚拟网卡lo:0并配置ip
ifconfig ens33:1 192.168.233.18 netmask 255.255.255.0
#添加路由192.168.233.18走ens33:1网卡
route add -host 192.168.233.18 dev ens33:1
#编辑内核参数,添加内容
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
ha1,ha2操作
#yum安装ipvsadm服务,即LVS负载均衡的真实服务名
yum install ipvsadm -yipvsadm-save>/etc/sysconfig/ipvsadm
systemctl start ipvsadm.service 不执行这部会报错,应为没有这个文件
#安装keepalived.x86_64
yum install keepalived.x86_64 -y
#编辑内核文件
vim /etc/sysctl.conf
#查看添加内核内容有则成功
#编辑配置文件
vim keepalived.conf
#编辑配置文件
修改内容:
global_defs模块中
smtp_server 127.0.0.1
#改为127.0.0.1即改为本机
router_id LVS_01
#第一条LVS名称改为LVS_01
#vrrp_skip_check_adv_addr
#vrrp_strict
#vrrp_garp_interval 0
#vrrp_gna_interval 0
#以上4行安全机制添加#号注释掉
virtual_ipaddress {
192.168.30.100
}
#虚拟主机地址改为要配置的虚拟ip,可配置多个,若只写一个将模板的其他2个删除。
virtual_server 192.168.30.100 80 { #虚拟ip地址改为配置的虚拟ip和web服务端口号
delay_loop 6
lb_algo rr
lb_kind DR #模式改为DR模式即直连模式
persistence_timeout 50
protocol TCP
real_server 192.168.30.12 80 { #第一台真实ip地址和web服务端口号
weight 1
TCP_CHECK {
#添加TCP_check 注意分号前有空格,检测真实服务器健康状态,即检测后端服务器80端口,若不正常则访问其他台服务器
#添加连接端口80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.30.13 80 { #第二台真实ip地址和web服务端口号
weight 1
TCP_CHECK {
#添加TCP_check 注意分号前有空格,检测真实服务器健康状态,即检测后端服务器80端口,若不正常则访问其他台服务器
connect_port 80 #添加连接端口80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
#俩台真实服务器后的所有内容删除即可,修改完成后保存退出。
LVS_02配置文件修改:
scp keepalived.conf 192.168.233.111:/etc/keepalived/
#将配置好的配置文件传到LVS_02服务器上
#第一步修改 router_id LVS_02 名称改为LVS_02
#第二步要修改vrrp_instance VI_1模块中的state为BACKUP,和priority 90改为比主LVS优先级低的等级
#其他不用修改保存即可
systemctl restart ipvsadm keepalived #重启服务
ipvsadm -ln
显示内容:
TCP 192.168.233.18:80 rr persistent 50
-> 192.168.233.69:80 Route 1 0 0
-> 192.168.233.186:80 Route 1 0 1
#查看有2个真实服务器的ip即可,注意二台真实服务器的80端口必须开着才能都检测到
slave01 与 slave02
安装tomcat,并启动
yum install tomcat -y
Systemctl start tomcat
cd /var/lib/tomcat/webapps
mkdir test
cd test
vim index.jsp # 添加动态页面内容:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
test
<% out.println("动态页面:test tomcat01");%>
#页面输出内容为test tomcat01,tomcat02服务器上则将01改为02方便查看实验效果
#创建动态资源页面
vim /etc/tomcat/server.xml
#修改主配置文件内容125行,原有内容不需要更改,对比此段内容添加即可
unpackWARs="true" autoDeploy="true"
xmlNamespaceAware="false">
path="" reloadable="true" />
#指定自己的tomcat页面访问文件路径
systemctl restart tomcat
#重启服务在本机分别输入192.168.233.69:8080和192.168.233.186:8080测试能否访问到内容
Slave01 slave02
vim /etc/nginx/conf.d/upstream.conf
#直接在nginx子配置文件夹中创建一个新的子配置文件,因为是yum安装无须在主配置文件中指定子配置文件
文件内容:
upstream nginxtest{
server 192.168.233.69:8080;
server 192.168.233.186:8080;
}
#反向代理模块,将2个tomcat的ip和端口号写入其中,反代模块名称为nginxtest#server模块中的第一个location模块,指定了根为html,支持主页文件类型三个,注意分号结尾
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css)$ {
root /usr/share/nginx/html;
}
#server模块中第二个location模块,配置不区分大小写的任意开头只要以.()括号内的任意一个内容结尾则为静态资源访问/usr/share/nginx/html下的页面location ~ .*\.jsp$ {
proxy_pass http://nginxtest;
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#server模块中第三个location模块,配置不区分大小写的任意开头只要.jsp结尾就由nginx反向代理模块nginxtest进行处理
Master01
yum install nfs-utils.x86_64 rpcbind -y #安装软件包
mkdir /share #创建共享目录
chmod -R 777 /share/ #修改权限
vim /etc/exports #编辑配置文件
/share *
/share 192.168.233.0/24(rw,sync,no_root_squash)
#共享目录 网段 读写,同步,无root权限
systemctl start rpcbind
systemctl start nfs
exportfs -v
showmount -e #查看本机发布的 NFS 共享目录
slave节点
将共享目录挂载到本地
vim /etc/fstab
tee /etc/yum.repos.d/mysql.repo <
[mysql]
name=mysql5.7
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-5.7-community-el7-x86_64/
gpgcheck=0
EOF
#换源
yum -y install mysql-community-server
systemctl start mysqld
登录mysql
mysql -u root -p
grep password /var/log/mysqld.log #过滤出mysql的密码
mysql -u root -p'密码' #特殊符号用单引号
mysql> alter user root@'localhost' identified by 'Admin@123'; #进去后修改密码
vim /etc/my.cnf
[mysqld]
character-set-server=utf8mb4 #修改字符集
2.8.安装mha
yum install epel-release.noarch -y
将安装包传到 /opt 下,下载并安装
主节点需要安装
yum install -y mha4mysql-node-0.58-0.el7.centos.noarch.rpm
yum install -y mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
#先安装node再装manager
从节点安装
yum install mha4mysql-node-0.58-0.el7.centos.noarch.rpm -y
在主节点上,基于key验证
ssh-keygen
cd ~
ssh-copy-id 127.0.0.1
#自己和自己实现免密钥登录
rsync -a .ssh 192.168.233.69:/root/
rsync -a .ssh 192.168.233.186:/root/
建立mha 文件夹 和配置文件
mkdir /etc/mastermha
vim /etc/mastermha/app1.cnf
[server default]
user=mhauser
password=Admin@123
manager_workdir=/data/mastermha/app1/
manager_log=/data/mastermha/app1/manager.log
remote_workdir=/data/mastermha/app1/
ssh_user=root
repl_user=test
repl_password=Admin@123
ping_interval=1
master_ip_failover_script=/usr/local/bin/master_ip_failover
#report_script=/usr/local/bin/sendmail.sh 可以不加
check_repl_delay=0
master_binlog_dir=/data/mysql/
[server1]
hostname=192.168.233.199
candidate_master=1
[server2]
hostname=192.168.233.69
candidate_master=1
[server3]
vim 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.233.33/24';#设置Virtual IP
my $gateway = '192.168.233.1';#网关Gateway IP
my $interface = 'ens33';
my $key = "1";
my $ssh_start_vip = "/sbin/ifconfig $interface:$key $vip;/sbin/arping -I $interface -c 3 -s $vip $gateway >/dev/null 2>&1";
my $ssh_stop_vip = "/sbin/ifconfig $interface:$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" ) {
# $orig_master_host, $orig_master_ip, $orig_master_port are passed.
# If you manage master ip address at global catalog database,
# invalidate orig_master_ip here.
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" ) {
# all arguments are passed.
# If you manage master ip address at global catalog database,
# activate new_master_ip here.
# You can also grant write access (create user, set read_only=0, etc) here.
my $exit_code = 10;
eval {
print "Enabling the VIP - $vip on the new master - $new_master_host \n";
&start_vip();
$exit_code = 0;
};
exit $exit_code;
}
elsif ( $command eq "start" ) {
# all arguments are passed.
# If you manage master ip address at global catalog database,
# activate new_master_ip here.
# You can also grant write access (create user, set read_only=0, etc) here.
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";
`ssh $ssh_user\@$orig_master_host \" $ssh_start_vip \"`;
exit 0;
}
else {
&usage();
exit 1;
}
}
# A simple system call that enable the VIP on the new master
sub start_vip() {
`ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
# A simple system call that disable the VIP on the old_master
sub stop_vip() {
`ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
}
sub usage {
"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";
mv master_ip_failover /usr/local/bin/ #脚本文件文件剪切到/usr/local/bin/目录下
chmod +x /usr/local/bin/master_ip_failover #添加可执行权限使其可以执行
ifconfig ens33:1 192.168.247.188/24 #MHA配置虚拟ip,主挂掉后虚拟ip到备主上
Master执行:
vim /etc/my.cnf
[mysqld] #此行下添加内容,开启主从复制和二进制日志
server_id=99
log-bin=/data/mysql/mysql-bin
skip_name_resolve=1
mkdir /data/mysql/ -p #创建二进制日志保存路径文件
chown mysql.mysql /data/ -R #修改文件夹权限
systemctl restart mysqld #重启mysql服务
#登录数据库
show master status; #查看记录二进制日志位置,一会从配置使用
grant replication slave on *.* to test@'192.168.247.%' identified by 'Admin@123';
#建立复制用户
grant all on *.* to mhauser@'192.168.247.%' identified by 'Admin@123';
两个slave设置
vim /etc/my.cnf #[mysqld]此行下添加内容,开启主从复制和二进制日志
server_id=100 #两台ID不一样,前大后小
log-bin=/data/mysql/mysql-bin
relay-log=relay-log-bin
relay-log-index=slave-relay-bin.index
skip_name_resolve=1
mkdir /data/mysql/ -p #创建二进制日志保存路径文件
chown mysql.mysql /data/ -R #修改文件夹权限
systemctl restart mysqld #重启mysql服务
mysql -uroot -pAdmin@123 #登录数据库
CHANGE MASTER TO
MASTER_HOST='192.168.233.199',
MASTER_USER='test',
MASTER_PASSWORD='Admin@123',
MASTER_PORT=3306,
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=744; # 这个会变化配置master信息
start slave #开启主从复制
MHA服务器上检查 mha 环境并启动mha
masterha_check_ssh --conf=/etc/mastermha/app1.cnf #检查mha的ssh免密登录环境
masterha_check_repl --conf=/etc/mastermha/app1.cnf #检查mha主从环境
MySQL Replication Health is OK. #结尾行出现此信息表示无问题
再次验证
开启mha
masterha_check_status --conf=/etc/mastermha/app1.cnf
#查看mha状态,默认是停止状态stop
#开启MHA,默认是前台运行,生产环境一般为后台执行
nohup masterha_manager --conf=/etc/mastermha/app1.cnf &> /dev/null
#非后台需要等待较长时间启动成功,此过程为前台运行,需要重新开一个窗口去查看状态即可
masterha_manager --conf=/etc/mastermha/app1.cnf
#查看状态
masterha_check_status --conf=/etc/mastermha/app1.cnf
至此实验成功