简体   繁体   English

Pod 未与 GKE 中的服务通信

[英]Pod not communicating with service in GKE

On a GKE cluster, I have client and server pods, with a client service and a server service.在 GKE 集群上,我有客户端和服务器 pod,带有客户端服务和服务器服务。

My server service is:我的服务器服务是:

apiVersion: v1
kind: Service
metadata:
  name: server-cluster-ip-service
spec:
  type: ClusterIP
  selector:
    app: server-deployment
  ports:
    - port: 5000
      targetPort: 5000

When I access the client pod shell and run当我访问客户端 pod shell 并运行时

nslookup server-cluster-ip-service

I get我得到

Server:         server-IP
Address:        server-IP-Address

Name:   server-cluster-ip-service.default.svc.cluster.local
Address: IPAddress

** server can't find server-cluster-ip-service.svc.cluster.local: NXDOMAIN

** server can't find server-cluster-ip-service.cluster.local: NXDOMAIN


** server can't find server-cluster-ip-service.svc.cluster.local: NXDOMAIN
** server can't find server-cluster-ip-service.cluster.local: NXDOMAIN

** server can't find server-cluster-ip-service.us-central1-c.c.my-cluster.internal: NXDOMAIN

** server can't find server-cluster-ip-service.google.internal: NXDOMAIN

** server can't find server-cluster-ip-service.us-central1-c.c.my-cluster: NXDOMAIN

** server can't find server-cluster-ip-service.google.internal: NXDOMAIN

** server can't find server-cluster-ip-service.c.my-cluster.internal: NXDOMAIN
** server can't find server-cluster-ip-service.c.my-cluster.internal: NXDOMAIN

The service is running on port 5000, because when I set up a pod with busybox, I can curl from that pod like so:该服务在端口 5000 上运行,因为当我使用 busybox 设置一个 pod 时,我可以像这样从那个 pod 卷曲:

curl server-cluster-ip-service:5000

And it returns json from my server.它从我的服务器返回 json。

After experimenting with what address to put in the fetch request in my client code, the only way I can get a 200 response is with this:在尝试在我的客户端代码中放入获取请求的地址后,我获得 200 响应的唯一方法是:

const getAllUsers = async () => {
    console.log("GETTING ALL USERS");
    const response = await fetch("server-cluster-ip-service.default.svc.cluster.local", {
  mode: 'cors',
  headers: {
    'Access-Control-Allow-Origin':'*'
  }
});
    const resp = await response
    console.log("RESPONSE", resp)
    const json = await response.json();

    setUsers(json);
  };

在此处输入图片说明

which returns not json and not apparently on port 5000,它返回的不是 json 并且显然不在端口 5000 上,

whereas all attempts to query the service at port 5000 fail.而在端口 5000 查询服务的所有尝试都失败了。

I have this running locally and it works fine.我在本地运行它,它工作正常。 I have ruled out arm processor issues with my mac by building and pushing docker images in GKE from the cloud console.通过从云控制台在 GKE 中构建和推送 docker 图像,我已经排除了我的 mac 的 arm 处理器问题。 I am fairly confident this is a GKE issue, because the dns works locally, but why would it not work with GKE?我相当有信心这是一个 GKE 问题,因为 dns 在本地工作,但为什么它不能与 GKE 一起工作? I don't have any network policies I've set myself - could there be a node security group blocking access?我没有自己设置的任何网络策略 - 是否有节点安全组阻止访问? I read about "shielding" as a node security policy configured at setup, but I don't know how to check if that's been configured?我读到“屏蔽”作为在设置时配置的节点安全策略,但我不知道如何检查是否已配置?

Complete code below:完整代码如下:

My server code is:我的服务器代码是:

const express = require("express");
const bodyParser = require("body-parser");

var cors = require("cors");

const PORT = 5000;

const app = express();
app.use(cors());

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(express.static("public"));

app.listen(PORT, function () {
  console.log("listening on 5000");
});

app.get("/", (req, res) => {
  console.log("PROCESSING GET USERS REQUEST");
  const list = ["item1", "item2", "item3"];
  res.json(list);
});

My client code is:我的客户端代码是:

import { useState, useEffect } from "react";

import "./editUser.css";

    function EditUser() {
      const [users, setUsers] = useState([]);
    
      const getAllUsers = async () => {
        console.log("GETTING ALL USERS");
        const response = await fetch("http://server-cluster-ip-service:5000");
        const json = await response.json();
    
        setUsers(json);
      };
    
      useEffect(() => {
        getAllUsers();
      }, []);
    
      return (
        <div className="App">
          <h1 data-position="header">Merr k8s testbed</h1>
    
          <section data-position="quotes">
            <h2>Console</h2>
            <ul>
              {users &&
                users.map((user) => (
                  <li>
                    <h3>{user}</h3>
                  </li>
                ))}
            </ul>
          </section>
        </div>
      );
    }
    
    export default EditUser;

My client-deployment.yaml is:我的 client-deployment.yaml 是:

kind: Deployment
metadata:
  name: client-deployment
  labels:
    app: client-deployment
    component: web
spec:
  replicas: 3
  selector:
    matchLabels:
      app: client-deployment
  template:
    metadata: 
      labels:
        app: client-deployment
        component: web
    spec:
      containers:
        - name: client
          image: myDocker/k8s-client:latest
          ports:
            - containerPort: 3000

My server-deployment.yaml is:我的 server-deployment.yaml 是:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: server-deployment
  labels:
    app: server-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: server-deployment
  template:
    metadata:
      labels:
        app: server-deployment
    spec:
      containers:
        - name: server
          image: myDocker/k8s-server:latest
          ports:
            - containerPort: 5000

My client-cluster-ip-service.yaml is:我的 client-cluster-ip-service.yaml 是:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: server-deployment
  labels:
    app: server-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: server-deployment
  template:
    metadata:
      labels:
        app: server-deployment
    spec:
      containers:
        - name: server
          image: myDocker/k8s-server:latest
          ports:
            - containerPort: 5000

My server-cluster-ip-service.yaml is:我的 server-cluster-ip-service.yaml 是:

apiVersion: v1
kind: Service
metadata:
  name: server-cluster-ip-service
spec:
  type: ClusterIP
  selector:
    app: server-deployment
  ports:
    - port: 5000
      targetPort: 5000

I was able to see that you and jabbson concluded that probably the issue is with React.我能够看到您和 jabbson 得出的结论是问题可能出在 React 上。 Just in case let me share with you that, a common root cause for this kind of issue is that the DNS inside Busybox does not work properly, due to the version (I cannot see in the screenshots and the code, the version that you are using).以防万一让我与您分享,此类问题的一个常见根本原因是 Busybox 中的 DNS 无法正常工作,由于版本(我无法在屏幕截图和代码中看到您使用的版本)使用)。 Most of the cases work with busybox images 1.28.4, empirically talking.根据经验,大多数情况下使用 busybox 图像 1.28.4。 You can try using that version.您可以尝试使用该版本。

You can use the following URL's thread as reference dns can't resolve kubernetes.default and/or cluster.local您可以使用以下 URL 的线程作为参考dns can't resolve kubernetes.default 和/或 cluster.local

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM