0
votes

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.

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

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

1
why do you need this? what is the use case behind it?Al-waleed Shihadeh
I host a platform for video conferencing rooms and participants of the same room (instance running on a pod) must establish a direct video/audio connection to the room. Otherwise the participants media stream cannot be exchanged because they arrive at different rooms (instances).iY1NQ
You can look at headless services along with the statefulsetArun P Johny
A couple considerations. 1) With the proposed solution, the pods will scale but not the services. You will have to automate the provisioning of a Service entry on every new pod. 2) When using headless services you have two options, querying via DNS and parsing the output OR fetching the Endpoints objects via the apiserver which will contain the pods IPs.davidmontoyago
Headless services would be an option, but cannot directly exposed to the internet. I found a possible solution described here: One NodePort service is used with one pod per node with cluster wide load balancing disabled (externalTrafficPolicy=Local).iY1NQ

1 Answers

1
votes

As explained in the comments above I found the solution provided here by using NodePort service targeting a StatefulSet with externalTrafficPolicy=Local. 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 pod anti-affinity.