zoukankan      html  css  js  c++  java
  • 14、高可用keepalived搭建及切换

    keepalived主从切换试验:
    1、先搭建192.168.1.20与192.168.1.21的主主架构     192.168.1.76为VIP
    2、在2机都安装keepalived
    1.  tar -zxvf keepalived-1.2.13.tar.gz 
    2.  cd keepalived-1.2.13
    3.  ./configure -prefix=/usr/local/keepalived
    4.  make
    5. make install
    6.  [root@likun keepalived-1.2.13]# cat /opt/mysql/mysql.sh 
    7. #!/bin/sh
    8. pkill keepalived
    9. [root@likun keepalived-1.2.13]# chmod +x /opt/mysql/mysql.sh
    3、配置20机器的keepalived配置文件
    1. [root@likun keepalived-1.2.13]# cat /usr/local/keepalived/etc/keepalived.conf
    2. !ConfigurationFilefor keepalived 
    3.  
    4. global_defs { 
    5.    notification_email { 
    6.      likun@163.com 
    7.    } 
    8.    router_id mysql-ha 
    9. } 
    10.  
    11. vrrp_instance VI_20 { 
    12.     state BACKUP
    13.     interface eth0 
    14.     virtual_router_id 20
    15.     priority 90
    16.     advert_int 1 
    17.     nopreempt        
    18.     authentication { 
    19.         auth_type PASS 
    20.         auth_pass 1111  
    21.     } 
    22.     virtual_ipaddress {   
    23.         192.168.1.76
    24.     } 
    25. } 
    26.  
    27. virtual_server 192.168.1.763306{ 
    28.     delay_loop 2 
    29.     lb_algo wrr 
    30.     lb_kind DR 
    31.     persistence_timeout 60 
    32.     protocol TCP 
    33.  
    34.     real_server 192.168.1.203306{
    35.         weight 3 
    36.         notify_down /opt/mysql/mysql.sh 
    37.             TCP_CHECK { 
    38.             connect_timeout 10 
    39.             nb_get_retry 3 
    40.             delay_before_retry 3 
    41.             connect_port 3306
    42.         } 
    43.     } 
    44. } 
    4、配置21机器的keepalived配置文件
    1. !ConfigurationFilefor keepalived 
    2.  
    3. global_defs { 
    4.    notification_email { 
    5.      likun@163.com 
    6.    } 
    7.    router_id mysql-ha 
    8. } 
    9.  
    10. vrrp_instance VI_21 { 
    11.     state BACKUP
    12.     interface eth0 
    13.     virtual_router_id 20        --注意2边要一致
    14.     priority 100
    15.     advert_int 1 
    16.     nopreempt        
    17.     authentication { 
    18.         auth_type PASS 
    19.         auth_pass 1111  
    20.     } 
    21.     virtual_ipaddress {   
    22.         192.168.1.76
    23.     } 
    24. } 
    25.  
    26. virtual_server 192.168.1.763306{ 
    27.     delay_loop 2 
    28.     lb_algo wrr 
    29.     lb_kind DR 
    30.     persistence_timeout 60 
    31.     protocol TCP 
    32.  
    33.     real_server 192.168.1.213306{
    34.         weight 3 
    35.         notify_down /opt/mysql/mysql.sh 
    36.             TCP_CHECK { 
    37.             connect_timeout 10 
    38.             nb_get_retry 3 
    39.             delay_before_retry 3 
    40.             connect_port 3306
    41.         } 
    42.     } 
    43. } 
    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登陆数据库
    1. [root@likun1 keepalived-1.2.13]# mysql -h192.168.1.76-P3306 -uroot -proot
    2. Welcome to the MySQL monitor.  Commands end with ; or g.
    3. YourMySQL connection id is 682
    4. Server version:5.5.30-log Source distribution
    5. Copyright(c)2000,2013,Oracle and/or its affiliates.All rights reserved.
    6. Oracle is a registered trademark of OracleCorporation and/or its
    7. affiliates.Other names may be trademarks of their respective
    8. owners.
    9. Type'help;' or 'h'for help.Type'c' to clear the current input statement.
    10. mysql> 
     
    21机器查看IP:
    1. [root@likun1 keepalived-1.2.13]# ip addr show
    2. 1: lo:<LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    3.     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    4.     inet 127.0.0.1/8 scope host lo
    5.     inet6 ::1/128 scope host 
    6.        valid_lft forever preferred_lft forever
    7. 2: eth0:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    8.     link/ether 08:00:27:5d:ef:c3 brd ff:ff:ff:ff:ff:ff
    9.     inet 192.168.1.21/24 brd 192.168.1.255 scope global eth0
    10.     inet 192.168.1.76/32 scope global eth0
    11.     inet6 fe80::a00:27ff:fe5d:efc3/64 scope link 
    12.        valid_lft forever preferred_lft forever
     
    20机器查看IP:
    1. [root@likun keepalived-1.2.13]# ip addr show
    2. 1: lo:<LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    3.     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    4.     inet 127.0.0.1/8 scope host lo
    5.     inet6 ::1/128 scope host 
    6.        valid_lft forever preferred_lft forever
    7. 2: eth0:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    8.     link/ether 08:00:27:00:58:57 brd ff:ff:ff:ff:ff:ff
    9.     inet 192.168.1.20/24 brd 192.168.1.255 scope global eth0
    10.     inet6 fe80::a00:27ff:fe00:5857/64 scope link 
    11.        valid_lft forever preferred_lft forever
    关闭20节点的mysql,发现VIP漂移到21上,并且20上的keepalived被kill
     
    • 杀掉master的keepalived,vip会飘到slave上,slave的日志:
    1. Jul2020:03:31 likun Keepalived_vrrp[8547]: VRRP_Instance(VI_20)Transition to MASTER STATE
    2. Jul2020:03:32 likun Keepalived_vrrp[8547]: VRRP_Instance(VI_20)Entering MASTER STATE
    3. Jul2020:03:32 likun Keepalived_vrrp[8547]: VRRP_Instance(VI_20) setting protocol VIPs.
    4. Jul2020:03:32 likun Keepalived_vrrp[8547]: VRRP_Instance(VI_20)Sending gratuitous ARPs on eth0 for192.168.1.76
    5. Jul2020:03:32 likun Keepalived_healthcheckers[8546]:Netlink reflector reports IP 192.168.1.76 added
    • 再启动master的keepalived,不会抢vip,变为backup state,master的日志:
    1. Jul2020:03:49 likun1 Keepalived[7416]:StartingKeepalived v1.2.13(07/20,2014)
    2. Jul2020:03:49 likun1 Keepalived[7417]:StartingHealthcheck child process, pid=7418
    3. Jul2020:03:49 likun1 Keepalived[7417]:Starting VRRP child process, pid=7419
    4. Jul2020:03:49 likun1 Keepalived_vrrp[7419]:Netlink reflector reports IP 192.168.1.21 added
    5. Jul2020:03:49 likun1 Keepalived_healthcheckers[7418]:Netlink reflector reports IP 192.168.1.21 added
    6. Jul2020:03:49 likun1 Keepalived_healthcheckers[7418]:Netlink reflector reports IP fe80::a00:27ff:fe5d:efc3 added
    7. Jul2020:03:49 likun1 Keepalived_healthcheckers[7418]:RegisteringKernel netlink reflector
    8. Jul2020:03:49 likun1 Keepalived_healthcheckers[7418]:RegisteringKernel netlink command channel
    9. Jul2020:03:49 likun1 Keepalived_vrrp[7419]:Netlink reflector reports IP fe80::a00:27ff:fe5d:efc3 added
    10. Jul2020:03:49 likun1 Keepalived_vrrp[7419]:RegisteringKernel netlink reflector
    11. Jul2020:03:49 likun1 Keepalived_vrrp[7419]:RegisteringKernel netlink command channel
    12. Jul2020:03:49 likun1 Keepalived_vrrp[7419]:Registering gratuitous ARP shared channel
    13. Jul2020:03:49 likun1 Keepalived_vrrp[7419]:Opening file '/usr/local/keepalived/etc/keepalived.conf'.
    14. Jul2020:03:49 likun1 Keepalived_vrrp[7419]:Configuration is using:62953Bytes
    15. Jul2020:03:49 likun1 Keepalived_vrrp[7419]:UsingLinkWatch kernel netlink reflector...
    16. Jul2020:03:49 likun1 Keepalived_vrrp[7419]: VRRP_Instance(VI_21)Entering BACKUP STATE
    17. Jul2020:03:49 likun1 Keepalived_vrrp[7419]: VRRP sockpool:[ifindex(2), proto(112), unicast(0), fd(10,11)]
    18. Jul2020:03:49 likun1 Keepalived_healthcheckers[7418]:Opening file '/usr/local/keepalived/etc/keepalived.conf'.
    19. Jul2020:03:49 likun1 Keepalived_healthcheckers[7418]:Configuration is using:11555Bytes
    20. Jul2020:03:49 likun1 Keepalived_healthcheckers[7418]:UsingLinkWatch kernel netlink reflector...
    21. Jul2020:03:49 likun1 Keepalived_healthcheckers[7418]:Activating healthchecker for service [192.168.1.21]:3306
     
     
    切换时不会kill keepalived的方式
    20、21先配置主主架构
    2边数据库做授权:
    root@localhost 19:30:38 [db3]> 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:
    1. [root@likun ~]# cat /usr/local/keepalived/etc/keepalived.conf 
    2. vrrp_script vs_mysql_20 {
    3.     script "/usr/local/keepalived/checkMySQL.py -h 192.168.1.20 -P 3306"
    4.     interval 60 
    5. }
    6. vrrp_instance VI_20 {
    7.     state BACKUP
    8.     nopreempt
    9.     interface eth0
    10.     virtual_router_id 21
    11.     priority 100
    12.     advert_int 5
    13.     authentication {
    14.         auth_type PASS
    15.         auth_pass 1111
    16.     }
    17.     track_script {
    18.        vs_mysql_20
    19.     }
    20.     virtual_ipaddress {
    21.         192.168.1.76
    22.     }
    23. } 
    21:
    1. [root@likun1 ~]# cat /usr/local/keepalived/etc/keepalived.conf 
    2. vrrp_script vs_mysql_21 {
    3.     script "/usr/local/keepalived/checkMySQL.py -h 192.168.1.21 -P 3306"
    4.     interval 60 
    5. }
    6. vrrp_instance VI_21 {
    7.     state BACKUP
    8.     nopreempt
    9.     interface eth0
    10.     virtual_router_id 21
    11.     priority 90
    12.     advert_int 5
    13.     authentication {
    14.         auth_type PASS
    15.         auth_pass 1111
    16.     }
    17.     track_script {
    18.        vs_mysql_21
    19.     }
    20.     virtual_ipaddress {
    21.         192.168.1.76
    22.     }
    23. } 
    启动20的keepalived,并查看日志,发现启动为master状态,vip绑定:
    1. [root@likun ~]#/usr/local/keepalived/sbin/keepalived -f /usr/local/keepalived/etc/keepalived.conf -D
    2. [root@likun ~]# tail -f /var/log/messages
    3. Aug  820:58:41 likun Keepalived[8023]:StoppingKeepalived v1.2.13(07/20,2014)
    4. Aug  820:59:04 likun Keepalived[8064]:StartingKeepalived v1.2.13(07/20,2014)
    5. Aug  820:59:04 likun Keepalived[8065]:StartingHealthcheck child process, pid=8066
    6. Aug  820:59:04 likun Keepalived[8065]:Starting VRRP child process, pid=8067
    7. Aug  820:59:04 likun Keepalived_vrrp[8067]:Netlink reflector reports IP 192.168.1.20 added
    8. Aug  820:59:04 likun Keepalived_vrrp[8067]:Netlink reflector reports IP 10.0.3.15 added
    9. Aug  820:59:04 likun Keepalived_vrrp[8067]:Netlink reflector reports IP fe80::a00:27ff:fe00:5857 added
    10. Aug  820:59:04 likun Keepalived_healthcheckers[8066]:Netlink reflector reports IP 192.168.1.20 added
    11. Aug  820:59:04 likun Keepalived_vrrp[8067]:Netlink reflector reports IP fe80::a00:27ff:fe5d:683a added
    12. Aug  820:59:04 likun Keepalived_vrrp[8067]:RegisteringKernel netlink reflector
    13. Aug  820:59:04 likun Keepalived_vrrp[8067]:RegisteringKernel netlink command channel
    14. Aug  820:59:04 likun Keepalived_vrrp[8067]:Registering gratuitous ARP shared channel
    15. Aug  820:59:04 likun Keepalived_healthcheckers[8066]:Netlink reflector reports IP 10.0.3.15 added
    16. Aug  820:59:04 likun Keepalived_healthcheckers[8066]:Netlink reflector reports IP fe80::a00:27ff:fe00:5857 added
    17. Aug  820:59:04 likun Keepalived_healthcheckers[8066]:Netlink reflector reports IP fe80::a00:27ff:fe5d:683a added
    18. Aug  820:59:04 likun Keepalived_healthcheckers[8066]:RegisteringKernel netlink reflector
    19. Aug  820:59:04 likun Keepalived_healthcheckers[8066]:RegisteringKernel netlink command channel
    20. Aug  820:59:05 likun Keepalived_vrrp[8067]:Opening file '/usr/local/keepalived/etc/keepalived.conf'.
    21. Aug  820:59:05 likun Keepalived_healthcheckers[8066]:Opening file '/usr/local/keepalived/etc/keepalived.conf'.
    22. Aug  820:59:05 likun Keepalived_vrrp[8067]:Configuration is using:62993Bytes
    23. Aug  820:59:05 likun Keepalived_healthcheckers[8066]:Configuration is using:5287Bytes
    24. Aug  820:59:05 likun Keepalived_vrrp[8067]:UsingLinkWatch kernel netlink reflector...
    25. Aug  820:59:05 likun Keepalived_vrrp[8067]: VRRP_Instance(VI_20)Entering BACKUP STATE
    26. Aug  820:59:05 likun Keepalived_vrrp[8067]: VRRP sockpool:[ifindex(2), proto(112), unicast(0), fd(10,11)]
    27. Aug  820:59:05 likun Keepalived_healthcheckers[8066]:UsingLinkWatch kernel netlink reflector...
    28. Aug  820:59:07 likun Keepalived_vrrp[8067]: VRRP_Script(vs_mysql_20) succeeded
    29. Aug  820:59:20 likun Keepalived_vrrp[8067]: VRRP_Instance(VI_20)Transition to MASTER STATE
    30. Aug  820:59:25 likun Keepalived_vrrp[8067]: VRRP_Instance(VI_20)Entering MASTER STATE
    31. Aug  820:59:25 likun Keepalived_vrrp[8067]: VRRP_Instance(VI_20) setting protocol VIPs.
    32. Aug  820:59:25 likun Keepalived_vrrp[8067]: VRRP_Instance(VI_20)Sending gratuitous ARPs on eth0 for192.168.1.76
    33. Aug  820:59:25 likun Keepalived_healthcheckers[8066]:Netlink reflector reports IP 192.168.1.76 added
    34. Aug  820:59:30 likun Keepalived_vrrp[8067]: VRRP_Instance(VI_20)Sending gratuitous ARPs on eth0 for192.168.1.76
    启动21的keepalived,并查看日志,启动为backup状态:
    1. [root@likun1 ~]#/usr/local/keepalived/sbin/keepalived -f /usr/local/keepalived/etc/keepalived.conf -D
    2. [root@likun1 ~]# tail -f /var/log/messages
    3. Aug  918:11:27 likun1 Keepalived[3355]:StartingKeepalived v1.2.13(07/20,2014)
    4. Aug  918:11:27 likun1 Keepalived[3356]:StartingHealthcheck child process, pid=3357
    5. Aug  918:11:27 likun1 Keepalived[3356]:Starting VRRP child process, pid=3358
    6. Aug  918:11:27 likun1 Keepalived_vrrp[3358]:Netlink reflector reports IP 192.168.1.21 added
    7. Aug  918:11:27 likun1 Keepalived_healthcheckers[3357]:Netlink reflector reports IP 192.168.1.21 added
    8. Aug  918:11:27 likun1 Keepalived_healthcheckers[3357]:Netlink reflector reports IP 10.0.3.15 added
    9. Aug  918:11:27 likun1 Keepalived_healthcheckers[3357]:Netlink reflector reports IP fe80::a00:27ff:fe5d:efc3 added
    10. Aug  918:11:27 likun1 Keepalived_vrrp[3358]:Netlink reflector reports IP 10.0.3.15 added
    11. Aug  918:11:27 likun1 Keepalived_healthcheckers[3357]:Netlink reflector reports IP fe80::a00:27ff:fe97:754a added
    12. Aug  918:11:27 likun1 Keepalived_healthcheckers[3357]:RegisteringKernel netlink reflector
    13. Aug  918:11:27 likun1 Keepalived_healthcheckers[3357]:RegisteringKernel netlink command channel
    14. Aug  918:11:27 likun1 Keepalived_vrrp[3358]:Netlink reflector reports IP fe80::a00:27ff:fe5d:efc3 added
    15. Aug  918:11:27 likun1 Keepalived_vrrp[3358]:Netlink reflector reports IP fe80::a00:27ff:fe97:754a added
    16. Aug  918:11:27 likun1 Keepalived_vrrp[3358]:RegisteringKernel netlink reflector
    17. Aug  918:11:27 likun1 Keepalived_vrrp[3358]:RegisteringKernel netlink command channel
    18. Aug  918:11:27 likun1 Keepalived_vrrp[3358]:Registering gratuitous ARP shared channel
    19. Aug  918:11:30 likun1 Keepalived_vrrp[3358]:Opening file '/usr/local/keepalived/etc/keepalived.conf'.
    20. Aug  918:11:30 likun1 Keepalived_healthcheckers[3357]:Opening file '/usr/local/keepalived/etc/keepalived.conf'.
    21. Aug  918:11:30 likun1 Keepalived_vrrp[3358]:Configuration is using:63001Bytes
    22. Aug  918:11:30 likun1 Keepalived_healthcheckers[3357]:Configuration is using:5295Bytes
    23. Aug  918:11:30 likun1 Keepalived_vrrp[3358]:UsingLinkWatch kernel netlink reflector...
    24. Aug  918:11:30 likun1 Keepalived_vrrp[3358]: VRRP_Instance(VI_21)Entering BACKUP STATE
    25. Aug  918:11:30 likun1 Keepalived_vrrp[3358]: VRRP sockpool:[ifindex(2), proto(112), unicast(0), fd(10,11)]
    26. Aug  918:11:30 likun1 Keepalived_healthcheckers[3357]:UsingLinkWatch kernel netlink reflector...
    27. Aug  918:11:31 likun1 Keepalived_vrrp[3358]: VRRP_Script(vs_mysql_21) succeeded
    20机器关闭mysql,看到20 keepalived切换为fault状态,21机器切换为master状态并绑定vip
    1. 20日志:
    2. Aug  823:09:05 likun Keepalived_vrrp[8067]: VRRP_Script(vs_mysql_20) failed
    3. Aug  823:09:08 likun Keepalived_vrrp[8067]: VRRP_Instance(VI_20)Entering FAULT STATE
    4. Aug  823:09:08 likun Keepalived_vrrp[8067]: VRRP_Instance(VI_20) removing protocol VIPs.
    5. Aug  823:09:08 likun Keepalived_vrrp[8067]: VRRP_Instance(VI_20)Now in FAULT state
    6. Aug  823:09:08 likun Keepalived_healthcheckers[8066]:Netlink reflector reports IP 192.168.1.76 removed
    1. 21日志:
    2. Aug  920:21:00 likun1 Keepalived_vrrp[3358]: VRRP_Instance(VI_21)Transition to MASTER STATE
    3. Aug  920:21:05 likun1 Keepalived_vrrp[3358]: VRRP_Instance(VI_21)Entering MASTER STATE
    4. Aug  920:21:05 likun1 Keepalived_vrrp[3358]: VRRP_Instance(VI_21) setting protocol VIPs.
    5. Aug  920:21:05 likun1 Keepalived_healthcheckers[3357]:Netlink reflector reports IP 192.168.1.76 added
    6. Aug  920:21:05 likun1 avahi-daemon[1320]:Registeringnew address record for192.168.1.76 on eth0.IPv4.
    7. Aug  920:21:05 likun1 Keepalived_vrrp[3358]: VRRP_Instance(VI_21)Sending gratuitous ARPs on eth0 for192.168.1.76
    8. Aug  920:21:10 likun1 Keepalived_vrrp[3358]: VRRP_Instance(VI_21)Sending gratuitous ARPs on eth0 for192.168.1.76
    20机器再次启动mysql,keepalived会切换为backup状态,但不会抢vip
    1. Aug  823:49:05 likun Keepalived_vrrp[8067]: VRRP_Script(vs_mysql_20) succeeded
    2. Aug  823:49:08 likun Keepalived_vrrp[8067]: VRRP_Instance(VI_20)Entering BACKUP STATE
     
     
     
    keepalived双主带一从的配置方式
    20 <-->21(双主)
     |
    22(从库)
     
     
  • 相关阅读:
    TCP三次握手和四次挥手
    shell脚本进阶
    shell脚本编程基础
    centos6/7通用查看系统版本
    OSI与TCP/IP网络模型分层
    RAID基础知识总结
    yum的初步了解与使用
    Red Hat Enterprise Linux 官方正式版镜像下载
    MYSQL的基本使用,以及错误代码的意思
    人生第一次研读MFC截图工具的笔记心得
  • 原文地址:https://www.cnblogs.com/kissdb/p/4009621.html
Copyright © 2011-2022 走看看