如何使用无服务器 VPC 访问云功能

How to use Serverless VPC access for Cloud functions

我已经使用示例 Spring 引导应用程序创建了一个 Kubernetes 集群,它在 public ip 上运行良好。现在我想在 Kubernetes 集群中访问 Spring 启动的终点。我已经按照 Google 中的教程进行配置无服务器 VPC 访问。 (https://cloud.google.com/vpc/docs/configure-serverless-vpc-access?hl=bg)。我已经创建了无服务器 VPC 访问并用于云功能之一。

现在我的问题是,如何从我的云功能连接 Kubernetes 集群的内部 ip?。我用 Go 写过代码。

package p

import (
    "fmt"
    "io/ioutil"
    "net/http"
)

func HelloWorld(w http.ResponseWriter, r *http.Request) {
    fmt.Println("Starting the application...")

    response, err := http.Get("http://10.59.247.177:47002/")
    if err != nil {
        fmt.Fprint(w, "The HTTP request failed with error %s\n", err)
    } else {
        data, _ := ioutil.ReadAll(response.Body)
        fmt.Fprint(w, string(data))
    }
}

但我收到错误消息:HTTP 请求失败,错误为 %s 获取http://10.59.247.177:47002/:拨打tcp 10.59.247.177:47002:i/o超时

默认情况下,Kubernetes 服务在 Kubernetes 集群内部。您必须公开服务,以便来自 Kubernetes 外部的应用程序可以连接到它。

Kubernetes 中主要有 3 种方式暴露服务:

  1. Public 负载均衡器。服务暴露在互联网上。
  2. 内部负载平衡器。服务在 VPC 和区域内部公开。
  3. 节点端口。服务暴露在 Kube 节点 IP 地址上的某个高端口号上。这使得服务在 VPC 内部和区域之间可见。

在此处阅读更多内容https://kubernetes.io/docs/concepts/services-networking/service/#publishing-services-service-types and here https://cloud.google.com/kubernetes-engine/docs/tutorials/http-balancer