昊漫玉 发表于 2015-9-4 09:06:50

14、高可用keepalived搭建及切换

keepalived主从切换试验:
1、先搭建192.168.1.20与192.168.1.21的主主架构   192.168.1.76为VIP
2、在2机都安装keepalived


[*] tar -zxvf keepalived-1.2.13.tar.gz
[*] cd keepalived-1.2.13
[*] ./configure -prefix=/usr/local/keepalived
[*] make
[*] make install
[*] # cat /opt/mysql/mysql.sh
[*] #!/bin/sh
[*] pkill keepalived
[*] # chmod +x /opt/mysql/mysql.sh

3、配置20机器的keepalived配置文件


[*]# cat /usr/local/keepalived/etc/keepalived.conf
[*]!ConfigurationFilefor keepalived
[*]
[*]global_defs {
[*]   notification_email {
[*]   likun@163.com
[*]   }
[*]   router_id mysql-ha
[*]}
[*]
[*]vrrp_instance VI_20 {
[*]    state BACKUP
[*]    interface eth0
[*]    virtual_router_id 20
[*]    priority 90
[*]    advert_int 1
[*]    nopreempt      
[*]    authentication {
[*]      auth_type PASS
[*]      auth_pass 1111
[*]    }
[*]    virtual_ipaddress {   
[*]      192.168.1.76
[*]    }
[*]}
[*]
[*]virtual_server 192.168.1.763306{
[*]    delay_loop 2
[*]    lb_algo wrr
[*]    lb_kind DR
[*]    persistence_timeout 60
[*]    protocol TCP
[*]
[*]    real_server 192.168.1.203306{
[*]      weight 3
[*]      notify_down /opt/mysql/mysql.sh
[*]            TCP_CHECK {
[*]            connect_timeout 10
[*]            nb_get_retry 3
[*]            delay_before_retry 3
[*]            connect_port 3306
[*]      }
[*]    }
[*]}

4、配置21机器的keepalived配置文件


[*]!ConfigurationFilefor keepalived
[*]
[*]global_defs {
[*]   notification_email {
[*]   likun@163.com
[*]   }
[*]   router_id mysql-ha
[*]}
[*]
[*]vrrp_instance VI_21 {
[*]    state BACKUP
[*]    interface eth0
[*]    virtual_router_id 20      --注意2边要一致
[*]    priority 100
[*]    advert_int 1
[*]    nopreempt      
[*]    authentication {
[*]      auth_type PASS
[*]      auth_pass 1111
[*]    }
[*]    virtual_ipaddress {   
[*]      192.168.1.76
[*]    }
[*]}
[*]
[*]virtual_server 192.168.1.763306{
[*]    delay_loop 2
[*]    lb_algo wrr
[*]    lb_kind DR
[*]    persistence_timeout 60
[*]    protocol TCP
[*]
[*]    real_server 192.168.1.213306{
[*]      weight 3
[*]      notify_down /opt/mysql/mysql.sh
[*]            TCP_CHECK {
[*]            connect_timeout 10
[*]            nb_get_retry 3
[*]            delay_before_retry 3
[*]            connect_port 3306
[*]      }
[*]    }
[*]}

5、测试

[*]在20机器测试,mysql没有启动的情况下,keepalived启动会被自动杀掉,证明keepalived配置正确

service mysql stop
/usr/local/keepalived/sbin/keepalived -f /usr/local/keepalived/etc/keepalived.conf -D
ps -ef|grep keepalived      看到没有keepalived进程


[*]在2边启动mysql并启动keepalived,测试用vip登陆数据库



[*]# mysql -h192.168.1.76-P3306 -uroot -proot
[*]Welcome to the MySQL monitor.Commands end with ; or \g.
[*]YourMySQL connection id is 682
[*]Server version:5.5.30-log Source distribution
[*]
[*]Copyright(c)2000,2013,Oracle and/or its affiliates.All rights reserved.
[*]
[*]Oracle is a registered trademark of OracleCorporation and/or its
[*]affiliates.Other names may be trademarks of their respective
[*]owners.
[*]
[*]Type'help;' or '\h'for help.Type'\c' to clear the current input statement.
[*]
[*]mysql>


21机器查看IP:


[*]# ip addr show
[*]1: lo:<LOOPBACK,UP,LOWER_UP> 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:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
[*]    link/ether 08:00:27:5d:ef:c3 brd ff:ff:ff:ff:ff:ff
[*]    inet 192.168.1.21/24 brd 192.168.1.255 scope global eth0
[*]    inet 192.168.1.76/32 scope global eth0
[*]    inet6 fe80::a00:27ff:fe5d:efc3/64 scope link
[*]       valid_lft forever preferred_lft forever


20机器查看IP:


[*]# ip addr show
[*]1: lo:<LOOPBACK,UP,LOWER_UP> 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:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
[*]    link/ether 08:00:27:00:58:57 brd ff:ff:ff:ff:ff:ff
[*]    inet 192.168.1.20/24 brd 192.168.1.255 scope global eth0
[*]    inet6 fe80::a00:27ff:fe00:5857/64 scope link
[*]       valid_lft forever preferred_lft forever

关闭20节点的mysql,发现VIP漂移到21上,并且20上的keepalived被kill


[*]杀掉master的keepalived,vip会飘到slave上,slave的日志:



[*]Jul2020:03:31 likun Keepalived_vrrp: VRRP_Instance(VI_20)Transition to MASTER STATE
[*]Jul2020:03:32 likun Keepalived_vrrp: VRRP_Instance(VI_20)Entering MASTER STATE
[*]Jul2020:03:32 likun Keepalived_vrrp: VRRP_Instance(VI_20) setting protocol VIPs.
[*]Jul2020:03:32 likun Keepalived_vrrp: VRRP_Instance(VI_20)Sending gratuitous ARPs on eth0 for192.168.1.76
[*]Jul2020:03:32 likun Keepalived_healthcheckers:Netlink reflector reports IP 192.168.1.76 added


[*]再启动master的keepalived,不会抢vip,变为backup state,master的日志:



[*]Jul2020:03:49 likun1 Keepalived:StartingKeepalived v1.2.13(07/20,2014)
[*]Jul2020:03:49 likun1 Keepalived:StartingHealthcheck child process, pid=7418
[*]Jul2020:03:49 likun1 Keepalived:Starting VRRP child process, pid=7419
[*]Jul2020:03:49 likun1 Keepalived_vrrp:Netlink reflector reports IP 192.168.1.21 added
[*]Jul2020:03:49 likun1 Keepalived_healthcheckers:Netlink reflector reports IP 192.168.1.21 added
[*]Jul2020:03:49 likun1 Keepalived_healthcheckers:Netlink reflector reports IP fe80::a00:27ff:fe5d:efc3 added
[*]Jul2020:03:49 likun1 Keepalived_healthcheckers:RegisteringKernel netlink reflector
[*]Jul2020:03:49 likun1 Keepalived_healthcheckers:RegisteringKernel netlink command channel
[*]Jul2020:03:49 likun1 Keepalived_vrrp:Netlink reflector reports IP fe80::a00:27ff:fe5d:efc3 added
[*]Jul2020:03:49 likun1 Keepalived_vrrp:RegisteringKernel netlink reflector
[*]Jul2020:03:49 likun1 Keepalived_vrrp:RegisteringKernel netlink command channel
[*]Jul2020:03:49 likun1 Keepalived_vrrp:Registering gratuitous ARP shared channel
[*]Jul2020:03:49 likun1 Keepalived_vrrp:Opening file '/usr/local/keepalived/etc/keepalived.conf'.
[*]Jul2020:03:49 likun1 Keepalived_vrrp:Configuration is using:62953Bytes
[*]Jul2020:03:49 likun1 Keepalived_vrrp:UsingLinkWatch kernel netlink reflector...
[*]Jul2020:03:49 likun1 Keepalived_vrrp: VRRP_Instance(VI_21)Entering BACKUP STATE
[*]Jul2020:03:49 likun1 Keepalived_vrrp: VRRP sockpool:
[*]Jul2020:03:49 likun1 Keepalived_healthcheckers:Opening file '/usr/local/keepalived/etc/keepalived.conf'.
[*]Jul2020:03:49 likun1 Keepalived_healthcheckers:Configuration is using:11555Bytes
[*]Jul2020:03:49 likun1 Keepalived_healthcheckers:UsingLinkWatch kernel netlink reflector...
[*]Jul2020:03:49 likun1 Keepalived_healthcheckers:Activating healthchecker for service :3306



切换时不会kill keepalived的方式
20、21先配置主主架构

2边数据库做授权:
root@localhost 19:30:38 > GRANT REPLICATION CLIENT ON *.* TO 'monitor'@'%' IDENTIFIED BY 'm0n1tor';
Query OK, 0 rows affected (0.00 sec)
flush privileges

2边系统拷贝检测脚本并+x
chmod +x /usr/local/keepalived/checkMySQL.py      (脚本在附件,要检查一下脚本变量值)

两边配置文件做成如下:
20:


[*]# cat /usr/local/keepalived/etc/keepalived.conf
[*]vrrp_script vs_mysql_20 {
[*]    script "/usr/local/keepalived/checkMySQL.py -h 192.168.1.20 -P 3306"
[*]    interval 60
[*]}
[*]vrrp_instance VI_20 {
[*]    state BACKUP
[*]    nopreempt
[*]    interface eth0
[*]    virtual_router_id 21
[*]    priority 100
[*]    advert_int 5
[*]    authentication {
[*]      auth_type PASS
[*]      auth_pass 1111
[*]    }
[*]    track_script {
[*]       vs_mysql_20
[*]    }
[*]    virtual_ipaddress {
[*]      192.168.1.76
[*]    }
[*]}

21:


[*]# cat /usr/local/keepalived/etc/keepalived.conf
[*]vrrp_script vs_mysql_21 {
[*]    script "/usr/local/keepalived/checkMySQL.py -h 192.168.1.21 -P 3306"
[*]    interval 60
[*]}
[*]vrrp_instance VI_21 {
[*]    state BACKUP
[*]    nopreempt
[*]    interface eth0
[*]    virtual_router_id 21
[*]    priority 90
[*]    advert_int 5
[*]    authentication {
[*]      auth_type PASS
[*]      auth_pass 1111
[*]    }
[*]    track_script {
[*]       vs_mysql_21
[*]    }
[*]    virtual_ipaddress {
[*]      192.168.1.76
[*]    }
[*]}


启动20的keepalived,并查看日志,发现启动为master状态,vip绑定:


[*]#/usr/local/keepalived/sbin/keepalived -f /usr/local/keepalived/etc/keepalived.conf -D
[*]# tail -f /var/log/messages
[*]Aug820:58:41 likun Keepalived:StoppingKeepalived v1.2.13(07/20,2014)
[*]Aug820:59:04 likun Keepalived:StartingKeepalived v1.2.13(07/20,2014)
[*]Aug820:59:04 likun Keepalived:StartingHealthcheck child process, pid=8066
[*]Aug820:59:04 likun Keepalived:Starting VRRP child process, pid=8067
[*]Aug820:59:04 likun Keepalived_vrrp:Netlink reflector reports IP 192.168.1.20 added
[*]Aug820:59:04 likun Keepalived_vrrp:Netlink reflector reports IP 10.0.3.15 added
[*]Aug820:59:04 likun Keepalived_vrrp:Netlink reflector reports IP fe80::a00:27ff:fe00:5857 added
[*]Aug820:59:04 likun Keepalived_healthcheckers:Netlink reflector reports IP 192.168.1.20 added
[*]Aug820:59:04 likun Keepalived_vrrp:Netlink reflector reports IP fe80::a00:27ff:fe5d:683a added
[*]Aug820:59:04 likun Keepalived_vrrp:RegisteringKernel netlink reflector
[*]Aug820:59:04 likun Keepalived_vrrp:RegisteringKernel netlink command channel
[*]Aug820:59:04 likun Keepalived_vrrp:Registering gratuitous ARP shared channel
[*]Aug820:59:04 likun Keepalived_healthcheckers:Netlink reflector reports IP 10.0.3.15 added
[*]Aug820:59:04 likun Keepalived_healthcheckers:Netlink reflector reports IP fe80::a00:27ff:fe00:5857 added
[*]Aug820:59:04 likun Keepalived_healthcheckers:Netlink reflector reports IP fe80::a00:27ff:fe5d:683a added
[*]Aug820:59:04 likun Keepalived_healthcheckers:RegisteringKernel netlink reflector
[*]Aug820:59:04 likun Keepalived_healthcheckers:RegisteringKernel netlink command channel
[*]Aug820:59:05 likun Keepalived_vrrp:Opening file '/usr/local/keepalived/etc/keepalived.conf'.
[*]Aug820:59:05 likun Keepalived_healthcheckers:Opening file '/usr/local/keepalived/etc/keepalived.conf'.
[*]Aug820:59:05 likun Keepalived_vrrp:Configuration is using:62993Bytes
[*]Aug820:59:05 likun Keepalived_healthcheckers:Configuration is using:5287Bytes
[*]Aug820:59:05 likun Keepalived_vrrp:UsingLinkWatch kernel netlink reflector...
[*]Aug820:59:05 likun Keepalived_vrrp: VRRP_Instance(VI_20)Entering BACKUP STATE
[*]Aug820:59:05 likun Keepalived_vrrp: VRRP sockpool:
[*]Aug820:59:05 likun Keepalived_healthcheckers:UsingLinkWatch kernel netlink reflector...
[*]Aug820:59:07 likun Keepalived_vrrp: VRRP_Script(vs_mysql_20) succeeded
[*]Aug820:59:20 likun Keepalived_vrrp: VRRP_Instance(VI_20)Transition to MASTER STATE
[*]Aug820:59:25 likun Keepalived_vrrp: VRRP_Instance(VI_20)Entering MASTER STATE
[*]Aug820:59:25 likun Keepalived_vrrp: VRRP_Instance(VI_20) setting protocol VIPs.
[*]Aug820:59:25 likun Keepalived_vrrp: VRRP_Instance(VI_20)Sending gratuitous ARPs on eth0 for192.168.1.76
[*]Aug820:59:25 likun Keepalived_healthcheckers:Netlink reflector reports IP 192.168.1.76 added
[*]Aug820:59:30 likun Keepalived_vrrp: VRRP_Instance(VI_20)Sending gratuitous ARPs on eth0 for192.168.1.76

启动21的keepalived,并查看日志,启动为backup状态:


[*]#/usr/local/keepalived/sbin/keepalived -f /usr/local/keepalived/etc/keepalived.conf -D
[*]# tail -f /var/log/messages
[*]Aug918:11:27 likun1 Keepalived:StartingKeepalived v1.2.13(07/20,2014)
[*]Aug918:11:27 likun1 Keepalived:StartingHealthcheck child process, pid=3357
[*]Aug918:11:27 likun1 Keepalived:Starting VRRP child process, pid=3358
[*]Aug918:11:27 likun1 Keepalived_vrrp:Netlink reflector reports IP 192.168.1.21 added
[*]Aug918:11:27 likun1 Keepalived_healthcheckers:Netlink reflector reports IP 192.168.1.21 added
[*]Aug918:11:27 likun1 Keepalived_healthcheckers:Netlink reflector reports IP 10.0.3.15 added
[*]Aug918:11:27 likun1 Keepalived_healthcheckers:Netlink reflector reports IP fe80::a00:27ff:fe5d:efc3 added
[*]Aug918:11:27 likun1 Keepalived_vrrp:Netlink reflector reports IP 10.0.3.15 added
[*]Aug918:11:27 likun1 Keepalived_healthcheckers:Netlink reflector reports IP fe80::a00:27ff:fe97:754a added
[*]Aug918:11:27 likun1 Keepalived_healthcheckers:RegisteringKernel netlink reflector
[*]Aug918:11:27 likun1 Keepalived_healthcheckers:RegisteringKernel netlink command channel
[*]Aug918:11:27 likun1 Keepalived_vrrp:Netlink reflector reports IP fe80::a00:27ff:fe5d:efc3 added
[*]Aug918:11:27 likun1 Keepalived_vrrp:Netlink reflector reports IP fe80::a00:27ff:fe97:754a added
[*]Aug918:11:27 likun1 Keepalived_vrrp:RegisteringKernel netlink reflector
[*]Aug918:11:27 likun1 Keepalived_vrrp:RegisteringKernel netlink command channel
[*]Aug918:11:27 likun1 Keepalived_vrrp:Registering gratuitous ARP shared channel
[*]Aug918:11:30 likun1 Keepalived_vrrp:Opening file '/usr/local/keepalived/etc/keepalived.conf'.
[*]Aug918:11:30 likun1 Keepalived_healthcheckers:Opening file '/usr/local/keepalived/etc/keepalived.conf'.
[*]Aug918:11:30 likun1 Keepalived_vrrp:Configuration is using:63001Bytes
[*]Aug918:11:30 likun1 Keepalived_healthcheckers:Configuration is using:5295Bytes
[*]Aug918:11:30 likun1 Keepalived_vrrp:UsingLinkWatch kernel netlink reflector...
[*]Aug918:11:30 likun1 Keepalived_vrrp: VRRP_Instance(VI_21)Entering BACKUP STATE
[*]Aug918:11:30 likun1 Keepalived_vrrp: VRRP sockpool:
[*]Aug918:11:30 likun1 Keepalived_healthcheckers:UsingLinkWatch kernel netlink reflector...
[*]Aug918:11:31 likun1 Keepalived_vrrp: VRRP_Script(vs_mysql_21) succeeded

20机器关闭mysql,看到20 keepalived切换为fault状态,21机器切换为master状态并绑定vip


[*]20日志:
[*]Aug823:09:05 likun Keepalived_vrrp: VRRP_Script(vs_mysql_20) failed
[*]Aug823:09:08 likun Keepalived_vrrp: VRRP_Instance(VI_20)Entering FAULT STATE
[*]Aug823:09:08 likun Keepalived_vrrp: VRRP_Instance(VI_20) removing protocol VIPs.
[*]Aug823:09:08 likun Keepalived_vrrp: VRRP_Instance(VI_20)Now in FAULT state
[*]Aug823:09:08 likun Keepalived_healthcheckers:Netlink reflector reports IP 192.168.1.76 removed



[*]21日志:
[*]Aug920:21:00 likun1 Keepalived_vrrp: VRRP_Instance(VI_21)Transition to MASTER STATE
[*]Aug920:21:05 likun1 Keepalived_vrrp: VRRP_Instance(VI_21)Entering MASTER STATE
[*]Aug920:21:05 likun1 Keepalived_vrrp: VRRP_Instance(VI_21) setting protocol VIPs.
[*]Aug920:21:05 likun1 Keepalived_healthcheckers:Netlink reflector reports IP 192.168.1.76 added
[*]Aug920:21:05 likun1 avahi-daemon:Registeringnew address record for192.168.1.76 on eth0.IPv4.
[*]Aug920:21:05 likun1 Keepalived_vrrp: VRRP_Instance(VI_21)Sending gratuitous ARPs on eth0 for192.168.1.76
[*]Aug920:21:10 likun1 Keepalived_vrrp: VRRP_Instance(VI_21)Sending gratuitous ARPs on eth0 for192.168.1.76

20机器再次启动mysql,keepalived会切换为backup状态,但不会抢vip


[*]Aug823:49:05 likun Keepalived_vrrp: VRRP_Script(vs_mysql_20) succeeded
[*]Aug823:49:08 likun Keepalived_vrrp: VRRP_Instance(VI_20)Entering BACKUP STATE




keepalived双主带一从的配置方式
20 <-->21(双主)
|
22(从库)

  
页: [1]
查看完整版本: 14、高可用keepalived搭建及切换