为AWS EC2设置路由转发需要注意的坑

225 阅读2分钟

一个使用AWS的同事偶然在AWS EC2上尝试自建K8S的时候,发现AWS VPC(CIDR 172.50.0.0/16)中有Linux A(172.50.2.60)和Linux B(172.50.2.18)时,A和B间可以正常互相通信(ping正常),但是如果B中安装了Docker或者calico时,如果让A去和B中安装的Docker网桥(172.17.0.1)或者非AWS VPC CIDR的IP通信时,怎么也无法正常的完成通信。

具体的示意图如下:

A(172.50.2.60)希望ping通Docker的网桥(172.17.0.1) AWS EC2 route.drawio.png

Linux B(172.50.2.18)中安装了Docker,这个Docker有网桥172.17.0.11730388254589.png

此时A(172.50.2.60)可以ping通Linux B(172.50.2.18),无法ping通Docker的网桥172.17.0.1 1730388350239.png

我们在Linux A(172.50.2.60)添加路由: route add -net 172.17.0.0/16 gw 172.50.2.18,希望Linux A(172.50.2.60)ping Docker的网桥172.17.0.1时,先走到Linux B(172.50.2.18),遗憾的是依然无法ping通Docker的网桥172.17.0.1 1730388407433.png

这个同事在自己的IDC中做了反复的测试,其实上述的设置在IDC确认是可以ping通的,为什么AWS VPC网络环境中无法正常的进行呢?

反复的研究后,我们发现AWS EC2的网卡默认有Source/destination checking,如果是非AWS VPC CIDR内的自定义private ip互相通信,这个检查会进行阻止!

You can enable or disable source/destination checks, which ensure that the instance is either the source or the destination of any traffic that it receives. Source/destination checks are enabled by default. You must disable source/destination checks if the instance runs services such as network address translation, routing, or firewalls.

解决思路:关闭Source/destination checking,建议通信双方的Linux都关闭检查

我们可以在AWS控制台上进行关闭操作 1730389592921.png

1730389678992.png

完成操作后,我们检查会发现网络路由按照我们的设置正常工作了 1730388464123.png