到这一步的时候, 是主备部署已经处理好, 请关注:mysql主备部署[高可用]
这次使用的是keepalived-1.2.22.tar.gz版, 官网地址:keeplived官网
笼统知识请自行查询百度下面开始步骤
第一步:安装keepalived-1.2.22
解压 tar -zxvf keepalived-1.2.22.tar.gz 编译 cd keepalived-1.2.22 ./configure --prefix=/usr/local/keepalived/(新建文件夹喔) 安装 make && make install 服务脚本 cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/keepalived 配置文件 cp /usr/local/keepalived/etc/keepalived.conf /etc/keepalived/keepalived.conf 服务 cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived 加入服务 chkconfig --add keepalived chkconfig keepalived on service keepalived start #启动服务 service keepalived stop #停止服务 service keepalived restart #重启服务
上面的额操作主备机器都需要安装
第二步:配置文件
master:keepalived.conf
! Configuration File for keepalived global_defs { router_id HA_MySQL } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 100 advert_int 1 nopreempt authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.88.88 } } virtual_server 192.168.88.88 3306 { delay_loop 2 lb_algo wrr lb_kind DR persistence_timeout 60 protocol TCP real_server 192.168.75.61 3306 { weight 3 notify_down /etc/keepalived/down.sh TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 3306 } } }
slave:keepalived.conf
! Configuration File for keepalived global_defs { router_id HA_MySQL } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 90 advert_int 1 nopreempt authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.88.88 } } virtual_server 192.168.88.88 3306 { delay_loop 2 lb_algo wrr lb_kind DR persistence_timeout 60 protocol TCP real_server 192.168.75.62 3306 { weight 3 # echo"62....1" notify_down /etc/keepalived/down.sh # notify_up /etc/keepalived/up.sh # echo"62....2" TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 3306 } } }
down:down.sh
#!/bin/bash pkill keepalived
第三步:测试
主:192.168.75.61 备:192.168.75.62 主: [root@qwzs08 log]#service mysqld start [root@qwzs08 log]#service keeplived start 保证已经启动成功! [root@qwzs08 log]#ip addr 会显示出下面的vip(我上面配置文件把master放在61) 1: lo:mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: mtu 1500 qdisc mq state UNKNOWN qlen 1000 link/ether 00:50:56:89:a3:28 brd ff:ff:ff:ff:ff:ff inet 192.168.75.61/24 brd 192.168.75.255 scope global eth0 inet 192.168.88.88/32 scope global eth0 inet6 fe80::250:56ff:fe89:a328/64 scope link valid_lft forever preferred_lft forever 表示vip正常 此时在备机 [root@qwzs07 log]#ip addr 1: lo: mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: mtu 1500 qdisc mq state UNKNOWN qlen 1000 link/ether 00:50:56:89:cb:a6 brd ff:ff:ff:ff:ff:ff inet 192.168.75.62/24 brd 192.168.75.255 scope global eth0 inet6 fe80::250:56ff:fe89:cba6/64 scope link valid_lft forever preferred_lft forever 是没有vip的, 恭喜您!配置成功!
第四步:漂移
模拟主机宕机 ps aux | grep mysqld root 1176 0.0 0.0 11336 1408 ? S 15:38 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/mysql3306.pid mysql 1591 7.1 1.8 2736136 597708 ? Sl 15:38 3:19 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/usr/local/mysql/data/mysql3306.err --pid-file=/usr/local/mysql/data/mysql3306.pid root 1978 0.0 0.0 103248 844 pts/0 S+ 16:24 0:00 grep mysql kill -9 1176 1591 此时,主机的mysql挂掉了,您怎么确定是有没有漂移呢? 查一下vip ip addr 1: lo:mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: mtu 1500 qdisc mq state UNKNOWN qlen 1000 link/ether 00:50:56:89:cb:a6 brd ff:ff:ff:ff:ff:ff inet 192.168.75.61/24 brd 192.168.75.255 scope global eth0 inet6 fe80::250:56ff:fe89:cba6/64 scope link valid_lft forever preferred_lft forever 已经没有了 再去备机查一下vip ip addr 1: lo: mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: mtu 1500 qdisc mq state UNKNOWN qlen 1000 link/ether 00:50:56:89:a3:28 brd ff:ff:ff:ff:ff:ff inet 192.168.75.62/24 brd 192.168.75.255 scope global eth0 inet 192.168.88.88/32 scope global eth0 inet6 fe80::250:56ff:fe89:a328/64 scope link valid_lft forever preferred_lft forever look, 漂过来了 现在切换到主机把的mysql和keeplived开启 此时主机的服务已经开起来了, 但是vip并没有漂过去, 这就是nopreempt的作用, 防止脑裂 这时候加入备机slave挂掉, vip就会漂移到主机master, 实现高可用主从切换
此时如果能实现漂移就是成功了, 下面是我遇到的问题
1.keepalived执行后日志狂刷, 2.脚本并没有执行(down.sh),具体情况就是mysql挂掉后,keeplived并没有自杀,导致vip漂移失败, 但是手动关闭keeplived后会实现漂移, 原因就在于down脚本没有得到执行。
日志报错:
Nov 21 16:05:27 qwzs08 abrt[13378]: Not saving repeating crash in '/usr/sbin/keepalived'
Nov 21 16:05:27 qwzs08 Keepalived_healthcheckers: IPVS: Can't initialize ipvs: Protocol not available
Nov 21 16:05:27 qwzs08 Keepalived_healthcheckers: Netlink reflector reports IP 192.168.75.62 added
Nov 21 16:05:27 qwzs08 Keepalived_healthcheckers: Netlink reflector reports IP 192.168.75.62 added
Nov 21 16:05:27 qwzs08 Keepalived_healthcheckers: Registering Kernel netlink reflector
Nov 21 16:05:27 qwzs08 Keepalived_healthcheckers: Registering Kernel netlink command channel
Nov 21 16:05:27 qwzs08 Keepalived_healthcheckers: Opening file '/etc/keepalived/keepalived.conf'.
Nov 21 16:05:27 qwzs08 Keepalived_healthcheckers: Configuration is using : 9635 Bytes
很纠结,经过排查,原因是ip_vs模块系统默认没有自动加载
执行命令:
查看 lsmod | grep ip_vs (如果没有任何输出则表示ip_vs模块并没有被内核加载) 手动加载 modprobe ip_vs modprobe ip_vs_wrr
然后再看日志已经恢复正常
此时再查看ip_vs
lsmod | grep ip_vs
ip_vs_wrr 2179 1 ip_vs 115643 3 ip_vs_wrr libcrc32c 1246 1 ip_vs ipv6 321422 68 ip_vs,ip6t_REJECT,nf_conntrack_ipv6,nf_defrag_ipv6
ok!