AWS VPC 路由表实战:权重 100 vs 200 配置实现主备链路自动切换
在云架构设计中,网络流量的高可用性始终是核心诉求之一。AWS VPC 路由表的权重(Weight)机制为解决这一需求提供了精细化的控制手段。本文将深入探讨如何通过权重值的差异化配置(如100与200),实现Direct Connect专线与VPC对等连接之间的智能流量调度与故障自动切换。
1. 路由权重与优先级的核心原理
在AWS网络架构中,权重值越低表示路由优先级越高。这一设计逻辑与常见的权重认知相反,需要特别注意:
- 权重100:通常作为主链路默认值(如通过VGW传播的Direct Connect路由)
- 权重200:通过手动配置实现次优先级(如VPC对等连接的备份路由)
当存在多条相同目标前缀的路由时,系统会优先选择权重值更低的路由条目。这种机制与传统的优先级(Priority)设计形成鲜明对比:
| 对比维度 | 权重(Weight) | 优先级(Priority) |
|---|---|---|
| 判断逻辑 | 数值越小优先级越高 | 数值越小优先级越高 |
| 生效范围 | 相同前缀长度路由间比较 | 不同路由规则间的执行顺序 |
| 典型应用 | 链路质量差异化的主备切换 | 路由规则的全局执行顺序控制 |
提示:AWS路由表对同前缀路由的优先级判断规则为:前缀长度越长越优先 > 前缀长度相同时权重值越低越优先
2. 主备链路配置实战
2.1 基础环境准备
首先确保已部署以下资源:
- 激活的Direct Connect连接及虚拟私有网关(VGW)
- 已建立的VPC对等连接(pcx-xxxxxx)
- 目标子网关联的自定义路由表
通过AWS CLI检查当前路由表配置:
aws ec2 describe-route-tables \ --route-table-ids rt-xxxxxx \ --query 'RouteTables[].Routes[]'典型输出示例显示存在两条同前缀路由:
[ { "DestinationCidrBlock": "172.31.0.0/16", "GatewayId": "vgw-xxxxxx", "Origin": "EnableVgwRoutePropagation", "State": "active" }, { "DestinationCidrBlock": "172.31.0.0/16", "VpcPeeringConnectionId": "pcx-xxxxxx", "Origin": "CreateRoute", "State": "active" } ]2.2 权重配置关键操作
步骤1:删除现有对等连接路由
aws ec2 delete-route \ --route-table-id rt-xxxxxx \ --destination-cidr-block 172.31.0.0/16 \ --vpc-peering-connection-id pcx-xxxxxx步骤2:添加带权重的备份路由
aws ec2 create-route \ --route-table-id rt-xxxxxx \ --destination-cidr-block 172.31.0.0/16 \ --vpc-peering-connection-id pcx-xxxxxx \ --weight 200步骤3:验证路由优先级
aws ec2 describe-route-tables \ --route-table-ids rt-xxxxxx \ --query 'RouteTables[].Routes[]' \ --output table正常状态下应看到类似输出:
----------------------------------------------------- | DescribeRoutes | +-------------------+----------------+--------------+ | DestinationCidr | GatewayId | Weight | |-------------------+----------------+--------------| | 172.31.0.0/16 | vgw-xxxxxx | 100 | | 172.31.0.0/16 | pcx-xxxxxx | 200 | +-------------------+----------------+--------------+3. 故障切换机制验证
3.1 模拟Direct Connect中断
通过禁用VGW路由传播测试故障转移:
aws ec2 disable-vgw-route-propagation \ --route-table-id rt-xxxxxx \ --gateway-id vgw-xxxxxx观察路由表变化:
watch -n 5 "aws ec2 describe-route-tables \ --route-table-ids rt-xxxxxx \ --query 'RouteTables[].Routes[]' \ --output table"约3-5分钟后,原vgw路由条目状态将变为blackhole,流量自动切换至权重200的VPC对等连接。
3.2 恢复测试
重新启用路由传播:
aws ec2 enable-vgw-route-propagation \ --route-table-id rt-xxxxxx \ --gateway-id vgw-xxxxxx流量将自动回切至权重100的Direct Connect链路,整个过程无需人工干预。
4. Terraform自动化部署方案
以下模板实现全自动化的主备路由配置:
resource "aws_route" "primary_dx" { route_table_id = aws_route_table.main.id destination_cidr_block = "172.31.0.0/16" gateway_id = aws_vpn_gateway.vgw.id } resource "aws_route" "backup_peering" { route_table_id = aws_route_table.main.id destination_cidr_block = "172.31.0.0/16" vpc_peering_connection_id = aws_vpc_peering_connection.peer.id weight = 200 depends_on = [aws_route.primary_dx] }关键参数说明:
weight参数仅对部分路由类型生效- 通过
depends_on确保主路由优先创建 - 结合
aws_ec2_transit_gateway_route_table_propagation可实现更复杂的混合云组网
5. 高级应用场景
5.1 多级权重配置
对于需要灰度发布的场景,可采用多级权重策略:
| 链路类型 | 权重值 | 流量比例 |
|---|---|---|
| 主Direct Connect | 100 | 70% |
| 备Direct Connect | 150 | 20% |
| VPC对等连接 | 200 | 10% |
配置示例:
# 主链路 aws ec2 create-route --route-table-id rt-xxxx --destination 10.1.0.0/16 \ --gateway-id vgw-aaaa --weight 100 # 备链路 aws ec2 create-route --route-table-id rt-xxxx --destination 10.1.0.0/16 \ --gateway-id vgw-bbbb --weight 150 # 最后保障链路 aws ec2 create-route --route-table-id rt-xxxx --destination 10.1.0.0/16 \ --vpc-peering-connection-id pcx-cccc --weight 2005.2 结合路由分析工具
通过AWS Route Analyzer进行预验证:
import boto3 client = boto3.client('ec2') response = client.analyze_route_tables( RouteTableIds=['rt-xxxxxx'], Filters=[{ 'Name': 'route.gateway-id', 'Values': ['vgw-xxxxxx'] }] ) print(response['RouteTables'][0]['RouteAnalysisResults'])输出包含路径可行性、安全组规则等深度分析数据,辅助排查配置问题。
5.3 与BGP路由的协同
当使用Direct Connect的BGP协议时,可通过以下方式增强可靠性:
- 在BGP侧设置更高的Local Preference值(默认为100)
- 通过AS Path Prepending降低备用路径优先级
- 配置MED值影响入站流量路径
典型BGP配置示例:
router bgp 65100 neighbor 192.0.2.1 remote-as 64512 neighbor 192.0.2.1 route-map PRIMARY_IN in neighbor 203.0.113.1 remote-as 64512 neighbor 203.0.113.1 route-map SECONDARY_IN in route-map PRIMARY_IN permit 10 set local-preference 200 route-map SECONDARY_IN permit 10 set local-preference 150