通过 VPN 从 GKE Pod 流出的流量

Egress traffic from GKE Pod through VPN

我有一个 VPC 网络,其子网在 10.100.0.0/16 范围内,节点位于其中。有一个路由和防火墙规则应用于范围 10.180.102.0/23,它路由并允许来自 VPN 隧道的流量to/coming。

如果我在 10.100.0.0/16 范围内部署节点,我可以 ping 我在 10.180.102.0/23 范围内的设备。但是,该节点内的 pod 运行 无法 ping 通 10.180.102.0/23 范围内的设备。我认为这与 pods 生活在不同的 IP 范围 (10.12.0.0/14) 中有关。

如何配置我的网络,以便我可以 ping/communicate 使用 10.180.102.0/23 范围内的设备?

我想从 GKE 中 IP 地址的一些术语开始。

网络命名空间:基于MAN page,网络命名空间在逻辑上是网络堆栈的另一个副本,具有自己的路由、防火墙规则和网络设备.这个网络命名空间将节点的物理网络接口与 Pod 连接起来。此网络命名空间还连接到 Linux 网桥,允许同一节点上的 pods 之间的通信和外部通信。

Pod IP: 分配给 Pod 的 IP 地址,在集群创建期间可在 Pod 地址范围 选项中配置。 GKE 将此 IP 分配给 Pod 的网络命名空间中的虚拟网络接口,并路由到节点的物理网络接口,例如 eth0。

节点 IP: 分配给节点物理网络接口的 IP 地址为 eth0。此节点 IP 在网络命名空间上配置为与 pods.

通信

集群 IP: 已分配且在服务生命周期内保持稳定的 IP 地址。使用网络命名空间允许节点和外部网络之间的通信。

这是我的信息来源; GKE Network Overview 我在哪里也找到了这个注释:

Warning: Do not manually make changes to nodes because they are overridden by GKE, and your cluster may not function correctly. The only reason to access a node directly is to debug problems with your configuration.


然后,如果您希望在您的 GKE 集群和另一个网络之间建立通信,我建议您使用不同的服务:

外部负载均衡器 管理来自集群外部和 Google 云虚拟私有云 (VPC) 网络的流量。他们使用与 Google 云网络关联的转发规则将流量路由到 Kubernetes 节点。

内部负载均衡器 管理来自同一 VPC 网络的流量。与外部负载均衡器一样,它们使用与 Google 云网络关联的转发规则将流量路由到 Kubernetes 节点。

HTTP(S) 负载平衡器 是用于 HTTP(S) 流量的专用外部负载平衡器。他们使用 Ingress 资源而不是转发规则将流量路由到 Kubernetes 节点。

您可以在此 documentation 中找到有关不同服务的更多详细信息。

总的来说,pod 不能直接与外部资源通信。 您应该使用服务并将 pod 公开给服务。

我不太记得具体如何解决,但我发布了我必须帮助@tdensmore 的内容。

您必须编辑 ip-masq-agent(它是 GKE 上伪装 IP 的代理 运行ning),此配置负责让 pods 进入节点,到达 GCP VPC 网络的其他部分,更具体地说是 VPN。因此,它允许 pods 与可通过 VPN 访问的设备通信。

首先,我们将在 kube-system 命名空间内工作,我们将把配置 ip-masq-agent 的 configmap 放入 config 文件中:

nonMasqueradeCIDRs:
  - 10.12.0.0/14  # The IPv4 CIDR the cluster is using for Pods (required)
  - 10.100.0.0/16 # The IPv4 CIDR of the subnetwork the cluster is using for Nodes (optional, works without but I guess its better with it)
masqLinkLocal: false
resyncInterval: 60s

和运行kubectl create configmap ip-masq-agent --from-file config --namespace kube-system

之后,配置 ip-masq-agent,将其放入 ip-masq-agent.yml 文件中:

apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
  name: ip-masq-agent
  namespace: kube-system
spec:
  template:
    metadata:
      labels:
        k8s-app: ip-masq-agent
    spec:
      hostNetwork: true
      containers:
      - name: ip-masq-agent
        image: gcr.io/google-containers/ip-masq-agent-amd64:v2.4.1
        args:
            - --masq-chain=IP-MASQ
            # To non-masquerade reserved IP ranges by default, uncomment the line below.
            # - --nomasq-all-reserved-ranges
        securityContext:
          privileged: true
        volumeMounts:
          - name: config
            mountPath: /etc/config
      volumes:
        - name: config
          configMap:
            # Note this ConfigMap must be created in the same namespace as the daemon pods - this spec uses kube-system
            name: ip-masq-agent
            optional: true
            items:
              # The daemon looks for its config in a YAML file at /etc/config/ip-masq-agent
              - key: config
                path: ip-masq-agent
      tolerations:
      - effect: NoSchedule
        operator: Exists
      - effect: NoExecute
        operator: Exists
      - key: "CriticalAddonsOnly"
        operator: "Exists"

和运行kubectl -n kube-system apply -f ip-masq-agent.yml

注意:我已经很久没做这个了,这里有更多信息link:https://cloud.google.com/kubernetes-engine/docs/how-to/ip-masquerade-agent