简体   繁体   English

如何将多个服务映射到一个Kubernetes Ingress路径?

[英]How do I map multiple services to one Kubernetes Ingress path?

How do I set a Kubernentes Ingress and Controller to essentially do what the following nginx.conf file does: 如何设置Kubernentes Ingress和Controller基本上执行以下nginx.conf文件的操作:

upstream backend {
    server server1.example.com       weight=5;
    server server2.example.com:8080;

    server backup1.example.com:8080   backup;
}

I want one http endpoint to map to multiple Kubernetes services with a preference for a primary one but also have a backup one. 我希望一个http端点映射到多个Kubernetes服务,优先选择主要服务,但也有一个备份服务。 (For my particular project, I need to have multiple services instead of one service with multiple pods.) (对于我的特定项目,我需要有多个服务而不是一个具有多个pod的服务。)

Here's my attempted ingress.yaml file. 这是我尝试的ingress.yaml文件。 I'm quite certain that the way I'm listing the multiple backends is incorrect. 我很确定我列出多个后端的方式是不正确的。 How would I do it? 我该怎么办? And how do I set the "backup" flag? 我如何设置“备份”标志?

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: fanout-ingress
  annotations:
    ingress.kubernetes.io/rewrite-target: /
    kubernetes.io/ingress.class: "nginx"
    # kubernetes.io/ingress.global-static-ip-name: "kubernetes-ingress"
spec:
  rules:
  - http:
      paths:
      - path: /
        backend:
          serviceName: server1
          servicePort: 
      - path: /
        backend:
          serviceName: server2
          servicePort: 8080
      - path: /
        backend:
          serviceName: backup1
          servicePort: 8080

I'm running Kubernetes on GKE. 我在GKE上运行Kubernetes。

Kubernetes Ingress is incapable of this. Kubernetes Ingress无能为力。

You could create a new service that targets server1, server2 and backup1 and use that in the Ingress. 您可以创建一个以server1,server2和backup1为目标的新服务,并在Ingress中使用它。 But the backends will be used in a round robin fashion. 但后端将以循环方式使用。

You can create a Deployment and a Service of (stateless) nginx reverse proxies with the config you wish and use that in Ingress. 您可以使用您希望的配置创建(无状态)nginx反向代理的部署和服务,并在Ingress中使用它。

You can do simple fanout based on path or name based virtual hosting . 您可以根据路径或基于名称的虚拟主机进行简单的扇出

However, you'd need to distinguish based on something (other than port, since it's an Ingress), so your two options would be virtual host or path. 但是,您需要根据某些东西进行区分(除了端口,因为它是Ingress),因此您的两个选项将是虚拟主机或路径。

Paths will not work with some services that expect a standard path. 路径不适用于某些期望标准路径的服务。 Judging based on your example you'd most likely want to have something like a.example.com and b.example.com. 根据您的示例判断,您最有可能想要像a.example.com和b.example.com这样的内容。 Here's the example from the Kubernetes docs: 以下是Kubernetes文档中的示例:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: name-virtual-host-ingress
spec:
  rules:
    - host: foo.bar.com
      http:
        paths:
          - backend:
              serviceName: service1
              servicePort: 80
    - host: bar.foo.com
      http:
        paths:
          - backend:
              serviceName: service2
              servicePort: 80

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM