Kubernetes docker 容器中的前端 Vue.js 应用程序无法连接到后端

2023-11-29

I have built a front-end Vue.js application, running on a docker container under kubernetes environment. the backend is also in the same kubernetes cluster (I am using Minikube for the project). When running it gets error net::ERR_NAME_NOT_RESOLVED when connecting to back-end containers: enter image description here

在容器内部,使用curl连接到后端没有问题:

$ kubectl exec -it deployment/hpl-browser-deployment -- sh
/ # curl http://hpl-manager-service:2354
{
  "message": "Manager status", 
  "state": "IDLE"
}

I used axios对于 API 服务:

import axios from 'axios';

export default class APIService {
  API_URL = '';

  constructor(apiAddress) {
    this.API_URL = apiAddress;
  }

  async get() {
    console.log('ApiService: get()');
    try {
      const response = await axios.get(this.API_URL);
      console.log(`ApiService: get result: ${response.data}`);
      return response.data;
    } catch (error) {
      console.error(error);
      return error;
    }
  }

  async postPlainText(data) {
    console.log(`ApiService: post() - data: ${data}`);
    try {
      const response = await axios.post(this.API_URL,
        data,
        {
          headers: {
            'Content-Type': 'text/plain',
            Accept: '*/*',
          },
        });
      console.log(`ApiService: post result: ${response.data}`);
      return response.data;
    } catch (error) {
      console.error(error);
      return error;
    }
  }
}

当我端口转发后端服务并连接到时,应用程序在开发环境上运行没有问题http://localhost:2354.

我想知道什么可能会导致这个问题?


您的前端 vue.js 应用程序仅托管在容器中。该应用程序实际上是从客户端的浏览器运行的。作为 API 的后端也需要可供客户端的浏览器访问。前端和后端的通信不经过前端的容器,而是直接从客户端到后端。

在这种情况下,不使用/不需要前端容器和后端容器之间的连接,因为在响应客户端之前,您没有从前端容器渲染任何内容。如果您使用的是服务器端渲染技术,例如 PHP、Django、.net、Nodejs 等,您需要连接到后端来获取一些数据并渲染一些内容,然后再回复客户端,那么之间的连接前端容器和后端容器是相关的。

您当前的设置与在 CDN 上托管应用程序/代码并访问单独服务(公开可用)上托管的 API 没有什么不同。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Kubernetes docker 容器中的前端 Vue.js 应用程序无法连接到后端 的相关文章

随机推荐