大网数据广告活动
服务器运维问答

Routing an existing path to another pod with nginx in Kubernetes

· 52CE 我爱测

I have an application (demoapp) in GKE which is exposed to internet with a GCE ingress like this: - host: demoapp.com http: paths: - backend: serviceName: demoapp-service servicePort: 80 The application has many customers with ea…

问题原文

原帖链接 · Fleuri · CC BY-SA 4.0

I have an application (demoapp) in GKE which is exposed to internet with a GCE ingress like this:

- host: demoapp.com                                                                                                                                                                                                                
  http:                                                                                                                                                                                                                                 
    paths:                                                                                                                                                                                                                              
    - backend:                                                                                                                                                                                                                          
        serviceName: demoapp-service                                                                                                                                                                                                
        servicePort: 80            

The application has many customers with each customer having their own page with a path scheme like demoapp.com/customers/customer1 I'm rolling out a new version of the application (demoapp2) and would like to gather some feedback first from certain customers. Thus I'd like to route certain customer paths to the new application instead of the old so that the e.g. demoapp.com/customers/customer1 would resolve to demoapp2-service's path instead of demoapp-service's. This however doesn't seem work:

- host: demoapp.com                                                                                                                                                                                                                
  http:                                                                                                                                                                                                                                 
    paths:
    - backend:
        serviceName: demoapp2-service
        servicePort: 80
      path: /customers/customer1                                                                                                                                                                                                        
    - backend:                                                                                                                                                                                                                          
        serviceName: demoapp-service                                                                                                                                                                                                
        servicePort: 80            

I've tried other variations, like adding path: / or path: /* to demoapp-service routing but I guess the working url would be demoapp.com/customers/customer1/customers/customer1 even if the default didn't override the path. So next I thought that I could have something like this in demoapp nginx configuration:

location /customers/customer1 {                                                                                                                                                                                  
      proxy_pass http://demoapp2-service/customers/customer1;
}

But that doesn't seem to work either and I don't see anything in logs. Does anyone know how to pull this scheme off?

已采纳解答

原帖链接 · Vit · CC BY-SA 4.0

You can dig in the NGINX Plus Ingress Controller rewrite annotation functionality and fit it for your needs.

PLease find official example here.

Example: In the following example we load balance two applications that require URI rewriting:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: cafe-ingress
  annotations:
    nginx.org/rewrites: "serviceName=tea-svc rewrite=/;serviceName=coffee-svc rewrite=/beans/"
spec:
  rules:
  - host: cafe.example.com
    http:
      paths:
      - path: /tea/
        backend:
          serviceName: tea-svc
          servicePort: 80
      - path: /coffee/
        backend:
          serviceName: coffee-svc
          servicePort: 80

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-01-16)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。