<p>My backend has 5 different deployments, each serving the request in a specific port.</p>

<p>Frontend service contacts the backend service with the necessary details. The backend service should be able to decide the required pod (from one of the 5 deployments) to serve the request.</p>

<p>Instead of creating 5 different services, is it possible to link a single service to multiple deployments?</p>

<p>Kubernetes version: 1.12
Cloud being used: Amazon EKS</p>

<p>PS: My requirement is different from <a href="https://github.com/kubernetes/kubernetes/issues/24875" rel="nofollow noreferrer">https://github.com/kubernetes/kubernetes/issues/24875</a>
Similar question is unanswered: <a href="https://stackoverflow.com/questions/36885886/wire-multiple-deployments-to-one-service-in-kubernetes">&quot;Wire&quot; multiple Deployments to one Service in Kubernetes</a></p>

Rajavinoth
<p>The exact answer to your quest is: it is not possible today.
As you have correctly seen in the <a href="https://github.com/kubernetes/kubernetes/issues/24875" rel="noreferrer">issue</a> and in the <a href="https://stackoverflow.com/questions/36885886/wire-multiple-deployments-to-one-service-in-kubernetes">question</a> (both facing the same situation) this could be a future implementation.</p>

<p>A possible solution/workaround is to delegate the problem to an upper layer but basically it depends on the situation and different services are always required.</p>

<p>Assuming that your deployments are 5 different application that do different things (otherwise why 5 different deployment and not 1 with <code>n</code> replicas?) and assuming they are http applications, you can use the <a href="https://kubernetes.io/docs/concepts/services-networking/ingress/" rel="noreferrer">ingress resource</a> to ruote the traffic to the right deployment/service (assuming one service per deployment).</p>

<p>If your 5 deployment are created/updated/managed together (eg: are all in the same helm deployment) you can create a fanout ingress: </p>

<pre><code>apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: simple-fanout-example
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: foo.bar.com
    http:
      paths:
      - path: /foo
        backend:
          serviceName: service1
          servicePort: 4200
      - path: /bar
        backend:
          serviceName: service2
          servicePort: 8080
      - path: /aaaa
        backend:
          serviceName: service3
          servicePort: 4200
      - path: /bbbbb
        backend:
          serviceName: service4
          servicePort: 8080
      - path: /ccc
        backend:
          serviceName: service5
          servicePort: 8080

</code></pre>

<p>Or, if your 5 deployment are separated you can create 5 Different ingress serources with the same idea:</p>

<pre><code>apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress-for-app-1
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: foo.bar.com
    http:
      paths:
      - path: /foo
        backend:
          serviceName: service1
          servicePort: 4200

</code></pre>

<pre><code>apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress-for-app-1
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: foo.bar.com
    http:
      paths:
      - path: /bar
        backend:
          serviceName: service2
          servicePort: 8080

</code></pre>

<p>and so on....</p>

<p>Creating 5 ingress or 1 fanout should produce the same result.</p>

<p>This approach works well with a <a href="https://github.com/kubernetes/ingress-nginx" rel="noreferrer">nginx ingress controller</a> but pay attention only to two things</p>

<ul>
<li>path match: with the nginx controller version > 0.22 <code>nginx.ingress.kubernetes.io/rewrite-target: /</code> is an exact match. For example, if you wold like to redirect from <code>/foo</code> to <code>/</code> preserving all the uri after <code>/foo</code> (<code>/foo/something?parameter=parameter_value</code> to <code>/something?parameter=parameter_value</code>) your ingress rewrite rule should be like this:</li>
</ul>

<pre><code>apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress-for-app-1
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: "/$1"
spec:
  rules:
  - host: foo.bar.com
    http:
      paths:
      - path: /foo/(.*)
        backend:
          serviceName: service1
          servicePort: 4200
</code></pre>

<ul>
<li>conflict route: avoid conflicting rout, eg <code>path: /foo/(.*)</code> and path: <code>/foo/bar/(.*)</code> where a request for <code>/foo/bar/something</code> would match both the paths. The behavior could be difficult to predict and, if it worked as expected, it would not be stable</li>
</ul>
