如何给keepalived配置vip(虚拟ip)

首先为了测试机器,准备两台服务器:

  • host1 172.28.23.73
  • host2 172.28.23.96
  • 虚拟ip 172.28.23.100

第一步:在线安装keepalived中间件

sudo yum install -y keepalived

有时候需要先卸载再安装

sudo yum -y remove keepalived

第二步:配置 keepalived

(1)host1 上 keepalived 配置

$ vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 101
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.28.23.100
    }
}

简单的解释下参数

  • eth0是网卡,有些是eth33,可以通过ifconfig命令进行查看;
  • priority 是用来标记主从服务器的,priority 101表示主服务器,priority 100表示从服务器;
  • virtual_ipaddress表示虚拟ip的地址

(2)host2 上 keepalived 配置

$ vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.28.23.100
    }
}

这两个配置文件差别就在priority 101priority 100,分别表示主从关系。

第三步: 启动 keepalived 服务

systemctl start keepalived.service
systemctl enable keepalived.service

第四步:查看IP地址信息

  • host1上 IP 地址信息
ip addr show eth0

得到输出:

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether fa:16:3e:ab:d2:19 brd ff:ff:ff:ff:ff:ff
    inet 172.28.23.73/24 brd 172.28.23.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet 172.28.23.100/32 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 2001:fecc:0:623::9/64 scope global 
       valid_lft forever preferred_lft forever
    inet6 fe80::f816:3eff:feab:d219/64 scope link 
       valid_lft forever preferred_lft forever

其中可以看到 172.28.23.100/32 scope global eth0,说明现在host1是作为虚拟IP的master来运行的。

  • host2 上 IP 地址信息
$ ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether fa:16:3e:95:87:6b brd ff:ff:ff:ff:ff:ff
    inet 172.28.23.96/24 brd 172.28.23.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 2001:fecc:0:623::a/64 scope global 
       valid_lft forever preferred_lft forever
    inet6 fe80::f816:3eff:fe95:876b/64 scope link 
       valid_lft forever preferred_lft forever

此时host2上ip地址信息中不包含虚拟IP “172.28.23.100” 信息。
第五步:验证 IP 地址 Failover

  • 现在手动停止host1上的 keepalived 服务
systemctl start keepalived.service

重新查询host1的ip信息

ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether fa:16:3e:ab:d2:19 brd ff:ff:ff:ff:ff:ff
    inet 172.28.23.73/24 brd 172.28.23.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 2001:fecc:0:623::9/64 scope global 
       valid_lft forever preferred_lft forever
    inet6 fe80::f816:3eff:feab:d219/64 scope link 
       valid_lft forever preferred_lft forever

此时host1上ip地址信息中不包含虚拟IP “172.28.23.100” 信息。

接着在host2上面先停止在开启keepalived服务

systemctl stop keepalived.service
systemctl start keepalived.service
  • 再次查询host2的IP的信息
# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether fa:16:3e:95:87:6b brd ff:ff:ff:ff:ff:ff
    inet 172.28.23.96/24 brd 172.28.23.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet 172.28.23.100/32 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 2001:fecc:0:623::a/64 scope global 
       valid_lft forever preferred_lft forever
    inet6 fe80::f816:3eff:fe95:876b/64 scope link 
       valid_lft forever preferred_lft forever

现在可以看到 host2 上 ip 地址信息中已经包含虚拟IP “172.28.23.100” 信息了。
此时如果再把 host1 上的 keepalived 服务启动,会发现虚拟IP “172.28.23.100” 又重新绑定到 host1 上了。

如何添加ipv6地址类型为虚拟IP(vip)

  • 思路是一样的,在确认好服务器支撑ipv6的情况下,将vip换成ipv6格式的地址就行,若是服务器不支持ipv6,则可以根据教程来添加ipv6支持!
  • 如将172.28.23.100改成2001:fecc:0:622::a/64
首先:重新修改houst1上的keepalived.conf文件
vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 101
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        2001:fecc:0:622::a/64 #ipv6地址
    }
}
  • 重启keepalived服务
systemctl restart keepalived.service
  • 再次查看IP信息
ip addr show eth0

得到以下输出:

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether fa:16:3e:ab:d2:19 brd ff:ff:ff:ff:ff:ff
    inet 172.28.23.73/24 brd 172.28.23.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 2001:fecc:0:622::a/64 scope global nodad  #虚拟的ipv6地址
       valid_lft forever preferred_lft forever
    inet6 2001:fecc:0:623::9/64 scope global 
       valid_lft forever preferred_lft forever
    inet6 fe80::f816:3eff:feab:d219/64 scope link 
       valid_lft forever preferred_lft forever

其他操作和之前添加vip是一样的。

你可能感兴趣的:(keepalived,虚拟IP,运维,运维)