这里是 AWS 新手。我正在尝试创建一个包含多个 EC2 节点的堆栈,每个节点都应该有两个网络接口(interface)。
一个接口(interface)应该是公共(public)的并连接到互联网,另一个接口(interface)应该是私有(private)的。接口(interface)应属于不同的子网,以便可以独立路由(数据平面/控制平面)。
以下是我在 CloudFormation 中尝试过的操作:
Resources:
[.....]
Host1:
Type: 'AWS::EC2::Instance'
Properties:
ImageId: 'ami-02541b8af977f6cdd' # Amazon Linux x86
InstanceType: 't2.micro'
KeyName: !Ref KeyName
NetworkInterfaces:
- SubnetId: !Ref SubnetPublic
AssociatePublicIpAddress: true
DeleteOnTermination: true
DeviceIndex: '0'
GroupSet:
- !Ref SecurityGroup
- SubnetId: !Ref SubnetPrivate
AssociatePublicIpAddress: false
DeleteOnTermination: true
DeviceIndex: '1'
GroupSet:
- !Ref SecurityGroup
但这会导致错误:
使用多个网络接口(interface)启动时无法指定 AssociatePublicIPAddress 参数。
此处提供完整的 YAML 文件:https://gist.github.com/kmansoft/39f7be10553195f41b8201e5638073f2
我该如何解决这个问题?
最佳答案
有事情要做。
诀窍是仅在 EC2 实例中指定一个网络接口(interface) - 然后可以设置 AssociatePublicIpAddress: true
- 并单独添加第二个网络接口(interface)。
Resources:
[...]
Host1:
Type: 'AWS::EC2::Instance'
Properties:
ImageId: 'ami-02541b8af977f6cdd' # Amazon Linux x86
InstanceType: 't2.micro'
AvailabilityZone: !Select [0, !GetAZs '']
KeyName: !Ref KeyName
NetworkInterfaces:
- SubnetId: !Ref SubnetPublic
AssociatePublicIpAddress: true
DeleteOnTermination: true
DeviceIndex: '0'
GroupSet:
- !Ref SecurityGroup
Tags:
- Key: Name
Value: 'simple - host1'
Host1Eth1:
Type: 'AWS::EC2::NetworkInterface'
Properties:
SubnetId: !Ref SubnetPrivate
GroupSet:
- !Ref SecurityGroup
Tags:
- Key: Name
Value: 'simple - host1 eth1'
Host1Eth1Attachment:
Type: 'AWS::EC2::NetworkInterfaceAttachment'
Properties:
DeleteOnTermination: true
DeviceIndex: 1
NetworkInterfaceId: !Ref Host1Eth1
InstanceId: !Ref Host1
在我的配置中,eth0是从30.0.1.0/24分配的,eth1是从30.0.2.0/24分配的。
这是创建实例后的路由表:
default via 30.0.1.1 dev eth0
default via 30.0.2.1 dev eth1 metric 10001
30.0.1.0/24 dev eth0 proto kernel scope link src 30.0.1.145
30.0.2.0/24 dev eth1 proto kernel scope link src 30.0.2.251
169.254.169.254 dev eth0
这非常接近我所需要的,只需要使用实例 UserData 进行一些调整。
完整的 YAML 脚本:https://gist.github.com/kmansoft/c490e7958b8ff8f1d2eb14a6cd115f08
关于amazon-web-services - AWS EC2 - 多个(公共(public)/私有(private))网络接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72193979/