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

Google Cloud容器引擎中Helm部署Nginx-Ingress多路径注解配置问询

Hey there! Let's walk through how to configure your custom Helm chart's Ingress to properly route traffic to /v1, /v2, and other paths using the NGINX Ingress Controller you've deployed on GKE. Since you already have the default backend working, we just need to tweak the Ingress rules and annotations for path-based routing.

First, Key Concepts to Remember

NGINX Ingress uses annotations to control routing behavior—like whether to use regex for paths, rewrite URLs, or enforce HTTPS. We'll focus on the most common scenarios for your use case.

Example values.yaml Configuration for Your Custom Chart

Below are two common setups depending on how your backend services are structured:

Scenario 1: Your services listen on the root path (/)

If your v1 service serves content from its root (e.g., http://v1-service/ should handle domain.com/v1/foo), you'll need to rewrite the URL path to strip the /v1 prefix. Here's how to set that up:

ingress:
  enabled: true
  # Make sure this matches the ingressClass of your NGINX controller (default is "nginx")
  ingressClassName: nginx
  annotations:
    # Enable regex matching for path patterns
    nginx.ingress.kubernetes.io/use-regex: "true"
    # Rewrite the URL: $2 captures everything after /v1/ (or /v2/)
    nginx.ingress.kubernetes.io/rewrite-target: /$2
    # Optional: Force HTTPS (recommended for production)
    nginx.ingress.kubernetes.io/ssl-redirect: "true"
  hosts:
    - host: domain.com
      paths:
        - path: /v1(/|$)(.*)
          pathType: ImplementationSpecific
          backend:
            service:
              name: your-v1-service # Replace with your v1 service name
              port:
                number: 80 # Replace with your service's port
        - path: /v2(/|$)(.*)
          pathType: ImplementationSpecific
          backend:
            service:
              name: your-v2-service # Replace with your v2 service name
              port:
                number: 80
  # Optional: Add TLS config if you have a certificate secret
  tls:
    - secretName: domain-tls-secret # Replace with your TLS secret name
      hosts:
        - domain.com

Scenario 2: Your services already listen on /v1//v2 paths

If your v1 service is already configured to handle requests at /v1/ (e.g., http://v1-service/v1/foo), you can use simple prefix matching without URL rewriting:

ingress:
  enabled: true
  ingressClassName: nginx
  annotations:
    nginx.ingress.kubernetes.io/ssl-redirect: "true" # Optional HTTPS enforce
  hosts:
    - host: domain.com
      paths:
        - path: /v1/*
          pathType: Prefix
          backend:
            service:
              name: your-v1-service
              port:
                number: 80
        - path: /v2/*
          pathType: Prefix
          backend:
            service:
              name: your-v2-service
              port:
                number: 80
  tls: [] # Add TLS config here if needed

Important Notes for GKE

  • Verify Ingress Class: Run kubectl get ingressclasses to confirm the name of your NGINX Ingress Controller's ingress class (default is nginx). Make sure this matches the ingressClassName in your values.
  • Check Load Balancer IP: After deploying your chart, run kubectl get ingress to see if the ADDRESS field has an external IP (this is GCP's load balancer IP). Update your domain's DNS to point to this IP.
  • Test Routing: Use curl to test paths:
    curl https://domain.com/v1/health
    curl https://domain.com/v2/health
    
    You should get responses from your respective services.

Troubleshooting Tips

  • If paths aren't routing correctly, check the NGINX controller logs with:
    kubectl logs -n <nginx-namespace> <nginx-controller-pod-name>
    
    Look for errors related to path matching or service resolution.
  • Ensure your backend services are running and reachable from the Ingress controller.

内容的提问来源于stack exchange,提问作者Chris G.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 10:08:42