2019独角兽企业重金招聘Python工程师标准>>>
1 高可用安装
1.1 集群信息
主机 | IP | 组件 |
bdc212 | 192.168.13.212 | LVS:ipvsadm-1.27-7.el7.x86_64.rpm Keepalived: keepalived-1.2.4.tar.gz 应用:Percona-XtraDB-Cluster |
bdc213 | 192.168.13.213 | LVS:ipvsadm-1.27-7.el7.x86_64.rpm Keepalived: keepalived-1.2.4.tar.gz 应用:Percona-XtraDB-Cluster |
bdc214 | 192.168.13.214 | 应用:Percona-XtraDB-Cluster |
1.2 安装准备
- 上传安装所需的压缩包到/opt目录
keepalived-1.2.4.tar.gz
(http://www.keepalived.org/software/keepalived-1.2.4.tar.gz)
- 安装组件所需依赖包
yum install openssl-devel popt-devel libnl* -y
1.3 安装LVS+keepalived
在两台Director Server上分别安装LVS+Keepalived,BACKUP服务器与MASTER服务器一致,先安装lvs再安装keepalived。
1.3.3 安装LVS
Ipvs(IP Virtual Server)是整个负载均衡的基础,没有这个基础,故障隔离与失败切换就毫无意义了。
yum install ipvsadm –y
1.3.4 安装keepalived
解压包:
tar zxvf keepalived-1.2.4.tar.gz
初始化:
cd keepalived-1.2.4
./configure --prefix=/usr/local/keepalived
Keepalived configuration
------------------------
Keepalived version : 1.2.4
Compiler : gcc
Compiler flags : -g -O2
Extra Lib : -lpopt -lssl -lcrypto -lnl
Use IPVS Framework : Yes
IPVS sync daemon support : Yes
IPVS use libnl : Yes
Use VRRP Framework : Yes
Use VRRP VMAC : Yes
Use Debug flags : No
编译并安装:
make&&make install
Make complete
make -C keepalived install
make[1]: Entering directory `/root/Desktop/keepalived-1.2.1/keepalived'
install -d /usr/local/keepalived/sbin
install -m 700 ../bin/keepalived /usr/local/keepalived/sbin/
install -d /usr/local/keepalived/etc/rc.d/init.d
install -m 755 etc/init.d/keepalived.init /usr/local/keepalived/etc/rc.d/init.d/keepalived
install -d /usr/local/keepalived/etc/sysconfig
install -m 755 etc/init.d/keepalived.sysconfig /usr/local/keepalived/etc/sysconfig/keepalived
install -d /usr/local/keepalived/etc/keepalived/samples
install -m 644 etc/keepalived/keepalived.conf /usr/local/keepalived/etc/keepalived/
install -m 644 ../doc/samples/* /usr/local/keepalived/etc/keepalived/samples/
install -d /usr/local/keepalived/share/man/man5
install -d /usr/local/keepalived/share/man/man8
install -m 644 ../doc/man/man5/keepalived.conf.5 /usr/local/keepalived/share/man/man5
install -m 644 ../doc/man/man8/keepalived.8 /usr/local/keepalived/share/man/man8
make[1]: Leaving directory `/root/Desktop/keepalived-1.2.1/keepalived'
make -C genhash install
make[1]: Entering directory `/root/Desktop/keepalived-1.2.1/genhash'
install -d /usr/local/keepalived/bin
install -m 755 ../bin/genhash /usr/local/keepalived/bin/
install -d /usr/local/keepalived/share/man/man1
install -m 644 ../doc/man/man1/genhash.1 /usr/local/keepalived/share/man/man1
make[1]: Leaving directory `/root/Desktop/keepalived-1.2.1/genhash'
1.4 配置路由转发
1> 更改配置文件
vi /etc/sysctl.conf
net.ipv4.ip_forward = 1
2> 刷新系统配置
sysctl -p
1.5 配置keepalived.cnf
keepalived的配置分为三类: 全局配置(global configuration),VRRPD配置,LVS配置。
全局配置就是对整个keepalived起效的配置,不管是否使用LVS。VRRPD是keepalived的核心,LVS配置只在要使用keepalived来配置的管理LVS时需要使用,如果仅仅使用keepalived来做HA,LVS的配置是不需要的。如果在只有一个负载均衡器的场合,就不须 VRRP 实例定义块。
mkdir /etc/keepalived
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_MASTER #BACKUP LVS_BACKUP
}
vrrp_instance VI_1 {
state MASTER #BACKUP BACKUP
interface eno16777736
virtual_router_id 51
priority 100 #BACKUP 80
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.252.199
}
}
virtual_server 192.168.252.199 3306 {
delay_loop 6
lb_algo rr
lb_kind NAT
# persistence_timeout 5
protocol TCP
real_server 192.168.252.159 3306 {
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
real_server 192.168.252.160 3306 {
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
real_server 192.168.252.161 3306 {
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
注:配置backup的/etc/keepalived/keepalived.conf,只需将批注部分改一下即可。
2 keepalived集群启动
2.1 master节点启动
/usr/local/keepalived/sbin/keepalived start
检查虚拟IP启动情况
方法1:
tail -30 /var/log/message
Jun 15 16:24:25 localhost Keepalived_vrrp[54297]: Interface queue is empty
Jun 15 16:24:25 localhost Keepalived_vrrp[54297]: No such interface, virbr0
Jun 15 16:24:25 localhost Keepalived_vrrp[54297]: No such interface, virbr0-nic
Jun 15 16:24:25 localhost Keepalived_vrrp[54297]: Registering Kernel netlink reflector
Jun 15 16:24:25 localhost Keepalived_vrrp[54297]: Registering Kernel netlink command channel
Jun 15 16:24:25 localhost Keepalived_vrrp[54297]: Registering gratuitous ARP shared channel
Jun 15 16:24:25 localhost Keepalived_vrrp[54297]: Opening file '/etc/keepalived/keepalived.conf'.
Jun 15 16:24:25 localhost Keepalived_vrrp[54297]: Configuration is using : 61916 Bytes
Jun 15 16:24:25 localhost Keepalived_vrrp[54297]: Using LinkWatch kernel netlink reflector...
Jun 15 16:24:25 localhost Keepalived_healthcheckers[54296]: Activating healthchecker for service [192.168.252.199]:3306
Jun 15 16:24:25 localhost Keepalived_healthcheckers[54296]: Activating healthchecker for service [192.168.252.199]:3306
Jun 15 16:24:25 localhost Keepalived_healthcheckers[54296]: Activating healthchecker for service [192.168.252.199]:3306
Jun 15 16:24:25 localhost Keepalived_vrrp[54297]: VRRP_Instance(VI_1) Transition to MASTER STATE
Jun 15 16:24:26 localhost Keepalived_vrrp[54297]: VRRP_Instance(VI_1) Entering MASTER STATE
Jun 15 16:24:26 localhost avahi-daemon[768]: Registering new address record for 192.168.252.199 on eno16777736.IPv4.
方法2:
[root@localhost Desktop]#ip addr
2.2 backup节点启动
/usr/local/keepalived/sbin/keepalived start
2.3 查看路由分发情况
查看ipvsadm
[root@localhost Desktop]#ipvsadm
也能够通过mysqladmin查看本地mysql服务连接状况
/opt/percona/bin/mysqladmin -uroot -p123456 status
3 PXC故障测试
3.1 杀死mysql服务
杀死节点三上的mysql服务
kill -9 mysqld’s pid
3.2 查看ipvsadm
[root@localhost Desktop]# ipvsadm
注:负载均衡服务解除了节点三,只向节点一二分发包。
3.3 重启mysql服务
/usr/share/percona-xtradb-cluster/mysql.server start
3.4 再查看ipvsadm
[root@localhost Desktop]# ipvsadm
注:负载均衡服务重新把节点三放入了路由分发列表。
4 高可用测试
4.1 杀死master的KP服务
Kill -9 keepalived'pid
4.2 查看组建工作情况
1> VIP持有情况:
查看master ip:#ip add
Master释放了VIP:192.168.13.100
查看backup ip:#ip add
Backup接收了VIP:192.168.13.100,继续提供负载均衡服务
2> ipvsadm工作情况
查看master Ipvsadm:负载均衡没有工作
[root@localhost keepalived-1.2.4]# ipvsadm
查看backup ipvsadm:负载均衡仍然提供服务
[root@localhost keepalived-1.2.4]# ipvsadm