0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

VRRP - keepalived

0
Posted at

VRRPを試す

Linuxのkeepalivedを用いて、ネットワーク上の機器を冗長化するVRRP(Virtual Router Redundancy Protocol)を、GNS3上で確認してみる。

ネットワーク

topology.png

  • 右端の2台のUbuntuServerが冗長化対象
    • 仮想IPアドレス:192.168.10.1
    • 優先度が高いIPアドレス(上、UbuntuServer):192.168.10.101
    • 優先度が低いIPアドレス(下、UbuntuServer2nd):192.168.10.102
  • L2SW
    • 冗長化対象UbuntuServerとRouterとに接続
    • 検証時、インターフェースe0/0の「shut」や「no shut」を実施
  • Router
    • NAT
  • PC
    • 検証時、仮想IPアドレスへのping実行

VRRP設定

Linuxでは、keepalivedを用いる。まずは、インストール。

$ apt install keepalived

以降、設定を記載するが、トライ&エラーした関連で、コメント行が存在する。下記は、本環境で、最もうまく動作した設定である。

優先度が高い機器
/etc/keepalived/keepalived.conf
global_defs {
    router_id 1st
}

vrrp_instance VI_1 {
    #state MASTER
    state BACKUP
    interface enp0s3
    virtual_router_id 51
    priority 200
    advert_int 1
    #use_vmac
    nopreempt

    #garp_master_delay 1
    #garp_master_repeat 5

    authentication {
        auth_type PASS
        auth_pass Himitsu
    }

    virtual_ipaddress {
        192.168.10.1/24
    }
}
優先度が低い機器
/etc/keepalived/keepalived.conf
global_defs {
    router_id 2nd
}

vrrp_instance VI_1 {
    state BACKUP
    interface enp0s3
    virtual_router_id 51
    priority 100
    advert_int 1
    #use_vmac
    nopreempt

    #garp_master_delay 1
    #garp_master_repeat 5

    authentication {
        auth_type PASS
        auth_pass Himitsu
    }

    virtual_ipaddress {
        192.168.10.1/24
    }
}

主な点を下記。

  • 仮想IPアドレス:192.168.10.1
  • 仮想ルーターID:51
  • 優先度:高200、低100
  • nopreempt:一度優先権を握ったら渡さない(実はうまく動作しているのかよくわからず)
  • 優先度にかかわらず、「state BACKUP」を用いる
  • use_vmac:仮想Macアドレス利用(今回は利用せず)

検証

PCから仮想IPアドレス(192.168.10.1)へのpingを実行し、途中で、L2SWのインターフェースを「shut(使用不可)」および「no shut(復帰、使用可)」を実施する。下記はコマンドの一例である。

L2SW(config)#interface ethernet 0/0
L2SW(config-if)#shut
L2SW(config-if)#
*Jun 20 05:47:53.001: %LINK-5-CHANGED: Interface Ethernet0/0, changed state to administratively down
*Jun 20 05:47:54.004: %LINEPROTO-5-UPDOWN: Line protocol on Interface Ethernet0/0, changed state to down
L2SW(config-if)#
L2SW(config-if)#no shut
L2SW(config-if)#
*Jun 20 05:48:33.676: %LINK-3-UPDOWN: Interface Ethernet0/0, changed state to up
*Jun 20 05:48:34.684: %LINEPROTO-5-UPDOWN: Line protocol on Interface Ethernet0/0, changed state to up
L2SW(config-if)#

ping

上記keepalived.confのケース

ping結果にコメント追記。

PC1> ping 192.168.10.1 -c 1000
84 bytes from 192.168.10.1 icmp_seq=1 ttl=63 time=15.766 ms
84 bytes from 192.168.10.1 icmp_seq=2 ttl=63 time=21.535 ms

84 bytes from 192.168.10.1 icmp_seq=40 ttl=63 time=17.821 ms
84 bytes from 192.168.10.1 icmp_seq=41 ttl=63 time=14.343 ms
192.168.10.1 icmp_seq=42 timeout    # この直前に「shut」実行
192.168.10.1 icmp_seq=43 timeout
84 bytes from 192.168.10.1 icmp_seq=44 ttl=63 time=25.798 ms
84 bytes from 192.168.10.1 icmp_seq=45 ttl=63 time=20.223 ms

84 bytes from 192.168.10.1 icmp_seq=78 ttl=63 time=18.216 ms
84 bytes from 192.168.10.1 icmp_seq=79 ttl=63 time=18.809 ms
84 bytes from 192.168.10.1 icmp_seq=80 ttl=63 time=11.775 ms    # このあたりで「no shut」実行
84 bytes from 192.168.10.1 icmp_seq=81 ttl=63 time=21.818 ms
84 bytes from 192.168.10.1 icmp_seq=82 ttl=63 time=18.566 ms

84 bytes from 192.168.10.1 icmp_seq=138 ttl=63 time=22.508 ms
84 bytes from 192.168.10.1 icmp_seq=139 ttl=63 time=19.368 ms
84 bytes from 192.168.10.1 icmp_seq=140 ttl=63 time=16.011 ms
84 bytes from 192.168.10.1 icmp_seq=141 ttl=63 time=16.582 ms
84 bytes from 192.168.10.1 icmp_seq=142 ttl=63 time=16.638 ms
84 bytes from 192.168.10.1 icmp_seq=143 ttl=63 time=20.226 ms

「no shut」による切り戻し後、一度も、pingが途絶えることがなかった。

上記keepalived.confの以外のケース

例えば、「use_vmac」利用時、「nopreempt」未使用時のケースである。

PC1> ping 192.168.10.1 -c 1000
84 bytes from 192.168.10.1 icmp_seq=1 ttl=63 time=20.524 ms
84 bytes from 192.168.10.1 icmp_seq=2 ttl=63 time=16.368 ms

84 bytes from 192.168.10.1 icmp_seq=19 ttl=63 time=15.185 ms
84 bytes from 192.168.10.1 icmp_seq=20 ttl=63 time=13.892 ms
192.168.10.1 icmp_seq=21 timeout    # この直前に「shut」実行
192.168.10.1 icmp_seq=22 timeout
84 bytes from 192.168.10.1 icmp_seq=23 ttl=63 time=33.933 ms
84 bytes from 192.168.10.1 icmp_seq=24 ttl=63 time=12.872 ms

84 bytes from 192.168.10.1 icmp_seq=29 ttl=63 time=13.632 ms
84 bytes from 192.168.10.1 icmp_seq=30 ttl=63 time=18.181 ms    # このあたりで「no shut」実行
84 bytes from 192.168.10.1 icmp_seq=31 ttl=63 time=15.630 ms
84 bytes from 192.168.10.1 icmp_seq=32 ttl=63 time=17.107 ms
84 bytes from 192.168.10.1 icmp_seq=33 ttl=63 time=20.502 ms
84 bytes from 192.168.10.1 icmp_seq=34 ttl=63 time=20.049 ms

84 bytes from 192.168.10.1 icmp_seq=59 ttl=63 time=12.691 ms
84 bytes from 192.168.10.1 icmp_seq=60 ttl=63 time=17.569 ms
84 bytes from 192.168.10.1 icmp_seq=61 ttl=63 time=19.055 ms
84 bytes from 192.168.10.1 icmp_seq=62 ttl=63 time=18.318 ms
192.168.10.1 icmp_seq=63 timeout    # このあたりで、切り戻しが発生
192.168.10.1 icmp_seq=64 timeout
192.168.10.1 icmp_seq=65 timeout
192.168.10.1 icmp_seq=66 timeout

192.168.10.1 icmp_seq=173 timeout
192.168.10.1 icmp_seq=174 timeout
192.168.10.1 icmp_seq=175 timeout
84 bytes from 192.168.10.1 icmp_seq=176 ttl=63 time=21.290 ms    # 切り戻しからの復帰
84 bytes from 192.168.10.1 icmp_seq=177 ttl=63 time=17.911 ms
84 bytes from 192.168.10.1 icmp_seq=178 ttl=63 time=16.608 ms

切り戻しから復帰に100秒以上もかかった。仮想Macアドレスを利用することで、効果があると思われたが、実際はそうでもなかった。この辺りはひとつ前のルーティング機器のふるまいにも依存しそうである。

IPアドレス

正常時

優先度が高い機器
$ ip addr

2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:c3:0f:7a brd ff:ff:ff:ff:ff:ff
    inet 192.168.10.101/24 brd 192.168.10.255 scope global enp0s3
       valid_lft forever preferred_lft forever
    inet 192.168.10.1/24 scope global secondary enp0s3
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fec3:f7a/64 scope link 
       valid_lft forever preferred_lft forever

自身のIPアドレス(192.168.10.101)と仮想IPアドレス(192.168.10.1)とが見える。

優先度が低い機器
$ ip addr

2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:54:f1:00 brd ff:ff:ff:ff:ff:ff
    inet 192.168.10.102/24 brd 192.168.10.255 scope global enp0s3
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe54:f100/64 scope link 
       valid_lft forever preferred_lft forever

自身のIPアドレス(192.168.10.102)のみが見える。

L2SWのe0/0が「shut」時

優先度が高い機器

こちらは、低い機器の状況がかかわらず、先ほどと同じ。

$ ip addr

2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:c3:0f:7a brd ff:ff:ff:ff:ff:ff
    inet 192.168.10.101/24 brd 192.168.10.255 scope global enp0s3
       valid_lft forever preferred_lft forever
    inet 192.168.10.1/24 scope global secondary enp0s3
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fec3:f7a/64 scope link 
       valid_lft forever preferred_lft forever
優先度が低い機器
$ ip addr

2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:54:f1:00 brd ff:ff:ff:ff:ff:ff
    inet 192.168.10.102/24 brd 192.168.10.255 scope global enp0s3
       valid_lft forever preferred_lft forever
    inet 192.168.10.1/24 scope global secondary enp0s3
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe54:f100/64 scope link 
       valid_lft forever preferred_lft forever

優先度が高い機器との疎通がとれないため、仮想IPアドレス(192.168.10.1)を取得して、それと自身のIPアドレス(192.168.10.102)とが見える。

keepalivedログ

優先度が高い機器
$ sudo journalctl -u keepalived

Jun 20 05:58:29 nyon Keepalived[706]: Starting Keepalived v2.2.8 (04/04,2023), git commit v2.2.7-154-g292b299e+
Jun 20 05:58:29 nyon Keepalived[706]: Running on Linux 6.8.0-87-generic #88-Ubuntu SMP PREEMPT_DYNAMIC Sat Oct 11 09:28:41 UTC 2025 (built for Linux 6.8.0)
Jun 20 05:58:29 nyon Keepalived[706]: Command line: '/usr/sbin/keepalived' '--dont-fork'
Jun 20 05:58:29 nyon Keepalived[706]: Configuration file /etc/keepalived/keepalived.conf
Jun 20 05:58:29 nyon Keepalived[706]: NOTICE: setting config option max_auto_priority should result in better keepalived performance
Jun 20 05:58:29 nyon Keepalived[706]: Starting VRRP child process, pid=743
Jun 20 05:58:29 nyon Keepalived[706]: Startup complete
Jun 20 05:58:29 nyon Keepalived_vrrp[743]: (VI_1) Entering BACKUP STATE (init)
Jun 20 05:58:29 nyon systemd[1]: Started keepalived.service - Keepalive Daemon (LVS and VRRP).
Jun 20 05:58:32 nyon Keepalived_vrrp[743]: (VI_1) Entering MASTER STATE

Jun 20 06:05:48 nyon Keepalived_vrrp[743]: (VI_1) Received advert from 192.168.10.102 with lower priority 100, ours 200, forcing new election

起動当初はBACKUP、すぐにMASTERとなる。不具合時はVRRPパケットを受信しないので、MASTERのままである。また、低い機器からのVRRPパケットを受信しても、そのままMASTERである。

優先度が低い機器
$ sudo journalctl -u keepalived

Jun 20 05:58:42 nyon systemd[1]: Starting keepalived.service - Keepalive Daemon (LVS and VRRP)...
Jun 20 05:58:43 nyon Keepalived[718]: Starting Keepalived v2.2.8 (04/04,2023), git commit v2.2.7-154-g292b299e+
Jun 20 05:58:43 nyon Keepalived[718]: Running on Linux 6.8.0-87-generic #88-Ubuntu SMP PREEMPT_DYNAMIC Sat Oct 11 09:28:41 UTC 2025 (built for Linux 6.8.0)
Jun 20 05:58:43 nyon Keepalived[718]: Command line: '/usr/sbin/keepalived' '--dont-fork'
Jun 20 05:58:43 nyon Keepalived[718]: Configuration file /etc/keepalived/keepalived.conf
Jun 20 05:58:43 nyon Keepalived[718]: NOTICE: setting config option max_auto_priority should result in better keepalived performance
Jun 20 05:58:43 nyon Keepalived[718]: Starting VRRP child process, pid=753
Jun 20 05:58:43 nyon Keepalived[718]: Startup complete
Jun 20 05:58:43 nyon Keepalived_vrrp[753]: (VI_1) Entering BACKUP STATE (init)
Jun 20 05:58:43 nyon systemd[1]: Started keepalived.service - Keepalive Daemon (LVS and VRRP).

Jun 20 06:03:23 nyon Keepalived_vrrp[753]: (VI_1) Entering MASTER STATE
Jun 20 06:03:25 nyon Keepalived_vrrp[753]: (VI_1) Master received advert from 192.168.10.101 with higher priority 200, ours 100
Jun 20 06:03:25 nyon Keepalived_vrrp[753]: (VI_1) Entering BACKUP STATE

Jun 20 06:04:37 nyon Keepalived_vrrp[753]: (VI_1) Entering MASTER STATE

Jun 20 06:05:47 nyon Keepalived_vrrp[753]: (VI_1) Master received advert from 192.168.10.101 with higher priority 200, ours 100
Jun 20 06:05:47 nyon Keepalived_vrrp[753]: (VI_1) Entering BACKUP STATE

起動当初はBACKUP、すぐにMASTERとなるが、優先度が高い機器からのVRRPパケットを受信し、すぐにBACKUPに戻っている。その後、高い機器からのVRRPパケットを受信しなくなると、MASTERとなり、再度、高い機器からのVRRPパケットを受信し、BACKUPに戻っている。

ネットワークパケット

L2SWと優先度が低い機器(UbuntuServer2nd)との間で取得したパケットを紹介。

全体の流れ 

shut(不具合発生)時(切り替え)

101to102.png

優先度が低い機器(192.168.10.102)では、通常、VRRPパケットを出していないが、高い機器(192.168.10.101)からのVRRPパケットを受信しなくなると、仮想IPアドレス(192.168.10.1)をARPで宣言し、VRRPパケットを送出していることがわかる。

なお、ここでは、VRRPがマルチキャストパケットとなっているが、ユニキャストとすることもできるらしい。

no shut(不具合解消)時(切り戻し)

102to101.png

優先度が低い機器(192.168.10.102)では、高い機器(192.168.10.101)からのVRRPおよび仮想IPアドレス(192.168.10.1)ARP宣言を受信すると、VRRPパケット送出を止める。

VRRPパケット

優先度が高い機器

1stVRRP.png

keepalived.confの設定にそった内容である。優先度200が見える。

優先度が低い機器

2ndVRRP.png

こちらも、keepalived.confの設定にそった内容。優先度100が見える。

終わりに

一つ手前のL3機器(ここでは、Router)のARPテーブルやMacアドレスのハンドリング次第で、結果は異なる可能性がある。不具合発生時の切り替えよりも、不具合解消時の切り戻しがポイントと感じた。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?