【问题标题】:Access a Node Port Service in a private GKE Cluster from another GKE private cluster从另一个 GKE 私有集群访问私有 GKE 集群中的节点端口服务
【发布时间】:2020-03-26 00:57:53
【问题描述】:

我使用的是 Google 云,我有两个 GKE 私有集群。

其中一个包含一些作为 nodePort 安装的服务。另一个集群需要连接到这个集群并访问暴露的服务。

服务暴露的集群只有一个节点具有私有 IP。我可以使用这个私有 IP 从另一个集群成功地 ping 这个节点。

但是我如何才能访问这些服务?

我也尝试配置一些防火墙规则,但没有成功。

【问题讨论】:

标签: google-kubernetes-engine firewall


【解决方案1】:

请看下面的示例,该示例显示了如何在两个私有 GKE 集群之间建立与服务 (NodePort) 的连接:

本示例将使用两个 GKE 集群:

  • gke-private-cluster-main - 这将是带有简单 hello-app 的集群
  • gke-private-cluster-europe - 此集群将能够与主集群通信

为简化起见,所有集群将只有一个节点。

gke-private-cluster-main 上创建部署和服务

下面是一个简单的hello-app 示例和一个将在端口30051 上公开hello-app 的服务:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello
spec:
  selector:
    matchLabels:
      app: hello
      version: 1.0.0
  replicas: 3
  template:
    metadata:
      labels:
        app: hello
        version: 1.0.0
    spec:
      containers:
      - name: hello
        image: "gcr.io/google-samples/hello-app:1.0"
        env:
        - name: "PORT"
          value: "50001"
---
apiVersion: v1
kind: Service
metadata:
  name: hello-service
spec:
  selector:
    app: hello
  ports:
    - name: hello-port
      port: 50001
      targetPort: 50001
      nodePort: 30051
  type: NodePort

应用它并检查生成此 pod 的节点的 内部 IP 地址。您可以使用以下任一方法进行检查:

  • GCP -> Compute Engine -> VM Instances
  • kubectl get nodes -o wide

在我的例子中是10.156.0.2

尝试从gke-private-cluster-europe访问它

您可以通过 SSH 连接到 gke-private-cluster-europe 的节点并尝试从节点调用命令: curl 10.156.0.2:30051。您应该能够与该服务进行通信并获得如下输出:

Hello, world!
Version: 1.0.0
Hostname: hello-5d79ccdb55-vrrbs

要从 pod 内部检查连接,您需要一个已经内置 curl 的图像。互联网是各种很棒的东西的地方,实际上有一个可用 curl 的图像。您可以使用以下 YAML 生成带有 curl 的 pod:

apiVersion: v1
kind: Pod
metadata:
  name: curl
  namespace: default
spec:
  containers:
  - image: curlimages/curl
    command:
      - sleep
      - "infinity"
    imagePullPolicy: IfNotPresent
    name: curl
  restartPolicy: Always

应用上面的YAML后,你可以exec进入pod并使用以下命令检查自己:

  • $ kubectl exec -it curl -- /bin/sh
  • $ curl 10.156.0.2:30051

集群内部的输出将如下所示:

curl: (28) Failed to connect to 10.156.0.2 port 30051: Operation timed out

它适用于节点,但不适用于 pod。

允许流量:

要允许上述网络连接,您需要:

  • 打开Google Cloud Platform
    • 检查gke-private-cluster-main 节点的网络标记
      • 转到Compute Engine
      • 找到gke-private-cluster-main的节点
      • 点击获取更多详情
      • 复制应该类似于:gke-gke-private-cluster-main-80fe50b2-node 的网络标记
    • 检查gke-private-cluster-europepod地址范围
      • 转到Kubernetes Engine
      • 找到您的gke-private-cluster-europe
      • 点击获取更多详情
      • 复制 pod 地址范围,应类似于:10.24.0.0/14

复制网络标签和 pod 范围后,您可以创建防火墙规则。请前往:

VPC Network -> Firewall rules -> Create a firewall rule

请仔细查看使用网络标签和 pod range ip 的部分,因为它会因您而异。

应用它并再次检查 gke-private-cluster-europe 中的 pod 是否可以访问 10.156.0.2:30051

它应该给你下面的输出:

Hello, world!
Version: 1.0.0
Hostname: hello-5d79ccdb55-6s8xh

如果您对此有任何疑问,请告诉我。

【讨论】:

    猜你喜欢
    • 2019-08-10
    • 2020-01-30
    • 2020-04-22
    • 2019-04-05
    • 1970-01-01
    • 1970-01-01
    • 2020-04-15
    • 1970-01-01
    • 2022-06-12
    相关资源
    最近更新 更多