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

本地Minikube与Ingress配置问题:域名路由异常

问题排查与修复方案

核心问题1:Ingress与Service的Namespace不匹配

你的Ingress配置指定了namespace: local,但两个LoadBalancer服务(frontend-service、backend-service)如果没有显式指定namespace,默认会部署在default namespace下。Ingress只能引用同一namespace内的Service,跨namespace无法直接访问,这会导致Ingress无法正确找到frontend-service,最终 fallback 到第一个匹配的规则(backend-service的规则),所以两个域名都返回后端服务的响应。

修复方式二选一:

  • 方式1:修改Ingress的namespace为default,和Service保持一致:
    kind: Ingress
    metadata:
      name: test-ingress
      # 移除或注释掉namespace配置,默认即为default
      # namespace: local
      annotations:
        nginx.ingress.kubernetes.io/rewrite-target: /$1
    spec:
      rules:
      - host: test-api.local.it
        http:
          paths:
          - path: "/"
            pathType: Prefix
            backend:
              service:
                name: backend-service
                port:
                  number: 80
      - host: test.local.it
        http:
          paths:
            - path: "/"
              pathType: Prefix
              backend:
                service:
                  name: frontend-service
                  port:
                    number: 80
    
  • 方式2:将两个Service部署到local namespace,修改Service配置添加namespace: local:
    以frontend-service为例:
    kind: Service
    metadata:
      name: frontend-service
      namespace: local
    spec:
      type: LoadBalancer
      selector:
        app: frontend
      ports:
        - protocol: TCP
          port: 80
          targetPort: transport
    
    backend-service做同样修改,添加namespace: local字段。

核心问题2:不必要的Rewrite注解引发潜在异常

你的Ingress配置了nginx.ingress.kubernetes.io/rewrite-target: /$1,但path设置为"/"且pathType是Prefix,此时正则表达式无法捕获到任何分组($1为空),会导致请求被重写为//。虽然这不是当前问题的直接诱因,但会引发额外请求异常,若你的服务不需要路径重写,建议移除该注解:

metadata:
  name: test-ingress
  # namespace: local(根据实际情况调整)
  annotations:
    # 移除以下重写注解
    # nginx.ingress.kubernetes.io/rewrite-target: /$1

额外检查项

  1. 确认Minikube Ingress插件已启用:
    执行命令开启Ingress插件:

    minikube addons enable ingress
    

    开启后等待Ingress Controller Pod启动完成,可通过kubectl get pods -n kube-system查看nginx-ingress-controller的运行状态。

  2. 验证Ingress资源状态:
    执行命令查看Ingress的地址和规则:

    # 若Ingress在default namespace
    kubectl get ingress
    # 若Ingress在local namespace
    kubectl get ingress -n local
    

    确保ADDRESS字段有有效值(Minikube中通常为集群内部IP或127.0.0.1),且规则映射正确。

  3. 确认Pod标签与Service选择器匹配:
    检查frontend和backend的Pod是否带有对应的app: frontend、app: backend标签,确保Service能正确关联到目标Pod。


内容的提问来源于stack exchange,提问作者stecava

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 11:42:46