You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Minikube中前端与后端Pod连接故障排查求助

问题描述

在Minikube部署前后端应用时遇到连接问题:

  • 前后端Deployment均配置了ClusterIP和NodePort服务,通过minikube service frontend-entrypoint在浏览器访问前端时,前端请求后端URL http://fastapi-cluster-ip-service:8000/api/v1/baseline/building_type?building_type=commercial 返回 (failed)net::ERR_EMPTY_RESPONSE。
  • 但进入前端Pod内部执行 curl -X GET "http://fastapi-cluster-ip-service:8000/api/v1/baseline/building_type?building_type=commercial" 能得到预期结果。
  • 尝试过Ingress或LoadBalancer暴露集群但未成功,目前回退到NodePort方案。

相关配置文件如下:

component_backend.yaml

apiVersion: v1
kind: Service
metadata:
  name: backend-entrypoint
spec:
  selector:
    component: fastapi
  ports:
  - name: http2
    port: 8000
    targetPort: 8000
  type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: fastapi-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: fastapi
  template:
    metadata:
      labels:
        component: fastapi
    spec:
      containers:
      - name: fastapi-container
        image: xxx/yyy:zzz
        ports:
            - containerPort: 8000
        env:
          - name: DB_USERNAME
            valueFrom:
              configMapKeyRef:
                name: app-variables
                key: DB_USERNAME
          [...]
      imagePullSecrets:
      - name: myregistrykey
---
apiVersion: v1
kind: Service
metadata:
  name: fastapi-cluster-ip-service
spec:
  type: LoadBalancer
  selector:
    component: fastapi
  ports:
    - port: 8000
      targetPort: 8000
  externalIPs:
    - <minikube ip>

componente_frontend.yaml

apiVersion: v1
kind: Service
metadata:
  name: frontend-entrypoint
spec:
  selector:
    component: react
  ports:
  - name: http1
    port: 3000
    targetPort: 3000
  type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: react-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: react
  template:
    metadata:
      labels:
        app: react
    spec:
      containers:
      - name: react-container
        image: xxx/yyy:zzz
        ports:
        - containerPort: 3000
        env:
          - name: BASELINE_API_URL
            valueFrom:
              configMapKeyRef:
                name: app-variables
                key: BASELINE_API_URL
      imagePullSecrets:
      - name: myregistrykey
---
apiVersion: v1
kind: Service
metadata:
  name: react-cluster-ip-service
spec:
  type: LoadBalancer
  selector:
    component: react
  ports:
    - port: 3000
      targetPort: 3000
  externalIPs:
    - <minikube ip>

BASELINE_API_URL 配置为后端ClusterIP服务名称 fastapi-cluster-ip-service。

ingress_service.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-service
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  rules:
  - host: sfs.baseline
    http:
      paths:
        - path: /
          pathType: Prefix
          backend:
            service:
              name: frontend-entrypoint
              port:
                name: http1        
        - path: /api
          pathType: Prefix
          backend:
            service:
              name: backend-entrypoint
              port:
                name: http2
问题分析
  • 核心原因:浏览器运行前端代码时,请求从本地机器发起,不属于Kubernetes集群内部网络,无法解析fastapi-cluster-ip-service这个集群专属的服务域名。而前端Pod处于集群网络内,Kubernetes的CoreDNS能正常解析服务名,因此curl请求成功。
  • 配置错误:前端react-cluster-ip-service的selector为component: react,但前端Pod的标签是app: react,标签不匹配导致服务无法关联到Pod,这也是之前LoadBalancer方案失败的原因之一;后端fastapi-cluster-ip-service设为LoadBalancer类型并指定externalIPs,但Minikube中LoadBalancer需通过minikube tunnel才能正常暴露,直接配置externalIPs无法正常工作。
解决方案

方案1:使用Ingress统一暴露(推荐)

  1. 启用Minikube Ingress插件:
minikube addons enable ingress
  1. 修正标签匹配问题:将前端Deployment的Pod标签改为component: react,或把react-cluster-ip-service的selector改为app: react,确保服务能关联到Pod。
  2. 调整Ingress配置(若后端API无需路径重写,可移除rewrite-target注释):
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-service
spec:
  rules:
  - host: sfs.baseline
    http:
      paths:
        - path: /
          pathType: Prefix
          backend:
            service:
              name: frontend-entrypoint
              port:
                name: http1        
        - path: /api
          pathType: Prefix
          backend:
            service:
              name: backend-entrypoint
              port:
                name: http2
  1. 修改BASELINE_API_URL为相对路径/api,前端请求会自动发送到当前域名的/api路径,由Ingress转发至后端服务。
  2. 在本地hosts文件添加映射:
<minikube ip> sfs.baseline

替换<minikube ip>为minikube ip命令获取的地址。
6. 重新部署所有配置后,浏览器访问http://sfs.baseline即可实现前后端正常通信。

方案2:使用后端NodePort服务的外部地址

  1. 获取后端NodePort服务的外部访问URL:
minikube service backend-entrypoint --url
  1. 修改BASELINE_API_URL为该URL(格式如http://<minikube ip>:<nodePort>),浏览器从本地发起请求时可直接访问后端服务。

方案3:修正LoadBalancer服务配置

  1. 修正前端服务的标签匹配问题:将react-cluster-ip-service的selector改为app: react,或把前端Pod标签改为component: react。
  2. 执行minikube tunnel命令,创建本地到集群的网络隧道,使LoadBalancer类型服务可被外部访问。
  3. 修改BASELINE_API_URL为后端LoadBalancer服务的外部地址(即<minikube ip>:8000)。

内容的提问来源于stack exchange,提问作者Anxious-Individual81

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 02:06:04