【问题标题】:Unable to connect to www.googleapis.com from GKE无法从 GKE 连接到 www.googleapis.com
【发布时间】:2019-12-31 18:48:48
【问题描述】:

我有一个在我的 GKE 集群中运行的应用程序需要访问 www.googleapis.com。我还利用网络策略来增强安全性。

在默认拒绝所有出口流量的情况下,我自然无法连接到www.googleapis.com。我得到了错误

INFO 0827 14:33:53.313241 retry_util.py] Retrying request, attempt #3...
DEBUG 0827 14:33:53.313862 http_wrapper.py] Caught socket error, retrying: timed out
DEBUG 0827 14:33:53.314035 http_wrapper.py] Retrying request to url https://www.googleapis.com/storage/v1/b?project=development&projection=noAcl&key=AIzaSyDnac<key>bmJM&fields=nextPageToken%2Citems%2Fid&alt=json&maxResults=1000 after exception timed out

发现主机名www.googleapis.com对应IP216.58.207.36

所以我继续在我的网络策略中创建了一个出口条目

  spec:
    egress:
    - ports:
      - port: 443
        protocol: TCP
      to:
      - ipBlock:
          cidr: 216.58.207.36/32

现在从 Pod 中,我可以远程登录这个端点了

$ telnet googleapis.com 443
Trying 216.58.207.36...
Connected to googleapis.com.
Escape character is '^]'.

但由于某种原因,我仍然遇到同样的错误

INFO 0827 14:36:15.767508 retry_util.py] Retrying request, attempt #5...
DEBUG 0827 14:36:15.768018 http_wrapper.py] Caught socket error, retrying: timed out
DEBUG 0827 14:36:15.768128 http_wrapper.py] Retrying request to url https://www.googleapis.com/storage/v1/b?project=development&projection=noAcl&key=AIzaSyDnac<key>bmJM&fields=nextPageToken%2Citems%2Fid&alt=json&maxResults=1000 after exception timed out

但是,如果我删除网络策略,我可以连接

INFO 0827 14:40:24.177456 base_api.py] Body: (none)
INFO 0827 14:40:24.177595 transport.py] Attempting refresh to obtain initial access_token
WARNING 0827 14:40:24.177864 multiprocess_file_storage.py] Credentials file could not be loaded, will ignore and overwrite.
DEBUG 0827 14:40:24.177957 multiprocess_file_storage.py] Read credential file
WARNING 0827 14:40:24.178036 multiprocess_file_storage.py] Credentials file could not be loaded, will ignore and overwrite.
DEBUG 0827 14:40:24.178090 multiprocess_file_storage.py] Read credential file
WARNING 0827 14:40:24.356631 multiprocess_file_storage.py] Credentials file could not be loaded, will ignore and overwrite.
DEBUG 0827 14:40:24.356972 multiprocess_file_storage.py] Read credential file
DEBUG 0827 14:40:24.357510 multiprocess_file_storage.py] Wrote credential file /var/lib/jenkins/.gsutil/credstore2.
connect: (www.googleapis.com, 443)
send: 'GET /storage/v1/b?project=development&fields=nextPageToken%2Citems%2Fid&alt=json&projection=noAcl&maxResults=1000 HTTP/1.1\r\nHost: www.googleapis.com\r\ncontent-length: 0\r\nauthorization: REDACTED

我的网络策略默认允许所有入口流量

ingress:
- {}
podSelector: {}

知道我在这里可能缺少什么吗?在这种情况下,我需要将其他 IP 地址列入白名单吗?

编辑

当网络策略到位时,我使用curl 进行了测试,我得到了

*   Trying 2a00:1450:4001:80b::200a...
* TCP_NODELAY set
* Immediate connect fail for 2a00:1450:4001:80b::200a: Cannot assign requested address
*   Trying 2a00:1450:4001:80b::200a...
* TCP_NODELAY set
* Immediate connect fail for 2a00:1450:4001:80b::200a: Cannot assign requested address
*   Trying 2a00:1450:4001:80b::200a...
* TCP_NODELAY set
* Immediate connect fail for 2a00:1450:4001:80b::200a: Cannot assign requested address
*   Trying 2a00:1450:4001:80b::200a...
* TCP_NODELAY set
* Immediate connect fail for 2a00:1450:4001:80b::200a: Cannot assign requested address
*   Trying 2a00:1450:4001:80b::200a...
* TCP_NODELAY set
* Immediate connect fail for 2a00:1450:4001:80b::200a: Cannot assign requested address
*   Trying 2a00:1450:4001:80b::200a...
* TCP_NODELAY set
* Immediate connect fail for 2a00:1450:4001:80b::200a: Cannot assign requested address

删除网络策略时不会发生这种情况。

【问题讨论】:

  • www.googleapis.com 不对应 IP 172.217.13.109 - IP 会根据位置、时间等变化。您不能指望它保持不变。
  • 请张贴全网政策yaml
  • 如果您从 5 个不同的位置执行 nslookup googleapis.com,您最终将有 30 个 IP 地址列入白名单,即使这样,明天也不一定有效。你需要一种不同的方法,比如 Istio。

标签: kubernetes google-cloud-platform google-api kubernetes-networkpolicy


【解决方案1】:

@mensi 的评论是正确的,www.googleapis.com 后面有多个 IP。例如,您可以看到,通过多次 ping URL,您很可能每次都获得不同的 IP。

最简单的解决方案是默认允许所有出口:

spec:
  podSelector: {}
  egress:
  - {}
  policyTypes:
  - Egress

您也可以尝试允许所有 Google API 的公共 IP 范围,但由于 Google 似乎没有发布这些范围的列表(仅受限的.googleapis.com 和 private.googleapis.com here),因此可能会更难一些。

【讨论】:

    猜你喜欢
    • 2019-06-11
    • 1970-01-01
    • 2016-01-31
    • 1970-01-01
    • 2021-06-24
    • 2019-08-16
    • 2022-01-18
    • 2021-10-07
    相关资源
    最近更新 更多