仅按主机名进行动态路由 - Kubernetes 入口规则
我有一个部署了多个不同服务的K8S群集,并希望使用单个 Ingress
将每个传入请求通过唯一的主机名 dns
将每个传入请求路由到适当的服务。
当前,我只能在使用root路径IE service-123.app.com
时能够解决请求。
一旦我尝试用路径提出请求,就无法解决。路径是通往每种服务的有效路径。例如,应用程序期望 service-com/page/12345
。
我可能不完全了解K8S Ingress规则的期望,但是我希望它将仅基于主机名与适当服务的道路相匹配。
我在这里错过了很简单的东西吗?任何帮助都非常感谢。谢谢!
这是我的配置文件。
Ingress.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
......
name: app-name
namespace: default
spec:
rules:
- host: service-123.app.com
http:
- path: "/*"
backend:
serviceName: service-123
servicePort: 80
- host: service-456.app.com
http:
paths:
- path: "/*"
backend:
serviceName: service-456
servicePort: 80
service.yaml
---
apiVersion: v1
kind: Service
metadata:
annotations: {}
labels:
app: service-123
name: service-123
namespace: default
spec:
ports:
- name: port8080
port: 80
targetPort: 8080
selector:
app: service-123
type: NodePort
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定您使用的是哪个 K8s 和入口控制器,但在更高版本的 K8s 中,您可以指定
pathType
可以更好地处理路径通配符。你会得到这样的结果:
如果你使用 nginx 入口控制器 一个好方法通过查看入口控制器生成的实际 nginx.conf 来查看正确的 nginx 配置。
Not sure which K8s and ingress controller you are using, but in the later K8s you can specify the
pathType
which takes care of path wildcards more nicely.You would have something like this:
If you are using an nginx ingress controller a good way to see the right nginx configuration is by looking at the actual
nginx.conf
generated by the ingress controller.