<p>Perhaps I'm not searching correctly or I'm not understanding a fundamental piece of the puzzle.</p>
<p>I have an application which is an ASP.NET web api serving http requests.  It will have let's say 3 replicas. In addition to the three replicas, I want a 4th replica (or instance) of the exact same image, but an environment variable changed.  This fourth instance will be using all the same code and processing background requests from an AWS Kinesis stream.</p>
<p>To prevent handling the messages more than once, we want the 4th instance to be the <em>only</em> instance handling the kinesis messages.</p>
<p>We're using helm.</p>
<p>I've got this part working pretty well with 2 separate deployments, one has 3 replicas, and the other has 1, easy.</p>
<p>The part I cannot figure out how to make the 4th instance ALSO eligible to serve HTTP requests.  Since the kinesis messages are low volume, there's little to no concern with using the resources for the listener pod to <em>also</em> serve http requests.</p>
<p>How can I make a kubernetes service that can load balance between the two deployments?</p>
<p>I've tried something like this</p>
<p><strong>service.yaml</strong></p>
<pre class="lang-yaml prettyprint-override"><code>spec:
  type: ClusterIP
  ports:
    - port: 80
      targetPort: http
      protocol: TCP
      name: http
    - port: 80
      targetPort: http-observer
      protocol: TCP
      name: http-observer
</code></pre>
<p><strong>deployment-api.yaml</strong></p>
<pre class="lang-yaml prettyprint-override"><code>ports:
  - name: http
    containerPort: 8800
    protocol: TCP
</code></pre>
<p><strong>deployment-observer.yaml</strong></p>
<pre class="lang-yaml prettyprint-override"><code>ports:
  - name: http-observer
    containerPort: 8800
    protocol: TCP
</code></pre>
<p>This yields the following error though:</p>
<pre><code> Error: Service &quot;my-service-name-here&quot; is invalid: spec.ports[1]: Duplicate value: core.ServicePort{Name:&quot;&quot;, Protocol:&quot;TCP&quot;, AppProtocol:(*string)(nil), Port:80, TargetPort:intstr.IntOrString{Type:0, IntVal:0, StrVal:&quot;&quot;}
</code></pre>
<p>Presumeably because I can't have 2 port 80s on the same service.</p>
<p>I want a service exposing port 80 to equally load balance according to whatever k8s HTTP traffic load balancing rules exist naturally between port 8800 on the three api pods and port 8800 on the observer pod.  Is this possible?  Am I going about this <em>all wrong</em>?</p>
<p>Any guidance would be appreciated.</p>
<p><em><strong>NOTE: Due to build pipeline, and dependency management, it's important that the two pods remain one code base, so we're not entertaining splitting the kinesis listener code and the API code into two separate apps with a shared library.</strong></em></p>

AceGambit
<p>What you're trying to achieve is to basically split traffic between different versions of the same application.</p>
<p>The easiest option is to use kubernetes internals for that. You can use one service that serves both deployments by setting the same label/selector on all three.</p>
<pre><code>kind: Service
[...]
spec:
  selector:
    app.kubernetes.io/name: my-service
  ports:
    - port: 80
      targetPort: http
      protocol: TCP
      name: http
</code></pre>
<pre><code># Deployment 1
kind: Deployment
[...]
spec:
  replicas: 3
  selector:
    matchLabels:
      app.kubernetes.io/name: my-service
  template:
    metadata:
      labels:
        app.kubernetes.io/name: my-service
---
# Deployment 2
kind: Deployment
[...]
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: my-service
  template:
    metadata:
      labels:
        app.kubernetes.io/name: my-service
</code></pre>
<p>By that kubernetes will split traffic between those 4 pods, where the second deployment is the one with a different environment variable.</p>
<p>If you need more sophisticated traffic splitting you could use something like istio to setup different versions and let istio handle the split, see <a href="https://istio.io/latest/docs/tasks/traffic-management/traffic-shifting/" rel="nofollow noreferrer">docs</a> (it's for canary deployments, but you could use it for your use case as well).</p>
