keepalvied 虚拟ip
第一步:主服务器上设置虚拟ip (也可以不设置虚拟ip,keepalived启动的时候会自动设置)
ifconfig ens33:1 192.168.136.131 netmask 255.255.255.0 给ens33网卡添加虚拟ip
ip addr del 192.168.136.131/24 dev ens33 删除ens33的虚拟ip
ip addr 查看ip情况
keepalvied 的日志存放在 /var/log/messages 可以tail -f /var/log/messages 查看日志,发现keepalived的问题
第二步,两台服务器分别安装keepalived
shell> cd /usr/local/src shell> wget http://www.keepalived.org/software/keepalived-1.2.24.tar.gz shell> tar -zxvf keepalived-1.2.24.tar.gz shell> cd keepalived-1.2.24 shell> ./configure --prefix=/usr/local/keepalived shell> make && make install
配置在/etc/keepalived下面建立keepalived.conf配置文件
/etc/keepalived/keepalived.conf /etc/rc.d/init.d/keepalived /etc/sysconfig/keepalived
将配置文件拷贝到系统对应的目录下
shell> mkdir /etc/keepalived shell> cp /usr/local/keepalived/etc/keepalived.conf /etc/keepalived/keepalived.conf shell> cp /usr/local/keepalived/sbin/keepalived /etc/rc.d/init.d/keepalived shell> cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived
service keepalived -D 启动keepalived
keepalived正常运行后,会启动3个进程,其中一个是父进程,负责监控其子进程。一个是vrrp子进程,另外一个是checkers子进程
第三步两台服务器分配配置keepalived.conf
第一台主服务器配置文件
! Configuration File for keepalived global_defs { router_id LVS_DEVEL #标识,双主相同 } vrrp_instance VI_1 { state BACKUP #注意,主从两端都配置成了backup,因为使用了nopreempt,即非抢占模式 interface ens33 virtual_router_id 51 #分组,主备相同 priority 100 #优先级,这个高一点则先把它作为master advert_int 1 nopreempt #不主动抢占资源,设置非抢占模式 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.136.141 #虚拟ip } } virtual_server 192.168.136.141 3306 { delay_loop 2 lb_algo wrr #带有权重的轮询 lb_kind DR persistence_timeout 60 #同一IP的连接60秒内被分配到同一台真实服务器 protocol TCP real_server 192.168.136.128 3306 { #真实服务器ip weight 3 #权重为3 notify_down /home/keepalived_shutdown.sh #当mysq服down时,执行此脚本,杀死keepalived实现切换, 自杀脚本. TCP_CHECK { connect_timeout 11 nb_get_retry 3 delay_before_retry 3 connect_port 3306 } } }
从服务器配置文件
! Configuration File for keepalived global_defs { router_id LVS_DEVEL } vrrp_instance VI_1 { state BACKUP interface ens33 virtual_router_id 51 priority 90 #优先级,这个低一点 advert_int 1 nopreempt authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.136.141 #虚拟IP (VIP) } } virtual_server 192.168.136.141 3306 { #真实ip delay_loop 2 #每2秒检查一次real_server存活 lb_algo wrr lb_kind DR persistence_timeout 60 protocol TCP real_server 192.168.136.129 3306 { weight 1 notify_down /home/keepalived_shutdown.sh TCP_CHECK { connect_timeout 11 #连接超时时间 nb_get_retry 3 #重连次数 delay_before_retry 3 #重连间隔时间 connect_port 3306 #健康检查端口,配置自己mysql服务端口 } } }
有一点要注意的是,主从两端的state,都配置成了backup,因为使用了nopreempt,即非抢占模式。
举个例子,当主端先启动mysql实例和keepalived后,如果此时从端也启动了mysql实例和keepalived,那么vip不会跳到从端上去,即使它的优先级为100,要大于主端的90
而如果不设置nopreempt,那么这个时候,又分2种情况:
1.state相同,即都是master或都是backup
优先级高的,会占有vip,和角色无关
2.state不同,即master->backup或backup->master
优先级高的,会占有vip,和角色无关
前提不同,结果都是一样的,即优先级是主导,谁的优先级高,vip就漂到谁那里
可以看到A和B配置的共同点,就是virtual_ipaddress都是,这样当我们使用mysql的时候,就连接到这个虚拟ip上,由它进行转发到实体机。priority值高的先被访问,作为Master。这样实体机mysql的down机重启和转换主从角色则由keepalived来完成,对应用层透明,让应用无法感知。两台机器要通局域网,虚拟IP会被设置在他们共有的局域网内,不通的话两台机器各设置各的虚拟IP
杀死keepalived的脚本
vim /data/keepalived_shutdown.sh
#!/bin/bash #kill掉keepalived进程,以防止脑裂问题。 pkill keepalived