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

9/22/2017

How do I set a Kubernentes Ingress and Controller to essentially do what the following nginx.conf file does:

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. (For my particular project, I need to have multiple services instead of one service with multiple pods.)

Here's my attempted ingress.yaml file. 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.

-- gunit
google-cloud-platform
kubernetes
load-balancing
nginx

2 Answers

1/10/2019

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.

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. Here's the example from the Kubernetes docs:

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
-- Cosmin Lehene
Source: StackOverflow

9/22/2017

Kubernetes Ingress is incapable of this.

You could create a new service that targets server1, server2 and backup1 and use that in the 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.

-- Janos Lenart
Source: StackOverflow