【问题标题】:Vanilla JS ERR_NAME_NOT_RESOLVED on minikube clusterminikube 集群上的 Vanilla JS ERR_NAME_NOT_RESOLVED
【发布时间】:2021-11-05 21:16:06
【问题描述】:

我在本地 minikube 集群上运行了一个最小的 Web 应用程序。 后端在/be/test/hi 公开一个API,服务名称也是。 当我从前端向后端发送 GET 请求时,我得到:

main.js:15 GET http://be/be/test/hi net::ERR_NAME_NOT_RESOLVED

如果我从前端 POD 运行 nslookup be,我会得到正确的 dns 分辨率,并运行 curl be/be/test/hi 我从后端得到了正确的响应(一个简单的“Hello world”字符串)

我错过了什么?

后端.yaml

apiVersion: v1
kind: Service
metadata:
  name: be
spec:
  ports:
    - port: 80
      targetPort: 8080
  selector:
    app: be
  type: LoadBalancer
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: be
spec:
  replicas: 1
  selector:
    matchLabels:
      app: be
  template:
    metadata:
      labels:
        app: be
    spec:
      containers:
      - name: be
        image: kubebe
        imagePullPolicy: Never
        ports:
          - containerPort: 8080

前端.yaml

apiVersion: v1
kind: Service
metadata:
  name: fe
spec:
  ports:
    - port: 80
      targetPort: 8080
  selector:
    app: fe
  type: LoadBalancer
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: frontend
spec:
  replicas: 1
  selector:
    matchLabels:
      app: fe
  template:
    metadata:
      labels:
        app: fe
    spec:
      containers:
      - name: fe
        image: kubefe
        imagePullPolicy: Never
        ports:
          - containerPort: 8080

main.js

const URL="http://be/be/test/hi"

  function httpGetAsync(){
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.onreadystatechange = function() { 
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
            console.log(xmlHttp.responseText);
    }
    xmlHttp.open("GET", URL, true); 
    xmlHttp.send(null);
}

前端 Dockerfile(kubefe 镜像):

FROM centos/httpd-24-centos7:2.4
RUN mkdir -p /var/www/html/fe
COPY ./index.html ./main.js /var/www/html/fe/

编辑:我已经解决了我的问题,但我认为实际问题仍未得到解答。

为了解决这个问题,我简单地从我的 url 中删除了协议和主机,并在 /etc/httpd/conf.d/default-site.conf 中设置了代理规则

默认站点.conf

<VirtualHost *:8080> 
  ProxyPreserveHost Off
  ProxyRequests Off
  ProxyPass /be/ http://be/be/
</VirtualHost> 

【问题讨论】:

    标签: javascript docker apache kubernetes minikube


    【解决方案1】:

    您的前端代码 (javascript) 在您的浏览器中执行,而不是在 kubernetes 集群 (nginx pod) 中执行

    这里有 3 个选项,

    1. 为您的前端创建一个 NodePort 服务
    2. 依靠 kubeproxy 进行端口转发
    3. 创建入口

    【讨论】:

    • 最后我为 apache pod 设置了代理规则,我明白了你的意思,事实上,我错过了这个概念。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-23
    相关资源
    最近更新 更多