AWS 放错在Private子网的机器如何用公网IP连进去
今天面试了一下,面试官问到AWS的子网问题,来说说AWS的最佳实践: 首先是VPC的划定,然后就是三个public子网,三个private子网,都是一个zone分布在a、b、c三个不同机房 来保证最大冗余性,然后pub子网通过IGW来出公网,那Private子网就通过NAT出公网。 但是,但是,但是: 如果你把一台EC2服务器一开始就放错了子网,放到了private子网里,然后上面又跑了重要的服务,无法迁移,无法重启,而且整个vpc里只有这一台ec2,其它东西都是aws的服务或者市场的服务又或者fargate、lambda之类的无服务器,这时候你想进去调试,那就麻烦大了 那能不能给这台private的机器加上公网ip来当作跳板机直接访问呢? 答案是肯定的,可以。 但是又来了,如果这么配置了,你要对路由非常的熟悉,因为随后发生错乱的情况可能需要你手动添加路由,最麻烦的不是配置网卡,而是配置路由! 做法如下: 一、动态添加弹性ip到这个ec2的第二个网卡 二、配置网络 # vi /etc/netplan/50-cloud-init.yaml # This file is generated from information provided by the datasource. Changes # to it will not persist across an instance reboot. To disable cloud-init's # network configuration capabilities, write a file # /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following: # network: {config: disabled} network: ethernets: enX0: addresses: - 10.0.132.169/20 #The private IP address of primary ENI nameservers: addresses: - 10.0.0.2 routes: - to: 0.0.0.0/0 via: 10.0.128.1 # Default gateway, you can find it using** ip r** command table: 1001 - to: 10.0.132.169 via: 0.0.0.0 scope: link table: 1001 routing-policy: - from: 10.0.132.169 table: 1001 dhcp4: no dhcp6: false match: macaddress: 06:36:82:ef:39:39 set-name: enX0 enX1: addresses: - 10.0.3.176/20 #The private IP address of primary ENI gateway4: 10.0.0.1 nameservers: addresses: - 8.8.8.8 - 1.1.1.1 routes: - to: 0.0.0.0/0 via: 10.0.0.1 # Default gateway, you can find it using** ip r** command table: 1002 - to: 10.0.3.176 via: 0.0.0.0 scope: link table: 1002 routing-policy: - from: 10.0.3.176 table: 1002 - from: 120.116.111.99 table: 1002 dhcp4: no dhcp6: false match: macaddress: 06:29:c3:b2:c5:f9 set-name: enX1 version: 2 netplan apply 详细解释一下,enX0是private子网的网卡,enX1是弹性IP的网卡,注意,即使是弹性IP,也是个内网地址,这两个IP呢,都有各自独立的网关,第二个网卡还有自己的DNS。这就容易发生错乱了,因为缺省路由走第一个网卡,那如果从第二个网卡的公网IP入,出的时候走第一张网卡,那就有意思了。 ...