tags:
# eNSP路由器上看arp
dis arp
system-view
# 给eNSP路由器分别配置两个地址 int 是interface的简写
int G0/0/0
ip a 192.168.1.1 24
dis this
# dis int G0/0/0 看接口的一些信息int G0/0/1
ip a 192.168.2.1 24
dis thisdis ip int b
# 右键抓包
arp -a # 看arp缓存
ping 192.168.2.2
# 我们发现mac地址是192.168.1.1 而不是192.168.2.2
# 路由器的每一个接口都是一个广播域 接口举例:G0/0/0
# arp的广播 对于pc1而言出去的数据包都要发给网关192.168.1.1 当192.168.1.2发给192.168.1.1它的arp请求就会限制在这个广播域中 不会通告给别的广播域的
# 数据包先从192.168.1.2发给路由器的接口, 然后路由器再去路由表一个个问。
arp -a
# 源ip 目的ip每次arp转发都没变 但是请求中源mac和目的mac每次转发都会变
# AR1 查看一下配置 配置很简单就是在接口上配置一下ip和开启prox arp就可以
dis current-configuration
# 测试上面是否能通信host1
arp -d # 清楚一下arp
ping 192.168.2.1
arp -a
# AR1上查看接口 发现上面arp中的mac地址就是这个接口的
dis int g0/0/0
# 第一台主机192.168.44.128上 eth0相当于ns0的网卡
ip link add veth0 type veth peer name eth0
ip netns add ns0
ip link set eth0 netns ns0
ip netns exec ns0 ip a add 10.1.1.10/24 dev eth0
ip netns exec ns0 ip link set eth0 up
ip netns exec ns0 ifconfig # 查看ns0中的eth0的配置
# 这里为了和calico相似 我们手动添加和calico配置一样的路由169.254.1.1
ip netns exec ns0 ip route add 169.254.1.1 dev eth0 scope link # 不给掩码就是32位的 scope link路由在同一的链路上才生效 就是一个二层
ip netns exec ns0 ip route add default via 169.254.1.1 dev eth0 # ns默认路由
# 查看一下配置的路由表
ip netns exec ns0 route -n # 或者ip netns exec ns0 ip route
# 删除一下由 ip netns exec ns0 ip a add 10.1.1.10/24 dev eth0 自动创建的路由
ip netns exec ns0 route delete -net 10.1.1.0 netmask 255.255.255.0
ip netns exec ns0 route -n
ip link set veth0 up
# 主机上 10.1.1.10 地址通过veth0发出去
ip route add 10.1.1.10 dev veth0 scope link
# 目的地址要发给第二个节点192.168.44.133
ip route add 10.1.1.20 via 192.168.44.133 dev ens33
route -n# 第二个主机192.168.44.133配置
ip link add veth0 type veth peer name eth0
ip netns add ns1
ip link set eth0 netns ns1
ip netns exec ns1 ip a add 10.1.1.20/24 dev eth0
ip netns exec ns1 ip link set eth0 up
ip netns exec ns1 ifconfig # 查看ns0中的eth0的配置
# 这里为了和calico相似 我们手动添加和calico配置一样的路由169.254.1.1
ip netns exec ns1 ip route add 169.254.1.1 dev eth0 scope link # 不给掩码就是32位的 scope link路由在同一的链路上才生效 就是一个二层
ip netns exec ns1 ip route add default via 169.254.1.1 dev eth0 # ns默认路由
# 查看一下配置的路由表
ip netns exec ns1 route -n # 或者ip netns exec ns0 ip route
# 删除一下由 ip netns exec ns0 ip a add 10.1.1.10/24 dev eth0 自动创建的路由
ip netns exec ns1 route delete -net 10.1.1.0 netmask 255.255.255.0
ip netns exec ns1 route -n
ip link set veth0 up
# 主机上 10.1.1.20 地址通过veth0发出去
ip route add 10.1.1.20 dev veth0 scope link
# 目的地址要发给第一个节点192.168.44.128
ip route add 10.1.1.10 via 192.168.44.128 dev ens33
route -n# 在哪个接口上使能arp 需要去打开这个功能 这里在veth0上使能 两台机器都需要执行
cat /proc/sys/net/ipv4/conf/veth0/proxy_arp
echo 1 > /proc/sys/net/ipv4/conf/veth0/proxy_arp# 做ping测试 可以ping通
ip netns exec ns0 ping 10.1.1.20# 查看为什么是通的 首先到ns0网关169.254.1.1 这个网关是虚拟的不存在(是一个保留地址不会被使用) 因为开启了proxy arp 那么veth0就把可以自己的mac地址回给eth0
# 然后ns0中eth0又封装了一个tcp/ip协议包 源ip就是ens33本机ip 目的ip就是10.1.1.20 源mac是eth0的mac 目的mac是veth0返回过来的mac(这里注意)
ip netns exec ns0 arp -a
# 抓个包 ip netns exec ns0 ping 10.1.1.20
ip netns exec ns0 tcpdump -i eth0 -w proxy.cap
# 或者
ip netns exec ns0 tcpdump -i eth0 -nn # 这里把自己mac返回给eth0tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
22:51:06.148520 IP 10.1.1.10 > 10.1.1.20: ICMP echo request, id 30338, seq 1, length 64
22:51:06.148849 IP 10.1.1.20 > 10.1.1.10: ICMP echo reply, id 30338, seq 1, length 64
22:51:07.162802 IP 10.1.1.10 > 10.1.1.20: ICMP echo request, id 30338, seq 2, length 64
22:51:07.163088 IP 10.1.1.20 > 10.1.1.10: ICMP echo reply, id 30338, seq 2, length 64
22:51:08.188207 IP 10.1.1.10 > 10.1.1.20: ICMP echo request, id 30338, seq 3, length 64
22:51:08.188676 IP 10.1.1.20 > 10.1.1.10: ICMP echo reply, id 30338, seq 3, length 64
22:51:09.193255 IP 10.1.1.10 > 10.1.1.20: ICMP echo request, id 30338, seq 4, length 64
22:51:09.194665 IP 10.1.1.20 > 10.1.1.10: ICMP echo reply, id 30338, seq 4, length 64
22:51:11.452278 ARP, Request who-has 169.254.1.1 tell 10.1.1.10, length 28
22:51:11.452304 ARP, Request who-has 10.1.1.10 tell 192.168.44.128, length 28
22:51:11.452315 ARP, Reply 10.1.1.10 is-at 3a:49:17:91:08:0a, length 28
22:51:11.452320 ARP, Reply 169.254.1.1 is-at e6:04:3f:b0:1f:ae, length 28 # 可以把echo 0 > /proc/sys/net/ipv4/conf/veth0/proxy_arp 关了 马上就访问不通了
# pc1 ip:192.168.0.2 掩码:255.255.255.0 网关:192.168.0.1
# server1 ip: 2.2.2.1 掩码:255.255.255.0
# AR1路由器接口配置
# interface GigabitEthernet0/0/0
# ip address 192.168.0.1 255.255.255.0
#
# interface GigabitEthernet0/0/1
# ip address 2.2.2.2 255.255.255.0
# net策略 把192.168.0.2映射到2.2.2.3
# nat static global 2.2.2.3 inside 192.168.0.2 netmask 255.255.255.255
# 路由器出接口抓包
ping 2.2.2.1
# 添加brO linux bridge
ip netns add ns1
ip l a br0 type bridge
ip l s br0 up
# 配置brO和veth:
ip l a veth0 type veth peer name br-veth0
ip l s veth0 netns ns1
ip l s br-veth0 master br0
ip l s br-veth0 up
# 配置ns1中接口
ip netns exec ns1 ip a a 10.1.1.2/24 dev veth0
ip netns exec ns1 ip l s veth0 up
ip a a 10.1.1.1/24 dev br0
ip l s br0 up
# 添加路由
ip netns exec ns1 ifconfig lo up # 回环
ip netns exec ns1 route add -net 0.0.0.0 netmask 0.0.0.0 gw 10.1.1.1
ip netns exec ns1 route -n
# 修改内核转发
echo 1 > /proc/sys/net/ipv4/ip_forward
cat /proc/sys/net/ipv4/ip_forward
# 添加SNAT规则:
# POSTROUTING 出去的数据包 源地址10.1.1.0/24 不是发往br0的 ! -o br0 -j跳 到源地址映射
# 如果不加! -o br0 那么两个网段的pod通信时 就会映射到别的地址 就有问题
ip netns exec ns1 ping 114.114.114.114 # 没做SNAT 直接ping 不通的
iptables -t nat -A POSTROUTING -s 10.1.1.0/24 ! -o br0 -j MASQUERADE
# 这个时候我们ping 114.114.114.114
ip netns exec ns1 ping 114.114.114.114 # 可以ping通过
# 过程: 从ns中到网关10.1.1.1 br01上 然后到宿主机的默认路由 默认路由走的是ens33的网卡 然后从宿主机出去时换成主机的ip[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.44.2 0.0.0.0 UG 100 0 0 ens33
10.1.1.0 0.0.0.0 255.255.255.0 U 0 0 0 br0
192.168.44.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33# tip: 网卡没up ifconfig看不到 ifconfig -a可以看到
docker run --name dnat -p 2022:22 --network=bridge --privileged=true --restart=always -td centos:7 /sbin/init# 启动容器内的22端口
docker exec -it dnat bash
yum -y install net-tools
yum -y install openssh-server
systemctl restart sshd
netstat -ntlp | grep 22
passwd # 配置密码# 通过xshell ip 宿主机的ip 端口2022 就可以连接容器内的终端
# 查看宿主机iptables
iptables-save
# 看到 新加自定义链 只要不是从docker0进来的 目的端口是2022 dnat到容器的22端口
-A DOCKER ! -i docker0 -p tcp -m tcp --dport 2022 -j DNAT --to-destination 172.17.0.3:22# 这个也可以链接 通过10.1.1.1根据路由表决定到宿主机的ens33网卡的2022端口
ip netns exec ns1 ssh 10.1.1.1 -p 2022
本文发布于:2024-01-31 03:11:43,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170664190324921.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |