【问题标题】:Cannot connect to RDS in another VPC via VPC peering无法通过 VPC 对等连接到另一个 VPC 中的 RDS
【发布时间】:2021-05-04 14:04:46
【问题描述】:

我有两个 VPC:

  • VPC A
    • RDS 实例
  • VPC B
    • EC2 实例

子网也很少:

  • VPC A
    • 私人A
    • 私人B
    • 同行A
  • VPC B
    • 私人A
    • 私人B
    • 同行A

RDS 在私有 A、私有 B、VPC A 的 Peer A 中。

EC2 在 VPC B 的 Peer A 中。

我想从 EC2 连接到 RDS 实例。

我创建了一个对等互连:

resource "aws_vpc_peering_connection" "a_to_b" {
  vpc_id      = aws_vpc.a.id
  peer_vpc_id = aws_vpc.b.id
  auto_accept = true

  accepter {
    allow_remote_vpc_dns_resolution = true
  }

  requester {
    allow_remote_vpc_dns_resolution = true
  }
}

resource "aws_vpc_peering_connection_accepter" "a_to_b" {
  vpc_peering_connection_id = aws_vpc_peering_connection.a_to_b.id
  auto_accept               = true
}

我也有整个 CIDR 块的路由表,如下所示:

resource "aws_route_table" "a_peer" {
  vpc_id = aws_vpc.a.id
}

resource "aws_route_table_association" "a_peer" {
  route_table_id = aws_route_table.a_peer.id
  subnet_id      = aws_subnet.a_peer.id
}

resource "aws_route" "a_peer_b" {
  route_table_id            = aws_route_table.a_peer.id
  destination_cidr_block    = aws_subnet.b_peer.cidr_block
  vpc_peering_connection_id = aws_vpc_peering_connection.a_to_b.id
}
resource "aws_route_table" "b_peer" {
  vpc_id = aws_vpc.b.id
}

resource "aws_route_table_association" "b_peer" {
  route_table_id = aws_route_table.b_peer.id
  subnet_id      = aws_subnet.b_peer.id
}

resource "aws_route" "b_peer_a" {
  route_table_id            = aws_route_table.b_peer.id
  destination_cidr_block    = aws_subnet.a_peer.cidr_block
  vpc_peering_connection_id = aws_vpc_peering_connection.a_to_b.id
}

我还创建了从 RDS 实例上的 ingressegress 到 EC2 安全组的安全组。

当我通过 SSH 连接到 EC2 时,我可以获得 DNS:

$ nslookup rds.xxxxxxxxxxx.eu-west-2.rds.amazonaws.com
Server:     192.16.0.2
Address:    192.16.0.2#53

Non-authoritative answer:
Name:   rds.xxxxxxxxxxx.eu-west-2.rds.amazonaws.com
Address: 10.16.192.135

但是,curl 无法连接:

$ curl rds.xxxxxxxxxxx.eu-west-2.rds.amazonaws.com:5432

预期的响应是:

$ curl rds.xxxxxxxxxxx.eu-west-2.rds.amazonaws.com:5432
curl: (52) Empty reply from server

VPC 对等是“活动的”,并且路由表与 Terraform 匹配。

如何连接?

【问题讨论】:

  • 您能说明一下您的设置吗?为什么您只路由到单个子网苹果酒系列? RDS 的第二个子网在哪里? RDS 必须在两个 AZ 中,因此它必须使用两个子网。您如何知道您的 RDS 主服务器实际上位于您指定的单个子网中,而不是所需的另一个子网中?
  • @Marcin RDS 位于多个子网中:两个私有子网和对等子网。
  • 您只设置到单个子网的路由,例如destination_cidr_block = aws_subnet.a_peer.cidr_block。其他子网呢?为什么不像往常那样对等 VPC cidr。
  • 我不希望其他 VPC 中的所有资源都可以访问。也许我在这里采取了错误的方法?
  • 您可以尝试在路由表中使用 VPC 苹果酒范围,而不是单个子网吗?

标签: amazon-web-services terraform amazon-vpc terraform-provider-aws


【解决方案1】:

我自己做了一些测试,我很确定问题是由您的路由引起的,假设您的 VPC 中的所有其他内容都是正确的,因为 VPC 和子网定义不正确显示。

具体来说,您写道“RDS 在私有 A、私有 B、VPC A 的对等 A 中”。这意味着 RDS 主节点可能位于这些子网中的任何一个中。您无法控制它,因为它由 RDS 来选择要使用的子网。创建 RDS 时,您只能通过选择 AZ 来部分控制它。随后,您的对等路由表应涵盖所有这三个子网。实现这一目标的最简单方法是使用 VPC CIDR 范围:

# Route from instance in VPC B to any subnet in VPC A which
# hosts your RDS in all its subnets
resource "aws_route" "b_peer_a" {
  route_table_id            = aws_route_table.b_peer.id
  destination_cidr_block    = aws_vpc.a.cidr_block
  vpc_peering_connection_id = aws_vpc_peering_connection.a_to_b.id
}

那么您还需要有一个VPC A 中的路由表与您的所有子网的对等连接相关联:

resource "aws_route_table" "a_peer" {
  vpc_id = aws_vpc.a.id
}

resource "aws_route_table_association" "a_peer" {
  route_table_id = aws_route_table.a_peer.id
  subnet_id      = aws_subnet.a_peer.id
}

resource "aws_route_table_association" "a_private1" {
  route_table_id = aws_route_table.a_peer.id
  subnet_id      = aws_subnet.a_private1.id
}

resource "aws_route_table_association" "a_private2" {
  route_table_id = aws_route_table.a_peer.id
  subnet_id      = aws_subnet.a_private2.id
}

resource "aws_route" "a_peer_b" {
  route_table_id            = aws_route_table.a_peer.id
  destination_cidr_block    = aws_subnet.b_peer.cidr_block
  vpc_peering_connection_id = aws_vpc_peering_connection.a_to_b.id
}

【讨论】:

  • 感谢您的调查。这样做的问题是我在 A 的私有子网中有其他服务不应该是对等互连的一部分。也许我需要 2 个对等子集,将 RDS 移动到两者中,然后以某种方式连接 VPC A 中的私有子网和对等子网?
  • @sdgfsdh 是的,在这种情况下最好隔离 RDS,而不是将其与同一子网中的其他服务混合。
  • 成功!我找到的解决方案是:VPC A 中的 2 个对等子网,RDS 位于这些子网中。 VPC A 中的对等子网与 VPC B 的私有子网之间的路由表
猜你喜欢
  • 2023-01-18
  • 2021-12-25
  • 1970-01-01
  • 2017-01-13
  • 2021-07-25
  • 1970-01-01
  • 2018-10-20
  • 2021-04-19
  • 2018-05-11
相关资源
最近更新 更多