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

Kubernetes Ingress仅根路径/可用,多服务子路径配置404求助

Fixing Ingress Routing for Multi-Service Kubernetes Deployments on AWS

Let's tackle your 404 issue and get your multi-service routing working as expected. There are two key problems with your current setup: incorrect rewrite rules in the Ingress configuration, and a misaligned port mapping in your Service.

1. The Rewrite Target Misconfiguration

Your current ingress.kubernetes.io/rewrite-target: / annotation tells the ingress controller to rewrite every matched path to the root (/). So when you access http://my.url.com/servicea/api/ping, the controller forwards the request to your service as / instead of /api/ping—which is why your service returns a 404 (it doesn't have an endpoint at the root).

To fix this, we need to use capturing groups in the path pattern to preserve the suffix of the URL after /servicea. We'll also use the modern ingress-nginx annotation (nginx.ingress.kubernetes.io/rewrite-target) for better compatibility.

Corrected Ingress Configuration

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: boardingservice
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  rules:
  - host: my.url.com
    http:
      paths:
      - path: /servicea(/|$)(.*)
        backend:
          serviceName: servicea-nodeport
          servicePort: 80
      # Add paths for other services here (e.g., serviceb)
      - path: /serviceb(/|$)(.*)
        backend:
          serviceName: serviceb-nodeport
          servicePort: 80

How This Works:

  • The path pattern /servicea(/|$)(.*) matches:
    • /servicea (exact match)
    • /servicea/ (trailing slash)
    • /servicea/api/ping (and any other suffix)
  • The rewrite-target: /$2 uses the second capturing group ((.*)) to forward only the suffix to your service. So http://my.url.com/servicea/api/ping gets rewritten to /api/ping when sent to servicea.

2. Misaligned Service Port Mapping

Looking at your Deployment and Service configurations:

  • Your servicea container exposes port 8080
  • Your Service servicea-nodeport sets targetPort: 8081, which points to the serviceb container in your Deployment

This means even if your ingress routing was correct, traffic would be sent to the wrong container! Fix the Service to point to servicea's actual port:

Corrected Service Configuration

apiVersion: v1
kind: Service
metadata:
  name: servicea-nodeport
spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 8080  # Updated to match servicea's container port
    nodePort: 30124
  selector:
    app: servicea

3. Apply and Test

  1. Apply the corrected configurations:
    kubectl apply -f ingress.yaml
    kubectl apply -f service.yaml
    
  2. Test the endpoint:
    curl http://my.url.com/servicea/api/ping
    

This should now route correctly to your servicea container's /api/ping endpoint.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:21:51