仅按主机名进行动态路由 - Kubernetes 入口规则

发布于 2025-01-17 13:34:16 字数 1175 浏览 5 评论 0 原文

我有一个部署了多个不同服务的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

I have a K8s cluster with multiple different services deployed and would like to use a single Ingress to route each incoming request to the appropriate service via a unique hostname DNS.

Currently, I've only been able to resolve a request when using the root path i.e. service-123.app.com.

As soon as I try to make a request with a path it doesn't resolve. The paths are valid paths to each service. For example, service-123.app.com/page/12345 would be expected by the application.

I might not fully understand how K8s Ingress rules are expected to work, but I hoped that it would match based on hostname only and simply forward on the path to the appropriate service.

Am I missing something very simple here? Any help is much appreciated. Thanks!

Here are my config files.

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃兔兔 2025-01-24 13:34:16

不确定您使用的是哪个 K8s 和入口控制器,但在更高版本的 K8s 中,您可以指定 pathType 可以更好地处理路径通配符。

你会得到这样的结果:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    ......
  name: app-name
  namespace: default
spec:
  rules:
  - host: service-123.app.com
    http:
      - path: /
        pathType: Prefix
        backend:
          serviceName: service-123
          servicePort: 80
  - host: service-456.app.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          serviceName: service-456
          servicePort: 80

如果你使用 nginx 入口控制器 一个好方法通过查看入口控制器生成的实际 nginx.conf 来查看正确的 nginx 配置。

$ kubectl cp <nginx-ingress-controller-pod>:nginx.conf nginx.conf
$ cat nginx.conf

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:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    ......
  name: app-name
  namespace: default
spec:
  rules:
  - host: service-123.app.com
    http:
      - path: /
        pathType: Prefix
        backend:
          serviceName: service-123
          servicePort: 80
  - host: service-456.app.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          serviceName: service-456
          servicePort: 80

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.

$ kubectl cp <nginx-ingress-controller-pod>:nginx.conf nginx.conf
$ cat nginx.conf
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文