有没有一种方法可以根据上下文路径在内部将url重定向到特定服务? [英] Is there a way url redirect internally to particular service based on context path?

查看:58
本文介绍了有没有一种方法可以根据上下文路径在内部将url重定向到特定服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个群集中运行着多个相同的Pod,但名称空间不同.这是在Kubernetes中运行的Web应用程序.我有URL < HOSTNAME> ::< PORT>/context/abc/def/......我想根据上下文重定向到特定的服务.有没有一种方法可以使用入口控制器来实现?还是我可以通过入口使用不同的端口来实现它?

如果URL为< HOSTNAME>:< PORT>/abc/def/....,我的Web应用程序可以正常工作.由于我必须使用相同的URL访问不同的Pod,因此我要为其添加上下文.我们还有其他方法可以实现此用例吗?

解决方案

您可以使用 rewrite-target 来做到这一点.在下面的示例中,我使用了 rewrite.bar.com < HOSTNAME> 值和 80 的< PORT> 值代码>.

  apiVersion:extensions/v1beta1种类:入口元数据:注释:nginx.ingress.kubernetes.io/rewrite-target:/$ 2名称:改写命名空间:默认规格:规则:-主机:rewrite.bar.comhttp:路径:-后端:serviceName:上下文服务servicePort:80路径:/context1(/| $)(.*)-后端:serviceName:上下文服务2servicePort:80路径:/context2(/| $)(.*) 

例如,上面的入口定义将导致以下重写:

对于上下文1服务,

rewrite.bar.com/context1 重写为 rewrite.bar.com/.

rewrite.bar.com/context2 重写为 rewrite.bar.com/用于上下文2服务.

对于上下文1服务,

rewrite.bar.com/context1/new 重写为 rewrite.bar.com/new .

对于上下文2服务,

rewrite.bar.com/context2/new 重写为 rewrite.bar.com/new .

There are multiple same pods running in one cluster but different namespaces. This is the web application running in Kubernetes. I have the URL <HOSTNAME>:<PORT>/context/abc/def/...... I want to redirect to particular service based on the context. Is there a way i can achieve it using ingress controller ? Or Is there any way i can achieve it using different ports through ingress ?

My web application works fine if the URL is <HOSTNAME>:<PORT>/abc/def/...... Since i have to access the different pods using the same URL, I am adding context to it. Do we have any other way to achieve this use case ?

解决方案

You can do that with rewrite-target. In example below i used <HOSTNAME> value of rewrite.bar.com and <PORT> with value 80.

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
  name: rewrite
  namespace: default
spec:
  rules:
  - host: rewrite.bar.com
    http:
      paths:
      - backend:
          serviceName: context-service
          servicePort: 80
        path: /context1(/|$)(.*)
      - backend:
          serviceName: context-service2
          servicePort: 80
        path: /context2(/|$)(.*)       

For example, the ingress definition above will result in the following rewrites:

rewrite.bar.com/context1 rewrites to rewrite.bar.com/ for context 1 service.

rewrite.bar.com/context2 rewrites to rewrite.bar.com/ for context 2 service.

rewrite.bar.com/context1/new rewrites to rewrite.bar.com/new for context 1 service.

rewrite.bar.com/context2/new rewrites to rewrite.bar.com/new for context 2 service.

这篇关于有没有一种方法可以根据上下文路径在内部将url重定向到特定服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆