阿里云平台使用的ack创建的pod与服务器中的MongoDB不在同一网段如何解决
在阿里云 Kubernetes (ACK) 环境中,Pod 在 100.210.20.0/24
网段,服务器(ECS)在 100.210.10.0/24
网段,默认情况下 Pod 无法直接访问服务器上的 MongoDB。原因和解决方案如下:
一、网络不通的原因
-
跨子网隔离:
- Pod 网段 (
100.210.20.0/24
) 和服务器网段 (100.210.10.0/24
) 属于不同子网 - 阿里云 VPC 默认不会为不同子网自动配置路由
- Pod 网段 (
-
安全组限制:
- 服务器的安全组可能未放行 K8s Pod 网段
- MongoDB 默认监听
127.0.0.1
或内网 IP,未暴露给其他网段
-
容器网络特性:
- Pod 网络是 Overlay 网络,与主机网络隔离
- Pod 访问跨子网服务需要 VPC 路由支持
二、解决方案
通过以下任一方案打通网络:
方案一:配置 VPC 路由表
graph TD A[Pod 100.210.20.5] -->|跨子网访问| B[路由表] B --> C[添加路由规则] C --> D[目标:100.210.10.0/24
下一跳:服务器ECS] D --> E[MongoDB服务器]
- 在 VPC 控制台 → 路由表 → 添加路由条目:
- 目标网段:
100.210.10.0/24
- 下一跳类型:
ECS 实例
- 选择 MongoDB 服务器
- 目标网段:
- 反向路由(服务器到Pod):
- 目标网段:
100.210.20.0/24
- 下一跳:
Kubernetes Worker 节点
- 目标网段:
方案二:调整安全组规则
在 ECS 控制台 → 安全组 → 添加规则:
规则方向:入方向协议类型:TCP端口范围:27017(或MongoDB端口)授权对象:100.210.20.0/24 # Pod网段优先级:1(高优先级)
方案三:修改 MongoDB 绑定地址
在 MongoDB 服务器执行:
# 修改配置文件sudo nano /etc/mongod.conf
修改为:
net: bindIp: 0.0.0.0 # 监听所有地址
方案四:通过 Kubernetes Service 暴露
在 K8s 集群中创建 Service:
apiVersion: v1kind: Servicemetadata: name: mongo-externalspec: type: ExternalName externalName: mongodb-server.private # 服务器内网域名 ports: - port: 27017
Pod 通过 mongo-external:27017
访问
三、连通性测试方法
# 1. 在Pod中测试端口连通性kubectl exec -it <pod-name> -- telnet 100.210.10.x 27017# 2. 在服务器抓包验证sudo tcpdump -i any port 27017 and host 100.210.20.x
四、阿里云特殊配置建议
-
使用 VPC对等连接:
#mermaid-svg-cKRVG8BMqYsWGQYq {font-family:\"trebuchet ms\",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-cKRVG8BMqYsWGQYq .error-icon{fill:#552222;}#mermaid-svg-cKRVG8BMqYsWGQYq .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-cKRVG8BMqYsWGQYq .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-cKRVG8BMqYsWGQYq .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-cKRVG8BMqYsWGQYq .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-cKRVG8BMqYsWGQYq .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-cKRVG8BMqYsWGQYq .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-cKRVG8BMqYsWGQYq .marker{fill:#333333;stroke:#333333;}#mermaid-svg-cKRVG8BMqYsWGQYq .marker.cross{stroke:#333333;}#mermaid-svg-cKRVG8BMqYsWGQYq svg{font-family:\"trebuchet ms\",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-cKRVG8BMqYsWGQYq .label{font-family:\"trebuchet ms\",verdana,arial,sans-serif;color:#333;}#mermaid-svg-cKRVG8BMqYsWGQYq .cluster-label text{fill:#333;}#mermaid-svg-cKRVG8BMqYsWGQYq .cluster-label span{color:#333;}#mermaid-svg-cKRVG8BMqYsWGQYq .label text,#mermaid-svg-cKRVG8BMqYsWGQYq span{fill:#333;color:#333;}#mermaid-svg-cKRVG8BMqYsWGQYq .node rect,#mermaid-svg-cKRVG8BMqYsWGQYq .node circle,#mermaid-svg-cKRVG8BMqYsWGQYq .node ellipse,#mermaid-svg-cKRVG8BMqYsWGQYq .node polygon,#mermaid-svg-cKRVG8BMqYsWGQYq .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-cKRVG8BMqYsWGQYq .node .label{text-align:center;}#mermaid-svg-cKRVG8BMqYsWGQYq .node.clickable{cursor:pointer;}#mermaid-svg-cKRVG8BMqYsWGQYq .arrowheadPath{fill:#333333;}#mermaid-svg-cKRVG8BMqYsWGQYq .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-cKRVG8BMqYsWGQYq .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-cKRVG8BMqYsWGQYq .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-cKRVG8BMqYsWGQYq .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-cKRVG8BMqYsWGQYq .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-cKRVG8BMqYsWGQYq .cluster text{fill:#333;}#mermaid-svg-cKRVG8BMqYsWGQYq .cluster span{color:#333;}#mermaid-svg-cKRVG8BMqYsWGQYq div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\"trebuchet ms\",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-cKRVG8BMqYsWGQYq :root{--mermaid-font-family:\"trebuchet ms\",verdana,arial,sans-serif;}对等连接K8s VPCECS VPC
- 在专有网络控制台创建双向对等连接
-
启用 NAT网关:
- 为 Pod 配置 SNAT,使其使用 Worker 节点 IP 访问外部服务
重要提示:生产环境建议组合使用方案一 + 方案二,既保持网络隔离又保证连通性。修改后重启 MongoDB 服务生效。