<p>I need to route traffic (real-time audio/video) directly into specific container of pods. The number of pods should be scaled horizontally with a replica set. My solution now is to create a StatefulSet with as many NodePort-type services as there are pods.</p>

<pre class="lang-yaml prettyprint-override"><code>apiVersion: apps/v1
kind: StatefulSet
metadata:
  labels:
    app: foobar
  name: foobar-app
spec:
  serviceName: foobar
  replicas: 2
  selector:
    matchLabels:
      app: foobar
  template:
    metadata:
      labels:
        app: foobar
    spec:
      containers:
      - image: foobar:latest
        name: foobar
---
apiVersion: v1
kind: Service
metadata:
  name: foobar-service-0
spec:
  type: NodePort
  selector:
    statefulset.kubernetes.io/pod-name: foobar-app-0
  ports:
    - protocol: TCP
      nodePort: 30036
      port: 3000
---
apiVersion: v1
kind: Service
metadata:
  name: foobar-service-1
spec:
  type: NodePort
  selector:
    statefulset.kubernetes.io/pod-name: foobar-app-1
  ports:
    - protocol: TCP
      nodePort: 30037
      port: 3000
</code></pre>

<p>Is this considered an acceptable solution or is there a better one for creating services for each pod?</p>

<p>As explained in the comments above I found the solution provided <a href="https://stackoverflow.com/questions/46456239/how-to-expose-a-headless-service-for-a-statefulset-externally-in-kubernetes/46468433#46468433">here</a> by using NodePort service targeting a StatefulSet with <code>externalTrafficPolicy=Local</code>. This disables the cluster wide load balancing between different nodes. The prerequisite is that only one pod of the stateful set may run per node, which can be achieved by setting <a href="https://kubernetes.io/docs/concepts/configuration/assign-pod-node/#always-co-located-in-the-same-node" rel="nofollow noreferrer">pod anti-affinity</a>.</p>
