iY1NQ
iY1NQ

Reputation: 2514

Route Traffic Into Specific Pod of a Replica Set

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?

Upvotes: 1

Views: 2186

Answers (1)

iY1NQ
iY1NQ

Reputation: 2514

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.

Upvotes: 1

Related Questions