【问题标题】:connection refused from frontend to backend in k8s clusterk8s集群中从前端到后端的连接被拒绝
【发布时间】:2021-03-16 09:45:09
【问题描述】:

我在 Kubernetes 中部署了一个应用程序(正面:Angular,背面:Spring)。我有 3 个 pod:正面、背面、数据库(Mongo)。我的 pod 运行正常,并且后面的 pod 已成功连接到 Mongo pod。我使用 NGINX 对 Angular 应用程序进行了 docker 化。

现在,当我访问三个 pod 的日志时,我没有任何问题,即使是 spring 应用程序也已成功启动。 O 甚至使用 curl x Post, get... 从前端 pod 测试 API 服务,一切正常。

但是在我的浏览器中,没有任何效果。它显示 'connection to http//:localhost:8080/api refused' !!
我在我的 Angular 项目中删除了任何 'localhost' 单词,并将它们全部替换为后面的服务名称,但仍然告诉我 'cannot connect to localhost'!!!

这里是services.yaml

apiVersion: v1
kind: Service
metadata:
  name: front-service
  labels:
    app: front
spec:
  ports:
  - port: 80
    targetPort: 80
    protocol: TCP
    name: http
  selector:
    app: front
  type: NodePort
---
apiVersion: v1
kind: Service
metadata:
  name: back-service
  labels:
    app: back
spec:
  ports:
  - port: 8080
    targetPort: 8080
    protocol: TCP
    name: http
  selector:
    app: back
  type: ClusterIP

Dockerfile 的 Angular:

   # Stage 0, "build-stage", based on Node.js, to build and compile the frontend
FROM node:10.8.0 as build-stage
WORKDIR /app
RUN rm -rf node_modules
RUN npm cache clean --force 
RUN npm install
COPY package*.json /app/
COPY ./ /app/
ARG configuration=production
RUN npm run build -- --output-path=./dist/parkingangular 

# Stage 1, based on Nginx, to have only the compiled app, ready for production with Nginx
FROM nginx:1.15
#Copy ci-dashboard-dist
COPY --from=build-stage /app/dist/parkingangular/ /usr/share/nginx/html
#Copy default nginx configuration
RUN rm -rf /etc/nginx/conf.d/default.conf
COPY ./nginx.conf /etc/nginx/conf.d
EXPOSE 80

nginx.conf:

upstream back-service {
    server back-service:8080;
}

server {
  listen 80;
  server_name localhost;
  location / {
      root /usr/share/nginx/html;
      index index.html index.htm;
    }
  location /api {
     proxy_pass http://back-service;
    }
}

get pods:

NAME                        READY   STATUS    RESTARTS   AGE
backend-755c4bd78d-r4gk4    1/1     Running   0          125m
backend-755c4bd78d-z2pjt    1/1     Running   0          125m
frontend-546f8db776-62srl   1/1     Running   0          116m
frontend-546f8db776-jngt8   1/1     Running   0          116m
mongodb-66689f754d-hn5tm    1/1     Running   1          2d7h

kubectl get svc:

NAME            TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
back-service    ClusterIP   10.108.235.196   <none>        8080/TCP       5d3h
front-service   NodePort    10.103.234.24    <none>        80:32594/TCP   5d4h
kubernetes      ClusterIP   10.96.0.1        <none>        443/TCP        5d7h
mongoservice    ClusterIP   10.109.235.5     <none>        27017/TCP      2d8h

我什至让 npm clear cache 和 npm clean 但徒劳无功!!

【问题讨论】:

  • 使用 nginx ingree 的入口解决了问题 :) 谢谢大家

标签: angular docker nginx kubernetes


【解决方案1】:

您在使用“localhost”时是否已将端口转发到本地计算机?

kubectl port-forward -n <NAMESPACE> <POD> 8080:8080

不确定在您的 localhost:8080 上什么应用程序会准确回答。 您可能需要转发到另一个本地端口。

或者你可以使用任何kubernetes主机的ip地址并加入服务端口。

【讨论】:

  • 感谢您回答我!背面是一个运行在 8080 上的 spring 应用程序,前面是在 nginx 中使用端口 80 进行 angular dockerized
  • 我在window的docker桌面使用kubernetes,所以k8s的ip地址总是localhost
  • 这是我将端口转发到后 pod 时得到的结果:kubectl port-forward backend-598ffc77db-5btvr 8080:8080 Forwarding from 127.0.0.1:8080 -> 8080 Forwarding from [:: 1]:8080 -> 8080
  • 使用 nginx ingree 的入口解决了问题 :) 谢谢大家
【解决方案2】:

使用 nginx ingress 解决了一个问题 :) 我使用了 nginx 控制器: 首先下载两个 nginx 控制器 yaml 文件,然后创建 ingress.yaml 并将路径设置为字体和返回:

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.30.0/deploy/static/mandatory.yaml

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.30.0/deploy/static/provider/cloud-generic.yaml

ingress.yaml:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
    name: frontend-ingress
spec:
    rules:
    - http:
        paths:
        - path: /api
          backend:
            serviceName: back-service
            servicePort: 8080
        - path: /
          backend:
            serviceName: front-service
            servicePort: 80

【讨论】:

    猜你喜欢
    • 2020-07-14
    • 2020-07-27
    • 2021-02-25
    • 1970-01-01
    • 2021-08-29
    • 1970-01-01
    • 2021-01-18
    • 2014-12-26
    • 1970-01-01
    相关资源
    最近更新 更多