4、配置keepalived 在线视频演示
1)编辑配置文件
- #guration File for keepalived
- #global define
- global_defs {
- router_id Hadb_1
- }
- vrrp_sync_group VGM {
- group {
- VI_HA
- }
- }
- vrrp_script chk_mysql {
- script "killall -0 mysqld"
- interval 5
- }
- # vvrp_instance define #
- vrrp_instance VI_HA {
- state MASTER
- interface eth0
- virtual_router_id 51
- priority 100
- advert_int 5
- authentication {
- auth_type PASS
- auth_pass hamysql
- }
- virtual_ipaddress {
- 10.0.0.20
- }
- track_script {
- chk_mysql
- }
- }
2)启动Keepalived服务
分别启动两台服务器的Keepalived服务并观察其日志,效验高可用是否成功。
- [10.0.0.18@localhost ~]# service keepalived start
- [10.0.0.18@localhost ~]# tail /var/log/messages
- Jun 15 03:06:35 localhost Keepalived_vrrp: VRRP_Script(chk_mysql) succeeded
- Jun 15 03:06:36 localhost Keepalived_vrrp: VRRP_Instance(VI_HA) Transition to MASTER STATE
- Jun 15 03:06:41 localhost Keepalived_vrrp: VRRP_Instance(VI_HA) Entering MASTER STATE
- Jun 15 03:06:41 localhost Keepalived_vrrp: VRRP_Instance(VI_HA) setting protocol VIPs.
- Jun 15 03:06:41 localhost Keepalived_vrrp: VRRP_Instance(VI_HA) Sending gratuitous ARPs on eth0 for 10.0.0.20
- Jun 15 03:06:41 localhost Keepalived_vrrp: VRRP_Group(VGM) Syncing instances to MASTER state
- Jun 15 03:06:41 localhost Keepalived_vrrp: Netlink reflector reports IP 10.0.0.18 added
- Jun 15 03:06:41 localhost Keepalived_healthcheckers: Netlink reflector reports IP 10.0.0.18 added
- Jun 15 03:06:46 localhost Keepalived_vrrp: VRRP_Instance(VI_HA) Sending gratuitous ARPs on eth0 for 10.0.0.20
5、测试
1)找一台装有mysql客户端的windows或linux机器登陆VIP测试。
- [root@CentOS ~]# mysql -uroot -p123456 -h10.0.0.20
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 7
- Server version: 5.0.77-log Source distribution
- Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
2)keepalived故障转移测试
A:在windows或linux客户端一直去ping VIP,然后关闭10.0.0.18上的keepalived,正常情况下VIP就会切换到10.0.0.19上面去
B:开启10.0.0.18上的keepalived,看是否能自动切换
3)MySQL故障转移测试
A:在10.0.0.18上关闭MySQL服务,看VIP是否会切换到10.0.0.19上
- [10.0.0.18@localhost ~]# service mysqld stop
- [10.0.0.18@localhost ~]# tail /var/log/messages -f
- Jun 15 03:20:35 localhost Keepalived_vrrp: VRRP_Script(chk_mysql) failed
- Jun 15 03:20:36 localhost Keepalived_vrrp: VRRP_Instance(VI_HA) Entering FAULT STATE
- Jun 15 03:20:36 localhost Keepalived_vrrp: VRRP_Instance(VI_HA) removing protocol VIPs.
- Jun 15 03:20:36 localhost Keepalived_vrrp: VRRP_Instance(VI_HA) Now in FAULT state
- Jun 15 03:20:36 localhost Keepalived_vrrp: VRRP_Group(VGM) Syncing instances to FAULT state
- Jun 15 03:20:36 localhost Keepalived_vrrp: Netlink reflector reports IP 10.0.0.18 removed
- Jun 15 03:20:36 localhost Keepalived_healthcheckers: Netlink reflector reports IP 10.0.0.18 removed
- [10.0.0.19@localhost ~]# tail /var/log/messages -f
- Jun 15 03:20:47 localhost Keepalived_vrrp: VRRP_Instance(VI_HA) Transition to MASTER STATE
- Jun 15 03:20:47 localhost Keepalived_vrrp: VRRP_Group(VGM) Syncing instances to MASTER state
- Jun 15 03:20:52 localhost Keepalived_vrrp: VRRP_Instance(VI_HA) Entering MASTER STATE
- Jun 15 03:20:52 localhost Keepalived_vrrp: VRRP_Instance(VI_HA) setting protocol VIPs.
- Jun 15 03:20:52 localhost Keepalived_vrrp: VRRP_Instance(VI_HA) Sending gratuitous ARPs on eth0 for 10.0.0.20
- Jun 15 03:20:52 localhost Keepalived_vrrp: Netlink reflector reports IP 10.0.0.19 added
- Jun 15 03:20:52 localhost Keepalived_healthcheckers: Netlink reflector reports IP 10.0.0.19 added
- Jun 15 03:20:57 localhost Keepalived_vrrp: VRRP_Instance(VI_HA) Sending gratuitous ARPs on eth0 for 10.0.0.20
B:开启10.0.0.18上的MySQL和keepalived
- [10.0.0.18@localhost ~]# service mysqld start
- [10.0.0.19@localhost ~]# tail /var/log/messages -f
- Jun 15 03:23:52 localhost Keepalived_vrrp: VRRP_Instance(VI_HA) Received higher prio advert
- Jun 15 03:23:52 localhost Keepalived_vrrp: VRRP_Instance(VI_HA) Entering BACKUP STATE
- Jun 15 03:23:52 localhost Keepalived_vrrp: VRRP_Instance(VI_HA) removing protocol VIPs.
- Jun 15 03:23:52 localhost Keepalived_vrrp: VRRP_Group(VGM) Syncing instances to BACKUP state
- Jun 15 03:23:52 localhost Keepalived_vrrp: Netlink reflector reports IP 10.0.0.19 removed
- Jun 15 03:23:52 localhost Keepalived_healthcheckers: Netlink reflector reports IP 10.0.0.19 removed
C:下客户端连接的MySQL的VIP,在切换时我执行了一个MySQL查询命令
- mysql> show tables;
- No connection. Trying to reconnect...
- Connection id: 10
- Current database: test
- +----------------+
- | Tables_in_test |
- +----------------+
- | t2 |
- +----------------+
- 1 row in set (0.00 sec)