暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

如何基于radvd模拟IPv6玩转Keepalived实践详解

大数据从业者 2023-12-29
668

背景

毫不夸张地说,IPv6地址数量可以覆盖地球上的每一粒沙子。IPV6优势显著,尤其是支持无状态的自动配置(Stateless Auto Configuration)技术。而IPv4自动配置必须依赖DHCP服务器完成,所以IPv4仅支持有状态的自动配置(Stateful Auto configuration)。IPv6路由器监听主机发送的自动配置请求包(RS包)、然后返回给主机包含IPv6地址前缀等信息的数据包(RA),主机接收到RA包、整合信息(如:IPv6地址前缀、主机MAC 地址等)生成自己的IPv6地址。

本文来自于笔者在做公司项目适配IPv6协议过程中的实践总结,这里以Keepalived为例进行描述。如果手头没有IPv6路由器,可以使用radvd模拟IPv6路由器完成IPv6地址的无状态自动配置。

radvd详解

官方资料: https://radvd.litech.org

    Linux IPv6 Router Advertisement Daemon (radvd)
    The router advertisement daemon (radvd) is run by Linux or BSD systems acting as IPv6 routers.
    It sends Router Advertisement messages, specified by RFC 2461, to a local Ethernet LAN periodically and when requested by a node sending a Router Solicitation message.
    These messages are required for IPv6 stateless autoconfiguration.
    Linux is supported. Your mileage may vary on BSD.

    简单理解就是,radvd是一个软件模拟的IPv6路由器。
    方提供了部分操作系统对应的rpm包,直接下载安装即可:

      https://rpmfind.net/linux/rpm2html/search.php?query=radvd
      如:rpm -ivh radvd-2.17-3.el7.x86_64.rpm

      如果已有rpm或者radvd版本不满足使用要求,可以进行如下编译安装:

      1.  先编译安装依赖项yacc和flex

        wget https://invisible-island.net/datafiles/release/byacc.tar.gz
        tar -xvf byacc.tar.gz
        cd byacc-20230521/
        ./configure
        make
        make install
        yacc –h
        --------------------------------------
        wget http://sources.buildroot.net/flex/flex-2.6.4.tar.gz
        tar -xvf flex-2.6.4.tar.gz
        cd flex-2.6.4
        ./configure
        make
        make install
        flex –h

        2. 编译安装radvd

          wget http://sources.buildroot.net/radvd/radvd-2.19.tar.gz
          tar -xvf radvd-2.19.tar.gz
          cd radvd-2.19
          ./configure
          make
          make install


          3. 配置/etc/radvd.conf

            [root@felixzh3 ~]# cat etc/radvd.conf
            interface eth0 {
            AdvSendAdvert on;
            AdvCurHopLimit 64;
            MinRtrAdvInterval 198;
            MaxRtrAdvInterval 600;
            AdvDefaultLifetime 1800;
            AdvReachableTime 0;
            AdvRetransTimer 0;
            AdvDefaultPreference low;
            AdvHomeAgentFlag off;
            AdvOtherConfigFlag on;
            AdvManagedFlag off;
            prefix 2016:ac11:5cad:0::/64 {
            AdvOnLink on;
            AdvAutonomous on;
            AdvValidLifetime 2400;
            AdvPreferredLifetime 1800;
            };
            RDNSS fe80::2224:7ff:fe26:3014 {
            AdvRDNSSPreference 8;
            AdvRDNSSLifetime 1200;
            };
            };

            4. 启动radvd

              systemctl start radvd
              systemctl status radvd

              5. 查看主机自动配置IPv6地址


              其中,主机IPv6地址前缀与radvd.conf配置前缀一致。

              Keepalived详解

              1. 编译安装

              官网资料:https://www.keepalived.org/index.html

                wget https://www.keepalived.org/software/keepalived-2.2.0.tar.gz
                tar -xvf keepalived-2.2.0.tar.gz
                cd keepalived-2.2.0
                yum install -y openssl-* gcc-c++ kernel-devel*
                ./configure --prefix=/usr/local/keepalived6

                注意:必须确保编译环境已安装libnl/libnl-3(如:yum install libnl*),否则不支持IPv6。提示如下:

                  *** WARNING - this build will not support IPVS with IPv6. Please install libnl/libnl-3 dev libraries to support IPv6 with IPVS.

                  编译安装如下:

                    make -j 8 && make install

                    为方便管理,整理目录

                      cp /usr/local/keepalived6/etc/sysconfig/keepalived etc/sysconfig/
                      cp -r usr/local/keepalived6/etc/keepalived/ etc/
                      ln -sv usr/local/keepalived6/sbin/keepalived usr/sbin/
                      ln -sv usr/local/keepalived6/bin/genhash usr/bin/
                      cp keepalived/etc/init.d/keepalived /etc/init.d/

                      2. 配置 /etc/keepalived/keepalived.conf

                        global_defs {
                        router_id LVS_DEVEL
                        script_user root
                        vrrp_garp_master_delay 30
                        vrrp_garp_master_refresh 60
                        vrrp_garp_master_refresh_repeat 1
                        vrrp_garp_interval 1
                        }


                        vrrp_instance VI_1 {
                        state BACKUP
                        priority 120
                        interface eth0
                        virtual_router_id 111
                        advert_int 3
                        virtual_ipaddress {
                        2016:ac11:5cad:0:4ce0:8548:3ab0:c19c
                        }
                        }

                        3. 启动服务

                          systemctl start keepalived
                          systemctl status keepalived



                          通过ip addr可以看到keepalived添加的VIP,如下:

                          通过tcpdump可以看到IPv6数据包,如下:


                          总结
                          本文记录如何通过软件层面(radvd)模拟IPv6网络环境,以keepalived作为示例描述如何使用IPv6协议。以便于在没有专用的IPv6路由器的开发场景,适配IPv6化产品研发。



                          文章转载自大数据从业者,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

                          评论