首页 > 解决方案 > 谷歌 Kubernetes 引擎和 VPN

问题描述

我正在使用 Google Kubernetes Engine 部署一些需要连接到本地数据库的应用程序。为此,我配置了一个 VPN 隧道并创建了一个 VPC。

然后,我创建了一个使用该 VPC 的 GKE 集群(1 个节点),我可以通过连接到该节点并尝试 ping 数据库服务器来确认可以访问数据库

~ $ sudo toolbox ping 10.197.100.201
Spawning container root-gcr.io_google-containers_toolbox-20180309-00 on 
/var/lib/toolbox/root-gcr.io_google-containers_toolbox-20180309-00.
Press ^] three times within 1s to kill container.
PING 10.197.100.201 (10.197.100.201): 56 data bytes 
64 bytes from 10.197.100.201: icmp_seq=0 ttl=62 time=45.967 ms
64 bytes from 10.197.100.201: icmp_seq=1 ttl=62 time=44.186 ms`

但是,如果我尝试从 Pod 执行相同操作,我将无法连接。

root@one-shot-pod:/# traceroute 10.197.100.201
traceroute to 10.197.100.201 (10.197.100.201), 30 hops max, 60 byte 
packets
 1  10.0.0.1 (10.0.0.1)  0.046 ms  0.009 ms  0.007 ms
 2  * * *
 3  * * *```

我错过了什么?

标签: kubernetesgoogle-cloud-platformgoogle-kubernetes-enginegoogle-cloud-vpn

解决方案


经过一番调查,我找到了问题的根本原因。基本上,通信无法正常工作,因为有一种称为 ip masquerade ( https://cloud.google.com/kubernetes-engine/docs/how-to/ip-masquerade-agent ) 的东西用于 NAT 转换。

由于 GKE 有一些默认地址被配置为不伪装(在我使用的版本上,默认值为10.0.0.0/8172.16.0.0/12192.168.0.0/16在集群之外,解决方案是修改nonMasqueradeCIDRs并删除10.0.0.0/8和使用10.44.0.0/14(GKE 集群 CIDR)。

为了做到这一点,我使用了以下配置图:

apiVersion: v1
data:
  config: |-
    nonMasqueradeCIDRs:
      - 10.44.0.0/14
      - 172.16.0.0/12
      - 192.168.0.0/16
    resyncInterval: 60s
kind: ConfigMap
metadata:
  name: ip-masq-agent
  namespace: kube-system

之后,要应用配置,您可以使用以下命令上传 configmap:

kubectl create configmap ip-masq-agent --from-file <configmap file> --namespace kube-system

推荐阅读