前提准备:
七台机器: 10.10.10.143 ------------> lvs+keepalived ( master)
10.10.10.144 ------------> lvs+keepalived (backup)
10.10.10.145 ------------> web server1
10.10.10.146 ------------> web server2
10.10.10.149 ------------> mysql server1
10.10.10.150 ------------> mysql server2
10.10.10.138 ------------> 测试
vip : 10.10.10.111 -----------> mysql :3306
10.10.10.110 -----------> Apache:80
七台机器都关闭防火墙和selinux:
[root@hostname ~]# systemctl disable firewalld
[root@hostname ~]# systemctl stop firewalld
[root@hostname ~]# vim /etc/selinux/config
SELINUX=disabled
1.安装lvs:
在143.144机器上做:
yum install -y ipvsadm
设置路由转发:(DR模式可以不做)
/etc/sysctl.conf -------------> net.ipv4.ip_forward=1
设置虚拟网卡: VIP 10.10.10.111 ( 配置完keepalived后删掉,这儿仅供测试lvs)
[root@lo_node1 ~]# cd /etc/sysconfig/network-scripts/
[root@lo_node1 network-scripts]# cp ifcfg-ens33 ifcfg-ens33:10
[root@lo_node1 network-scripts]# vim ifcfg-ens33:10
设置ipvsadm:(一会+keepalived 时清空 -C)
[root@lo_node1 ~]# ipvsadm -A -t 10.10.10.110:80 -s rr
[root@lo_node1 ~]# ipvsadm -a -r 10.10.10.110:80 -r 10.10.10.145:80 -g
[root@lo_node1 ~]# ipvsadm -a -r 10.10.10.110:80 -r 10.10.10.146:80 -g
查看:
145.146 web 配置: (两个都做,网页标签要不一样)
安装Apache:
[root@lo_node3 ~]# yum install -y httpd
[root@lo_node3 ~]# echo 'test1'> /var/www/html/index.html
[root@lo_node3 ~]# systemctl start httpd
绑定vip:(两个都做)
( 可以执行脚本 lvs.sh )
[root@lo_node3 ~]# cd /etc/sysconfig/network-scripts/
[root@lo_node3 network-scripts]# cp ifcfg-lo ifcfg-lo:10
[root@lo_node3 network-scripts]# vim ifcfg-lo:10
arp抑制:(两个都做)
/etc/sysctl.conf:
net.ipv4.conf.lo.arp_ignore=1
net.ipv4.conf.lo.arp_announce=2
net.ipv4.conf.all.arp_ignore=1
net.ipv4.conf.all.arp_announce=2
net.ipv4.ip_forward=0
138测试:
[root@hostname ~]# for ((i=1;i<10;i++));do curl http://10.10.10.111; done
2.安装keepalived:
在143 (主)和 144(备) 机器上做:
方1 : yum 安装:
[root@lo_node2 ~]# yum install -y keepalived
方2: 源码安装:
mkdir /tools
cd /tools
wget http://www.keepalived.org/software/keepalived-1.2.16.tar.gz
yum install -y gcc gcc-c++ openssl-devel
tar -zxf keepalived-1.2.16.tar.gz -C /usr/local
cd /usr/local
cd keepalived-1.2.16/
./configure
make
make install
cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/ #生成启动命令
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived #创建默认的配置文件路径
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/ #拷贝keepalived到/etc/keepalived/
cp /usr/local/sbin/keepalived /usr/sbin/
chkconfig --add keepalived # 添加到系统环境
修改/etc/keepalived/keepalived.conf 文件并测试VIP的转移
3.lvs+keepalived+apache:
删掉143里面的/etc/sysconfig/network-script/ifcfg-ens33:10
/etc/keepalived/keepalived.conf文件为:
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_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 {
10.10.10.110
}
}
virturl_erver 10.10.10.110 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 300
protocol TCP
real_server 10.10.10.146 80 {
weight 1
TCP_CHECK {
connect_timeout 8
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 10.10.10.145 80 {
weight 1
TCP_CHECK {
connect_timeout 8
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
144 (backup上不同的三个位置):
router_id LVS_2
state BACKUP
priority 50
启动keepalived并在138上面测试
清空ipvs:
ipvsadm -C
启动keepalived:
systemctl start keepalived
在执行ipvsadm :会显示:(两台Apache都开启)
当其中一个Apache关闭后,ipvsadm -ln 会少一个,再次启动,会恢复;
4.lvs+keepalived+mysql主主复制:
149.150 机器上配置:
安装mysql(此处为二进制安装)
mkdir /tools
cd /tools
groupadd -r -g 306 mysql
useradd -r -g 306 -u 306 -s /sbin/nolgin -M mysql
tar -zxvf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz -C /usr/local
cd /usr/local
ln -sv mysql-5.7.23-linux-glibc2.12-x86_64 mysql
chown -R mysql.mysql /usr/local/mysql
cd mysql
bin/mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data
vim /etc/my.cnf
'''
[mysqld]
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
basedir=/usr/local/mysql
symbolic-links=0
'''
scp /etc/my.cnf [email protected]:/etc/
cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld
chmod +x /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
systemctl start mysqld
vim /etc/profile.d/mysql.sh
'''
export PATH=$PATH:/usr/local/mysql/bin
'''
source /etc/profile.d/mysql.sh
mysqladmin -uroot -p'Giiep?io?3tr' password '411723'
mysql -uroot -p
>mysql
做双主复制:(两台互做主从复制)
vim /etc/my.cnf
'''
[mysqld]
log-bin=mysql-bin
server_id=2
'''
### (确保两个数据库一致)
相同数据库
( 做数据库的同步: 加入slave 存在数据库db1,而其他两个client端数据库为空:
作数据库的备份及同步:
mysqldump -uroot -p -B db1 | gzip > /bak/db1.sql.gz
scp /bak/db1.sql.gz [email protected]:/bak/
gzip -d /bak/db1.sql.gz
mysql -uroot -p < db1.sql
测试: mysql -uroot -p -e "select * from db1.t1;"
)
####
主库:
mysql -uroot -p -e 'grant replication slave,replication client on *.* to repl@"10.10.10.%" identified by "411723";'
备库:
mysql>change master to
->master_host = '10.10.10.150', ( 主库IP )
->master_user = 'repl', ( 刚刚主库授权的用户 )
->master_password = '411723', ( 刚刚主库授权用户的密码 )
->master_log_file = 'master-bin.000002', ( 主库执行show master status 查看 )
->master_log_pos = 234;
start slave;
show slave status\G;
-----> 查看 slave_IO_running:yes, slave_sql_running:yes 即为成功
VIP 绑定和ARP和Apache一致
配置143.144的/etc/keepalived/keepalived.conf 文件:
同Apache
测试同Apache
最终/etc/keepalived/keeplived.conf 文件为(主143上):
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_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 {
10.10.10.111
10.10.10.110
}
}
virtual_server 10.10.10.111 3306 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
#persistence_timeout 300
protocol TCP
real_server 10.10.10.149 3306 {
weight 1
TCP_CHECK {
connect_timeout 8
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
real_server 10.10.10.150 3306 {
weight 1
TCP_CHECK {
connect_timeout 8
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
virtual_server 10.10.10.110 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
#persistence_timeout 300
protocol TCP
real_server 10.10.10.145 80 {
weight 1
TCP_CHECK {
connect_timeout 8
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 10.10.10.146 80 {
weight 1
TCP_CHECK {
connect_timeout 8
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}