我有一个在 kubernetes 中运行的服务,并且具有路径前缀 /api
。现在我想使用Ingress通过主机地址example.com/service1/
来访问它因为我有多项服务。但问题是 ingress 重定向了路径 service1/
的所有请求。带有该前缀 service1/
,但我希望它从 example.com/service1/
重定向只需 /
即可为我服务(因此,如果我请求 example.com/service1/api
,它将重定向到仅包含 /api
的服务)。我能实现这样的目标吗?我正在服务的 helm 图表中编写 Ingress 配置。
服务图表文件中的入口配置values.yaml
看起来像这样:
...
ingress:
enabled: true
className: ""
annotations: {}
# kubernetes.io/ingress.class: nginx // this comment was created when generating helm chart
hosts:
- host: example.com
paths:
- path: /service1(/|$)(.*)
pathType: ImplementationSpecific
backend:
serviceName: $name
servicePort: http
tls: []
...
和ingress.yaml
里面templates/
文件夹是由helm
生成的默认文件。当我为该服务创建图表时。它仅使用 values.yaml
中的值配置入口。我什么也没找到,只有this question这基本上是说我需要添加前缀 service1/
到我的服务或只是使用 /api
在入口配置中。但有适合我需求的解决方案吗?
最佳答案
这是一个社区 wiki 答案,旨在提高可见性。请随意扩展它。
根据评论中提供的解决方案(Medium post 中的方法 1 示例 2),Ingress 的可能 values.yaml
文件可能如下所示。
...
ingress:
enabled: true
className: ""
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: http://example.com/$2
hosts:
- host: example.com
paths:
- path: /service1(/|$)(.*)
backend:
serviceName: $name
servicePort: http
tls: []
...
关于kubernetes - 入口路径重定向、helm 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69927448/