Linux下keepalived配置

一、环境
主库(node01):192.168.12.35 端口:3306
从库(node02):192.168.12.36 端口:3306
vip:192.168.12.38

主库,主从数据库服务器已搭建MySQL主从。

2、配置keepalived实现MySQL主从高可用
keepalived配置如下:
node01:
! Configuration File for keepalived

global_defs {
   router_id node01
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_instance VI_1 {
    state MASTER
    interface ens32
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.12.38/24
    }
}

virtual_server 192.168.12.38 3306 {
    delay_loop 6
    lb_algo rr
    lb_kind NAT
    persistence_timeout 50
    protocol TCP

    real_server 192.168.12.35 3306 {
        weight 1
                notify_down /root/kill_keepalived.sh
        TCP_CHECK {
            connect_timeout 3
            connect_port 3306
        }
    }

    real_server 192.168.12.36 3306 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            connect_port 3306
        }
    }
}

脚本/root/kill_keepalived.sh内容如下:
#!/bin/bash
systemctl stop keepalived

node02:
! Configuration File for keepalived

global_defs {
   router_id node02
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens32
    virtual_router_id 51
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.12.38/24
    }
}

virtual_server 192.168.12.38 3306 {
    delay_loop 6
    lb_algo rr
    lb_kind NAT
    persistence_timeout 50
    protocol TCP

    real_server 192.168.12.35 3306 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            connect_port 3306
        }
    }

    real_server 192.168.12.36 3306 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            connect_port 3306
        }
    }
}

启动主从数据库服和keepalived服务,查看IP

[root@node01 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:50:56:30:cc:38 brd ff:ff:ff:ff:ff:ff
    inet 192.168.12.35/24 brd 192.168.12.255 scope global noprefixroute ens32
       valid_lft forever preferred_lft forever
    inet 192.168.12.38/24 scope global secondary ens32
       valid_lft forever preferred_lft forever
    inet6 fe80::df6c:9284:28fd:7480/64 scope link noprefixroute
       valid_lft forever preferred_lft forever

[root@node02 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:50:56:25:89:7e brd ff:ff:ff:ff:ff:ff
    inet 192.168.12.36/24 brd 192.168.12.255 scope global noprefixroute ens32
       valid_lft forever preferred_lft forever
    inet6 fe80::a547:b48:6f4c:6d64/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
    inet6 fe80::df6c:9284:28fd:7480/64 scope link tentative noprefixroute dadfailed
       valid_lft forever preferred_lft forever

3、vip切换测试
node01:
[root@node01 ~]# /etc/init.d/mysqld stop
Shutting down MySQL.. SUCCESS!
[root@node01 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:50:56:30:cc:38 brd ff:ff:ff:ff:ff:ff
    inet 192.168.12.35/24 brd 192.168.12.255 scope global noprefixroute ens32
       valid_lft forever preferred_lft forever
    inet6 fe80::df6c:9284:28fd:7480/64 scope link noprefixroute
       valid_lft forever preferred_lft forever

node02:
[root@node02 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:50:56:25:89:7e brd ff:ff:ff:ff:ff:ff
    inet 192.168.12.36/24 brd 192.168.12.255 scope global noprefixroute ens32
       valid_lft forever preferred_lft forever
    inet 192.168.12.38/24 scope global secondary ens32
       valid_lft forever preferred_lft forever
    inet6 fe80::a547:b48:6f4c:6d64/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
    inet6 fe80::df6c:9284:28fd:7480/64 scope link tentative noprefixroute dadfailed
       valid_lft forever preferred_lft forever

vip:192.168.12.38漂移到node02节点。

重新把node01数据库和keepalived服务启动:
[root@node01 ~]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!
[root@node01 ~]# systemctl start keepalived
[root@node01 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:50:56:30:cc:38 brd ff:ff:ff:ff:ff:ff
    inet 192.168.12.35/24 brd 192.168.12.255 scope global noprefixroute ens32
       valid_lft forever preferred_lft forever
    inet6 fe80::df6c:9284:28fd:7480/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
[root@node01 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:50:56:30:cc:38 brd ff:ff:ff:ff:ff:ff
    inet 192.168.12.35/24 brd 192.168.12.255 scope global noprefixroute ens32
       valid_lft forever preferred_lft forever
    inet 192.168.12.38/24 scope global secondary ens32
       valid_lft forever preferred_lft forever
    inet6 fe80::df6c:9284:28fd:7480/64 scope link noprefixroute
       valid_lft forever preferred_lft forever

再次查看node02节点IP:
[root@node02 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:50:56:25:89:7e brd ff:ff:ff:ff:ff:ff
    inet 192.168.12.36/24 brd 192.168.12.255 scope global noprefixroute ens32
       valid_lft forever preferred_lft forever
    inet6 fe80::a547:b48:6f4c:6d64/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
    inet6 fe80::df6c:9284:28fd:7480/64 scope link tentative noprefixroute dadfailed
       valid_lft forever preferred_lft forever

可以正常切换了

  

posted @ 2020-08-21 10:47  orcl  阅读(367)  评论(0编辑  收藏  举报