keepalived双主虚拟路由配置

我使用了两台虚拟机做测试

系统centos7.3

主机A:172.16.1.123

主机B:172.16.1.124

其实和普通配置keepalived差不多,就是复制多了一个vrrp_instance,并将部分参数做适当修改

安装keepalived

yum install keepalived httpd -y

两台主机都需要修改系统参数,使其允许绑定ip,在/etc/sysctl.conf中添加以下内容

net.ipv4.ip_nonlocal_bind = 1

然后执行sysctl -p 生效

接着修改keepalived配置文件,以下是主机A的配置

[root@controller logs]# cat /etc/keepalived/keepalived.conf
global_defs {
    router_id NodeA
}
vrrp_instance VI_1 {
    state MASTER   #一台设置为MASTER一台设置为BACKUP
    interface eth0  #绑定的网卡
    virtual_router_id 66  #随便设置一个id,但是注意,不能与局域网中的其他id冲突
    priority 100  #优先级
    advert_int 1  #周期
    authentication {  #认证类型
    auth_type PASS  #认证方式
    auth_pass 1111  #认证密码
        }
virtual_ipaddress {
    172.16.1.129/24  #vip
    }
}
vrrp_instance VI_2 {
        state MASTER
        interface eth0
        virtual_router_id 67 #这个id也需要与上面的不同
        priority 80
        advert_int 1
        authentication {
        auth_type PASS
        auth_pass 1111
    }
virtual_ipaddress {
        172.16.1.130/24 dev eth0
    }
}
            

主机B的配置文件如下

[root@compute1 ~]# cat /etc/keepalived/keepalived.conf
global_defs {
    router_id NodeB
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 66  #与上面的66一致
    priority 99  #因为是BACKUP,所以我们设置得比前面的小
    advert_int 1
    authentication    {
    auth_type PASS
    auth_pass 1111
}
virtual_ipaddress {
    172.16.1.129/24 dev eth0
}
}
vrrp_instance VI_2 {
        state BACKUP
        interface eth0
        virtual_router_id 67
        priority 79
        advert_int 1
        authentication  {
        auth_type PASS
        auth_pass 1111
}
virtual_ipaddress {
        172.16.1.130/24 dev eth0
}
}

启动keepalived

systemctl start keepalived httpd

接着我们设置主机A和主机B的http的web首页分别为this is 123和this is 124

[root@controller logs]# curl 172.16.1.129
this is 123
[root@controller logs]# systemctl stop keepalived
[root@controller logs]# curl 172.16.1.129
this is 124
[root@controller logs]# systemctl start keepalived
[root@controller logs]# curl 172.16.1.129
this is 123
[root@controller logs]# cat /var/www/html/index.html 
this is 123
[root@controller logs]#

可以发现ip会随着keepalived的变动而变动

posted on 2019-05-14 15:06  标配的小号  阅读(489)  评论(0编辑  收藏  举报

导航