Linux下keepalived配置

1、背景
节点1:192.168.12.35
节点2:192.168.12.36

2、keepalived安装
使用yum仓库安装keepalived
[root@node01 ~]# yum install -y keepalived
[root@node02 ~]# yum install -y keepalived

安装完成后会在/etc文件夹路径下生成keepalived的配置文件
/etc/keepalived/keepalived.conf

3、修改配置文件
节点一:
[root@node01 ~]# vi /etc/keepalived/keepalived.conf
! 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@node02 ~]# vi /etc/keepalived/keepalived.conf
! 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
        }
    }
}


4、开启keepalived自启动
[root@node01 ~]# systemctl enable keepalived
[root@node02 ~]# systemctl enable keepalived

节点一执行脚本/root/kill_keepalived.sh
[root@node01 ~]# cat /root/kill_keepalived.sh
#!/bin/bash
systemctl stop keepalived

  

posted @ 2020-08-30 21:04  orcl  阅读(194)  评论(0编辑  收藏  举报