簡體   English   中英

谷歌 Kubernetes 引擎和 VPN

[英]Google Kubernetes Engine & 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  * * *```

我錯過了什么?

經過一番調查,我找到了問題的根本原因。 基本上,通信無法正常工作,因為有一種稱為 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 )並且目標 IP 為 10.197 .100.201(10.0.0.0/8 的一部分)並且該 ip 在集群之外,解決方案是修改nonMasqueradeCIDRs並刪除10.0.0.0/8並改用10.44.0.0/14集群 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

之后,要應用配置,您可以使用以下命令上傳配置映射:

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

我在此博客中找到了解決方案

問題是默認的 iptables 配置如下所示:

iptables -A POSTROUTING ! -d 10.0.0.0/8 \
  -m comment --comment “kubenet: outbound traffic" -m addrtype \
  ! --dst-type LOCAL -j MASQUERADE -t nat

這意味着只有當目的地不在 10.0.0.0/8 中時,來自 Pod 的流量才會被 NAT 轉換到主機 IP。

這個 10.0.0.0/8 是問題所在:它太大了。

它還包括您的10.197.100.201 IP。

要解決此問題,您可以將以下 DaemonSet 添加到 Kubernetes 集群:

kind: DaemonSet
apiVersion: extensions/v1beta1
metadata:
  name: fix-nat
  labels:
    app: fix-nat
spec:
  template:
    metadata:
      labels:
        app: fix-nat
    spec:
      hostPID: true
      containers:
        - name: fix-nat
          image: gcr.io/google-containers/startup-script:v1
          imagePullPolicy: Always
          securityContext:
            privileged: true
          env:
          - name: STARTUP_SCRIPT
            value: |
              #! /bin/bash
              while true; do
                iptables-save | grep MASQUERADE | grep -q "NAT-VPN"
                if [ $? -ne 0 ]; then
                  echo "Missing NAT rule for VPN, adding it"
                  iptables -A POSTROUTING -d 10.197.100.0/24 -m comment --comment "NAT-VPN: SNAT for outbound traffic through VPN" -m addrtype ! --dst-type LOCAL -j MASQUERADE -t nat
                fi
                sleep 60
              done

這個小腳本將每分鍾檢查一次,永遠檢查我們是否有正確的 iptables 規則,如果沒有,則添加它。

請注意, pod 需要特權:true 才能從主機更改 iptables 規則。

我有同樣的問題,這解決了這個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM