将流量路由到副本集的特定 Pod

Route Traffic Into Specific Pod of a Replica Set

我需要将流量(实时 audio/video)直接路由到 pods 的特定容器中。 pods 的数量应该与副本集水平缩放。我现在的解决方案是创建一个 StatefulSet,其中包含与 pods.

一样多的 NodePort 类型服务
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

这被认为是一种可接受的解决方案,还是有更好的解决方案来为每个 pod 创建服务?

如上面评论中所述,我找到了提供的解决方案 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