栗子老师带你漫谈静态的那些事儿

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了栗子老师带你漫谈静态的那些事儿相关的知识,希望对你有一定的参考价值。

为什么需要非直连路由

直连路由的不足 不能去往非自己直连的其他网段(地方)
非直连路由:需要静态路由或动态路由,将网段添加到路由表中
技术图片
问题?AR1上没有到达如下的网段路由

23.1.1.0/24
2.2.2.2/32
3.3.3.3/32

技术图片

静态路由应用场景

  1. 静态路由是指由管理员手动配置和维护的路由
  2. 静态路由配置简单,无需像动态路由那样占用路由器的CPU资源来计算和分析路由更新
  3. 静态路由一般适用于结构简单的网络。不过,即使是在复杂网络环境中,合理地配置一些静态路由也可以改进网络的性能
    技术图片

    静态路由语法格式

    A.  [Huawei]ip route-static 目标网段 目标网段掩码 下一跳
    B.  [Huawei]ip route-static 目标网段 目标网段掩码 出接口
    C.  [Huawei]ip route-static 目标网段 目标网段掩码 出接口 下一跳     推荐使用该方式

    技术图片

    [R1]ip route-static 23.1.1.0 24 12.1.1.2(下一跳)   串行链路
    [R1]ip route-static 23.1.1.0 24 GigabitEthernet 0/0/0(出接口)   以太网
    [R1]ip route-static 23.1.1.0 24 GigabitEthernet 0/0/0 12.1.1.2(出接口+下一跳)

    目标网段:目标网段
    目标网段掩码:掩码 可以写255.255.255.0 或者24
    下一跳:到达目的地的下一站

    [R1]display ip routing-table protocol static   查看路由表中的静态路由

    实战演练通过静态路由实现网络互通

    技术图片

    在R1上面访问3.3.3.3路由,该如何实现?ping 3.3.3.3

    注意:ping 3.3.3.3 SIP:12.1.1.1 DIP:3.3.3.3
    因为R1配置接口IP地址后,只有12.1.1.0/24和1.1.1.1/32的直连路由,没有去往目标3.3.3.3的非直连路由,那么,我们可以在R1上面配置如下静态路由到达3.3.3.3

    ip route-static 3.3.3.3 255.255.255.255 g0/0/0 12.1.1.2

    配置完成后,R1上面ping 3.3.3.3 发现无法ping通
    因为数据在传递的过程中,R1发现到达3.3.3.3得下一跳是12.1.1.2 是R2设备,那么R1会把这个数据传递给R2设备,此时R2设备上没有到达3.3.3.3的路由,R2设备会丢弃这个报文
    那我们可以在R2上面写静态路由,使其到达3.3.3.3路由

    ip route-static 3.3.3.3 255.255.255.255 g0/0/1 23.1.1.3

    配置完成后,R1上面ping 3.3.3.3 发现无法ping通
    因为数据在传递的过程中,R1发现到达3.3.3.3得下一跳是12.1.1.2 是R2设备,那么R1会把这个数据传递给R2设备,R2设备到达3.3.3.3的下一跳是23.1.1.3 是R3设备,而3.3.3.3路由刚好是R3自身的直连路由,所以,R3会接收这个数据。
    但是,数据在传递过程中,数据既可以发过去,数据也要能够回来,即发数据是R1到R3 回数据是R3到R1 由于R3上面没有到达12.1.1.0/24网段的路由,所以R3无法回应数据

    ip route-static 12.1.1.0 255.255.255.255 g0/0/1 23.1.1.2

    在R3上面写完到达23.1.1.0的路由条目后,会把回应的数据包交给R2,R2发现到达12.1.1.0网段是自己的直连,因此把数据包交给R1

    <R1>ping 3.3.3.3
    PING 3.3.3.3: 56  data bytes, press CTRL_C to break
    Reply from 3.3.3.3: bytes=56 Sequence=1 ttl=254 time=40 ms
    Reply from 3.3.3.3: bytes=56 Sequence=2 ttl=254 time=30 ms
    Reply from 3.3.3.3: bytes=56 Sequence=3 ttl=254 time=50 ms
    Reply from 3.3.3.3: bytes=56 Sequence=4 ttl=254 time=20 ms
    Reply from 3.3.3.3: bytes=56 Sequence=5 ttl=254 time=40 ms
    
    --- 3.3.3.3 ping statistics ---
    5 packet(s) transmitted
    5 packet(s) received
    0.00% packet loss
    round-trip min/avg/max = 20/36/50 ms
    <R1>

    思考如下:如果在R1上面ping -a 1.1.1.1 3.3.3.3 能够ping通么?

    AR1的配置:

    ip route-static 3.3.3.3 255.255.255.255 12.1.1.2

    AR2的配置:

    ip route-static 3.3.3.3 255.255.255.255 23.1.1.3

    AR3的配置:

    ip route-static 1.1.1.1 255.255.255.255 23.1.1.2

    AR2的配置:

    ip route-static 1.1.1.1 255.255.255.255 12.1.1.1
测试如下:
<R1>ping -a 1.1.1.1 3.3.3.3
  PING 3.3.3.3: 56  data bytes, press CTRL_C to break
    Reply from 3.3.3.3: bytes=56 Sequence=1 ttl=254 time=30 ms
    Reply from 3.3.3.3: bytes=56 Sequence=2 ttl=254 time=40 ms
    Reply from 3.3.3.3: bytes=56 Sequence=3 ttl=254 time=30 ms
    Reply from 3.3.3.3: bytes=56 Sequence=4 ttl=254 time=40 ms
    Reply from 3.3.3.3: bytes=56 Sequence=5 ttl=254 time=30 ms

  --- 3.3.3.3 ping statistics ---
    5 packet(s) transmitted
    5 packet(s) received
    0.00% packet loss
    round-trip min/avg/max = 30/34/40 ms

思考如下:如果在R1上面ping -a 1.1.1.1 2.2.2.2能够ping通么?

AR1的配置:

ip route-static 2.2.2.2 255.255.255.255 12.1.1.2

AR2的配置:

ip route-static 1.1.1.1 255.255.255.255 12.1.1.1

测试如下:

<R1>ping -a 1.1.1.1 2.2.2.2
  PING 2.2.2.2: 56  data bytes, press CTRL_C to break
    Reply from 2.2.2.2: bytes=56 Sequence=1 ttl=255 time=140 ms
    Reply from 2.2.2.2: bytes=56 Sequence=2 ttl=255 time=20 ms
    Reply from 2.2.2.2: bytes=56 Sequence=3 ttl=255 time=30 ms
    Reply from 2.2.2.2: bytes=56 Sequence=4 ttl=255 time=30 ms
    Reply from 2.2.2.2: bytes=56 Sequence=5 ttl=255 time=30 ms

  --- 2.2.2.2 ping statistics ---
    5 packet(s) transmitted
    5 packet(s) received
    0.00% packet loss
    round-trip min/avg/max = 20/50/140 ms

思考如下:如果R2访问ping -a 2.2.2.2 3.3.3.3能够ping通么?

AR2的配置:

ip route-static 3.3.3.3 255.255.255.255 23.1.1.3

AR3的配置:

ip route-static 2.2.2.2 255.255.255.255 23.1.1.2

测试如下:

<R2>ping -a 2.2.2.2 3.3.3.3
  PING 3.3.3.3: 56  data bytes, press CTRL_C to break
    Reply from 3.3.3.3: bytes=56 Sequence=1 ttl=255 time=70 ms
    Reply from 3.3.3.3: bytes=56 Sequence=2 ttl=255 time=30 ms
    Reply from 3.3.3.3: bytes=56 Sequence=3 ttl=255 time=30 ms
    Reply from 3.3.3.3: bytes=56 Sequence=4 ttl=255 time=30 ms
    Reply from 3.3.3.3: bytes=56 Sequence=5 ttl=255 time=30 ms

  --- 3.3.3.3 ping statistics ---
    5 packet(s) transmitted
    5 packet(s) received
    0.00% packet loss
    round-trip min/avg/max = 30/38/70 ms

思考如下:如果要想实现全网互通?需要在AR1 2 3设备上面分别写几条静态路由

AR1如下配置:

ip route-static 2.2.2.2 255.255.255.255 12.1.1.2
ip route-static 3.3.3.3 255.255.255.255 12.1.1.2
ip route-static 23.1.1.0 255.255.255.0 12.1.1.2

AR2如下配置:

ip route-static 1.1.1.1 255.255.255.255 12.1.1.1
ip route-static 3.3.3.3 255.255.255.255 23.1.1.3

AR3如下配置:

ip route-static 1.1.1.1 255.255.255.255 23.1.1.2
ip route-static 12.1.1.0 255.255.255.255 23.1.1.2
ip route-static 2.2.2.2 255.255.255.255 23.1.1.2

测试如下:

<R1>ping 2.2.2.2
  PING 2.2.2.2: 56  data bytes, press CTRL_C to break
    Reply from 2.2.2.2: bytes=56 Sequence=1 ttl=255 time=30 ms
    Reply from 2.2.2.2: bytes=56 Sequence=2 ttl=255 time=10 ms
    Reply from 2.2.2.2: bytes=56 Sequence=3 ttl=255 time=30 ms
    Reply from 2.2.2.2: bytes=56 Sequence=4 ttl=255 time=30 ms
    Reply from 2.2.2.2: bytes=56 Sequence=5 ttl=255 time=20 ms

  --- 2.2.2.2 ping statistics ---
    5 packet(s) transmitted
    5 packet(s) received
    0.00% packet loss
    round-trip min/avg/max = 10/24/30 ms

<R1>ping 3.3.3.3
  PING 3.3.3.3: 56  data bytes, press CTRL_C to break
    Reply from 3.3.3.3: bytes=56 Sequence=1 ttl=254 time=50 ms
    Reply from 3.3.3.3: bytes=56 Sequence=2 ttl=254 time=30 ms
    Reply from 3.3.3.3: bytes=56 Sequence=3 ttl=254 time=30 ms
    Reply from 3.3.3.3: bytes=56 Sequence=4 ttl=254 time=30 ms
    Reply from 3.3.3.3: bytes=56 Sequence=5 ttl=254 time=20 ms

  --- 3.3.3.3 ping statistics ---
    5 packet(s) transmitted
    5 packet(s) received
    0.00% packet loss
    round-trip min/avg/max = 20/32/50 ms

<R1>ping 23.1.1.3
  PING 23.1.1.3: 56  data bytes, press CTRL_C to break
    Reply from 23.1.1.3: bytes=56 Sequence=1 ttl=254 time=30 ms
    Reply from 23.1.1.3: bytes=56 Sequence=2 ttl=254 time=40 ms
    Reply from 23.1.1.3: bytes=56 Sequence=3 ttl=254 time=40 ms
    Reply from 23.1.1.3: bytes=56 Sequence=4 ttl=254 time=50 ms
    Reply from 23.1.1.3: bytes=56 Sequence=5 ttl=254 time=40 ms

  --- 23.1.1.3 ping statistics ---
    5 packet(s) transmitted
    5 packet(s) received
    0.00% packet loss
    round-trip min/avg/max = 30/40/50 ms

<R1>ping -a 1.1.1.1 2.2.2.2
  PING 2.2.2.2: 56  data bytes, press CTRL_C to break
    Reply from 2.2.2.2: bytes=56 Sequence=1 ttl=255 time=30 ms
    Reply from 2.2.2.2: bytes=56 Sequence=2 ttl=255 time=30 ms
    Reply from 2.2.2.2: bytes=56 Sequence=3 ttl=255 time=30 ms
    Reply from 2.2.2.2: bytes=56 Sequence=4 ttl=255 time=30 ms
    Reply from 2.2.2.2: bytes=56 Sequence=5 ttl=255 time=20 ms

  --- 2.2.2.2 ping statistics ---
    5 packet(s) transmitted
    5 packet(s) received
    0.00% packet loss
    round-trip min/avg/max = 20/28/30 ms

<R1>ping -a 1.1.1.1 3.3.3.3
  PING 3.3.3.3: 56  data bytes, press CTRL_C to break
    Reply from 3.3.3.3: bytes=56 Sequence=1 ttl=254 time=50 ms
    Reply from 3.3.3.3: bytes=56 Sequence=2 ttl=254 time=30 ms
    Reply from 3.3.3.3: bytes=56 Sequence=3 ttl=254 time=30 ms
    Reply from 3.3.3.3: bytes=56 Sequence=4 ttl=254 time=30 ms
    Reply from 3.3.3.3: bytes=56 Sequence=5 ttl=254 time=30 ms

  --- 3.3.3.3 ping statistics ---
    5 packet(s) transmitted
    5 packet(s) received
    0.00% packet loss
    round-trip min/avg/max = 30/34/50 ms

<R1>ping -a 1.1.1.1 23.1.1.3
  PING 23.1.1.3: 56  data bytes, press CTRL_C to break
    Reply from 23.1.1.3: bytes=56 Sequence=1 ttl=254 time=30 ms
    Reply from 23.1.1.3: bytes=56 Sequence=2 ttl=254 time=30 ms
    Reply from 23.1.1.3: bytes=56 Sequence=3 ttl=254 time=30 ms
    Reply from 23.1.1.3: bytes=56 Sequence=4 ttl=254 time=30 ms
    Reply from 23.1.1.3: bytes=56 Sequence=5 ttl=254 time=40 ms

  --- 23.1.1.3 ping statistics ---
    5 packet(s) transmitted
    5 packet(s) received
    0.00% packet loss
    round-trip min/avg/max = 30/32/40 ms

<R1>

什么是默认路由

默认路由一般来末节点配置 好处就是仅仅用一条路由代替所有路由条目

[Huawei]ip route-static 0.0.0.0 0 下一跳

技术图片
R1的配置

ip route-static 2.2.2.2 255.255.255.255 12.1.1.2
ip route-static 3.3.3.3 255.255.255.255 12.1.1.2
ip route-static 23.1.1.0 255.255.255.0 12.1.1.2

简化本R1的配置

ip route-static 0.0.0.0 0 12.1.1.2   代表R1想要去往任何非直连路由都把数据包交给R2设备

简化本R3的配置

ip route-static 0.0.0.0 0.0.0.0 23.1.1.2 代表R3想要去往任何非直连路由都把数据包交给R2设备

思考如下:为什么R2上面不能写默认路由?写了之后有什么问题出现?
因为R2设备处于R1和R3之间,如果在R2上面写默认路由

ip route-static 0.0.0.0 0 12.1.1.1
ip route-static 0.0.0.0 0 23.1.1.3

如果此时R1想要访问3.3.3.3,R1把数据包丢给R2的时候,R2此时有两个下一跳 一个是R1 一个是R3 那R2到底是丢给R1呢还是R3呢?所以,R2上面建议大家写明细路由,不要写默认路由

静态路由使用出接口和下一跳的场景

静态路由使用出接口的场景

在串行接口上,可以通过指定下一跳地址或出接口或出接口+下一跳来配置静态路由
技术图片

[RTA]ip route-static 192.168.2.0 24 10.0.12.2
[RTA]ip route-static 192.169.2.0 24 s1/0/0
[RTA]ip route-static 192.169.2.0 24 s1/0/0 10.0.12.2

静态路由使用下一跳的场景

在广播型的接口(如以太网接口)上配置静态路由时,必须要指定下一跳地址
技术图片

[RTA]ip route-static 192.168.2.0 24 10.0.123.2
[RTA]ip route-static 192.169.2.0 24 G0/0/0 10.0.123.2

思考一下?为什么不能用出接口呢?

静态路由的负载分担

AR1上访问2.2.2.2的时候,可以通过路径AR1-AR3-AR2 或者AR1-AR4-AR2 的负载分担
技术图片
AR1的配置:

ip route-static 2.2.2.2 255.255.255.255 13.1.1.3
ip route-static 2.2.2.2 255.255.255.255 14.1.1.4

查看AR1的路由表

<R1>display ip routing-table protocol static 
Route Flags: R - relay, D - download to fib
------------------------------------------------------------------------------
Public routing table : Static
         Destinations : 1        Routes : 2        Configured Routes : 2

Static routing table status : <Active>
         Destinations : 1        Routes : 2

Destination/Mask    Proto   Pre  Cost      Flags NextHop         Interface

        2.2.2.2/32  Static  60   0          RD   13.1.1.3        GigabitEthernet
0/0/0
                    Static  60   0          RD   14.1.1.4        GigabitEthernet
0/0/1

Static routing table status : <Inactive>
         Destinations : 0        Routes : 0

<R1>

测试一下,利用tracert进行路由追踪

<R1>tracert -a 1.1.1.1 2.2.2.2

 traceroute to  2.2.2.2(2.2.2.2), max hops: 30 ,packet length: 40,press CTRL_C t
o break 

 1 13.1.1.3 130 ms  20 ms 14.1.1.4 30 ms 

 2 24.1.1.2 30 ms  20 ms  30 ms 
<R1>tracert -a 1.1.1.1 2.2.2.2

 traceroute to  2.2.2.2(2.2.2.2), max hops: 30 ,packet length: 40,press CTRL_C t
o break 

 1 14.1.1.4 140 ms  20 ms  10 ms 

 2 24.1.1.2 100 ms  20 ms  20 ms 
<R1>

什么是浮动路由

浮动路由就是只让主链路进行工作,备链路不让转发流量
当主链路出现问题的时候,路由器会选择备用的链路,当主链路恢复的时候 路由器会选择主链路
技术图片
在AR1上面的配置:

ip route-static 2.2.2.2 255.255.255.255 13.1.1.3     默认优先级是60
ip route-static 2.2.2.2 255.255.255.255 14.1.1.4 preference 70

查看AR1的路由表

[R1]display ip routing-table protocol static 
Route Flags: R - relay, D - download to fib
------------------------------------------------------------------------------
Public routing table : Static
         Destinations : 1        Routes : 2        Configured Routes : 2

Static routing table status : <Active>
         Destinations : 1        Routes : 1

Destination/Mask    Proto   Pre  Cost      Flags NextHop         Interface

        2.2.2.2/32  Static  60   0          RD   13.1.1.3        GigabitEthernet
0/0/0

Static routing table status : <Inactive>
         Destinations : 1        Routes : 1

Destination/Mask    Proto   Pre  Cost      Flags NextHop         Interface

        2.2.2.2/32  Static  70   0          R    14.1.1.4        GigabitEthernet
0/0/1

[R1]

当AR1的主链路down时候

[R1-GigabitEthernet0/0/0]shutdown 
Nov 16 2019 12:27:14-08:00 R1 %%01IFPDT/4/IF_STATE(l)[8]:Interface GigabitEthern
et0/0/0 has turned into DOWN state.
[R1]display ip routing-table protocol static 
Route Flags: R - relay, D - download to fib
------------------------------------------------------------------------------
Public routing table : Static
         Destinations : 1        Routes : 2        Configured Routes : 2

Static routing table status : <Active>
         Destinations : 1        Routes : 1

Destination/Mask    Proto   Pre  Cost      Flags NextHop         Interface

        2.2.2.2/32  Static  70   0          RD   14.1.1.4        GigabitEthernet
0/0/1

Static routing table status : <Inactive>
         Destinations : 1        Routes : 1

Destination/Mask    Proto   Pre  Cost      Flags NextHop         Interface

        2.2.2.2/32  Static  60   0               13.1.1.3        Unknown

[R1]

当AR1的主用链路恢复正常时

[R1-GigabitEthernet0/0/0]undo shutdown 
Nov 16 2019 12:28:25-08:00 R1 %%01IFPDT/4/IF_STATE(l)[10]:Interface GigabitEther
net0/0/0 has turned into UP state.
[R1]display ip routing-table protocol static 
Route Flags: R - relay, D - download to fib
------------------------------------------------------------------------------
Public routing table : Static
         Destinations : 1        Routes : 2        Configured Routes : 2

Static routing table status : <Active>
         Destinations : 1        Routes : 1

Destination/Mask    Proto   Pre  Cost      Flags NextHop         Interface

        2.2.2.2/32  Static  60   0          RD   13.1.1.3        GigabitEthernet
0/0/0

Static routing table status : <Inactive>
         Destinations : 1        Routes : 1

Destination/Mask    Proto   Pre  Cost      Flags NextHop         Interface

        2.2.2.2/32  Static  70   0          R    14.1.1.4        GigabitEthernet
0/0/1

[R1]

静态路由不足

静态路由---不能动态的根据现网拓扑的改变而改变
能不能开发一款协议出来,让网络设备之间进行交换各种路由网段协议呢?
于是在早期提出了rip协议 因为当时的网络结构比较简单,RIP协议能够很有的胜任,而现在网络结构比较庞大,RIP协议已经不能满足于现在的网络当中,所以我们企业网络中普遍都是OSPF路由协议,RIP协议我们现在只是作为一个学习的一个协议而已,现网中几乎不用RIP协议

以上是关于栗子老师带你漫谈静态的那些事儿的主要内容,如果未能解决你的问题,请参考以下文章

大白话带你梳理一下Dubbo的那些事儿

栗子老师带你认识华为网络设备

分布式系统的那些事儿 - 系统与系统之间的调用

越学越有趣:『手把手带你学NLP』系列项目07 ——机器翻译的那些事儿

信管•讲座回顾 |《数据仓库那些事儿》

五Spring Application那些事儿